Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/imageio/plugins/gif/RGBAnimationTest.java
38918 views
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 6324581
27
* @summary Test verifies that RGB images are written to animated GIF image use
28
* local color table if image palette is not equals to the global color
29
* table
30
*/
31
32
import java.awt.Color;
33
import java.awt.Graphics2D;
34
import java.awt.image.BufferedImage;
35
import java.io.ByteArrayInputStream;
36
import java.io.ByteArrayOutputStream;
37
import java.io.File;
38
import java.io.FileOutputStream;
39
import java.io.IOException;
40
import java.util.ArrayList;
41
42
import javax.imageio.IIOImage;
43
import javax.imageio.ImageIO;
44
import javax.imageio.ImageReader;
45
import javax.imageio.ImageTypeSpecifier;
46
import javax.imageio.ImageWriteParam;
47
import javax.imageio.ImageWriter;
48
import javax.imageio.metadata.IIOMetadata;
49
import javax.imageio.stream.ImageOutputStream;
50
51
import com.sun.imageio.plugins.gif.GIFImageMetadata;
52
53
public class RGBAnimationTest {
54
protected static String format = "GIF";
55
protected static boolean doSave = true;
56
57
Frame[] frames;
58
ImageWriter writer;
59
ImageReader reader;
60
61
public static void main(String[] args) throws IOException {
62
RGBAnimationTest test = new RGBAnimationTest();
63
test.doTest();
64
}
65
/** Creates a new instance of RGBAnimationTest */
66
public RGBAnimationTest() {
67
frames = new Frame[4];
68
69
70
frames[0] = new Frame(new Color[] {Color.red, Color.green});
71
frames[1] = new Frame(new Color[] {Color.green, Color.cyan});
72
frames[2] = new Frame(new Color[] {Color.cyan, Color.yellow});
73
frames[3] = new Frame(new Color[] {Color.yellow, Color.red});
74
75
writer = ImageIO.getImageWritersByFormatName(format).next();
76
reader = ImageIO.getImageReadersByFormatName(format).next();
77
}
78
79
public void doTest() throws IOException {
80
ByteArrayOutputStream baos = new ByteArrayOutputStream();
81
writer.reset();
82
ImageOutputStream ios = ImageIO.createImageOutputStream(baos);
83
writer.setOutput(ios);
84
85
ImageWriteParam wparam = prepareWriteParam();
86
87
IIOMetadata streamMetadata = prepareStreamMetadata(wparam);
88
89
writer.prepareWriteSequence(streamMetadata);
90
91
for (int i = 0; i < frames.length; i++) {
92
BufferedImage src = frames[i].getImage();
93
IIOMetadata imageMetadata = prepareImageMetadata(i, src, wparam);
94
IIOImage img = new IIOImage(src, null, imageMetadata);
95
96
writer.writeToSequence(img, wparam);
97
}
98
writer.endWriteSequence();
99
ios.flush();
100
ios.close();
101
102
if (doSave) {
103
File f = File.createTempFile("wr_test_", "." + format, new File("."));
104
System.out.println("Save to file: " + f.getCanonicalPath());
105
FileOutputStream fos = new FileOutputStream(f);
106
fos.write(baos.toByteArray());
107
fos.flush();
108
fos.close();
109
}
110
// read result
111
reader.reset();
112
ByteArrayInputStream bais =
113
new ByteArrayInputStream(baos.toByteArray());
114
reader.setInput(ImageIO.createImageInputStream(bais));
115
116
int minIndex = reader.getMinIndex();
117
int numImages = reader.getNumImages(true);
118
119
for (int i = 0; i < numImages; i++) {
120
BufferedImage dst = reader.read(i + minIndex);
121
frames[i].checkResult(dst);
122
}
123
}
124
125
protected IIOMetadata prepareImageMetadata(int i, BufferedImage img, ImageWriteParam wparam) {
126
GIFImageMetadata idata = (GIFImageMetadata)
127
writer.getDefaultImageMetadata(ImageTypeSpecifier.createFromRenderedImage(img), wparam);
128
129
idata.delayTime = 100;
130
idata.disposalMethod = 0;
131
idata.transparentColorFlag = false;
132
133
if (i == 0) {
134
ArrayList<byte[]> appIDs = new ArrayList<byte[]>();
135
appIDs.add(new String("NETSCAPE").getBytes());
136
ArrayList<byte[]> authCodes = new ArrayList<byte[]>();
137
authCodes.add(new String("2.0").getBytes());
138
ArrayList<byte[]> appData = new ArrayList<byte[]>();
139
byte[] authData = {1, 0, 0};
140
appData.add(authData);
141
142
idata.applicationIDs = appIDs;
143
idata.authenticationCodes = authCodes;
144
idata.applicationData = appData;
145
}
146
return idata;
147
}
148
149
protected ImageWriteParam prepareWriteParam() {
150
return writer.getDefaultWriteParam();
151
}
152
153
protected IIOMetadata prepareStreamMetadata(ImageWriteParam wparam) {
154
return writer.getDefaultStreamMetadata(wparam);
155
}
156
157
}
158
159
class Frame {
160
protected static int type = BufferedImage.TYPE_INT_RGB;
161
protected static int dx = 100;
162
protected static int h = 100;
163
164
protected Color[] colors;
165
protected BufferedImage img;
166
167
public Frame(Color[] colors) {
168
this.colors = colors;
169
img = null;
170
}
171
172
public BufferedImage getImage() {
173
if (img == null) {
174
img = new BufferedImage(dx * colors.length, h, type);
175
Graphics2D g = img.createGraphics();
176
for (int i = 0; i < colors.length; i++) {
177
g.setColor(colors[i]);
178
g.fillRect(dx * i, 0, dx, h);
179
}
180
}
181
return img;
182
}
183
184
public void checkResult(BufferedImage dst) {
185
int y = h / 2;
186
int x = dx / 2;
187
for (int i = 0; i < colors.length; i++) {
188
189
int srcRgb = img.getRGB(i * dx + x, y);
190
int dstRgb = dst.getRGB(i * dx + x, y);
191
192
if (srcRgb != dstRgb) {
193
throw new RuntimeException("Test failed due to color difference: " +
194
Integer.toHexString(dstRgb) + " instead of " +
195
Integer.toHexString(srcRgb));
196
}
197
}
198
}
199
}
200
201