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 24 import java.awt.AlphaComposite; 25 import java.awt.Color; 26 import java.awt.Graphics2D; 27 import java.awt.GraphicsConfiguration; 28 import java.awt.GraphicsDevice; 29 import java.awt.GraphicsEnvironment; 30 import java.awt.RenderingHints; 31 import java.awt.image.BufferedImage; 32 import java.awt.image.DataBuffer; 33 import java.awt.image.DataBufferByte; 34 import java.awt.image.DataBufferInt; 35 import java.awt.image.DataBufferShort; 36 import java.awt.image.VolatileImage; 37 38 import static java.awt.Transparency.TRANSLUCENT; 39 40 /** 41 * @test 42 * @key headful 43 * @bug 8062164 44 * @summary We should get correct alpha, when we draw to/from VolatileImage and 45 * bicubic interpolation is enabled 46 * @author Sergey Bylokhov 47 */ 48 public final class IncorrectAlphaConversionBicubic { 49 50 private static final Color RGB = new Color(200, 255, 7, 123); 51 private static final int SIZE = 100; 52 53 public static void main(final String[] args) { 54 final GraphicsEnvironment ge = 55 GraphicsEnvironment.getLocalGraphicsEnvironment(); 56 final GraphicsDevice gd = ge.getDefaultScreenDevice(); 57 final GraphicsConfiguration gc = gd.getDefaultConfiguration(); 58 final VolatileImage vi = 59 gc.createCompatibleVolatileImage(SIZE, SIZE, TRANSLUCENT); 60 final BufferedImage bi = makeUnmanagedBI(gc, TRANSLUCENT); 61 final int expected = bi.getRGB(2, 2); 62 63 int attempt = 0; 64 BufferedImage snapshot; 65 while (true) { 66 if (++attempt > 10) { 67 throw new RuntimeException("Too many attempts: " + attempt); 68 } 69 vi.validate(gc); 70 final Graphics2D g2d = vi.createGraphics(); 71 g2d.setComposite(AlphaComposite.Src); 72 g2d.scale(2, 2); 73 g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 74 RenderingHints.VALUE_INTERPOLATION_BICUBIC); 75 g2d.drawImage(bi, 0, 0, null); 76 g2d.dispose(); 77 78 snapshot = vi.getSnapshot(); 79 if (vi.contentsLost()) { 80 continue; 81 } 82 break; 83 } 84 final int actual = snapshot.getRGB(2, 2); 85 if (actual != expected) { 86 System.err.println("Actual: " + Integer.toHexString(actual)); 87 System.err.println("Expected: " + Integer.toHexString(expected)); 88 throw new RuntimeException("Test failed"); 89 } 90 } 91 92 private static BufferedImage makeUnmanagedBI(GraphicsConfiguration gc, 93 int type) { 94 BufferedImage img = gc.createCompatibleImage(SIZE, SIZE, type); 95 Graphics2D g2d = img.createGraphics(); 96 g2d.setColor(RGB); 97 g2d.fillRect(0, 0, SIZE, SIZE); 98 g2d.dispose(); 99 final DataBuffer db = img.getRaster().getDataBuffer(); 100 if (db instanceof DataBufferInt) { 101 ((DataBufferInt) db).getData(); 102 } else if (db instanceof DataBufferShort) { 103 ((DataBufferShort) db).getData(); 104 } else if (db instanceof DataBufferByte) { 105 ((DataBufferByte) db).getData(); 106 } else { 107 try { 108 img.setAccelerationPriority(0.0f); 109 } catch (final Throwable ignored) { 110 } 111 } 112 return img; 113 } 114 }