1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.awt.AlphaComposite;
  25 import java.awt.Color;
  26 import java.awt.Graphics2D;
  27 import java.awt.image.BufferedImage;
  28 import java.io.File;
  29 import java.io.IOException;
  30 
  31 import javax.imageio.ImageIO;
  32 
  33 import static java.awt.image.BufferedImage.TYPE_INT_ARGB;
  34 
  35 /**
  36  * @test
  37  * @key headful
  38  * @bug 8167310
  39  * @summary The clip should be correct if the scale is fractional
  40  */
  41 public final class IncorrectFractionalClip {
  42 
  43     private static final int SIZE = 128;
  44 
  45     public static final Color RED = new Color(255, 0, 0, 100);
  46 
  47     public static final Color GREEN = new Color(0, 255, 0, 100);
  48 
  49     public static final Color WHITE = new Color(0, 0, 0, 0);
  50 
  51     private static final double[] SCALES = {
  52             0.1, 0.25, 0.4, 0.5, 0.6, 1, 1.4, 1.5, 1.6, 2.0, 2.4, 2.5, 2.6, 4
  53     };
  54 
  55     static BufferedImage bi;
  56 
  57     static BufferedImage redI;
  58 
  59     static BufferedImage greenI;
  60 
  61     public static void main(final String[] args) throws Exception {
  62         bi = new BufferedImage(SIZE, SIZE, TYPE_INT_ARGB);
  63         redI = createImage(RED);
  64         greenI = createImage(GREEN);
  65 
  66         System.out.println("Will test fillRect");
  67         test(0, true);
  68         test(0, false);
  69         System.out.println("Will test DrawImage");
  70         test(1, true);
  71         test(1, false);
  72         System.out.println("Will test drawLine");
  73         test(2, true);
  74         test(2, false);
  75     }
  76 
  77     /**
  78      * This method draw/fills a number of rectangle, images and lines. Each of
  79      * them are painted one after anothere without gaps. Each time the clip is
  80      * set as one vertical/horizontal line. The resulted image should not
  81      * have any overlapping of different colors. The gaps are possible only in
  82      * case of Lines.
  83      */
  84     private static void test(final int testId, final boolean horiz)
  85             throws Exception {
  86         for (final double scale : SCALES) {
  87             // Initialize the main image
  88             Graphics2D g = bi.createGraphics();
  89             g.setComposite(AlphaComposite.Src);
  90             g.setColor(WHITE);
  91             g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
  92             g.setComposite(AlphaComposite.SrcOver);
  93 
  94             // set the scale in one direction
  95             if (horiz) {
  96                 g.scale(scale, 1);
  97             } else {
  98                 g.scale(1, scale);
  99             }
 100             // cover all units in the user space to touch all pixels in the
 101             // image after transform
 102             for (int step = 0; step < SIZE / scale; ++step) {
 103                 if (horiz) {
 104                     g.setClip(step, 0, 1, SIZE);
 105                 } else {
 106                     g.setClip(0, step, SIZE, 1);
 107                 }
 108                 switch (testId) {
 109                     case 0:
 110                         g.setColor(step % 2 == 0 ? RED : GREEN);
 111                         g.fillRect(0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE);
 112                         break;
 113                     case 1:
 114                         g.drawImage(step % 2 == 0 ? redI : greenI, 0, 0,
 115                                     Integer.MAX_VALUE, Integer.MAX_VALUE, null);
 116                         break;
 117                     case 2:
 118                         g.setColor(step % 2 == 0 ? RED : GREEN);
 119                         if (horiz) {
 120                             g.drawLine(step, 0, step, SIZE);
 121                         } else {
 122                             g.drawLine(0, step, SIZE, step);
 123                         }
 124                         break;
 125                     default:
 126                         throw new RuntimeException();
 127                 }
 128             }
 129             g.dispose();
 130             validate(bi, testId);
 131         }
 132     }
 133 
 134     private static void validate(final BufferedImage bi, final int testID)
 135             throws Exception {
 136         for (int x = 0; x < SIZE; ++x) {
 137             for (int y = 0; y < SIZE; ++y) {
 138                 int rgb = bi.getRGB(x, y);
 139                 if (rgb != GREEN.getRGB() && rgb != RED.getRGB()) {
 140                     if (testID == 2) {
 141                         // The drawLine should not overlap lines but it is
 142                         // possible to have gaps between lines.
 143                         if (rgb == WHITE.getRGB()) {
 144                             continue;
 145                         }
 146                         ImageIO.write(bi, "png", new File("image.png"));
 147                         throw new RuntimeException("Test failed.");
 148                     }
 149                 }
 150             }
 151         }
 152     }
 153 
 154     private static BufferedImage createImage(final Color color) {
 155         BufferedImage bi = new BufferedImage(SIZE, SIZE, TYPE_INT_ARGB);
 156         Graphics2D g = bi.createGraphics();
 157         g.setComposite(AlphaComposite.Src);
 158         g.setColor(color);
 159         g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
 160         g.dispose();
 161         return bi;
 162     }
 163 }