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.DataBuffer;
  32 import java.awt.image.DataBufferByte;
  33 import java.awt.image.DataBufferInt;
  34 import java.awt.image.DataBufferShort;
  35 import java.awt.image.VolatileImage;
  36 import java.io.File;
  37 import java.io.IOException;
  38 
  39 import javax.imageio.ImageIO;
  40 
  41 import static java.awt.Transparency.*;
  42 import static java.awt.image.BufferedImage.*;
  43 
  44 /**
  45  * @test
  46  * @bug 8029253
  47  * @summary Tests asymmetric source offsets when unmanaged image is drawn to VI.
  48  *          Results of the blit to compatibleImage are used for comparison.
  49  * @author Sergey Bylokhov
  50  */
  51 public final class IncorrectUnmanagedImageSourceOffset {
  52 
  53     private static final int[] TYPES = {TYPE_INT_RGB, TYPE_INT_ARGB,
  54                                         TYPE_INT_ARGB_PRE, TYPE_INT_BGR,
  55                                         TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR,
  56                                         TYPE_4BYTE_ABGR_PRE,
  57                                         /*TYPE_USHORT_565_RGB,
  58                                         TYPE_USHORT_555_RGB, TYPE_BYTE_GRAY,
  59                                         TYPE_USHORT_GRAY,*/ TYPE_BYTE_BINARY,
  60                                         TYPE_BYTE_INDEXED};
  61     private static final int[] TRANSPARENCIES = {OPAQUE, BITMASK, TRANSLUCENT};
  62 
  63     public static void main(final String[] args) throws IOException {
  64         for (final int viType : TRANSPARENCIES) {
  65             for (final int biType : TYPES) {
  66                 BufferedImage bi = makeUnmanagedBI(biType);
  67                 fill(bi);
  68                 test(bi, viType);
  69             }
  70         }
  71     }
  72 
  73     private static void test(BufferedImage bi, int type)
  74             throws IOException {
  75         GraphicsEnvironment ge = GraphicsEnvironment
  76                 .getLocalGraphicsEnvironment();
  77         GraphicsConfiguration gc = ge.getDefaultScreenDevice()
  78                                      .getDefaultConfiguration();
  79         VolatileImage vi = gc.createCompatibleVolatileImage(511, 255, type);
  80         BufferedImage gold = gc.createCompatibleImage(511, 255, type);
  81         // draw to compatible Image
  82         Graphics2D big = gold.createGraphics();
  83         // force scaled blit
  84         big.drawImage(bi, 7, 11, 127, 111, 7, 11, 127 * 2, 111, null);
  85         big.dispose();
  86         // draw to volatile image
  87         BufferedImage snapshot;
  88         while (true) {
  89             vi.validate(gc);
  90             if (vi.validate(gc) != VolatileImage.IMAGE_OK) {
  91                 try {
  92                     Thread.sleep(100);
  93                 } catch (final InterruptedException ignored) {
  94                 }
  95                 continue;
  96             }
  97             Graphics2D vig = vi.createGraphics();
  98             // force scaled blit
  99             vig.drawImage(bi, 7, 11, 127, 111, 7, 11, 127 * 2, 111, null);
 100             vig.dispose();
 101             snapshot = vi.getSnapshot();
 102             if (vi.contentsLost()) {
 103                 try {
 104                     Thread.sleep(100);
 105                 } catch (final InterruptedException ignored) {
 106                 }
 107                 continue;
 108             }
 109             break;
 110         }
 111         // validate images
 112         for (int x = 7; x < 127; ++x) {
 113             for (int y = 11; y < 111; ++y) {
 114                 if (gold.getRGB(x, y) != snapshot.getRGB(x, y)) {
 115                     ImageIO.write(gold, "png", new File("gold.png"));
 116                     ImageIO.write(snapshot, "png", new File("bi.png"));
 117                     throw new RuntimeException("Test failed.");
 118                 }
 119             }
 120         }
 121     }
 122 
 123     private static BufferedImage makeUnmanagedBI(final int type) {
 124         final BufferedImage bi = new BufferedImage(511, 255, type);
 125         final DataBuffer db = bi.getRaster().getDataBuffer();
 126         if (db instanceof DataBufferInt) {
 127             ((DataBufferInt) db).getData();
 128         } else if (db instanceof DataBufferShort) {
 129             ((DataBufferShort) db).getData();
 130         } else if (db instanceof DataBufferByte) {
 131             ((DataBufferByte) db).getData();
 132         } else {
 133             try {
 134                 bi.setAccelerationPriority(0.0f);
 135             } catch (final Throwable ignored) {
 136             }
 137         }
 138         return bi;
 139     }
 140 
 141     private static void fill(final Image image) {
 142         final Graphics2D graphics = (Graphics2D) image.getGraphics();
 143         graphics.setComposite(AlphaComposite.Src);
 144         for (int i = 0; i < image.getHeight(null); ++i) {
 145             graphics.setColor(new Color(i, 0, 0));
 146             graphics.fillRect(0, i, image.getWidth(null), 1);
 147         }
 148         graphics.dispose();
 149     }
 150 }