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 6570475
|
|
27 |
* @summary Test verifies that palette comparison procedure of
|
|
28 |
* ImageRepresentation class does not produce extra transparent
|
|
29 |
* pixels.
|
|
30 |
*
|
|
31 |
* @run main LUTCompareTest
|
|
32 |
*/
|
|
33 |
|
|
34 |
|
|
35 |
import java.awt.Color;
|
|
36 |
import java.awt.Dimension;
|
|
37 |
import java.awt.Graphics;
|
|
38 |
import java.awt.Graphics2D;
|
|
39 |
import java.awt.Image;
|
|
40 |
import java.awt.MediaTracker;
|
|
41 |
import java.awt.Toolkit;
|
|
42 |
import java.awt.image.BufferedImage;
|
|
43 |
import java.awt.image.DataBuffer;
|
|
44 |
import java.awt.image.ImageObserver;
|
|
45 |
import java.awt.image.IndexColorModel;
|
|
46 |
import java.awt.image.WritableRaster;
|
|
47 |
import java.io.File;
|
|
48 |
import java.io.IOException;
|
|
49 |
import java.util.Arrays;
|
|
50 |
import javax.imageio.IIOImage;
|
|
51 |
import javax.imageio.ImageIO;
|
|
52 |
import javax.imageio.ImageWriteParam;
|
|
53 |
import javax.imageio.ImageWriter;
|
|
54 |
import javax.imageio.stream.ImageOutputStream;
|
|
55 |
import javax.swing.JComponent;
|
|
56 |
import javax.swing.JFrame;
|
|
57 |
|
|
58 |
public class LUTCompareTest implements ImageObserver {
|
|
59 |
|
|
60 |
public static void main(String[] args) throws IOException {
|
|
61 |
Image img = createTestImage();
|
|
62 |
|
|
63 |
Toolkit tk = Toolkit.getDefaultToolkit();
|
|
64 |
|
|
65 |
LUTCompareTest o = new LUTCompareTest(img);
|
|
66 |
|
|
67 |
tk.prepareImage(img, -1, -1, o);
|
|
68 |
|
|
69 |
while(!o.isImageReady()) {
|
|
70 |
synchronized(lock) {
|
|
71 |
try {
|
|
72 |
lock.wait(200);
|
|
73 |
} catch (InterruptedException e) {
|
|
74 |
}
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
checkResults(img);
|
|
79 |
}
|
|
80 |
|
|
81 |
private static Object lock = new Object();
|
|
82 |
|
|
83 |
Image image;
|
|
84 |
|
|
85 |
boolean isReady = false;
|
|
86 |
|
|
87 |
public LUTCompareTest(Image img) {
|
|
88 |
this.image = img;
|
|
89 |
}
|
|
90 |
|
|
91 |
public boolean imageUpdate(Image image, int info,
|
|
92 |
int x, int y, int w, int h) {
|
|
93 |
if (image == this.image) {
|
|
94 |
System.out.println("Image status: " + dump(info));
|
|
95 |
synchronized(this) {
|
|
96 |
isReady = (info & ImageObserver.ALLBITS) != 0;
|
|
97 |
if (isReady) {
|
|
98 |
synchronized(lock) {
|
|
99 |
lock.notifyAll();
|
|
100 |
}
|
|
101 |
}
|
|
102 |
}
|
|
103 |
return !isReady;
|
|
104 |
} else {
|
|
105 |
return true;
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
|
109 |
public synchronized boolean isImageReady() {
|
|
110 |
return isReady;
|
|
111 |
}
|
|
112 |
|
|
113 |
private static void checkResults(Image image) {
|
|
114 |
BufferedImage buf = new BufferedImage(w, h,
|
|
115 |
BufferedImage.TYPE_INT_RGB);
|
|
116 |
Graphics2D g = buf.createGraphics();
|
|
117 |
g.setColor(Color.pink);
|
|
118 |
g.fillRect(0, 0, w, h);
|
|
119 |
|
|
120 |
g.drawImage(image, 0, 0, null);
|
|
121 |
|
|
122 |
g.dispose();
|
|
123 |
|
|
124 |
int rgb = buf.getRGB(w/2, h/2);
|
|
125 |
|
|
126 |
System.out.printf("Result color: %x\n", rgb);
|
|
127 |
|
|
128 |
/* Buffered image should be the same as the last frame
|
|
129 |
* of animated sequence (which is filled with blue).
|
|
130 |
* Any other color indicates the problem.
|
|
131 |
*/
|
|
132 |
if (rgb != 0xff0000ff) {
|
|
133 |
throw new RuntimeException("Test FAILED!");
|
|
134 |
}
|
|
135 |
|
|
136 |
System.out.println("Test PASSED.");
|
|
137 |
}
|
|
138 |
|
|
139 |
private static int w = 100;
|
|
140 |
private static int h = 100;
|
|
141 |
|
|
142 |
/* Create test image with two frames:
|
|
143 |
* 1) with {red, red} palette
|
|
144 |
* 2) with {blue, red } palette
|
|
145 |
*/
|
|
146 |
private static Image createTestImage() throws IOException {
|
|
147 |
BufferedImage frame1 = createFrame(new int[] { 0xffff0000, 0xffff0000 });
|
|
148 |
BufferedImage frame2 = createFrame(new int[] { 0xff0000ff, 0xffff0000 });
|
|
149 |
|
|
150 |
ImageWriter writer = ImageIO.getImageWritersByFormatName("GIF").next();
|
|
151 |
ImageOutputStream ios = ImageIO.createImageOutputStream(new File("lut_test.gif"));
|
|
152 |
ImageWriteParam param = writer.getDefaultWriteParam();
|
|
153 |
writer.setOutput(ios);
|
|
154 |
writer.prepareWriteSequence(null);
|
|
155 |
writer.writeToSequence(new IIOImage(frame1, null, null), param);
|
|
156 |
writer.writeToSequence(new IIOImage(frame2, null, null), param);
|
|
157 |
writer.endWriteSequence();
|
|
158 |
writer.reset();
|
|
159 |
writer.dispose();
|
|
160 |
|
|
161 |
ios.flush();
|
|
162 |
ios.close();
|
|
163 |
|
|
164 |
return Toolkit.getDefaultToolkit().createImage("lut_test.gif");
|
|
165 |
}
|
|
166 |
|
|
167 |
private static BufferedImage createFrame(int[] palette) {
|
|
168 |
IndexColorModel icm = new IndexColorModel(getNumBits(palette.length),
|
|
169 |
palette.length, palette, 0, false, -1, DataBuffer.TYPE_BYTE);
|
|
170 |
WritableRaster wr = icm.createCompatibleWritableRaster(w, h);
|
|
171 |
int[] samples = new int[w * h];
|
|
172 |
Arrays.fill(samples, 0);
|
|
173 |
wr.setSamples(0, 0, w, h, 0, samples);
|
|
174 |
|
|
175 |
BufferedImage img = new BufferedImage(icm, wr, false, null);
|
|
176 |
return img;
|
|
177 |
}
|
|
178 |
|
|
179 |
private static int getNumBits(int size) {
|
|
180 |
if (size < 0) {
|
|
181 |
throw new RuntimeException("Invalid palette size: " + size);
|
|
182 |
} else if (size < 3) {
|
|
183 |
return 1;
|
|
184 |
} else if (size < 5) {
|
|
185 |
return 2;
|
|
186 |
} else {
|
|
187 |
throw new RuntimeException("Palette size is not supported: " + size);
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
|
191 |
private static String[] name = new String[] {
|
|
192 |
"WIDTH", "HEIGHT", "PROPERTIES", "SOMEBITS",
|
|
193 |
"FRAMEBITS", "ALLBITS", "ERROR", "ABORT"
|
|
194 |
};
|
|
195 |
|
|
196 |
private static String dump(int info) {
|
|
197 |
String res = "";
|
|
198 |
int count = 0;
|
|
199 |
while (info != 0) {
|
|
200 |
//System.out.println("info = " + info);
|
|
201 |
if ((info & 0x1) == 1) {
|
|
202 |
res += name[count];
|
|
203 |
if ((info >> 1) != 0) {
|
|
204 |
res += " ";
|
|
205 |
}
|
|
206 |
|
|
207 |
}
|
|
208 |
count ++;
|
|
209 |
info = (info >> 1);
|
|
210 |
}
|
|
211 |
return res;
|
|
212 |
}
|
|
213 |
}
|