Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/imageio/plugins/tiff/ReadUnknownTagsTest.java
42283 views
1
/*
2
* Copyright (c) 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 8154058
27
* @author a.stepanov
28
* @summary Some checks for ignoring metadata
29
* @run main ReadUnknownTagsTest
30
*/
31
32
import java.awt.*;
33
import java.awt.image.BufferedImage;
34
import java.io.*;
35
import javax.imageio.*;
36
import javax.imageio.metadata.*;
37
38
import javax.imageio.stream.*;
39
import javax.imageio.plugins.tiff.*;
40
41
42
public class ReadUnknownTagsTest {
43
44
private final static int SZ = 50;
45
private final static Color C = Color.RED;
46
47
private final static int DESCRIPTION_TAG =
48
BaselineTIFFTagSet.TAG_IMAGE_DESCRIPTION;
49
private final static String DESCRIPTION = "A Test Image";
50
51
private final static int FAX_TAG = FaxTIFFTagSet.TAG_CLEAN_FAX_DATA;
52
private final static short FAX_DATA =
53
FaxTIFFTagSet.CLEAN_FAX_DATA_ERRORS_UNCORRECTED;
54
55
private final boolean ignoreMetadata;
56
private final boolean readUnknownTags;
57
58
public ReadUnknownTagsTest(boolean ignoreMetadata,
59
boolean readUnknownTags) {
60
this.ignoreMetadata = ignoreMetadata;
61
this.readUnknownTags = readUnknownTags;
62
}
63
64
private ImageWriter getTIFFWriter() {
65
66
java.util.Iterator<ImageWriter> writers =
67
ImageIO.getImageWritersByFormatName("TIFF");
68
if (!writers.hasNext()) {
69
throw new RuntimeException("No writers available for TIFF format");
70
}
71
return writers.next();
72
}
73
74
private ImageReader getTIFFReader() {
75
76
java.util.Iterator<ImageReader> readers =
77
ImageIO.getImageReadersByFormatName("TIFF");
78
if (!readers.hasNext()) {
79
throw new RuntimeException("No readers available for TIFF format");
80
}
81
return readers.next();
82
}
83
84
85
private void writeImage() throws Exception {
86
87
String fn = "test-" + ignoreMetadata + ".tiff";
88
OutputStream s = new BufferedOutputStream(new FileOutputStream(fn));
89
try (ImageOutputStream ios = ImageIO.createImageOutputStream(s)) {
90
91
ImageWriter writer = getTIFFWriter();
92
writer.setOutput(ios);
93
94
BufferedImage img = new BufferedImage(SZ, SZ,
95
BufferedImage.TYPE_INT_RGB);
96
Graphics g = img.getGraphics();
97
g.setColor(C);
98
g.fillRect(0, 0, SZ, SZ);
99
g.dispose();
100
101
ImageWriteParam param = writer.getDefaultWriteParam();
102
103
IIOMetadata md = writer.getDefaultImageMetadata(
104
new ImageTypeSpecifier(img), param);
105
106
TIFFDirectory dir = TIFFDirectory.createFromMetadata(md);
107
108
TIFFTag descTag =
109
BaselineTIFFTagSet.getInstance().getTag(DESCRIPTION_TAG);
110
dir.addTIFFField(new TIFFField(descTag, TIFFTag.TIFF_ASCII, 1,
111
new String[] {DESCRIPTION}));
112
113
TIFFTag faxTag = FaxTIFFTagSet.getInstance().getTag(FAX_TAG);
114
dir.addTIFFField(new TIFFField(faxTag, FAX_DATA));
115
116
writer.write(new IIOImage(img, null, dir.getAsMetadata()));
117
118
ios.flush();
119
writer.dispose();
120
}
121
s.close();
122
}
123
124
private void readAndCheckImage() throws Exception {
125
126
ImageReader reader = getTIFFReader();
127
128
String fn = "test-" + ignoreMetadata + ".tiff";
129
ImageInputStream s = ImageIO.createImageInputStream(new File(fn));
130
131
reader.setInput(s, false, ignoreMetadata);
132
133
int ni = reader.getNumImages(true);
134
check(ni == 1, "invalid number of images");
135
136
137
TIFFImageReadParam param = new TIFFImageReadParam();
138
// fax data are allowed by default
139
param.removeAllowedTagSet(FaxTIFFTagSet.getInstance());
140
141
// readUnknownTags setting
142
if (param.getReadUnknownTags()) {
143
throw new RuntimeException("Default readUnknownTags is not false");
144
}
145
param.setReadUnknownTags(readUnknownTags);
146
if (param.getReadUnknownTags() != readUnknownTags) {
147
throw new RuntimeException("Incorrect readUnknownTags setting "
148
+ "\"" + readUnknownTags + "\"");
149
}
150
151
// read images and metadata
152
IIOImage i = reader.readAll(0, param);
153
BufferedImage bi = (BufferedImage) i.getRenderedImage();
154
155
check(bi.getWidth() == SZ, "invalid width");
156
check(bi.getHeight() == SZ, "invalid height");
157
Color c = new Color(bi.getRGB(SZ / 2, SZ / 2));
158
check(c.equals(C), "invalid color");
159
160
IIOMetadata metadata = i.getMetadata();
161
162
//
163
// Verify presence of image metadata
164
//
165
if (metadata == null) {
166
throw new RuntimeException("No image metadata retrieved");
167
}
168
169
TIFFDirectory dir = TIFFDirectory.createFromMetadata(metadata);
170
171
//
172
// Verify presence of essential ImageWidth field regardless of
173
// settings of ignoreMetadata and readUnknownTags
174
//
175
int failures = 0;
176
if (!dir.containsTIFFField(BaselineTIFFTagSet.TAG_IMAGE_WIDTH)) {
177
System.err.println("Metadata is missing essential ImageWidth tag");
178
failures++;
179
} else {
180
TIFFField widthField =
181
dir.getTIFFField(BaselineTIFFTagSet.TAG_IMAGE_WIDTH);
182
System.out.printf("ImageWidth: %d%n", widthField.getAsLong(0));
183
}
184
185
//
186
// Verify presence of non-essential baseline ImageDescription field
187
// if and only if ignoreMetadata == false
188
//
189
boolean hasDescription = dir.containsTIFFField(DESCRIPTION_TAG);
190
System.out.println("ImageDescription (" + !ignoreMetadata + "): "
191
+ hasDescription);
192
if (ignoreMetadata && hasDescription) {
193
System.err.println
194
("Description metadata present despite ignoreMetadata");
195
failures++;
196
} else if (!ignoreMetadata && !hasDescription) {
197
System.err.println
198
("Description metadata absent despite !ignoreMetadata");
199
failures++;
200
}
201
202
//
203
// Verify presence of CleanFaxData field if and only if
204
// ignoreMetadata == false and readUnknownTags == true
205
//
206
boolean shouldHaveFaxField = !ignoreMetadata && readUnknownTags;
207
boolean hasFaxField = dir.containsTIFFField(FAX_TAG);
208
System.out.println("CleanFaxData (" + shouldHaveFaxField + "): "
209
+ hasFaxField);
210
211
if (ignoreMetadata) {
212
if (hasFaxField) {
213
System.err.println
214
("Fax metadata present despite ignoreMetadata");
215
failures++;
216
}
217
} else { // !ignoreMetadata
218
if (!readUnknownTags && hasFaxField) {
219
System.err.println
220
("Fax metadata present despite !readUnknownTags");
221
failures++;
222
} else if (readUnknownTags && !hasFaxField) {
223
System.err.println
224
("Fax metadata absent despite readUnknownTags");
225
failures++;
226
}
227
}
228
229
if (failures > 0) {
230
throw new RuntimeException("Test failed for ignoreMetadata "
231
+ ignoreMetadata + " and readUnknownTags " + readUnknownTags);
232
}
233
}
234
235
public void run() {
236
try {
237
writeImage();
238
readAndCheckImage();
239
} catch (Exception e) {
240
throw new RuntimeException(e);
241
}
242
}
243
244
private void check(boolean ok, String msg) {
245
if (!ok) { throw new RuntimeException(msg); }
246
}
247
248
public static void main(String[] args) {
249
int failures = 0;
250
251
System.out.println();
252
for (boolean ignoreMetadata : new boolean[] {false, true}) {
253
for (boolean readUnknownTags : new boolean[] {false, true}) {
254
try {
255
System.out.printf
256
("ignoreMetadata: %s, readUnknownTags: %s%n",
257
ignoreMetadata, readUnknownTags);
258
(new ReadUnknownTagsTest(ignoreMetadata,
259
readUnknownTags)).run();
260
} catch (Exception e) {
261
e.printStackTrace();
262
failures++;
263
} finally {
264
System.out.println();
265
}
266
}
267
}
268
}
269
}
270
271