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