2
|
1 |
/*
|
|
2 |
* Copyright 2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
20 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
21 |
* have any questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
/**
|
|
25 |
* @test
|
|
26 |
* @bug 6557713
|
|
27 |
* @summary Test verifies that PNG image writer correctly handles indexed images with
|
|
28 |
* various types of transparency.
|
|
29 |
*
|
|
30 |
* Test for 4bpp OPAQUE image
|
|
31 |
* @run main GrayPngTest 4 1 3
|
|
32 |
*
|
|
33 |
* Test for 4bpp BITMASK image with transparent pixel 3
|
|
34 |
* @run main GrayPngTest 4 2 3
|
|
35 |
*
|
|
36 |
* Test for 4bpp TRANSLUCENT image
|
|
37 |
* @run main GrayPngTest 4 3 3
|
|
38 |
*
|
|
39 |
* Test for 8bpp OPAQUE image
|
|
40 |
* @run main GrayPngTest 8 1 127
|
|
41 |
*
|
|
42 |
* Test for 8bpp BITMASK image with transparent pixel 127
|
|
43 |
* @run main GrayPngTest 8 2 127
|
|
44 |
*
|
|
45 |
* Test for 8bpp TRANSLUCENT image
|
|
46 |
* @run main GrayPngTest 8 3 127
|
|
47 |
*
|
|
48 |
*/
|
|
49 |
|
|
50 |
import java.awt.Color;
|
|
51 |
import java.awt.Transparency;
|
|
52 |
import java.awt.image.BufferedImage;
|
|
53 |
import java.awt.image.IndexColorModel;
|
|
54 |
import java.awt.image.WritableRaster;
|
|
55 |
import java.io.File;
|
|
56 |
import java.io.IOException;
|
|
57 |
import java.util.Arrays;
|
|
58 |
import javax.imageio.ImageIO;
|
|
59 |
|
|
60 |
public class GrayPngTest {
|
|
61 |
|
|
62 |
public static void main(String[] args) throws IOException {
|
|
63 |
/*
|
|
64 |
* Expected argiments:
|
|
65 |
* args[0] - bits per pixel. Supported range: [1, 8]
|
|
66 |
* args[1] - transparency type. Should be one form
|
|
67 |
* java.awt.Transparency type constants.
|
|
68 |
* args[2] - transparent pixel for BITMASK transparency type,
|
|
69 |
* otherwise is ignored.
|
|
70 |
*/
|
|
71 |
int bpp = 4;
|
|
72 |
int trans_type = Transparency.BITMASK;
|
|
73 |
int trans_pixel = 3;
|
|
74 |
try {
|
|
75 |
bpp = Integer.parseInt(args[0]);
|
|
76 |
trans_type = Integer.parseInt(args[1]);
|
|
77 |
trans_pixel = Integer.parseInt(args[2]);
|
|
78 |
} catch (NumberFormatException e) {
|
|
79 |
System.out.println("Ignore ncorrect bpp value: " + args[0]);
|
|
80 |
} catch (ArrayIndexOutOfBoundsException e) {
|
|
81 |
System.out.println("Default test argumens.");
|
|
82 |
}
|
|
83 |
|
|
84 |
new GrayPngTest(bpp).doTest(trans_type, trans_pixel);
|
|
85 |
}
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
private BufferedImage getTestImage(int trans_type, int trans_pixel) {
|
|
90 |
|
|
91 |
IndexColorModel icm = null;
|
|
92 |
switch(trans_type) {
|
|
93 |
case Transparency.OPAQUE:
|
|
94 |
icm = new IndexColorModel(bpp, numColors, r, g, b);
|
|
95 |
break;
|
|
96 |
case Transparency.BITMASK:
|
|
97 |
icm = new IndexColorModel(bpp, numColors, r, g, b, trans_pixel);
|
|
98 |
break;
|
|
99 |
case Transparency.TRANSLUCENT:
|
|
100 |
a = Arrays.copyOf(r, r.length);
|
|
101 |
icm = new IndexColorModel(bpp, numColors, r, g, b, a);
|
|
102 |
break;
|
|
103 |
default:
|
|
104 |
throw new RuntimeException("Invalid transparency: " + trans_type);
|
|
105 |
}
|
|
106 |
|
|
107 |
int w = 256 * 2;
|
|
108 |
int h = 200;
|
|
109 |
|
|
110 |
dx = w / (numColors);
|
|
111 |
|
|
112 |
WritableRaster wr = icm.createCompatibleWritableRaster(w, h);
|
|
113 |
for (int i = 0; i < numColors; i ++) {
|
|
114 |
int rx = i * dx;
|
|
115 |
|
|
116 |
int[] samples = new int[h * dx];
|
|
117 |
Arrays.fill(samples, i);
|
|
118 |
wr.setPixels(rx, 0, dx, h, samples);
|
|
119 |
}
|
|
120 |
|
|
121 |
// horizontal line with transparent color
|
|
122 |
int[] samples = new int[w * 10];
|
|
123 |
Arrays.fill(samples, trans_pixel);
|
|
124 |
wr.setPixels(0, h / 2 - 5, w, 10, samples);
|
|
125 |
|
|
126 |
// create index color model
|
|
127 |
return new BufferedImage(icm, wr, false, null);
|
|
128 |
}
|
|
129 |
|
|
130 |
static File pwd = new File(".");
|
|
131 |
|
|
132 |
private BufferedImage src;
|
|
133 |
private BufferedImage dst;
|
|
134 |
private int bpp;
|
|
135 |
private int numColors;
|
|
136 |
|
|
137 |
private int dx;
|
|
138 |
|
|
139 |
private byte[] r;
|
|
140 |
private byte[] g;
|
|
141 |
private byte[] b;
|
|
142 |
|
|
143 |
private byte[] a;
|
|
144 |
|
|
145 |
protected GrayPngTest(int bpp) {
|
|
146 |
if (0 > bpp || bpp > 8) {
|
|
147 |
throw new RuntimeException("Invalid bpp: " + bpp);
|
|
148 |
}
|
|
149 |
this.bpp = bpp;
|
|
150 |
numColors = (1 << bpp);
|
|
151 |
System.out.println("Num colors: " + numColors);
|
|
152 |
|
|
153 |
// create palette
|
|
154 |
r = new byte[numColors];
|
|
155 |
g = new byte[numColors];
|
|
156 |
b = new byte[numColors];
|
|
157 |
|
|
158 |
int dc = 0xff / (numColors - 1);
|
|
159 |
System.out.println("dc = " + dc);
|
|
160 |
|
|
161 |
for (int i = 0; i < numColors; i ++) {
|
|
162 |
byte l = (byte)(i * dc);
|
|
163 |
r[i] = l; g[i] = l; b[i] = l;
|
|
164 |
}
|
|
165 |
}
|
|
166 |
|
|
167 |
public void doTest() throws IOException {
|
|
168 |
for (int i = 0; i < numColors; i++) {
|
|
169 |
doTest(Transparency.BITMASK, i);
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
public void doTest(int trans_type, int trans_index) throws IOException {
|
|
174 |
src = getTestImage(trans_type, trans_index);
|
|
175 |
|
|
176 |
System.out.println("src: " + src);
|
|
177 |
|
|
178 |
File f = File.createTempFile("gray_png_" + bpp + "bpp_" +
|
|
179 |
trans_type + "tt_" +
|
|
180 |
trans_index + "tp_", ".png", pwd);
|
|
181 |
System.out.println("File: " + f.getAbsolutePath());
|
|
182 |
if (!ImageIO.write(src, "png", f)) {
|
|
183 |
throw new RuntimeException("Writing failed!");
|
|
184 |
};
|
|
185 |
|
|
186 |
try {
|
|
187 |
dst = ImageIO.read(f);
|
|
188 |
System.out.println("dst: " + dst);
|
|
189 |
} catch (Exception e) {
|
|
190 |
throw new RuntimeException("Test FAILED.", e);
|
|
191 |
}
|
|
192 |
|
|
193 |
checkImages();
|
|
194 |
}
|
|
195 |
|
|
196 |
private void checkImages() {
|
|
197 |
for (int i = 0; i < numColors; i++) {
|
|
198 |
int src_rgb = src.getRGB(i * dx, 5);
|
|
199 |
int dst_rgb = dst.getRGB(i * dx, 5);
|
|
200 |
|
|
201 |
// here we check transparency only due to possible colors space
|
|
202 |
// differences (sRGB in indexed source and Gray in gray+alpha destination)
|
|
203 |
if ((0xff000000 & src_rgb) != (0xff000000 & dst_rgb)) {
|
|
204 |
throw new RuntimeException("Test FAILED. Color difference detected: " +
|
|
205 |
Integer.toHexString(dst_rgb) + " instead of " +
|
|
206 |
Integer.toHexString(src_rgb) + " for index " + i);
|
|
207 |
|
|
208 |
}
|
|
209 |
}
|
|
210 |
}
|
|
211 |
}
|