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