Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/png/ITXtTest.java
41155 views
1
/*
2
* Copyright (c) 2008, 2016, 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 6541476 7059970
27
* @summary Test verifies that ImageIO PNG plug-in correctly handles the
28
* iTxt chunk (International textual data).
29
*
30
* @run main ITXtTest
31
*/
32
33
import java.awt.Color;
34
import java.awt.Graphics2D;
35
import java.awt.image.BufferedImage;
36
import java.io.File;
37
38
import javax.imageio.ImageIO;
39
import javax.imageio.ImageReader;
40
import javax.imageio.IIOImage;
41
import javax.imageio.ImageTypeSpecifier;
42
import javax.imageio.ImageWriter;
43
import javax.imageio.metadata.IIOMetadata;
44
import javax.imageio.metadata.IIOMetadataNode;
45
import javax.imageio.stream.ImageOutputStream;
46
import javax.imageio.stream.ImageInputStream;
47
48
import org.w3c.dom.Node;
49
50
public class ITXtTest {
51
static public void main(String args[]) {
52
ITXtTest t_en = new ITXtTest();
53
t_en.description = "xml - en";
54
t_en.keyword = "XML:com.adobe.xmp";
55
t_en.isCompressed = false;
56
t_en.compression = 0;
57
t_en.language = "en";
58
t_en.trasKeyword = "XML:com.adobe.xmp";
59
t_en.text = "<xml>Something</xml>";
60
61
doTest(t_en);
62
63
// check compression case
64
t_en.isCompressed = true;
65
t_en.description = "xml - en - compressed";
66
67
doTest(t_en);
68
69
ITXtTest t_ru = new ITXtTest();
70
t_ru.description = "xml - ru";
71
t_ru.keyword = "XML:com.adobe.xmp";
72
t_ru.isCompressed = false;
73
t_ru.compression = 0;
74
t_ru.language = "ru";
75
t_ru.trasKeyword = "\u0410\u0410\u0410\u0410\u0410 XML";
76
t_ru.text = "<xml>\u042A\u042F\u042F\u042F\u042F\u042F\u042F</xml>";
77
78
doTest(t_ru);
79
80
t_ru.isCompressed = true;
81
t_ru.description = "xml - ru - compressed";
82
83
doTest(t_ru);
84
}
85
86
87
String description;
88
89
String keyword;
90
boolean isCompressed;
91
int compression;
92
String language;
93
String trasKeyword;
94
String text;
95
96
97
public IIOMetadataNode getNode() {
98
IIOMetadataNode iTXt = new IIOMetadataNode("iTXt");
99
IIOMetadataNode iTXtEntry = new IIOMetadataNode("iTXtEntry");
100
iTXtEntry.setAttribute("keyword", keyword);
101
iTXtEntry.setAttribute("compressionFlag",
102
isCompressed ? "true" : "false");
103
iTXtEntry.setAttribute("compressionMethod",
104
Integer.toString(compression));
105
iTXtEntry.setAttribute("languageTag", language);
106
iTXtEntry.setAttribute("translatedKeyword",
107
trasKeyword);
108
iTXtEntry.setAttribute("text", text);
109
iTXt.appendChild(iTXtEntry);
110
return iTXt;
111
}
112
113
public static ITXtTest getFromNode(IIOMetadataNode n) {
114
ITXtTest t = new ITXtTest();
115
116
if (!"iTXt".equals(n.getNodeName())) {
117
throw new RuntimeException("Invalid node");
118
}
119
IIOMetadataNode e = (IIOMetadataNode)n.getFirstChild();
120
if (!"iTXtEntry".equals(e.getNodeName())) {
121
throw new RuntimeException("Invalid entry node");
122
}
123
t.keyword = e.getAttribute("keyword");
124
t.isCompressed =
125
Boolean.valueOf(e.getAttribute("compressionFlag")).booleanValue();
126
t.compression =
127
Integer.valueOf(e.getAttribute("compressionMethod")).intValue();
128
t.language = e.getAttribute("languageTag");
129
t.trasKeyword = e.getAttribute("translatedKeyword");
130
t.text = e.getAttribute("text");
131
132
return t;
133
}
134
135
@Override
136
public boolean equals(Object o) {
137
if (! (o instanceof ITXtTest)) {
138
return false;
139
}
140
ITXtTest t = (ITXtTest)o;
141
if (!keyword.equals(t.keyword)) { return false; }
142
if (isCompressed != t.isCompressed) { return false; }
143
if (compression != t.compression) { return false; }
144
if (!language.equals(t.language)) { return false; }
145
if (!trasKeyword.equals(t.trasKeyword)) { return false; }
146
if (!text.equals(t.text)) { return false; }
147
148
return true;
149
}
150
151
152
153
private static void doTest(ITXtTest src) {
154
155
System.out.println("Test: " + src.description);
156
157
File file = new File("test.png");
158
159
try {
160
writeTo(file, src);
161
ITXtTest dst = readFrom(file);
162
if (dst == null || !dst.equals(src)) {
163
throw new RuntimeException("Test failed.");
164
}
165
} finally {
166
file.delete();
167
}
168
System.out.println("Test passed.");
169
}
170
171
private static void writeTo(File f, ITXtTest t) {
172
BufferedImage src = createBufferedImage();
173
try (ImageOutputStream imageOutputStream =
174
ImageIO.createImageOutputStream(f)) {
175
176
ImageTypeSpecifier imageTypeSpecifier =
177
new ImageTypeSpecifier(src);
178
ImageWriter imageWriter =
179
ImageIO.getImageWritersByFormatName("PNG").next();
180
181
imageWriter.setOutput(imageOutputStream);
182
183
IIOMetadata m =
184
imageWriter.getDefaultImageMetadata(imageTypeSpecifier, null);
185
186
String format = m.getNativeMetadataFormatName();
187
Node root = m.getAsTree(format);
188
189
IIOMetadataNode iTXt = t.getNode();
190
root.appendChild(iTXt);
191
m.setFromTree(format, root);
192
193
imageWriter.write(new IIOImage(src, null, m));
194
System.out.println("Writing done.");
195
} catch (Throwable e) {
196
throw new RuntimeException("Writing test failed.", e);
197
}
198
}
199
200
private static ITXtTest readFrom(File f) {
201
try (ImageInputStream imageInputStream =
202
ImageIO.createImageInputStream(f)) {
203
204
ImageReader r = ImageIO.getImageReaders(imageInputStream).next();
205
r.setInput(imageInputStream);
206
207
IIOImage dst = r.readAll(0, null);
208
209
// look for iTXt node
210
IIOMetadata m = dst.getMetadata();
211
Node root = m.getAsTree(m.getNativeMetadataFormatName());
212
Node n = root.getFirstChild();
213
while (n != null && !"iTXt".equals(n.getNodeName())) {
214
n = n.getNextSibling();
215
}
216
if (n == null) {
217
throw new RuntimeException("No iTXt node!");
218
}
219
ITXtTest t = ITXtTest.getFromNode((IIOMetadataNode)n);
220
return t;
221
} catch (Throwable e) {
222
throw new RuntimeException("Reading test failed.", e);
223
}
224
}
225
226
private static BufferedImage createBufferedImage() {
227
BufferedImage image = new BufferedImage(128, 128,
228
BufferedImage.TYPE_4BYTE_ABGR_PRE);
229
Graphics2D graph = image.createGraphics();
230
graph.setPaintMode();
231
graph.setColor(Color.orange);
232
graph.fillRect(32, 32, 64, 64);
233
graph.dispose();
234
return image;
235
}
236
}
237
238
239