1 /* 2 * Copyright (c) 2005, 2017, 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 /* 25 * @test 26 * @key headful 27 * @bug 4339415 28 * @summary Tests that GIF writer plugin is able to write images with BITMASK 29 * transparency 30 */ 31 32 import java.awt.Color; 33 import java.awt.Graphics2D; 34 import java.awt.image.BufferedImage; 35 import java.awt.image.IndexColorModel; 36 import java.awt.image.WritableRaster; 37 import java.io.File; 38 import java.io.IOException; 39 40 import javax.imageio.ImageIO; 41 import javax.imageio.ImageWriter; 42 43 public class TransparencyTest { 44 45 protected static final String fname = "ttest.gif"; 46 protected BufferedImage src; 47 protected BufferedImage dst; 48 49 public static void main(String[] args) { 50 System.out.println("Test indexed image..."); 51 IndexColorModel icm = createIndexedBitmaskColorModel(); 52 BufferedImage img = createIndexedImage(200, 200, icm); 53 TransparencyTest t = new TransparencyTest(img); 54 55 try { 56 t.doTest(); 57 } catch (Exception e) { 58 throw new RuntimeException("Test failed!", e); 59 } 60 System.out.println("Test passed."); 61 } 62 63 protected TransparencyTest(BufferedImage src) { 64 this.src = src; 65 } 66 67 protected void doTest() throws IOException { 68 int w = src.getWidth(); 69 int h = src.getHeight(); 70 71 System.out.println("Write image..."); 72 try { 73 ImageWriter writer = 74 ImageIO.getImageWritersByFormatName("GIF").next(); 75 writer.setOutput(ImageIO.createImageOutputStream(new File(fname))); 76 writer.write(src); 77 } catch (Exception e) { 78 throw new RuntimeException("Test failed.", e); 79 } 80 System.out.println("Read image...."); 81 dst = ImageIO.read(new File(fname)); 82 83 BufferedImage tmp = new BufferedImage(w, 2 * h, 84 BufferedImage.TYPE_INT_ARGB); 85 Graphics2D g = tmp.createGraphics(); 86 g.setColor(Color.pink); 87 g.fillRect(0, 0, tmp.getWidth(), tmp.getHeight()); 88 89 g.drawImage(src, 0, 0, null); 90 g.drawImage(dst, 0, h, null); 91 92 int width = w / 8; 93 int x = 5 * width + width / 2; 94 for (int y = 0; y < h; y++) { 95 int argb = tmp.getRGB(x, y); 96 if (Color.pink.getRGB() != argb) { 97 throw new RuntimeException("Bad color at " + x + "," + y + 98 " - " + Integer.toHexString(argb)); 99 } 100 } 101 } 102 103 protected static BufferedImage createIndexedImage(int w, int h, 104 IndexColorModel icm) 105 { 106 BufferedImage img = new BufferedImage(w, h, 107 BufferedImage.TYPE_BYTE_INDEXED, 108 icm); 109 110 int mapSize = icm.getMapSize(); 111 int width = w / mapSize; 112 113 WritableRaster wr = img.getRaster(); 114 for (int i = 0; i < mapSize; i++) { 115 for (int y = 0; y < h; y++) { 116 for (int x = 0; x < width; x++) { 117 wr.setSample(i * width + x, y, 0, i); 118 } 119 } 120 } 121 return img; 122 } 123 124 protected static IndexColorModel createIndexedBitmaskColorModel() { 125 int paletteSize = 8; 126 byte[] red = new byte[paletteSize]; 127 byte[] green = new byte[paletteSize]; 128 byte[] blue = new byte[paletteSize]; 129 130 red[0] = (byte)0xff; green[0] = (byte)0x00; blue[0] = (byte)0x00; 131 red[1] = (byte)0x00; green[1] = (byte)0xff; blue[1] = (byte)0x00; 132 red[2] = (byte)0x00; green[2] = (byte)0x00; blue[2] = (byte)0xff; 133 red[3] = (byte)0xff; green[3] = (byte)0xff; blue[3] = (byte)0xff; 134 red[4] = (byte)0x00; green[4] = (byte)0x00; blue[4] = (byte)0x00; 135 red[5] = (byte)0x80; green[5] = (byte)0x80; blue[5] = (byte)0x80; 136 red[6] = (byte)0xff; green[6] = (byte)0xff; blue[6] = (byte)0x00; 137 red[7] = (byte)0x00; green[7] = (byte)0xff; blue[7] = (byte)0xff; 138 139 int numBits = 3; 140 141 IndexColorModel icm = new IndexColorModel(numBits, paletteSize, 142 red, green, blue, 5); 143 144 return icm; 145 } 146 }