1 /*
   2  * Copyright (c) 2014, 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.GraphicsConfiguration;
  28 import java.awt.GraphicsEnvironment;
  29 import java.awt.Image;
  30 import java.awt.image.BufferedImage;
  31 import java.awt.image.VolatileImage;
  32 import java.io.File;
  33 import java.io.IOException;
  34 
  35 import javax.imageio.ImageIO;
  36 
  37 /**
  38  * @test
  39  * @bug 8041129
  40  * @summary Tests asymmetric source offsets.
  41  * @author Sergey Bylokhov
  42  */
  43 public final class IncorrectSourceOffset {
  44 
  45     public static void main(final String[] args) throws IOException {
  46         GraphicsEnvironment ge = GraphicsEnvironment
  47                 .getLocalGraphicsEnvironment();
  48         GraphicsConfiguration gc = ge.getDefaultScreenDevice()
  49                                      .getDefaultConfiguration();
  50         VolatileImage vi = gc.createCompatibleVolatileImage(511, 255);
  51         BufferedImage bi = new BufferedImage(511, 255,
  52                                              BufferedImage.TYPE_INT_ARGB);
  53         BufferedImage gold = new BufferedImage(511, 255,
  54                                                BufferedImage.TYPE_INT_ARGB);
  55         fill(gold);
  56         while (true) {
  57             vi.validate(gc);
  58             fill(vi);
  59             if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
  60                 try {
  61                     Thread.sleep(100);
  62                 } catch (final InterruptedException ignored) {
  63                 }
  64                 continue;
  65             }
  66 
  67             Graphics2D big = bi.createGraphics();
  68             big.drawImage(vi, 7, 11, 127, 111, 7, 11, 127, 111, null);
  69             big.dispose();
  70             if (vi.contentsLost()) {
  71                 try {
  72                     Thread.sleep(100);
  73                 } catch (final InterruptedException ignored) {
  74                 }
  75                 continue;
  76             }
  77             break;
  78         }
  79 
  80         for (int x = 7; x < 127; ++x) {
  81             for (int y = 11; y < 111; ++y) {
  82                 if (gold.getRGB(x, y) != bi.getRGB(x, y)) {
  83                     ImageIO.write(gold, "png", new File("gold.png"));
  84                     ImageIO.write(bi, "png", new File("bi.png"));
  85                     throw new RuntimeException("Test failed.");
  86                 }
  87             }
  88         }
  89     }
  90 
  91     private static void fill(Image image) {
  92         Graphics2D graphics = (Graphics2D) image.getGraphics();
  93         graphics.setComposite(AlphaComposite.Src);
  94         for (int i = 0; i < image.getHeight(null); ++i) {
  95             graphics.setColor(new Color(i, 0, 0));
  96             graphics.fillRect(0, i, image.getWidth(null), 1);
  97         }
  98         graphics.dispose();
  99     }
 100 }