author | bae |
Sat, 01 Nov 2008 20:42:18 +0300 | |
changeset 1734 | 861400729115 |
parent 2 | 90ce3da70b43 |
child 2368 | 5a872c1edd4f |
permissions | -rw-r--r-- |
2 | 1 |
/* |
2 |
* Copyright 2000-2001 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. Sun designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Sun in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 |
* have any questions. |
|
24 |
*/ |
|
25 |
||
26 |
package com.sun.imageio.plugins.png; |
|
27 |
||
28 |
import java.awt.image.ColorModel; |
|
29 |
import java.awt.image.IndexColorModel; |
|
30 |
import java.awt.image.SampleModel; |
|
31 |
import java.util.ArrayList; |
|
32 |
import java.util.Iterator; |
|
33 |
import java.util.StringTokenizer; |
|
34 |
import javax.imageio.ImageTypeSpecifier; |
|
35 |
import javax.imageio.metadata.IIOInvalidTreeException; |
|
36 |
import javax.imageio.metadata.IIOMetadata; |
|
37 |
import javax.imageio.metadata.IIOMetadataFormat; |
|
38 |
import javax.imageio.metadata.IIOMetadataFormatImpl; |
|
39 |
import javax.imageio.metadata.IIOMetadataNode; |
|
40 |
import org.w3c.dom.Node; |
|
41 |
||
42 |
public class PNGMetadata extends IIOMetadata implements Cloneable { |
|
43 |
||
44 |
// package scope |
|
45 |
public static final String |
|
46 |
nativeMetadataFormatName = "javax_imageio_png_1.0"; |
|
47 |
||
48 |
protected static final String nativeMetadataFormatClassName |
|
49 |
= "com.sun.imageio.plugins.png.PNGMetadataFormat"; |
|
50 |
||
51 |
// Color types for IHDR chunk |
|
52 |
public static final String[] IHDR_colorTypeNames = { |
|
53 |
"Grayscale", null, "RGB", "Palette", |
|
54 |
"GrayAlpha", null, "RGBAlpha" |
|
55 |
}; |
|
56 |
||
57 |
public static final int[] IHDR_numChannels = { |
|
58 |
1, 0, 3, 3, 2, 0, 4 |
|
59 |
}; |
|
60 |
||
61 |
// Bit depths for IHDR chunk |
|
62 |
public static final String[] IHDR_bitDepths = { |
|
63 |
"1", "2", "4", "8", "16" |
|
64 |
}; |
|
65 |
||
66 |
// Compression methods for IHDR chunk |
|
67 |
public static final String[] IHDR_compressionMethodNames = { |
|
68 |
"deflate" |
|
69 |
}; |
|
70 |
||
71 |
// Filter methods for IHDR chunk |
|
72 |
public static final String[] IHDR_filterMethodNames = { |
|
73 |
"adaptive" |
|
74 |
}; |
|
75 |
||
76 |
// Interlace methods for IHDR chunk |
|
77 |
public static final String[] IHDR_interlaceMethodNames = { |
|
78 |
"none", "adam7" |
|
79 |
}; |
|
80 |
||
81 |
// Compression methods for iCCP chunk |
|
82 |
public static final String[] iCCP_compressionMethodNames = { |
|
83 |
"deflate" |
|
84 |
}; |
|
85 |
||
86 |
// Compression methods for zTXt chunk |
|
87 |
public static final String[] zTXt_compressionMethodNames = { |
|
88 |
"deflate" |
|
89 |
}; |
|
90 |
||
91 |
// "Unknown" unit for pHYs chunk |
|
92 |
public static final int PHYS_UNIT_UNKNOWN = 0; |
|
93 |
||
94 |
// "Meter" unit for pHYs chunk |
|
95 |
public static final int PHYS_UNIT_METER = 1; |
|
96 |
||
97 |
// Unit specifiers for pHYs chunk |
|
98 |
public static final String[] unitSpecifierNames = { |
|
99 |
"unknown", "meter" |
|
100 |
}; |
|
101 |
||
102 |
// Rendering intents for sRGB chunk |
|
103 |
public static final String[] renderingIntentNames = { |
|
104 |
"Perceptual", // 0 |
|
105 |
"Relative colorimetric", // 1 |
|
106 |
"Saturation", // 2 |
|
107 |
"Absolute colorimetric" // 3 |
|
108 |
||
109 |
}; |
|
110 |
||
111 |
// Color space types for Chroma->ColorSpaceType node |
|
112 |
public static final String[] colorSpaceTypeNames = { |
|
113 |
"GRAY", null, "RGB", "RGB", |
|
114 |
"GRAY", null, "RGB" |
|
115 |
}; |
|
116 |
||
117 |
// IHDR chunk |
|
118 |
public boolean IHDR_present; |
|
119 |
public int IHDR_width; |
|
120 |
public int IHDR_height; |
|
121 |
public int IHDR_bitDepth; |
|
122 |
public int IHDR_colorType; |
|
123 |
public int IHDR_compressionMethod; |
|
124 |
public int IHDR_filterMethod; |
|
125 |
public int IHDR_interlaceMethod; // 0 == none, 1 == adam7 |
|
126 |
||
127 |
// PLTE chunk |
|
128 |
public boolean PLTE_present; |
|
129 |
public byte[] PLTE_red; |
|
130 |
public byte[] PLTE_green; |
|
131 |
public byte[] PLTE_blue; |
|
132 |
||
133 |
// If non-null, used to reorder palette entries during encoding in |
|
134 |
// order to minimize the size of the tRNS chunk. Thus an index of |
|
135 |
// 'i' in the source should be encoded as index 'PLTE_order[i]'. |
|
136 |
// PLTE_order will be null unless 'initialize' is called with an |
|
137 |
// IndexColorModel image type. |
|
138 |
public int[] PLTE_order = null; |
|
139 |
||
140 |
// bKGD chunk |
|
141 |
// If external (non-PNG sourced) data has red = green = blue, |
|
142 |
// always store it as gray and promote when writing |
|
143 |
public boolean bKGD_present; |
|
144 |
public int bKGD_colorType; // PNG_COLOR_GRAY, _RGB, or _PALETTE |
|
145 |
public int bKGD_index; |
|
146 |
public int bKGD_gray; |
|
147 |
public int bKGD_red; |
|
148 |
public int bKGD_green; |
|
149 |
public int bKGD_blue; |
|
150 |
||
151 |
// cHRM chunk |
|
152 |
public boolean cHRM_present; |
|
153 |
public int cHRM_whitePointX; |
|
154 |
public int cHRM_whitePointY; |
|
155 |
public int cHRM_redX; |
|
156 |
public int cHRM_redY; |
|
157 |
public int cHRM_greenX; |
|
158 |
public int cHRM_greenY; |
|
159 |
public int cHRM_blueX; |
|
160 |
public int cHRM_blueY; |
|
161 |
||
162 |
// gAMA chunk |
|
163 |
public boolean gAMA_present; |
|
164 |
public int gAMA_gamma; |
|
165 |
||
166 |
// hIST chunk |
|
167 |
public boolean hIST_present; |
|
168 |
public char[] hIST_histogram; |
|
169 |
||
170 |
// iCCP chunk |
|
171 |
public boolean iCCP_present; |
|
172 |
public String iCCP_profileName; |
|
173 |
public int iCCP_compressionMethod; |
|
174 |
public byte[] iCCP_compressedProfile; |
|
175 |
||
176 |
// iTXt chunk |
|
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
177 |
public ArrayList<String> iTXt_keyword = new ArrayList<String>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
178 |
public ArrayList<Boolean> iTXt_compressionFlag = new ArrayList<Boolean>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
179 |
public ArrayList<Integer> iTXt_compressionMethod = new ArrayList<Integer>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
180 |
public ArrayList<String> iTXt_languageTag = new ArrayList<String>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
181 |
public ArrayList<String> iTXt_translatedKeyword = new ArrayList<String>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
182 |
public ArrayList<String> iTXt_text = new ArrayList<String>(); |
2 | 183 |
|
184 |
// pHYs chunk |
|
185 |
public boolean pHYs_present; |
|
186 |
public int pHYs_pixelsPerUnitXAxis; |
|
187 |
public int pHYs_pixelsPerUnitYAxis; |
|
188 |
public int pHYs_unitSpecifier; // 0 == unknown, 1 == meter |
|
189 |
||
190 |
// sBIT chunk |
|
191 |
public boolean sBIT_present; |
|
192 |
public int sBIT_colorType; // PNG_COLOR_GRAY, _GRAY_ALPHA, _RGB, _RGB_ALPHA |
|
193 |
public int sBIT_grayBits; |
|
194 |
public int sBIT_redBits; |
|
195 |
public int sBIT_greenBits; |
|
196 |
public int sBIT_blueBits; |
|
197 |
public int sBIT_alphaBits; |
|
198 |
||
199 |
// sPLT chunk |
|
200 |
public boolean sPLT_present; |
|
201 |
public String sPLT_paletteName; // 1-79 characters |
|
202 |
public int sPLT_sampleDepth; // 8 or 16 |
|
203 |
public int[] sPLT_red; |
|
204 |
public int[] sPLT_green; |
|
205 |
public int[] sPLT_blue; |
|
206 |
public int[] sPLT_alpha; |
|
207 |
public int[] sPLT_frequency; |
|
208 |
||
209 |
// sRGB chunk |
|
210 |
public boolean sRGB_present; |
|
211 |
public int sRGB_renderingIntent; |
|
212 |
||
213 |
// tEXt chunk |
|
214 |
public ArrayList tEXt_keyword = new ArrayList(); // 1-79 char Strings |
|
215 |
public ArrayList tEXt_text = new ArrayList(); // Strings |
|
216 |
||
217 |
// tIME chunk |
|
218 |
public boolean tIME_present; |
|
219 |
public int tIME_year; |
|
220 |
public int tIME_month; |
|
221 |
public int tIME_day; |
|
222 |
public int tIME_hour; |
|
223 |
public int tIME_minute; |
|
224 |
public int tIME_second; |
|
225 |
||
226 |
// tRNS chunk |
|
227 |
// If external (non-PNG sourced) data has red = green = blue, |
|
228 |
// always store it as gray and promote when writing |
|
229 |
public boolean tRNS_present; |
|
230 |
public int tRNS_colorType; // PNG_COLOR_GRAY, _RGB, or _PALETTE |
|
231 |
public byte[] tRNS_alpha; // May have fewer entries than PLTE_red, etc. |
|
232 |
public int tRNS_gray; |
|
233 |
public int tRNS_red; |
|
234 |
public int tRNS_green; |
|
235 |
public int tRNS_blue; |
|
236 |
||
237 |
// zTXt chunk |
|
238 |
public ArrayList zTXt_keyword = new ArrayList(); // Strings |
|
239 |
public ArrayList zTXt_compressionMethod = new ArrayList(); // Integers |
|
240 |
public ArrayList zTXt_text = new ArrayList(); // Strings |
|
241 |
||
242 |
// Unknown chunks |
|
243 |
public ArrayList unknownChunkType = new ArrayList(); // Strings |
|
244 |
public ArrayList unknownChunkData = new ArrayList(); // byte arrays |
|
245 |
||
246 |
public PNGMetadata() { |
|
247 |
super(true, |
|
248 |
nativeMetadataFormatName, |
|
249 |
nativeMetadataFormatClassName, |
|
250 |
null, null); |
|
251 |
} |
|
252 |
||
253 |
public PNGMetadata(IIOMetadata metadata) { |
|
254 |
// TODO -- implement |
|
255 |
} |
|
256 |
||
257 |
/** |
|
258 |
* Sets the IHDR_bitDepth and IHDR_colorType variables. |
|
259 |
* The <code>numBands</code> parameter is necessary since |
|
260 |
* we may only be writing a subset of the image bands. |
|
261 |
*/ |
|
262 |
public void initialize(ImageTypeSpecifier imageType, int numBands) { |
|
263 |
ColorModel colorModel = imageType.getColorModel(); |
|
264 |
SampleModel sampleModel = imageType.getSampleModel(); |
|
265 |
||
266 |
// Initialize IHDR_bitDepth |
|
267 |
int[] sampleSize = sampleModel.getSampleSize(); |
|
268 |
int bitDepth = sampleSize[0]; |
|
269 |
// Choose max bit depth over all channels |
|
270 |
// Fixes bug 4413109 |
|
271 |
for (int i = 1; i < sampleSize.length; i++) { |
|
272 |
if (sampleSize[i] > bitDepth) { |
|
273 |
bitDepth = sampleSize[i]; |
|
274 |
} |
|
275 |
} |
|
276 |
// Multi-channel images must have a bit depth of 8 or 16 |
|
277 |
if (sampleSize.length > 1 && bitDepth < 8) { |
|
278 |
bitDepth = 8; |
|
279 |
} |
|
280 |
||
281 |
// Round bit depth up to a power of 2 |
|
282 |
if (bitDepth > 2 && bitDepth < 4) { |
|
283 |
bitDepth = 4; |
|
284 |
} else if (bitDepth > 4 && bitDepth < 8) { |
|
285 |
bitDepth = 8; |
|
286 |
} else if (bitDepth > 8 && bitDepth < 16) { |
|
287 |
bitDepth = 16; |
|
288 |
} else if (bitDepth > 16) { |
|
289 |
throw new RuntimeException("bitDepth > 16!"); |
|
290 |
} |
|
291 |
IHDR_bitDepth = bitDepth; |
|
292 |
||
293 |
// Initialize IHDR_colorType |
|
294 |
if (colorModel instanceof IndexColorModel) { |
|
295 |
IndexColorModel icm = (IndexColorModel)colorModel; |
|
296 |
int size = icm.getMapSize(); |
|
297 |
||
298 |
byte[] reds = new byte[size]; |
|
299 |
icm.getReds(reds); |
|
300 |
byte[] greens = new byte[size]; |
|
301 |
icm.getGreens(greens); |
|
302 |
byte[] blues = new byte[size]; |
|
303 |
icm.getBlues(blues); |
|
304 |
||
305 |
// Determine whether the color tables are actually a gray ramp |
|
306 |
// if the color type has not been set previously |
|
307 |
boolean isGray = false; |
|
308 |
if (!IHDR_present || |
|
309 |
(IHDR_colorType != PNGImageReader.PNG_COLOR_PALETTE)) { |
|
310 |
isGray = true; |
|
311 |
int scale = 255/((1 << IHDR_bitDepth) - 1); |
|
312 |
for (int i = 0; i < size; i++) { |
|
313 |
byte red = reds[i]; |
|
314 |
if ((red != (byte)(i*scale)) || |
|
315 |
(red != greens[i]) || |
|
316 |
(red != blues[i])) { |
|
317 |
isGray = false; |
|
318 |
break; |
|
319 |
} |
|
320 |
} |
|
321 |
} |
|
322 |
||
323 |
// Determine whether transparency exists |
|
324 |
boolean hasAlpha = colorModel.hasAlpha(); |
|
325 |
||
326 |
byte[] alpha = null; |
|
327 |
if (hasAlpha) { |
|
328 |
alpha = new byte[size]; |
|
329 |
icm.getAlphas(alpha); |
|
330 |
} |
|
331 |
||
332 |
/* |
|
333 |
* NB: PNG_COLOR_GRAY_ALPHA color type may be not optimal for images |
|
334 |
* contained more than 1024 pixels (or even than 768 pixels in case of |
|
335 |
* single transparent pixel in palette). |
|
336 |
* For such images alpha samples in raster will occupy more space than |
|
337 |
* it is required to store palette so it could be reasonable to |
|
338 |
* use PNG_COLOR_PALETTE color type for large images. |
|
339 |
*/ |
|
340 |
||
341 |
if (isGray && hasAlpha && (bitDepth == 8 || bitDepth == 16)) { |
|
342 |
IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA; |
|
343 |
} else if (isGray && !hasAlpha) { |
|
344 |
IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY; |
|
345 |
} else { |
|
346 |
IHDR_colorType = PNGImageReader.PNG_COLOR_PALETTE; |
|
347 |
PLTE_present = true; |
|
348 |
PLTE_order = null; |
|
349 |
PLTE_red = (byte[])reds.clone(); |
|
350 |
PLTE_green = (byte[])greens.clone(); |
|
351 |
PLTE_blue = (byte[])blues.clone(); |
|
352 |
||
353 |
if (hasAlpha) { |
|
354 |
tRNS_present = true; |
|
355 |
tRNS_colorType = PNGImageReader.PNG_COLOR_PALETTE; |
|
356 |
||
357 |
PLTE_order = new int[alpha.length]; |
|
358 |
||
359 |
// Reorder the palette so that non-opaque entries |
|
360 |
// come first. Since the tRNS chunk does not have |
|
361 |
// to store trailing 255's, this can save a |
|
362 |
// considerable amount of space when encoding |
|
363 |
// images with only one transparent pixel value, |
|
364 |
// e.g., images from GIF sources. |
|
365 |
||
366 |
byte[] newAlpha = new byte[alpha.length]; |
|
367 |
||
368 |
// Scan for non-opaque entries and assign them |
|
369 |
// positions starting at 0. |
|
370 |
int newIndex = 0; |
|
371 |
for (int i = 0; i < alpha.length; i++) { |
|
372 |
if (alpha[i] != (byte)255) { |
|
373 |
PLTE_order[i] = newIndex; |
|
374 |
newAlpha[newIndex] = alpha[i]; |
|
375 |
++newIndex; |
|
376 |
} |
|
377 |
} |
|
378 |
int numTransparent = newIndex; |
|
379 |
||
380 |
// Scan for opaque entries and assign them |
|
381 |
// positions following the non-opaque entries. |
|
382 |
for (int i = 0; i < alpha.length; i++) { |
|
383 |
if (alpha[i] == (byte)255) { |
|
384 |
PLTE_order[i] = newIndex++; |
|
385 |
} |
|
386 |
} |
|
387 |
||
388 |
// Reorder the palettes |
|
389 |
byte[] oldRed = PLTE_red; |
|
390 |
byte[] oldGreen = PLTE_green; |
|
391 |
byte[] oldBlue = PLTE_blue; |
|
392 |
int len = oldRed.length; // All have the same length |
|
393 |
PLTE_red = new byte[len]; |
|
394 |
PLTE_green = new byte[len]; |
|
395 |
PLTE_blue = new byte[len]; |
|
396 |
for (int i = 0; i < len; i++) { |
|
397 |
PLTE_red[PLTE_order[i]] = oldRed[i]; |
|
398 |
PLTE_green[PLTE_order[i]] = oldGreen[i]; |
|
399 |
PLTE_blue[PLTE_order[i]] = oldBlue[i]; |
|
400 |
} |
|
401 |
||
402 |
// Copy only the transparent entries into tRNS_alpha |
|
403 |
tRNS_alpha = new byte[numTransparent]; |
|
404 |
System.arraycopy(newAlpha, 0, |
|
405 |
tRNS_alpha, 0, numTransparent); |
|
406 |
} |
|
407 |
} |
|
408 |
} else { |
|
409 |
if (numBands == 1) { |
|
410 |
IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY; |
|
411 |
} else if (numBands == 2) { |
|
412 |
IHDR_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA; |
|
413 |
} else if (numBands == 3) { |
|
414 |
IHDR_colorType = PNGImageReader.PNG_COLOR_RGB; |
|
415 |
} else if (numBands == 4) { |
|
416 |
IHDR_colorType = PNGImageReader.PNG_COLOR_RGB_ALPHA; |
|
417 |
} else { |
|
418 |
throw new RuntimeException("Number of bands not 1-4!"); |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
IHDR_present = true; |
|
423 |
} |
|
424 |
||
425 |
public boolean isReadOnly() { |
|
426 |
return false; |
|
427 |
} |
|
428 |
||
429 |
private ArrayList cloneBytesArrayList(ArrayList in) { |
|
430 |
if (in == null) { |
|
431 |
return null; |
|
432 |
} else { |
|
433 |
ArrayList list = new ArrayList(in.size()); |
|
434 |
Iterator iter = in.iterator(); |
|
435 |
while (iter.hasNext()) { |
|
436 |
Object o = iter.next(); |
|
437 |
if (o == null) { |
|
438 |
list.add(null); |
|
439 |
} else { |
|
440 |
list.add(((byte[])o).clone()); |
|
441 |
} |
|
442 |
} |
|
443 |
||
444 |
return list; |
|
445 |
} |
|
446 |
} |
|
447 |
||
448 |
// Deep clone |
|
449 |
public Object clone() { |
|
450 |
PNGMetadata metadata; |
|
451 |
try { |
|
452 |
metadata = (PNGMetadata)super.clone(); |
|
453 |
} catch (CloneNotSupportedException e) { |
|
454 |
return null; |
|
455 |
} |
|
456 |
||
457 |
// unknownChunkData needs deep clone |
|
458 |
metadata.unknownChunkData = |
|
459 |
cloneBytesArrayList(this.unknownChunkData); |
|
460 |
||
461 |
return metadata; |
|
462 |
} |
|
463 |
||
464 |
public Node getAsTree(String formatName) { |
|
465 |
if (formatName.equals(nativeMetadataFormatName)) { |
|
466 |
return getNativeTree(); |
|
467 |
} else if (formatName.equals |
|
468 |
(IIOMetadataFormatImpl.standardMetadataFormatName)) { |
|
469 |
return getStandardTree(); |
|
470 |
} else { |
|
471 |
throw new IllegalArgumentException("Not a recognized format!"); |
|
472 |
} |
|
473 |
} |
|
474 |
||
475 |
private Node getNativeTree() { |
|
476 |
IIOMetadataNode node = null; // scratch node |
|
477 |
IIOMetadataNode root = new IIOMetadataNode(nativeMetadataFormatName); |
|
478 |
||
479 |
// IHDR |
|
480 |
if (IHDR_present) { |
|
481 |
IIOMetadataNode IHDR_node = new IIOMetadataNode("IHDR"); |
|
482 |
IHDR_node.setAttribute("width", Integer.toString(IHDR_width)); |
|
483 |
IHDR_node.setAttribute("height", Integer.toString(IHDR_height)); |
|
484 |
IHDR_node.setAttribute("bitDepth", |
|
485 |
Integer.toString(IHDR_bitDepth)); |
|
486 |
IHDR_node.setAttribute("colorType", |
|
487 |
IHDR_colorTypeNames[IHDR_colorType]); |
|
488 |
// IHDR_compressionMethod must be 0 in PNG 1.1 |
|
489 |
IHDR_node.setAttribute("compressionMethod", |
|
490 |
IHDR_compressionMethodNames[IHDR_compressionMethod]); |
|
491 |
// IHDR_filterMethod must be 0 in PNG 1.1 |
|
492 |
IHDR_node.setAttribute("filterMethod", |
|
493 |
IHDR_filterMethodNames[IHDR_filterMethod]); |
|
494 |
IHDR_node.setAttribute("interlaceMethod", |
|
495 |
IHDR_interlaceMethodNames[IHDR_interlaceMethod]); |
|
496 |
root.appendChild(IHDR_node); |
|
497 |
} |
|
498 |
||
499 |
// PLTE |
|
500 |
if (PLTE_present) { |
|
501 |
IIOMetadataNode PLTE_node = new IIOMetadataNode("PLTE"); |
|
502 |
int numEntries = PLTE_red.length; |
|
503 |
for (int i = 0; i < numEntries; i++) { |
|
504 |
IIOMetadataNode entry = new IIOMetadataNode("PLTEEntry"); |
|
505 |
entry.setAttribute("index", Integer.toString(i)); |
|
506 |
entry.setAttribute("red", |
|
507 |
Integer.toString(PLTE_red[i] & 0xff)); |
|
508 |
entry.setAttribute("green", |
|
509 |
Integer.toString(PLTE_green[i] & 0xff)); |
|
510 |
entry.setAttribute("blue", |
|
511 |
Integer.toString(PLTE_blue[i] & 0xff)); |
|
512 |
PLTE_node.appendChild(entry); |
|
513 |
} |
|
514 |
||
515 |
root.appendChild(PLTE_node); |
|
516 |
} |
|
517 |
||
518 |
// bKGD |
|
519 |
if (bKGD_present) { |
|
520 |
IIOMetadataNode bKGD_node = new IIOMetadataNode("bKGD"); |
|
521 |
||
522 |
if (bKGD_colorType == PNGImageReader.PNG_COLOR_PALETTE) { |
|
523 |
node = new IIOMetadataNode("bKGD_Palette"); |
|
524 |
node.setAttribute("index", Integer.toString(bKGD_index)); |
|
525 |
} else if (bKGD_colorType == PNGImageReader.PNG_COLOR_GRAY) { |
|
526 |
node = new IIOMetadataNode("bKGD_Grayscale"); |
|
527 |
node.setAttribute("gray", Integer.toString(bKGD_gray)); |
|
528 |
} else if (bKGD_colorType == PNGImageReader.PNG_COLOR_RGB) { |
|
529 |
node = new IIOMetadataNode("bKGD_RGB"); |
|
530 |
node.setAttribute("red", Integer.toString(bKGD_red)); |
|
531 |
node.setAttribute("green", Integer.toString(bKGD_green)); |
|
532 |
node.setAttribute("blue", Integer.toString(bKGD_blue)); |
|
533 |
} |
|
534 |
bKGD_node.appendChild(node); |
|
535 |
||
536 |
root.appendChild(bKGD_node); |
|
537 |
} |
|
538 |
||
539 |
// cHRM |
|
540 |
if (cHRM_present) { |
|
541 |
IIOMetadataNode cHRM_node = new IIOMetadataNode("cHRM"); |
|
542 |
cHRM_node.setAttribute("whitePointX", |
|
543 |
Integer.toString(cHRM_whitePointX)); |
|
544 |
cHRM_node.setAttribute("whitePointY", |
|
545 |
Integer.toString(cHRM_whitePointY)); |
|
546 |
cHRM_node.setAttribute("redX", Integer.toString(cHRM_redX)); |
|
547 |
cHRM_node.setAttribute("redY", Integer.toString(cHRM_redY)); |
|
548 |
cHRM_node.setAttribute("greenX", Integer.toString(cHRM_greenX)); |
|
549 |
cHRM_node.setAttribute("greenY", Integer.toString(cHRM_greenY)); |
|
550 |
cHRM_node.setAttribute("blueX", Integer.toString(cHRM_blueX)); |
|
551 |
cHRM_node.setAttribute("blueY", Integer.toString(cHRM_blueY)); |
|
552 |
||
553 |
root.appendChild(cHRM_node); |
|
554 |
} |
|
555 |
||
556 |
// gAMA |
|
557 |
if (gAMA_present) { |
|
558 |
IIOMetadataNode gAMA_node = new IIOMetadataNode("gAMA"); |
|
559 |
gAMA_node.setAttribute("value", Integer.toString(gAMA_gamma)); |
|
560 |
||
561 |
root.appendChild(gAMA_node); |
|
562 |
} |
|
563 |
||
564 |
// hIST |
|
565 |
if (hIST_present) { |
|
566 |
IIOMetadataNode hIST_node = new IIOMetadataNode("hIST"); |
|
567 |
||
568 |
for (int i = 0; i < hIST_histogram.length; i++) { |
|
569 |
IIOMetadataNode hist = |
|
570 |
new IIOMetadataNode("hISTEntry"); |
|
571 |
hist.setAttribute("index", Integer.toString(i)); |
|
572 |
hist.setAttribute("value", |
|
573 |
Integer.toString(hIST_histogram[i])); |
|
574 |
hIST_node.appendChild(hist); |
|
575 |
} |
|
576 |
||
577 |
root.appendChild(hIST_node); |
|
578 |
} |
|
579 |
||
580 |
// iCCP |
|
581 |
if (iCCP_present) { |
|
582 |
IIOMetadataNode iCCP_node = new IIOMetadataNode("iCCP"); |
|
583 |
iCCP_node.setAttribute("profileName", iCCP_profileName); |
|
584 |
iCCP_node.setAttribute("compressionMethod", |
|
585 |
iCCP_compressionMethodNames[iCCP_compressionMethod]); |
|
586 |
||
587 |
Object profile = iCCP_compressedProfile; |
|
588 |
if (profile != null) { |
|
589 |
profile = ((byte[])profile).clone(); |
|
590 |
} |
|
591 |
iCCP_node.setUserObject(profile); |
|
592 |
||
593 |
root.appendChild(iCCP_node); |
|
594 |
} |
|
595 |
||
596 |
// iTXt |
|
597 |
if (iTXt_keyword.size() > 0) { |
|
598 |
IIOMetadataNode iTXt_parent = new IIOMetadataNode("iTXt"); |
|
599 |
for (int i = 0; i < iTXt_keyword.size(); i++) { |
|
600 |
IIOMetadataNode iTXt_node = new IIOMetadataNode("iTXtEntry"); |
|
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
601 |
iTXt_node.setAttribute("keyword", iTXt_keyword.get(i)); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
602 |
iTXt_node.setAttribute("compressionFlag", |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
603 |
iTXt_compressionFlag.get(i) ? "1" : "0"); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
604 |
iTXt_node.setAttribute("compressionMethod", |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
605 |
iTXt_compressionMethod.get(i).toString()); |
2 | 606 |
iTXt_node.setAttribute("languageTag", |
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
607 |
iTXt_languageTag.get(i)); |
2 | 608 |
iTXt_node.setAttribute("translatedKeyword", |
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
609 |
iTXt_translatedKeyword.get(i)); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
610 |
iTXt_node.setAttribute("text", iTXt_text.get(i)); |
2 | 611 |
|
612 |
iTXt_parent.appendChild(iTXt_node); |
|
613 |
} |
|
614 |
||
615 |
root.appendChild(iTXt_parent); |
|
616 |
} |
|
617 |
||
618 |
// pHYs |
|
619 |
if (pHYs_present) { |
|
620 |
IIOMetadataNode pHYs_node = new IIOMetadataNode("pHYs"); |
|
621 |
pHYs_node.setAttribute("pixelsPerUnitXAxis", |
|
622 |
Integer.toString(pHYs_pixelsPerUnitXAxis)); |
|
623 |
pHYs_node.setAttribute("pixelsPerUnitYAxis", |
|
624 |
Integer.toString(pHYs_pixelsPerUnitYAxis)); |
|
625 |
pHYs_node.setAttribute("unitSpecifier", |
|
626 |
unitSpecifierNames[pHYs_unitSpecifier]); |
|
627 |
||
628 |
root.appendChild(pHYs_node); |
|
629 |
} |
|
630 |
||
631 |
// sBIT |
|
632 |
if (sBIT_present) { |
|
633 |
IIOMetadataNode sBIT_node = new IIOMetadataNode("sBIT"); |
|
634 |
||
635 |
if (sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY) { |
|
636 |
node = new IIOMetadataNode("sBIT_Grayscale"); |
|
637 |
node.setAttribute("gray", |
|
638 |
Integer.toString(sBIT_grayBits)); |
|
639 |
} else if (sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) { |
|
640 |
node = new IIOMetadataNode("sBIT_GrayAlpha"); |
|
641 |
node.setAttribute("gray", |
|
642 |
Integer.toString(sBIT_grayBits)); |
|
643 |
node.setAttribute("alpha", |
|
644 |
Integer.toString(sBIT_alphaBits)); |
|
645 |
} else if (sBIT_colorType == PNGImageReader.PNG_COLOR_RGB) { |
|
646 |
node = new IIOMetadataNode("sBIT_RGB"); |
|
647 |
node.setAttribute("red", |
|
648 |
Integer.toString(sBIT_redBits)); |
|
649 |
node.setAttribute("green", |
|
650 |
Integer.toString(sBIT_greenBits)); |
|
651 |
node.setAttribute("blue", |
|
652 |
Integer.toString(sBIT_blueBits)); |
|
653 |
} else if (sBIT_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) { |
|
654 |
node = new IIOMetadataNode("sBIT_RGBAlpha"); |
|
655 |
node.setAttribute("red", |
|
656 |
Integer.toString(sBIT_redBits)); |
|
657 |
node.setAttribute("green", |
|
658 |
Integer.toString(sBIT_greenBits)); |
|
659 |
node.setAttribute("blue", |
|
660 |
Integer.toString(sBIT_blueBits)); |
|
661 |
node.setAttribute("alpha", |
|
662 |
Integer.toString(sBIT_alphaBits)); |
|
663 |
} else if (sBIT_colorType == PNGImageReader.PNG_COLOR_PALETTE) { |
|
664 |
node = new IIOMetadataNode("sBIT_Palette"); |
|
665 |
node.setAttribute("red", |
|
666 |
Integer.toString(sBIT_redBits)); |
|
667 |
node.setAttribute("green", |
|
668 |
Integer.toString(sBIT_greenBits)); |
|
669 |
node.setAttribute("blue", |
|
670 |
Integer.toString(sBIT_blueBits)); |
|
671 |
} |
|
672 |
sBIT_node.appendChild(node); |
|
673 |
||
674 |
root.appendChild(sBIT_node); |
|
675 |
} |
|
676 |
||
677 |
// sPLT |
|
678 |
if (sPLT_present) { |
|
679 |
IIOMetadataNode sPLT_node = new IIOMetadataNode("sPLT"); |
|
680 |
||
681 |
sPLT_node.setAttribute("name", sPLT_paletteName); |
|
682 |
sPLT_node.setAttribute("sampleDepth", |
|
683 |
Integer.toString(sPLT_sampleDepth)); |
|
684 |
||
685 |
int numEntries = sPLT_red.length; |
|
686 |
for (int i = 0; i < numEntries; i++) { |
|
687 |
IIOMetadataNode entry = new IIOMetadataNode("sPLTEntry"); |
|
688 |
entry.setAttribute("index", Integer.toString(i)); |
|
689 |
entry.setAttribute("red", Integer.toString(sPLT_red[i])); |
|
690 |
entry.setAttribute("green", Integer.toString(sPLT_green[i])); |
|
691 |
entry.setAttribute("blue", Integer.toString(sPLT_blue[i])); |
|
692 |
entry.setAttribute("alpha", Integer.toString(sPLT_alpha[i])); |
|
693 |
entry.setAttribute("frequency", |
|
694 |
Integer.toString(sPLT_frequency[i])); |
|
695 |
sPLT_node.appendChild(entry); |
|
696 |
} |
|
697 |
||
698 |
root.appendChild(sPLT_node); |
|
699 |
} |
|
700 |
||
701 |
// sRGB |
|
702 |
if (sRGB_present) { |
|
703 |
IIOMetadataNode sRGB_node = new IIOMetadataNode("sRGB"); |
|
704 |
sRGB_node.setAttribute("renderingIntent", |
|
705 |
renderingIntentNames[sRGB_renderingIntent]); |
|
706 |
||
707 |
root.appendChild(sRGB_node); |
|
708 |
} |
|
709 |
||
710 |
// tEXt |
|
711 |
if (tEXt_keyword.size() > 0) { |
|
712 |
IIOMetadataNode tEXt_parent = new IIOMetadataNode("tEXt"); |
|
713 |
for (int i = 0; i < tEXt_keyword.size(); i++) { |
|
714 |
IIOMetadataNode tEXt_node = new IIOMetadataNode("tEXtEntry"); |
|
715 |
tEXt_node.setAttribute("keyword" , (String)tEXt_keyword.get(i)); |
|
716 |
tEXt_node.setAttribute("value" , (String)tEXt_text.get(i)); |
|
717 |
||
718 |
tEXt_parent.appendChild(tEXt_node); |
|
719 |
} |
|
720 |
||
721 |
root.appendChild(tEXt_parent); |
|
722 |
} |
|
723 |
||
724 |
// tIME |
|
725 |
if (tIME_present) { |
|
726 |
IIOMetadataNode tIME_node = new IIOMetadataNode("tIME"); |
|
727 |
tIME_node.setAttribute("year", Integer.toString(tIME_year)); |
|
728 |
tIME_node.setAttribute("month", Integer.toString(tIME_month)); |
|
729 |
tIME_node.setAttribute("day", Integer.toString(tIME_day)); |
|
730 |
tIME_node.setAttribute("hour", Integer.toString(tIME_hour)); |
|
731 |
tIME_node.setAttribute("minute", Integer.toString(tIME_minute)); |
|
732 |
tIME_node.setAttribute("second", Integer.toString(tIME_second)); |
|
733 |
||
734 |
root.appendChild(tIME_node); |
|
735 |
} |
|
736 |
||
737 |
// tRNS |
|
738 |
if (tRNS_present) { |
|
739 |
IIOMetadataNode tRNS_node = new IIOMetadataNode("tRNS"); |
|
740 |
||
741 |
if (tRNS_colorType == PNGImageReader.PNG_COLOR_PALETTE) { |
|
742 |
node = new IIOMetadataNode("tRNS_Palette"); |
|
743 |
||
744 |
for (int i = 0; i < tRNS_alpha.length; i++) { |
|
745 |
IIOMetadataNode entry = |
|
746 |
new IIOMetadataNode("tRNS_PaletteEntry"); |
|
747 |
entry.setAttribute("index", Integer.toString(i)); |
|
748 |
entry.setAttribute("alpha", |
|
749 |
Integer.toString(tRNS_alpha[i] & 0xff)); |
|
750 |
node.appendChild(entry); |
|
751 |
} |
|
752 |
} else if (tRNS_colorType == PNGImageReader.PNG_COLOR_GRAY) { |
|
753 |
node = new IIOMetadataNode("tRNS_Grayscale"); |
|
754 |
node.setAttribute("gray", Integer.toString(tRNS_gray)); |
|
755 |
} else if (tRNS_colorType == PNGImageReader.PNG_COLOR_RGB) { |
|
756 |
node = new IIOMetadataNode("tRNS_RGB"); |
|
757 |
node.setAttribute("red", Integer.toString(tRNS_red)); |
|
758 |
node.setAttribute("green", Integer.toString(tRNS_green)); |
|
759 |
node.setAttribute("blue", Integer.toString(tRNS_blue)); |
|
760 |
} |
|
761 |
tRNS_node.appendChild(node); |
|
762 |
||
763 |
root.appendChild(tRNS_node); |
|
764 |
} |
|
765 |
||
766 |
// zTXt |
|
767 |
if (zTXt_keyword.size() > 0) { |
|
768 |
IIOMetadataNode zTXt_parent = new IIOMetadataNode("zTXt"); |
|
769 |
for (int i = 0; i < zTXt_keyword.size(); i++) { |
|
770 |
IIOMetadataNode zTXt_node = new IIOMetadataNode("zTXtEntry"); |
|
771 |
zTXt_node.setAttribute("keyword", (String)zTXt_keyword.get(i)); |
|
772 |
||
773 |
int cm = ((Integer)zTXt_compressionMethod.get(i)).intValue(); |
|
774 |
zTXt_node.setAttribute("compressionMethod", |
|
775 |
zTXt_compressionMethodNames[cm]); |
|
776 |
||
777 |
zTXt_node.setAttribute("text", (String)zTXt_text.get(i)); |
|
778 |
||
779 |
zTXt_parent.appendChild(zTXt_node); |
|
780 |
} |
|
781 |
||
782 |
root.appendChild(zTXt_parent); |
|
783 |
} |
|
784 |
||
785 |
// Unknown chunks |
|
786 |
if (unknownChunkType.size() > 0) { |
|
787 |
IIOMetadataNode unknown_parent = |
|
788 |
new IIOMetadataNode("UnknownChunks"); |
|
789 |
for (int i = 0; i < unknownChunkType.size(); i++) { |
|
790 |
IIOMetadataNode unknown_node = |
|
791 |
new IIOMetadataNode("UnknownChunk"); |
|
792 |
unknown_node.setAttribute("type", |
|
793 |
(String)unknownChunkType.get(i)); |
|
794 |
unknown_node.setUserObject((byte[])unknownChunkData.get(i)); |
|
795 |
||
796 |
unknown_parent.appendChild(unknown_node); |
|
797 |
} |
|
798 |
||
799 |
root.appendChild(unknown_parent); |
|
800 |
} |
|
801 |
||
802 |
return root; |
|
803 |
} |
|
804 |
||
805 |
private int getNumChannels() { |
|
806 |
// Determine number of channels |
|
807 |
// Be careful about palette color with transparency |
|
808 |
int numChannels = IHDR_numChannels[IHDR_colorType]; |
|
809 |
if (IHDR_colorType == PNGImageReader.PNG_COLOR_PALETTE && |
|
810 |
tRNS_present && tRNS_colorType == IHDR_colorType) { |
|
811 |
numChannels = 4; |
|
812 |
} |
|
813 |
return numChannels; |
|
814 |
} |
|
815 |
||
816 |
public IIOMetadataNode getStandardChromaNode() { |
|
817 |
IIOMetadataNode chroma_node = new IIOMetadataNode("Chroma"); |
|
818 |
IIOMetadataNode node = null; // scratch node |
|
819 |
||
820 |
node = new IIOMetadataNode("ColorSpaceType"); |
|
821 |
node.setAttribute("name", colorSpaceTypeNames[IHDR_colorType]); |
|
822 |
chroma_node.appendChild(node); |
|
823 |
||
824 |
node = new IIOMetadataNode("NumChannels"); |
|
825 |
node.setAttribute("value", Integer.toString(getNumChannels())); |
|
826 |
chroma_node.appendChild(node); |
|
827 |
||
828 |
if (gAMA_present) { |
|
829 |
node = new IIOMetadataNode("Gamma"); |
|
830 |
node.setAttribute("value", Float.toString(gAMA_gamma*1.0e-5F)); |
|
831 |
chroma_node.appendChild(node); |
|
832 |
} |
|
833 |
||
834 |
node = new IIOMetadataNode("BlackIsZero"); |
|
835 |
node.setAttribute("value", "true"); |
|
836 |
chroma_node.appendChild(node); |
|
837 |
||
838 |
if (PLTE_present) { |
|
839 |
boolean hasAlpha = tRNS_present && |
|
840 |
(tRNS_colorType == PNGImageReader.PNG_COLOR_PALETTE); |
|
841 |
||
842 |
node = new IIOMetadataNode("Palette"); |
|
843 |
for (int i = 0; i < PLTE_red.length; i++) { |
|
844 |
IIOMetadataNode entry = |
|
845 |
new IIOMetadataNode("PaletteEntry"); |
|
846 |
entry.setAttribute("index", Integer.toString(i)); |
|
847 |
entry.setAttribute("red", |
|
848 |
Integer.toString(PLTE_red[i] & 0xff)); |
|
849 |
entry.setAttribute("green", |
|
850 |
Integer.toString(PLTE_green[i] & 0xff)); |
|
851 |
entry.setAttribute("blue", |
|
852 |
Integer.toString(PLTE_blue[i] & 0xff)); |
|
853 |
if (hasAlpha) { |
|
854 |
int alpha = (i < tRNS_alpha.length) ? |
|
855 |
(tRNS_alpha[i] & 0xff) : 255; |
|
856 |
entry.setAttribute("alpha", Integer.toString(alpha)); |
|
857 |
} |
|
858 |
node.appendChild(entry); |
|
859 |
} |
|
860 |
chroma_node.appendChild(node); |
|
861 |
} |
|
862 |
||
863 |
if (bKGD_present) { |
|
864 |
if (bKGD_colorType == PNGImageReader.PNG_COLOR_PALETTE) { |
|
865 |
node = new IIOMetadataNode("BackgroundIndex"); |
|
866 |
node.setAttribute("value", Integer.toString(bKGD_index)); |
|
867 |
} else { |
|
868 |
node = new IIOMetadataNode("BackgroundColor"); |
|
869 |
int r, g, b; |
|
870 |
||
871 |
if (bKGD_colorType == PNGImageReader.PNG_COLOR_GRAY) { |
|
872 |
r = g = b = bKGD_gray; |
|
873 |
} else { |
|
874 |
r = bKGD_red; |
|
875 |
g = bKGD_green; |
|
876 |
b = bKGD_blue; |
|
877 |
} |
|
878 |
node.setAttribute("red", Integer.toString(r)); |
|
879 |
node.setAttribute("green", Integer.toString(g)); |
|
880 |
node.setAttribute("blue", Integer.toString(b)); |
|
881 |
} |
|
882 |
chroma_node.appendChild(node); |
|
883 |
} |
|
884 |
||
885 |
return chroma_node; |
|
886 |
} |
|
887 |
||
888 |
public IIOMetadataNode getStandardCompressionNode() { |
|
889 |
IIOMetadataNode compression_node = new IIOMetadataNode("Compression"); |
|
890 |
IIOMetadataNode node = null; // scratch node |
|
891 |
||
892 |
node = new IIOMetadataNode("CompressionTypeName"); |
|
893 |
node.setAttribute("value", "deflate"); |
|
894 |
compression_node.appendChild(node); |
|
895 |
||
896 |
node = new IIOMetadataNode("Lossless"); |
|
897 |
node.setAttribute("value", "true"); |
|
898 |
compression_node.appendChild(node); |
|
899 |
||
900 |
node = new IIOMetadataNode("NumProgressiveScans"); |
|
901 |
node.setAttribute("value", |
|
902 |
(IHDR_interlaceMethod == 0) ? "1" : "7"); |
|
903 |
compression_node.appendChild(node); |
|
904 |
||
905 |
return compression_node; |
|
906 |
} |
|
907 |
||
908 |
private String repeat(String s, int times) { |
|
909 |
if (times == 1) { |
|
910 |
return s; |
|
911 |
} |
|
912 |
StringBuffer sb = new StringBuffer((s.length() + 1)*times - 1); |
|
913 |
sb.append(s); |
|
914 |
for (int i = 1; i < times; i++) { |
|
915 |
sb.append(" "); |
|
916 |
sb.append(s); |
|
917 |
} |
|
918 |
return sb.toString(); |
|
919 |
} |
|
920 |
||
921 |
public IIOMetadataNode getStandardDataNode() { |
|
922 |
IIOMetadataNode data_node = new IIOMetadataNode("Data"); |
|
923 |
IIOMetadataNode node = null; // scratch node |
|
924 |
||
925 |
node = new IIOMetadataNode("PlanarConfiguration"); |
|
926 |
node.setAttribute("value", "PixelInterleaved"); |
|
927 |
data_node.appendChild(node); |
|
928 |
||
929 |
node = new IIOMetadataNode("SampleFormat"); |
|
930 |
node.setAttribute("value", |
|
931 |
IHDR_colorType == PNGImageReader.PNG_COLOR_PALETTE ? |
|
932 |
"Index" : "UnsignedIntegral"); |
|
933 |
data_node.appendChild(node); |
|
934 |
||
935 |
String bitDepth = Integer.toString(IHDR_bitDepth); |
|
936 |
node = new IIOMetadataNode("BitsPerSample"); |
|
937 |
node.setAttribute("value", repeat(bitDepth, getNumChannels())); |
|
938 |
data_node.appendChild(node); |
|
939 |
||
940 |
if (sBIT_present) { |
|
941 |
node = new IIOMetadataNode("SignificantBitsPerSample"); |
|
942 |
String sbits; |
|
943 |
if (sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY || |
|
944 |
sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) { |
|
945 |
sbits = Integer.toString(sBIT_grayBits); |
|
946 |
} else { // sBIT_colorType == PNGImageReader.PNG_COLOR_RGB || |
|
947 |
// sBIT_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA |
|
948 |
sbits = Integer.toString(sBIT_redBits) + " " + |
|
949 |
Integer.toString(sBIT_greenBits) + " " + |
|
950 |
Integer.toString(sBIT_blueBits); |
|
951 |
} |
|
952 |
||
953 |
if (sBIT_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA || |
|
954 |
sBIT_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) { |
|
955 |
sbits += " " + Integer.toString(sBIT_alphaBits); |
|
956 |
} |
|
957 |
||
958 |
node.setAttribute("value", sbits); |
|
959 |
data_node.appendChild(node); |
|
960 |
} |
|
961 |
||
962 |
// SampleMSB |
|
963 |
||
964 |
return data_node; |
|
965 |
} |
|
966 |
||
967 |
public IIOMetadataNode getStandardDimensionNode() { |
|
968 |
IIOMetadataNode dimension_node = new IIOMetadataNode("Dimension"); |
|
969 |
IIOMetadataNode node = null; // scratch node |
|
970 |
||
971 |
node = new IIOMetadataNode("PixelAspectRatio"); |
|
972 |
float ratio = pHYs_present ? |
|
973 |
(float)pHYs_pixelsPerUnitXAxis/pHYs_pixelsPerUnitYAxis : 1.0F; |
|
974 |
node.setAttribute("value", Float.toString(ratio)); |
|
975 |
dimension_node.appendChild(node); |
|
976 |
||
977 |
node = new IIOMetadataNode("ImageOrientation"); |
|
978 |
node.setAttribute("value", "Normal"); |
|
979 |
dimension_node.appendChild(node); |
|
980 |
||
981 |
if (pHYs_present && pHYs_unitSpecifier == PHYS_UNIT_METER) { |
|
982 |
node = new IIOMetadataNode("HorizontalPixelSize"); |
|
983 |
node.setAttribute("value", |
|
984 |
Float.toString(1000.0F/pHYs_pixelsPerUnitXAxis)); |
|
985 |
dimension_node.appendChild(node); |
|
986 |
||
987 |
node = new IIOMetadataNode("VerticalPixelSize"); |
|
988 |
node.setAttribute("value", |
|
989 |
Float.toString(1000.0F/pHYs_pixelsPerUnitYAxis)); |
|
990 |
dimension_node.appendChild(node); |
|
991 |
} |
|
992 |
||
993 |
return dimension_node; |
|
994 |
} |
|
995 |
||
996 |
public IIOMetadataNode getStandardDocumentNode() { |
|
997 |
if (!tIME_present) { |
|
998 |
return null; |
|
999 |
} |
|
1000 |
||
1001 |
IIOMetadataNode document_node = new IIOMetadataNode("Document"); |
|
1002 |
IIOMetadataNode node = null; // scratch node |
|
1003 |
||
1004 |
node = new IIOMetadataNode("ImageModificationTime"); |
|
1005 |
node.setAttribute("year", Integer.toString(tIME_year)); |
|
1006 |
node.setAttribute("month", Integer.toString(tIME_month)); |
|
1007 |
node.setAttribute("day", Integer.toString(tIME_day)); |
|
1008 |
node.setAttribute("hour", Integer.toString(tIME_hour)); |
|
1009 |
node.setAttribute("minute", Integer.toString(tIME_minute)); |
|
1010 |
node.setAttribute("second", Integer.toString(tIME_second)); |
|
1011 |
document_node.appendChild(node); |
|
1012 |
||
1013 |
return document_node; |
|
1014 |
} |
|
1015 |
||
1016 |
public IIOMetadataNode getStandardTextNode() { |
|
1017 |
int numEntries = tEXt_keyword.size() + |
|
1018 |
iTXt_keyword.size() + zTXt_keyword.size(); |
|
1019 |
if (numEntries == 0) { |
|
1020 |
return null; |
|
1021 |
} |
|
1022 |
||
1023 |
IIOMetadataNode text_node = new IIOMetadataNode("Text"); |
|
1024 |
IIOMetadataNode node = null; // scratch node |
|
1025 |
||
1026 |
for (int i = 0; i < tEXt_keyword.size(); i++) { |
|
1027 |
node = new IIOMetadataNode("TextEntry"); |
|
1028 |
node.setAttribute("keyword", (String)tEXt_keyword.get(i)); |
|
1029 |
node.setAttribute("value", (String)tEXt_text.get(i)); |
|
1030 |
node.setAttribute("encoding", "ISO-8859-1"); |
|
1031 |
node.setAttribute("compression", "none"); |
|
1032 |
||
1033 |
text_node.appendChild(node); |
|
1034 |
} |
|
1035 |
||
1036 |
for (int i = 0; i < iTXt_keyword.size(); i++) { |
|
1037 |
node = new IIOMetadataNode("TextEntry"); |
|
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1038 |
node.setAttribute("keyword", iTXt_keyword.get(i)); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1039 |
node.setAttribute("value", iTXt_text.get(i)); |
2 | 1040 |
node.setAttribute("language", |
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1041 |
iTXt_languageTag.get(i)); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1042 |
if (iTXt_compressionFlag.get(i)) { |
2 | 1043 |
node.setAttribute("compression", "deflate"); |
1044 |
} else { |
|
1045 |
node.setAttribute("compression", "none"); |
|
1046 |
} |
|
1047 |
||
1048 |
text_node.appendChild(node); |
|
1049 |
} |
|
1050 |
||
1051 |
for (int i = 0; i < zTXt_keyword.size(); i++) { |
|
1052 |
node = new IIOMetadataNode("TextEntry"); |
|
1053 |
node.setAttribute("keyword", (String)zTXt_keyword.get(i)); |
|
1054 |
node.setAttribute("value", (String)zTXt_text.get(i)); |
|
1055 |
node.setAttribute("compression", "deflate"); |
|
1056 |
||
1057 |
text_node.appendChild(node); |
|
1058 |
} |
|
1059 |
||
1060 |
return text_node; |
|
1061 |
} |
|
1062 |
||
1063 |
public IIOMetadataNode getStandardTransparencyNode() { |
|
1064 |
IIOMetadataNode transparency_node = |
|
1065 |
new IIOMetadataNode("Transparency"); |
|
1066 |
IIOMetadataNode node = null; // scratch node |
|
1067 |
||
1068 |
node = new IIOMetadataNode("Alpha"); |
|
1069 |
boolean hasAlpha = |
|
1070 |
(IHDR_colorType == PNGImageReader.PNG_COLOR_RGB_ALPHA) || |
|
1071 |
(IHDR_colorType == PNGImageReader.PNG_COLOR_GRAY_ALPHA) || |
|
1072 |
(IHDR_colorType == PNGImageReader.PNG_COLOR_PALETTE && |
|
1073 |
tRNS_present && |
|
1074 |
(tRNS_colorType == IHDR_colorType) && |
|
1075 |
(tRNS_alpha != null)); |
|
1076 |
node.setAttribute("value", hasAlpha ? "nonpremultipled" : "none"); |
|
1077 |
transparency_node.appendChild(node); |
|
1078 |
||
1079 |
if (tRNS_present) { |
|
1080 |
node = new IIOMetadataNode("TransparentColor"); |
|
1081 |
if (tRNS_colorType == PNGImageReader.PNG_COLOR_RGB) { |
|
1082 |
node.setAttribute("value", |
|
1083 |
Integer.toString(tRNS_red) + " " + |
|
1084 |
Integer.toString(tRNS_green) + " " + |
|
1085 |
Integer.toString(tRNS_blue)); |
|
1086 |
} else if (tRNS_colorType == PNGImageReader.PNG_COLOR_GRAY) { |
|
1087 |
node.setAttribute("value", Integer.toString(tRNS_gray)); |
|
1088 |
} |
|
1089 |
transparency_node.appendChild(node); |
|
1090 |
} |
|
1091 |
||
1092 |
return transparency_node; |
|
1093 |
} |
|
1094 |
||
1095 |
// Shorthand for throwing an IIOInvalidTreeException |
|
1096 |
private void fatal(Node node, String reason) |
|
1097 |
throws IIOInvalidTreeException { |
|
1098 |
throw new IIOInvalidTreeException(reason, node); |
|
1099 |
} |
|
1100 |
||
1101 |
// Get an integer-valued attribute |
|
1102 |
private String getStringAttribute(Node node, String name, |
|
1103 |
String defaultValue, boolean required) |
|
1104 |
throws IIOInvalidTreeException { |
|
1105 |
Node attr = node.getAttributes().getNamedItem(name); |
|
1106 |
if (attr == null) { |
|
1107 |
if (!required) { |
|
1108 |
return defaultValue; |
|
1109 |
} else { |
|
1110 |
fatal(node, "Required attribute " + name + " not present!"); |
|
1111 |
} |
|
1112 |
} |
|
1113 |
return attr.getNodeValue(); |
|
1114 |
} |
|
1115 |
||
1116 |
||
1117 |
// Get an integer-valued attribute |
|
1118 |
private int getIntAttribute(Node node, String name, |
|
1119 |
int defaultValue, boolean required) |
|
1120 |
throws IIOInvalidTreeException { |
|
1121 |
String value = getStringAttribute(node, name, null, required); |
|
1122 |
if (value == null) { |
|
1123 |
return defaultValue; |
|
1124 |
} |
|
1125 |
return Integer.parseInt(value); |
|
1126 |
} |
|
1127 |
||
1128 |
// Get a float-valued attribute |
|
1129 |
private float getFloatAttribute(Node node, String name, |
|
1130 |
float defaultValue, boolean required) |
|
1131 |
throws IIOInvalidTreeException { |
|
1132 |
String value = getStringAttribute(node, name, null, required); |
|
1133 |
if (value == null) { |
|
1134 |
return defaultValue; |
|
1135 |
} |
|
1136 |
return Float.parseFloat(value); |
|
1137 |
} |
|
1138 |
||
1139 |
// Get a required integer-valued attribute |
|
1140 |
private int getIntAttribute(Node node, String name) |
|
1141 |
throws IIOInvalidTreeException { |
|
1142 |
return getIntAttribute(node, name, -1, true); |
|
1143 |
} |
|
1144 |
||
1145 |
// Get a required float-valued attribute |
|
1146 |
private float getFloatAttribute(Node node, String name) |
|
1147 |
throws IIOInvalidTreeException { |
|
1148 |
return getFloatAttribute(node, name, -1.0F, true); |
|
1149 |
} |
|
1150 |
||
1151 |
// Get a boolean-valued attribute |
|
1152 |
private boolean getBooleanAttribute(Node node, String name, |
|
1153 |
boolean defaultValue, |
|
1154 |
boolean required) |
|
1155 |
throws IIOInvalidTreeException { |
|
1156 |
Node attr = node.getAttributes().getNamedItem(name); |
|
1157 |
if (attr == null) { |
|
1158 |
if (!required) { |
|
1159 |
return defaultValue; |
|
1160 |
} else { |
|
1161 |
fatal(node, "Required attribute " + name + " not present!"); |
|
1162 |
} |
|
1163 |
} |
|
1164 |
String value = attr.getNodeValue(); |
|
1165 |
if (value.equals("true")) { |
|
1166 |
return true; |
|
1167 |
} else if (value.equals("false")) { |
|
1168 |
return false; |
|
1169 |
} else { |
|
1170 |
fatal(node, "Attribute " + name + " must be 'true' or 'false'!"); |
|
1171 |
return false; |
|
1172 |
} |
|
1173 |
} |
|
1174 |
||
1175 |
// Get a required boolean-valued attribute |
|
1176 |
private boolean getBooleanAttribute(Node node, String name) |
|
1177 |
throws IIOInvalidTreeException { |
|
1178 |
return getBooleanAttribute(node, name, false, true); |
|
1179 |
} |
|
1180 |
||
1181 |
// Get an enumerated attribute as an index into a String array |
|
1182 |
private int getEnumeratedAttribute(Node node, |
|
1183 |
String name, String[] legalNames, |
|
1184 |
int defaultValue, boolean required) |
|
1185 |
throws IIOInvalidTreeException { |
|
1186 |
Node attr = node.getAttributes().getNamedItem(name); |
|
1187 |
if (attr == null) { |
|
1188 |
if (!required) { |
|
1189 |
return defaultValue; |
|
1190 |
} else { |
|
1191 |
fatal(node, "Required attribute " + name + " not present!"); |
|
1192 |
} |
|
1193 |
} |
|
1194 |
String value = attr.getNodeValue(); |
|
1195 |
for (int i = 0; i < legalNames.length; i++) { |
|
1196 |
if (value.equals(legalNames[i])) { |
|
1197 |
return i; |
|
1198 |
} |
|
1199 |
} |
|
1200 |
||
1201 |
fatal(node, "Illegal value for attribute " + name + "!"); |
|
1202 |
return -1; |
|
1203 |
} |
|
1204 |
||
1205 |
// Get a required enumerated attribute as an index into a String array |
|
1206 |
private int getEnumeratedAttribute(Node node, |
|
1207 |
String name, String[] legalNames) |
|
1208 |
throws IIOInvalidTreeException { |
|
1209 |
return getEnumeratedAttribute(node, name, legalNames, -1, true); |
|
1210 |
} |
|
1211 |
||
1212 |
// Get a String-valued attribute |
|
1213 |
private String getAttribute(Node node, String name, |
|
1214 |
String defaultValue, boolean required) |
|
1215 |
throws IIOInvalidTreeException { |
|
1216 |
Node attr = node.getAttributes().getNamedItem(name); |
|
1217 |
if (attr == null) { |
|
1218 |
if (!required) { |
|
1219 |
return defaultValue; |
|
1220 |
} else { |
|
1221 |
fatal(node, "Required attribute " + name + " not present!"); |
|
1222 |
} |
|
1223 |
} |
|
1224 |
return attr.getNodeValue(); |
|
1225 |
} |
|
1226 |
||
1227 |
// Get a required String-valued attribute |
|
1228 |
private String getAttribute(Node node, String name) |
|
1229 |
throws IIOInvalidTreeException { |
|
1230 |
return getAttribute(node, name, null, true); |
|
1231 |
} |
|
1232 |
||
1233 |
public void mergeTree(String formatName, Node root) |
|
1234 |
throws IIOInvalidTreeException { |
|
1235 |
if (formatName.equals(nativeMetadataFormatName)) { |
|
1236 |
if (root == null) { |
|
1237 |
throw new IllegalArgumentException("root == null!"); |
|
1238 |
} |
|
1239 |
mergeNativeTree(root); |
|
1240 |
} else if (formatName.equals |
|
1241 |
(IIOMetadataFormatImpl.standardMetadataFormatName)) { |
|
1242 |
if (root == null) { |
|
1243 |
throw new IllegalArgumentException("root == null!"); |
|
1244 |
} |
|
1245 |
mergeStandardTree(root); |
|
1246 |
} else { |
|
1247 |
throw new IllegalArgumentException("Not a recognized format!"); |
|
1248 |
} |
|
1249 |
} |
|
1250 |
||
1251 |
private void mergeNativeTree(Node root) |
|
1252 |
throws IIOInvalidTreeException { |
|
1253 |
Node node = root; |
|
1254 |
if (!node.getNodeName().equals(nativeMetadataFormatName)) { |
|
1255 |
fatal(node, "Root must be " + nativeMetadataFormatName); |
|
1256 |
} |
|
1257 |
||
1258 |
node = node.getFirstChild(); |
|
1259 |
while (node != null) { |
|
1260 |
String name = node.getNodeName(); |
|
1261 |
||
1262 |
if (name.equals("IHDR")) { |
|
1263 |
IHDR_width = getIntAttribute(node, "width"); |
|
1264 |
IHDR_height = getIntAttribute(node, "height"); |
|
1265 |
IHDR_bitDepth = getEnumeratedAttribute(node, "bitDepth", |
|
1266 |
IHDR_bitDepths); |
|
1267 |
IHDR_colorType = getEnumeratedAttribute(node, "colorType", |
|
1268 |
IHDR_colorTypeNames); |
|
1269 |
IHDR_compressionMethod = |
|
1270 |
getEnumeratedAttribute(node, "compressionMethod", |
|
1271 |
IHDR_compressionMethodNames); |
|
1272 |
IHDR_filterMethod = |
|
1273 |
getEnumeratedAttribute(node, |
|
1274 |
"filterMethod", |
|
1275 |
IHDR_filterMethodNames); |
|
1276 |
IHDR_interlaceMethod = |
|
1277 |
getEnumeratedAttribute(node, "interlaceMethod", |
|
1278 |
IHDR_interlaceMethodNames); |
|
1279 |
IHDR_present = true; |
|
1280 |
} else if (name.equals("PLTE")) { |
|
1281 |
byte[] red = new byte[256]; |
|
1282 |
byte[] green = new byte[256]; |
|
1283 |
byte[] blue = new byte[256]; |
|
1284 |
int maxindex = -1; |
|
1285 |
||
1286 |
Node PLTE_entry = node.getFirstChild(); |
|
1287 |
if (PLTE_entry == null) { |
|
1288 |
fatal(node, "Palette has no entries!"); |
|
1289 |
} |
|
1290 |
||
1291 |
while (PLTE_entry != null) { |
|
1292 |
if (!PLTE_entry.getNodeName().equals("PLTEEntry")) { |
|
1293 |
fatal(node, |
|
1294 |
"Only a PLTEEntry may be a child of a PLTE!"); |
|
1295 |
} |
|
1296 |
||
1297 |
int index = getIntAttribute(PLTE_entry, "index"); |
|
1298 |
if (index < 0 || index > 255) { |
|
1299 |
fatal(node, |
|
1300 |
"Bad value for PLTEEntry attribute index!"); |
|
1301 |
} |
|
1302 |
if (index > maxindex) { |
|
1303 |
maxindex = index; |
|
1304 |
} |
|
1305 |
red[index] = |
|
1306 |
(byte)getIntAttribute(PLTE_entry, "red"); |
|
1307 |
green[index] = |
|
1308 |
(byte)getIntAttribute(PLTE_entry, "green"); |
|
1309 |
blue[index] = |
|
1310 |
(byte)getIntAttribute(PLTE_entry, "blue"); |
|
1311 |
||
1312 |
PLTE_entry = PLTE_entry.getNextSibling(); |
|
1313 |
} |
|
1314 |
||
1315 |
int numEntries = maxindex + 1; |
|
1316 |
PLTE_red = new byte[numEntries]; |
|
1317 |
PLTE_green = new byte[numEntries]; |
|
1318 |
PLTE_blue = new byte[numEntries]; |
|
1319 |
System.arraycopy(red, 0, PLTE_red, 0, numEntries); |
|
1320 |
System.arraycopy(green, 0, PLTE_green, 0, numEntries); |
|
1321 |
System.arraycopy(blue, 0, PLTE_blue, 0, numEntries); |
|
1322 |
PLTE_present = true; |
|
1323 |
} else if (name.equals("bKGD")) { |
|
1324 |
bKGD_present = false; // Guard against partial overwrite |
|
1325 |
Node bKGD_node = node.getFirstChild(); |
|
1326 |
if (bKGD_node == null) { |
|
1327 |
fatal(node, "bKGD node has no children!"); |
|
1328 |
} |
|
1329 |
String bKGD_name = bKGD_node.getNodeName(); |
|
1330 |
if (bKGD_name.equals("bKGD_Palette")) { |
|
1331 |
bKGD_index = getIntAttribute(bKGD_node, "index"); |
|
1332 |
bKGD_colorType = PNGImageReader.PNG_COLOR_PALETTE; |
|
1333 |
} else if (bKGD_name.equals("bKGD_Grayscale")) { |
|
1334 |
bKGD_gray = getIntAttribute(bKGD_node, "gray"); |
|
1335 |
bKGD_colorType = PNGImageReader.PNG_COLOR_GRAY; |
|
1336 |
} else if (bKGD_name.equals("bKGD_RGB")) { |
|
1337 |
bKGD_red = getIntAttribute(bKGD_node, "red"); |
|
1338 |
bKGD_green = getIntAttribute(bKGD_node, "green"); |
|
1339 |
bKGD_blue = getIntAttribute(bKGD_node, "blue"); |
|
1340 |
bKGD_colorType = PNGImageReader.PNG_COLOR_RGB; |
|
1341 |
} else { |
|
1342 |
fatal(node, "Bad child of a bKGD node!"); |
|
1343 |
} |
|
1344 |
if (bKGD_node.getNextSibling() != null) { |
|
1345 |
fatal(node, "bKGD node has more than one child!"); |
|
1346 |
} |
|
1347 |
||
1348 |
bKGD_present = true; |
|
1349 |
} else if (name.equals("cHRM")) { |
|
1350 |
cHRM_whitePointX = getIntAttribute(node, "whitePointX"); |
|
1351 |
cHRM_whitePointY = getIntAttribute(node, "whitePointY"); |
|
1352 |
cHRM_redX = getIntAttribute(node, "redX"); |
|
1353 |
cHRM_redY = getIntAttribute(node, "redY"); |
|
1354 |
cHRM_greenX = getIntAttribute(node, "greenX"); |
|
1355 |
cHRM_greenY = getIntAttribute(node, "greenY"); |
|
1356 |
cHRM_blueX = getIntAttribute(node, "blueX"); |
|
1357 |
cHRM_blueY = getIntAttribute(node, "blueY"); |
|
1358 |
||
1359 |
cHRM_present = true; |
|
1360 |
} else if (name.equals("gAMA")) { |
|
1361 |
gAMA_gamma = getIntAttribute(node, "value"); |
|
1362 |
gAMA_present = true; |
|
1363 |
} else if (name.equals("hIST")) { |
|
1364 |
char[] hist = new char[256]; |
|
1365 |
int maxindex = -1; |
|
1366 |
||
1367 |
Node hIST_entry = node.getFirstChild(); |
|
1368 |
if (hIST_entry == null) { |
|
1369 |
fatal(node, "hIST node has no children!"); |
|
1370 |
} |
|
1371 |
||
1372 |
while (hIST_entry != null) { |
|
1373 |
if (!hIST_entry.getNodeName().equals("hISTEntry")) { |
|
1374 |
fatal(node, |
|
1375 |
"Only a hISTEntry may be a child of a hIST!"); |
|
1376 |
} |
|
1377 |
||
1378 |
int index = getIntAttribute(hIST_entry, "index"); |
|
1379 |
if (index < 0 || index > 255) { |
|
1380 |
fatal(node, |
|
1381 |
"Bad value for histEntry attribute index!"); |
|
1382 |
} |
|
1383 |
if (index > maxindex) { |
|
1384 |
maxindex = index; |
|
1385 |
} |
|
1386 |
hist[index] = |
|
1387 |
(char)getIntAttribute(hIST_entry, "value"); |
|
1388 |
||
1389 |
hIST_entry = hIST_entry.getNextSibling(); |
|
1390 |
} |
|
1391 |
||
1392 |
int numEntries = maxindex + 1; |
|
1393 |
hIST_histogram = new char[numEntries]; |
|
1394 |
System.arraycopy(hist, 0, hIST_histogram, 0, numEntries); |
|
1395 |
||
1396 |
hIST_present = true; |
|
1397 |
} else if (name.equals("iCCP")) { |
|
1398 |
iCCP_profileName = getAttribute(node, "profileName"); |
|
1399 |
iCCP_compressionMethod = |
|
1400 |
getEnumeratedAttribute(node, "compressionMethod", |
|
1401 |
iCCP_compressionMethodNames); |
|
1402 |
Object compressedProfile = |
|
1403 |
((IIOMetadataNode)node).getUserObject(); |
|
1404 |
if (compressedProfile == null) { |
|
1405 |
fatal(node, "No ICCP profile present in user object!"); |
|
1406 |
} |
|
1407 |
if (!(compressedProfile instanceof byte[])) { |
|
1408 |
fatal(node, "User object not a byte array!"); |
|
1409 |
} |
|
1410 |
||
1411 |
iCCP_compressedProfile = |
|
1412 |
(byte[])((byte[])compressedProfile).clone(); |
|
1413 |
||
1414 |
iCCP_present = true; |
|
1415 |
} else if (name.equals("iTXt")) { |
|
1416 |
Node iTXt_node = node.getFirstChild(); |
|
1417 |
while (iTXt_node != null) { |
|
1418 |
if (!iTXt_node.getNodeName().equals("iTXtEntry")) { |
|
1419 |
fatal(node, |
|
1420 |
"Only an iTXtEntry may be a child of an iTXt!"); |
|
1421 |
} |
|
1422 |
||
1423 |
String keyword = getAttribute(iTXt_node, "keyword"); |
|
1424 |
iTXt_keyword.add(keyword); |
|
1425 |
||
1426 |
boolean compressionFlag = |
|
1427 |
getBooleanAttribute(iTXt_node, "compressionFlag"); |
|
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1428 |
iTXt_compressionFlag.add(Boolean.valueOf(compressionFlag)); |
2 | 1429 |
|
1430 |
String compressionMethod = |
|
1431 |
getAttribute(iTXt_node, "compressionMethod"); |
|
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1432 |
iTXt_compressionMethod.add(Integer.valueOf(compressionMethod)); |
2 | 1433 |
|
1434 |
String languageTag = |
|
1435 |
getAttribute(iTXt_node, "languageTag"); |
|
1436 |
iTXt_languageTag.add(languageTag); |
|
1437 |
||
1438 |
String translatedKeyword = |
|
1439 |
getAttribute(iTXt_node, "translatedKeyword"); |
|
1440 |
iTXt_translatedKeyword.add(translatedKeyword); |
|
1441 |
||
1442 |
String text = getAttribute(iTXt_node, "text"); |
|
1443 |
iTXt_text.add(text); |
|
1444 |
||
1445 |
iTXt_node = iTXt_node.getNextSibling(); |
|
1446 |
} |
|
1447 |
} else if (name.equals("pHYs")) { |
|
1448 |
pHYs_pixelsPerUnitXAxis = |
|
1449 |
getIntAttribute(node, "pixelsPerUnitXAxis"); |
|
1450 |
pHYs_pixelsPerUnitYAxis = |
|
1451 |
getIntAttribute(node, "pixelsPerUnitYAxis"); |
|
1452 |
pHYs_unitSpecifier = |
|
1453 |
getEnumeratedAttribute(node, "unitSpecifier", |
|
1454 |
unitSpecifierNames); |
|
1455 |
||
1456 |
pHYs_present = true; |
|
1457 |
} else if (name.equals("sBIT")) { |
|
1458 |
sBIT_present = false; // Guard against partial overwrite |
|
1459 |
Node sBIT_node = node.getFirstChild(); |
|
1460 |
if (sBIT_node == null) { |
|
1461 |
fatal(node, "sBIT node has no children!"); |
|
1462 |
} |
|
1463 |
String sBIT_name = sBIT_node.getNodeName(); |
|
1464 |
if (sBIT_name.equals("sBIT_Grayscale")) { |
|
1465 |
sBIT_grayBits = getIntAttribute(sBIT_node, "gray"); |
|
1466 |
sBIT_colorType = PNGImageReader.PNG_COLOR_GRAY; |
|
1467 |
} else if (sBIT_name.equals("sBIT_GrayAlpha")) { |
|
1468 |
sBIT_grayBits = getIntAttribute(sBIT_node, "gray"); |
|
1469 |
sBIT_alphaBits = getIntAttribute(sBIT_node, "alpha"); |
|
1470 |
sBIT_colorType = PNGImageReader.PNG_COLOR_GRAY_ALPHA; |
|
1471 |
} else if (sBIT_name.equals("sBIT_RGB")) { |
|
1472 |
sBIT_redBits = getIntAttribute(sBIT_node, "red"); |
|
1473 |
sBIT_greenBits = getIntAttribute(sBIT_node, "green"); |
|
1474 |
sBIT_blueBits = getIntAttribute(sBIT_node, "blue"); |
|
1475 |
sBIT_colorType = PNGImageReader.PNG_COLOR_RGB; |
|
1476 |
} else if (sBIT_name.equals("sBIT_RGBAlpha")) { |
|
1477 |
sBIT_redBits = getIntAttribute(sBIT_node, "red"); |
|
1478 |
sBIT_greenBits = getIntAttribute(sBIT_node, "green"); |
|
1479 |
sBIT_blueBits = getIntAttribute(sBIT_node, "blue"); |
|
1480 |
sBIT_alphaBits = getIntAttribute(sBIT_node, "alpha"); |
|
1481 |
sBIT_colorType = PNGImageReader.PNG_COLOR_RGB_ALPHA; |
|
1482 |
} else if (sBIT_name.equals("sBIT_Palette")) { |
|
1483 |
sBIT_redBits = getIntAttribute(sBIT_node, "red"); |
|
1484 |
sBIT_greenBits = getIntAttribute(sBIT_node, "green"); |
|
1485 |
sBIT_blueBits = getIntAttribute(sBIT_node, "blue"); |
|
1486 |
sBIT_colorType = PNGImageReader.PNG_COLOR_PALETTE; |
|
1487 |
} else { |
|
1488 |
fatal(node, "Bad child of an sBIT node!"); |
|
1489 |
} |
|
1490 |
if (sBIT_node.getNextSibling() != null) { |
|
1491 |
fatal(node, "sBIT node has more than one child!"); |
|
1492 |
} |
|
1493 |
||
1494 |
sBIT_present = true; |
|
1495 |
} else if (name.equals("sPLT")) { |
|
1496 |
sPLT_paletteName = getAttribute(node, "name"); |
|
1497 |
sPLT_sampleDepth = getIntAttribute(node, "sampleDepth"); |
|
1498 |
||
1499 |
int[] red = new int[256]; |
|
1500 |
int[] green = new int[256]; |
|
1501 |
int[] blue = new int[256]; |
|
1502 |
int[] alpha = new int[256]; |
|
1503 |
int[] frequency = new int[256]; |
|
1504 |
int maxindex = -1; |
|
1505 |
||
1506 |
Node sPLT_entry = node.getFirstChild(); |
|
1507 |
if (sPLT_entry == null) { |
|
1508 |
fatal(node, "sPLT node has no children!"); |
|
1509 |
} |
|
1510 |
||
1511 |
while (sPLT_entry != null) { |
|
1512 |
if (!sPLT_entry.getNodeName().equals("sPLTEntry")) { |
|
1513 |
fatal(node, |
|
1514 |
"Only an sPLTEntry may be a child of an sPLT!"); |
|
1515 |
} |
|
1516 |
||
1517 |
int index = getIntAttribute(sPLT_entry, "index"); |
|
1518 |
if (index < 0 || index > 255) { |
|
1519 |
fatal(node, |
|
1520 |
"Bad value for PLTEEntry attribute index!"); |
|
1521 |
} |
|
1522 |
if (index > maxindex) { |
|
1523 |
maxindex = index; |
|
1524 |
} |
|
1525 |
red[index] = getIntAttribute(sPLT_entry, "red"); |
|
1526 |
green[index] = getIntAttribute(sPLT_entry, "green"); |
|
1527 |
blue[index] = getIntAttribute(sPLT_entry, "blue"); |
|
1528 |
alpha[index] = getIntAttribute(sPLT_entry, "alpha"); |
|
1529 |
frequency[index] = |
|
1530 |
getIntAttribute(sPLT_entry, "frequency"); |
|
1531 |
||
1532 |
sPLT_entry = sPLT_entry.getNextSibling(); |
|
1533 |
} |
|
1534 |
||
1535 |
int numEntries = maxindex + 1; |
|
1536 |
sPLT_red = new int[numEntries]; |
|
1537 |
sPLT_green = new int[numEntries]; |
|
1538 |
sPLT_blue = new int[numEntries]; |
|
1539 |
sPLT_alpha = new int[numEntries]; |
|
1540 |
sPLT_frequency = new int[numEntries]; |
|
1541 |
System.arraycopy(red, 0, sPLT_red, 0, numEntries); |
|
1542 |
System.arraycopy(green, 0, sPLT_green, 0, numEntries); |
|
1543 |
System.arraycopy(blue, 0, sPLT_blue, 0, numEntries); |
|
1544 |
System.arraycopy(alpha, 0, sPLT_alpha, 0, numEntries); |
|
1545 |
System.arraycopy(frequency, 0, |
|
1546 |
sPLT_frequency, 0, numEntries); |
|
1547 |
||
1548 |
sPLT_present = true; |
|
1549 |
} else if (name.equals("sRGB")) { |
|
1550 |
sRGB_renderingIntent = |
|
1551 |
getEnumeratedAttribute(node, "renderingIntent", |
|
1552 |
renderingIntentNames); |
|
1553 |
||
1554 |
sRGB_present = true; |
|
1555 |
} else if (name.equals("tEXt")) { |
|
1556 |
Node tEXt_node = node.getFirstChild(); |
|
1557 |
while (tEXt_node != null) { |
|
1558 |
if (!tEXt_node.getNodeName().equals("tEXtEntry")) { |
|
1559 |
fatal(node, |
|
1560 |
"Only an tEXtEntry may be a child of an tEXt!"); |
|
1561 |
} |
|
1562 |
||
1563 |
String keyword = getAttribute(tEXt_node, "keyword"); |
|
1564 |
tEXt_keyword.add(keyword); |
|
1565 |
||
1566 |
String text = getAttribute(tEXt_node, "value"); |
|
1567 |
tEXt_text.add(text); |
|
1568 |
||
1569 |
tEXt_node = tEXt_node.getNextSibling(); |
|
1570 |
} |
|
1571 |
} else if (name.equals("tIME")) { |
|
1572 |
tIME_year = getIntAttribute(node, "year"); |
|
1573 |
tIME_month = getIntAttribute(node, "month"); |
|
1574 |
tIME_day = getIntAttribute(node, "day"); |
|
1575 |
tIME_hour = getIntAttribute(node, "hour"); |
|
1576 |
tIME_minute = getIntAttribute(node, "minute"); |
|
1577 |
tIME_second = getIntAttribute(node, "second"); |
|
1578 |
||
1579 |
tIME_present = true; |
|
1580 |
} else if (name.equals("tRNS")) { |
|
1581 |
tRNS_present = false; // Guard against partial overwrite |
|
1582 |
Node tRNS_node = node.getFirstChild(); |
|
1583 |
if (tRNS_node == null) { |
|
1584 |
fatal(node, "tRNS node has no children!"); |
|
1585 |
} |
|
1586 |
String tRNS_name = tRNS_node.getNodeName(); |
|
1587 |
if (tRNS_name.equals("tRNS_Palette")) { |
|
1588 |
byte[] alpha = new byte[256]; |
|
1589 |
int maxindex = -1; |
|
1590 |
||
1591 |
Node tRNS_paletteEntry = tRNS_node.getFirstChild(); |
|
1592 |
if (tRNS_paletteEntry == null) { |
|
1593 |
fatal(node, "tRNS_Palette node has no children!"); |
|
1594 |
} |
|
1595 |
while (tRNS_paletteEntry != null) { |
|
1596 |
if (!tRNS_paletteEntry.getNodeName().equals( |
|
1597 |
"tRNS_PaletteEntry")) { |
|
1598 |
fatal(node, |
|
1599 |
"Only a tRNS_PaletteEntry may be a child of a tRNS_Palette!"); |
|
1600 |
} |
|
1601 |
int index = |
|
1602 |
getIntAttribute(tRNS_paletteEntry, "index"); |
|
1603 |
if (index < 0 || index > 255) { |
|
1604 |
fatal(node, |
|
1605 |
"Bad value for tRNS_PaletteEntry attribute index!"); |
|
1606 |
} |
|
1607 |
if (index > maxindex) { |
|
1608 |
maxindex = index; |
|
1609 |
} |
|
1610 |
alpha[index] = |
|
1611 |
(byte)getIntAttribute(tRNS_paletteEntry, |
|
1612 |
"alpha"); |
|
1613 |
||
1614 |
tRNS_paletteEntry = |
|
1615 |
tRNS_paletteEntry.getNextSibling(); |
|
1616 |
} |
|
1617 |
||
1618 |
int numEntries = maxindex + 1; |
|
1619 |
tRNS_alpha = new byte[numEntries]; |
|
1620 |
tRNS_colorType = PNGImageReader.PNG_COLOR_PALETTE; |
|
1621 |
System.arraycopy(alpha, 0, tRNS_alpha, 0, numEntries); |
|
1622 |
} else if (tRNS_name.equals("tRNS_Grayscale")) { |
|
1623 |
tRNS_gray = getIntAttribute(tRNS_node, "gray"); |
|
1624 |
tRNS_colorType = PNGImageReader.PNG_COLOR_GRAY; |
|
1625 |
} else if (tRNS_name.equals("tRNS_RGB")) { |
|
1626 |
tRNS_red = getIntAttribute(tRNS_node, "red"); |
|
1627 |
tRNS_green = getIntAttribute(tRNS_node, "green"); |
|
1628 |
tRNS_blue = getIntAttribute(tRNS_node, "blue"); |
|
1629 |
tRNS_colorType = PNGImageReader.PNG_COLOR_RGB; |
|
1630 |
} else { |
|
1631 |
fatal(node, "Bad child of a tRNS node!"); |
|
1632 |
} |
|
1633 |
if (tRNS_node.getNextSibling() != null) { |
|
1634 |
fatal(node, "tRNS node has more than one child!"); |
|
1635 |
} |
|
1636 |
||
1637 |
tRNS_present = true; |
|
1638 |
} else if (name.equals("zTXt")) { |
|
1639 |
Node zTXt_node = node.getFirstChild(); |
|
1640 |
while (zTXt_node != null) { |
|
1641 |
if (!zTXt_node.getNodeName().equals("zTXtEntry")) { |
|
1642 |
fatal(node, |
|
1643 |
"Only an zTXtEntry may be a child of an zTXt!"); |
|
1644 |
} |
|
1645 |
||
1646 |
String keyword = getAttribute(zTXt_node, "keyword"); |
|
1647 |
zTXt_keyword.add(keyword); |
|
1648 |
||
1649 |
int compressionMethod = |
|
1650 |
getEnumeratedAttribute(zTXt_node, "compressionMethod", |
|
1651 |
zTXt_compressionMethodNames); |
|
1652 |
zTXt_compressionMethod.add(new Integer(compressionMethod)); |
|
1653 |
||
1654 |
String text = getAttribute(zTXt_node, "text"); |
|
1655 |
zTXt_text.add(text); |
|
1656 |
||
1657 |
zTXt_node = zTXt_node.getNextSibling(); |
|
1658 |
} |
|
1659 |
} else if (name.equals("UnknownChunks")) { |
|
1660 |
Node unknown_node = node.getFirstChild(); |
|
1661 |
while (unknown_node != null) { |
|
1662 |
if (!unknown_node.getNodeName().equals("UnknownChunk")) { |
|
1663 |
fatal(node, |
|
1664 |
"Only an UnknownChunk may be a child of an UnknownChunks!"); |
|
1665 |
} |
|
1666 |
String chunkType = getAttribute(unknown_node, "type"); |
|
1667 |
Object chunkData = |
|
1668 |
((IIOMetadataNode)unknown_node).getUserObject(); |
|
1669 |
||
1670 |
if (chunkType.length() != 4) { |
|
1671 |
fatal(unknown_node, |
|
1672 |
"Chunk type must be 4 characters!"); |
|
1673 |
} |
|
1674 |
if (chunkData == null) { |
|
1675 |
fatal(unknown_node, |
|
1676 |
"No chunk data present in user object!"); |
|
1677 |
} |
|
1678 |
if (!(chunkData instanceof byte[])) { |
|
1679 |
fatal(unknown_node, |
|
1680 |
"User object not a byte array!"); |
|
1681 |
} |
|
1682 |
unknownChunkType.add(chunkType); |
|
1683 |
unknownChunkData.add(((byte[])chunkData).clone()); |
|
1684 |
||
1685 |
unknown_node = unknown_node.getNextSibling(); |
|
1686 |
} |
|
1687 |
} else { |
|
1688 |
fatal(node, "Unknown child of root node!"); |
|
1689 |
} |
|
1690 |
||
1691 |
node = node.getNextSibling(); |
|
1692 |
} |
|
1693 |
} |
|
1694 |
||
1695 |
private boolean isISOLatin(String s) { |
|
1696 |
int len = s.length(); |
|
1697 |
for (int i = 0; i < len; i++) { |
|
1698 |
if (s.charAt(i) > 255) { |
|
1699 |
return false; |
|
1700 |
} |
|
1701 |
} |
|
1702 |
return true; |
|
1703 |
} |
|
1704 |
||
1705 |
private void mergeStandardTree(Node root) |
|
1706 |
throws IIOInvalidTreeException { |
|
1707 |
Node node = root; |
|
1708 |
if (!node.getNodeName() |
|
1709 |
.equals(IIOMetadataFormatImpl.standardMetadataFormatName)) { |
|
1710 |
fatal(node, "Root must be " + |
|
1711 |
IIOMetadataFormatImpl.standardMetadataFormatName); |
|
1712 |
} |
|
1713 |
||
1714 |
node = node.getFirstChild(); |
|
1715 |
while (node != null) { |
|
1716 |
String name = node.getNodeName(); |
|
1717 |
||
1718 |
if (name.equals("Chroma")) { |
|
1719 |
Node child = node.getFirstChild(); |
|
1720 |
while (child != null) { |
|
1721 |
String childName = child.getNodeName(); |
|
1722 |
if (childName.equals("Gamma")) { |
|
1723 |
float gamma = getFloatAttribute(child, "value"); |
|
1724 |
gAMA_present = true; |
|
1725 |
gAMA_gamma = (int)(gamma*100000 + 0.5); |
|
1726 |
} else if (childName.equals("Palette")) { |
|
1727 |
byte[] red = new byte[256]; |
|
1728 |
byte[] green = new byte[256]; |
|
1729 |
byte[] blue = new byte[256]; |
|
1730 |
int maxindex = -1; |
|
1731 |
||
1732 |
Node entry = child.getFirstChild(); |
|
1733 |
while (entry != null) { |
|
1734 |
int index = getIntAttribute(entry, "index"); |
|
1735 |
if (index >= 0 && index <= 255) { |
|
1736 |
red[index] = |
|
1737 |
(byte)getIntAttribute(entry, "red"); |
|
1738 |
green[index] = |
|
1739 |
(byte)getIntAttribute(entry, "green"); |
|
1740 |
blue[index] = |
|
1741 |
(byte)getIntAttribute(entry, "blue"); |
|
1742 |
if (index > maxindex) { |
|
1743 |
maxindex = index; |
|
1744 |
} |
|
1745 |
} |
|
1746 |
entry = entry.getNextSibling(); |
|
1747 |
} |
|
1748 |
||
1749 |
int numEntries = maxindex + 1; |
|
1750 |
PLTE_red = new byte[numEntries]; |
|
1751 |
PLTE_green = new byte[numEntries]; |
|
1752 |
PLTE_blue = new byte[numEntries]; |
|
1753 |
System.arraycopy(red, 0, PLTE_red, 0, numEntries); |
|
1754 |
System.arraycopy(green, 0, PLTE_green, 0, numEntries); |
|
1755 |
System.arraycopy(blue, 0, PLTE_blue, 0, numEntries); |
|
1756 |
PLTE_present = true; |
|
1757 |
} else if (childName.equals("BackgroundIndex")) { |
|
1758 |
bKGD_present = true; |
|
1759 |
bKGD_colorType = PNGImageReader.PNG_COLOR_PALETTE; |
|
1760 |
bKGD_index = getIntAttribute(child, "value"); |
|
1761 |
} else if (childName.equals("BackgroundColor")) { |
|
1762 |
int red = getIntAttribute(child, "red"); |
|
1763 |
int green = getIntAttribute(child, "green"); |
|
1764 |
int blue = getIntAttribute(child, "blue"); |
|
1765 |
if (red == green && red == blue) { |
|
1766 |
bKGD_colorType = PNGImageReader.PNG_COLOR_GRAY; |
|
1767 |
bKGD_gray = red; |
|
1768 |
} else { |
|
1769 |
bKGD_red = red; |
|
1770 |
bKGD_green = green; |
|
1771 |
bKGD_blue = blue; |
|
1772 |
} |
|
1773 |
bKGD_present = true; |
|
1774 |
} |
|
1775 |
// } else if (childName.equals("ColorSpaceType")) { |
|
1776 |
// } else if (childName.equals("NumChannels")) { |
|
1777 |
||
1778 |
child = child.getNextSibling(); |
|
1779 |
} |
|
1780 |
} else if (name.equals("Compression")) { |
|
1781 |
Node child = node.getFirstChild(); |
|
1782 |
while (child != null) { |
|
1783 |
String childName = child.getNodeName(); |
|
1784 |
if (childName.equals("NumProgressiveScans")) { |
|
1785 |
// Use Adam7 if NumProgressiveScans > 1 |
|
1786 |
int scans = getIntAttribute(child, "value"); |
|
1787 |
IHDR_interlaceMethod = (scans > 1) ? 1 : 0; |
|
1788 |
// } else if (childName.equals("CompressionTypeName")) { |
|
1789 |
// } else if (childName.equals("Lossless")) { |
|
1790 |
// } else if (childName.equals("BitRate")) { |
|
1791 |
} |
|
1792 |
child = child.getNextSibling(); |
|
1793 |
} |
|
1794 |
} else if (name.equals("Data")) { |
|
1795 |
Node child = node.getFirstChild(); |
|
1796 |
while (child != null) { |
|
1797 |
String childName = child.getNodeName(); |
|
1798 |
if (childName.equals("BitsPerSample")) { |
|
1799 |
String s = getAttribute(child, "value"); |
|
1800 |
StringTokenizer t = new StringTokenizer(s); |
|
1801 |
int maxBits = -1; |
|
1802 |
while (t.hasMoreTokens()) { |
|
1803 |
int bits = Integer.parseInt(t.nextToken()); |
|
1804 |
if (bits > maxBits) { |
|
1805 |
maxBits = bits; |
|
1806 |
} |
|
1807 |
} |
|
1808 |
if (maxBits < 1) { |
|
1809 |
maxBits = 1; |
|
1810 |
} |
|
1811 |
if (maxBits == 3) maxBits = 4; |
|
1812 |
if (maxBits > 4 || maxBits < 8) { |
|
1813 |
maxBits = 8; |
|
1814 |
} |
|
1815 |
if (maxBits > 8) { |
|
1816 |
maxBits = 16; |
|
1817 |
} |
|
1818 |
IHDR_bitDepth = maxBits; |
|
1819 |
} else if (childName.equals("SignificantBitsPerSample")) { |
|
1820 |
String s = getAttribute(child, "value"); |
|
1821 |
StringTokenizer t = new StringTokenizer(s); |
|
1822 |
int numTokens = t.countTokens(); |
|
1823 |
if (numTokens == 1) { |
|
1824 |
sBIT_colorType = PNGImageReader.PNG_COLOR_GRAY; |
|
1825 |
sBIT_grayBits = Integer.parseInt(t.nextToken()); |
|
1826 |
} else if (numTokens == 2) { |
|
1827 |
sBIT_colorType = |
|
1828 |
PNGImageReader.PNG_COLOR_GRAY_ALPHA; |
|
1829 |
sBIT_grayBits = Integer.parseInt(t.nextToken()); |
|
1830 |
sBIT_alphaBits = Integer.parseInt(t.nextToken()); |
|
1831 |
} else if (numTokens == 3) { |
|
1832 |
sBIT_colorType = PNGImageReader.PNG_COLOR_RGB; |
|
1833 |
sBIT_redBits = Integer.parseInt(t.nextToken()); |
|
1834 |
sBIT_greenBits = Integer.parseInt(t.nextToken()); |
|
1835 |
sBIT_blueBits = Integer.parseInt(t.nextToken()); |
|
1836 |
} else if (numTokens == 4) { |
|
1837 |
sBIT_colorType = |
|
1838 |
PNGImageReader.PNG_COLOR_RGB_ALPHA; |
|
1839 |
sBIT_redBits = Integer.parseInt(t.nextToken()); |
|
1840 |
sBIT_greenBits = Integer.parseInt(t.nextToken()); |
|
1841 |
sBIT_blueBits = Integer.parseInt(t.nextToken()); |
|
1842 |
sBIT_alphaBits = Integer.parseInt(t.nextToken()); |
|
1843 |
} |
|
1844 |
if (numTokens >= 1 && numTokens <= 4) { |
|
1845 |
sBIT_present = true; |
|
1846 |
} |
|
1847 |
// } else if (childName.equals("PlanarConfiguration")) { |
|
1848 |
// } else if (childName.equals("SampleFormat")) { |
|
1849 |
// } else if (childName.equals("SampleMSB")) { |
|
1850 |
} |
|
1851 |
child = child.getNextSibling(); |
|
1852 |
} |
|
1853 |
} else if (name.equals("Dimension")) { |
|
1854 |
boolean gotWidth = false; |
|
1855 |
boolean gotHeight = false; |
|
1856 |
boolean gotAspectRatio = false; |
|
1857 |
||
1858 |
float width = -1.0F; |
|
1859 |
float height = -1.0F; |
|
1860 |
float aspectRatio = -1.0F; |
|
1861 |
||
1862 |
Node child = node.getFirstChild(); |
|
1863 |
while (child != null) { |
|
1864 |
String childName = child.getNodeName(); |
|
1865 |
if (childName.equals("PixelAspectRatio")) { |
|
1866 |
aspectRatio = getFloatAttribute(child, "value"); |
|
1867 |
gotAspectRatio = true; |
|
1868 |
} else if (childName.equals("HorizontalPixelSize")) { |
|
1869 |
width = getFloatAttribute(child, "value"); |
|
1870 |
gotWidth = true; |
|
1871 |
} else if (childName.equals("VerticalPixelSize")) { |
|
1872 |
height = getFloatAttribute(child, "value"); |
|
1873 |
gotHeight = true; |
|
1874 |
// } else if (childName.equals("ImageOrientation")) { |
|
1875 |
// } else if |
|
1876 |
// (childName.equals("HorizontalPhysicalPixelSpacing")) { |
|
1877 |
// } else if |
|
1878 |
// (childName.equals("VerticalPhysicalPixelSpacing")) { |
|
1879 |
// } else if (childName.equals("HorizontalPosition")) { |
|
1880 |
// } else if (childName.equals("VerticalPosition")) { |
|
1881 |
// } else if (childName.equals("HorizontalPixelOffset")) { |
|
1882 |
// } else if (childName.equals("VerticalPixelOffset")) { |
|
1883 |
} |
|
1884 |
child = child.getNextSibling(); |
|
1885 |
} |
|
1886 |
||
1887 |
if (gotWidth && gotHeight) { |
|
1888 |
pHYs_present = true; |
|
1889 |
pHYs_unitSpecifier = 1; |
|
1890 |
pHYs_pixelsPerUnitXAxis = (int)(width*1000 + 0.5F); |
|
1891 |
pHYs_pixelsPerUnitYAxis = (int)(height*1000 + 0.5F); |
|
1892 |
} else if (gotAspectRatio) { |
|
1893 |
pHYs_present = true; |
|
1894 |
pHYs_unitSpecifier = 0; |
|
1895 |
||
1896 |
// Find a reasonable rational approximation |
|
1897 |
int denom = 1; |
|
1898 |
for (; denom < 100; denom++) { |
|
1899 |
int num = (int)(aspectRatio*denom); |
|
1900 |
if (Math.abs(num/denom - aspectRatio) < 0.001) { |
|
1901 |
break; |
|
1902 |
} |
|
1903 |
} |
|
1904 |
pHYs_pixelsPerUnitXAxis = (int)(aspectRatio*denom); |
|
1905 |
pHYs_pixelsPerUnitYAxis = denom; |
|
1906 |
} |
|
1907 |
} else if (name.equals("Document")) { |
|
1908 |
Node child = node.getFirstChild(); |
|
1909 |
while (child != null) { |
|
1910 |
String childName = child.getNodeName(); |
|
1911 |
if (childName.equals("ImageModificationTime")) { |
|
1912 |
tIME_present = true; |
|
1913 |
tIME_year = getIntAttribute(child, "year"); |
|
1914 |
tIME_month = getIntAttribute(child, "month"); |
|
1915 |
tIME_day = getIntAttribute(child, "day"); |
|
1916 |
tIME_hour = |
|
1917 |
getIntAttribute(child, "hour", 0, false); |
|
1918 |
tIME_minute = |
|
1919 |
getIntAttribute(child, "minute", 0, false); |
|
1920 |
tIME_second = |
|
1921 |
getIntAttribute(child, "second", 0, false); |
|
1922 |
// } else if (childName.equals("SubimageInterpretation")) { |
|
1923 |
// } else if (childName.equals("ImageCreationTime")) { |
|
1924 |
} |
|
1925 |
child = child.getNextSibling(); |
|
1926 |
} |
|
1927 |
} else if (name.equals("Text")) { |
|
1928 |
Node child = node.getFirstChild(); |
|
1929 |
while (child != null) { |
|
1930 |
String childName = child.getNodeName(); |
|
1931 |
if (childName.equals("TextEntry")) { |
|
1932 |
String keyword = getAttribute(child, "keyword"); |
|
1933 |
String value = getAttribute(child, "value"); |
|
1934 |
String encoding = getAttribute(child, "encoding"); |
|
1935 |
String language = getAttribute(child, "language"); |
|
1936 |
String compression = |
|
1937 |
getAttribute(child, "compression"); |
|
1938 |
||
1939 |
if (isISOLatin(value)) { |
|
1940 |
if (compression.equals("zip")) { |
|
1941 |
// Use a zTXt node |
|
1942 |
zTXt_keyword.add(keyword); |
|
1943 |
zTXt_text.add(value); |
|
1944 |
zTXt_compressionMethod.add(new Integer(0)); |
|
1945 |
} else { |
|
1946 |
// Use a tEXt node |
|
1947 |
tEXt_keyword.add(keyword); |
|
1948 |
tEXt_text.add(value); |
|
1949 |
} |
|
1950 |
} else { |
|
1951 |
// Use an iTXt node |
|
1952 |
iTXt_keyword.add(keyword); |
|
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1953 |
iTXt_compressionFlag.add(Boolean.valueOf(compression.equals("zip"))); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1954 |
iTXt_compressionMethod.add(Integer.valueOf(0)); |
2 | 1955 |
iTXt_languageTag.add(language); |
1956 |
iTXt_translatedKeyword.add(keyword); // fake it |
|
1957 |
iTXt_text.add(value); |
|
1958 |
} |
|
1959 |
} |
|
1960 |
child = child.getNextSibling(); |
|
1961 |
} |
|
1962 |
// } else if (name.equals("Transparency")) { |
|
1963 |
// Node child = node.getFirstChild(); |
|
1964 |
// while (child != null) { |
|
1965 |
// String childName = child.getNodeName(); |
|
1966 |
// if (childName.equals("Alpha")) { |
|
1967 |
// } else if (childName.equals("TransparentIndex")) { |
|
1968 |
// } else if (childName.equals("TransparentColor")) { |
|
1969 |
// } else if (childName.equals("TileTransparencies")) { |
|
1970 |
// } else if (childName.equals("TileOpacities")) { |
|
1971 |
// } |
|
1972 |
// child = child.getNextSibling(); |
|
1973 |
// } |
|
1974 |
// } else { |
|
1975 |
// // fatal(node, "Unknown child of root node!"); |
|
1976 |
} |
|
1977 |
||
1978 |
node = node.getNextSibling(); |
|
1979 |
} |
|
1980 |
} |
|
1981 |
||
1982 |
// Reset all instance variables to their initial state |
|
1983 |
public void reset() { |
|
1984 |
IHDR_present = false; |
|
1985 |
PLTE_present = false; |
|
1986 |
bKGD_present = false; |
|
1987 |
cHRM_present = false; |
|
1988 |
gAMA_present = false; |
|
1989 |
hIST_present = false; |
|
1990 |
iCCP_present = false; |
|
1734
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1991 |
iTXt_keyword = new ArrayList<String>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1992 |
iTXt_compressionFlag = new ArrayList<Boolean>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1993 |
iTXt_compressionMethod = new ArrayList<Integer>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1994 |
iTXt_languageTag = new ArrayList<String>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1995 |
iTXt_translatedKeyword = new ArrayList<String>(); |
861400729115
6541476: PNG imageio plugin incorrectly handles iTXt chunk
bae
parents:
2
diff
changeset
|
1996 |
iTXt_text = new ArrayList<String>(); |
2 | 1997 |
pHYs_present = false; |
1998 |
sBIT_present = false; |
|
1999 |
sPLT_present = false; |
|
2000 |
sRGB_present = false; |
|
2001 |
tEXt_keyword = new ArrayList(); |
|
2002 |
tEXt_text = new ArrayList(); |
|
2003 |
tIME_present = false; |
|
2004 |
tRNS_present = false; |
|
2005 |
zTXt_keyword = new ArrayList(); |
|
2006 |
zTXt_compressionMethod = new ArrayList(); |
|
2007 |
zTXt_text = new ArrayList(); |
|
2008 |
unknownChunkType = new ArrayList(); |
|
2009 |
unknownChunkData = new ArrayList(); |
|
2010 |
} |
|
2011 |
} |