190
189
private int maxOpacity;
192
private int[] significantBits = null;
191
private int[] significantBits;
194
193
// Parameter information
196
195
// If true, the user wants destination alpha where applicable.
197
private boolean suppressAlpha = false;
196
private boolean suppressAlpha;
199
198
// If true, perform palette lookup internally
200
private boolean expandPalette = false;
199
private boolean expandPalette;
202
201
// If true, output < 8 bit gray images in 8 bit components format
203
private boolean output8BitGray = false;
202
private boolean output8BitGray;
205
204
// Create an alpha channel in the destination color model.
206
private boolean outputHasAlphaPalette = false;
205
private boolean outputHasAlphaPalette;
208
207
// Perform gamma correction on the image
209
private boolean performGammaCorrection = false;
208
private boolean performGammaCorrection;
211
210
// Expand GA to GGGA for compatbility with Java2D
212
private boolean expandGrayAlpha = false;
211
private boolean expandGrayAlpha;
214
213
// Produce an instance of PNGEncodeParam
215
private boolean generateEncodeParam = false;
214
private boolean generateEncodeParam;
217
216
// PNGDecodeParam controlling decode process
218
private PNGDecodeParam decodeParam = null;
217
private PNGDecodeParam decodeParam;
220
219
// PNGEncodeParam to store file details in
221
private PNGEncodeParam encodeParam = null;
220
private PNGEncodeParam encodeParam;
223
222
private boolean emitProperties = true;
225
private float fileGamma = 45455/100000.0F;
224
private float fileGamma = 45455 / 100000.0F;
227
226
private float userExponent = 1.0F;
229
228
private float displayExponent = 2.2F;
231
private float[] chromaticity = null;
230
private float[] chromaticity;
233
232
private int sRGBRenderingIntent = -1;
388
387
properties.put("file_type", "PNG v. 1.0");
392
long magic = distream.readLong();
393
if (magic != 0x89504e470d0a1a0aL) {
394
String msg = PropertyUtil.getString("PNGImageDecoder0");
395
throw new RuntimeException(msg);
397
} catch (Exception e) {
399
String msg = PropertyUtil.getString("PNGImageDecoder1");
390
long magic = distream.readLong();
391
if (magic != 0x89504e470d0a1a0aL) {
392
String msg = PropertyUtil.getString("PNGImageDecoder0");
400
393
throw new RuntimeException(msg);
407
String chunkType = getChunkType(distream);
408
if (chunkType.equals("IHDR")) {
409
chunk = readChunk(distream);
410
parse_IHDR_chunk(chunk);
411
} else if (chunkType.equals("PLTE")) {
412
chunk = readChunk(distream);
413
parse_PLTE_chunk(chunk);
414
} else if (chunkType.equals("IDAT")) {
415
chunk = readChunk(distream);
416
streamVec.add(new ByteArrayInputStream(chunk.getData()));
417
} else if (chunkType.equals("IEND")) {
418
chunk = readChunk(distream);
399
String chunkType = getChunkType(distream);
400
if (chunkType.equals("IHDR")) {
401
chunk = readChunk(distream);
402
parse_IHDR_chunk(chunk);
403
} else if (chunkType.equals("PLTE")) {
404
chunk = readChunk(distream);
405
parse_PLTE_chunk(chunk);
406
} else if (chunkType.equals("IDAT")) {
407
chunk = readChunk(distream);
408
streamVec.add(new ByteArrayInputStream(chunk.getData()));
409
} else if (chunkType.equals("IEND")) {
410
chunk = readChunk(distream);
419
412
parse_IEND_chunk(chunk);
420
break; // fall through to the bottom
421
} else if (chunkType.equals("bKGD")) {
422
chunk = readChunk(distream);
423
parse_bKGD_chunk(chunk);
424
} else if (chunkType.equals("cHRM")) {
425
chunk = readChunk(distream);
426
parse_cHRM_chunk(chunk);
427
} else if (chunkType.equals("gAMA")) {
428
chunk = readChunk(distream);
429
parse_gAMA_chunk(chunk);
430
} else if (chunkType.equals("hIST")) {
431
chunk = readChunk(distream);
432
parse_hIST_chunk(chunk);
433
} else if (chunkType.equals("iCCP")) {
434
chunk = readChunk(distream);
435
parse_iCCP_chunk(chunk);
436
} else if (chunkType.equals("pHYs")) {
437
chunk = readChunk(distream);
438
parse_pHYs_chunk(chunk);
439
} else if (chunkType.equals("sBIT")) {
440
chunk = readChunk(distream);
441
parse_sBIT_chunk(chunk);
442
} else if (chunkType.equals("sRGB")) {
443
chunk = readChunk(distream);
444
parse_sRGB_chunk(chunk);
445
} else if (chunkType.equals("tEXt")) {
446
chunk = readChunk(distream);
447
parse_tEXt_chunk(chunk);
448
} else if (chunkType.equals("tIME")) {
449
chunk = readChunk(distream);
450
parse_tIME_chunk(chunk);
451
} else if (chunkType.equals("tRNS")) {
452
chunk = readChunk(distream);
453
parse_tRNS_chunk(chunk);
454
} else if (chunkType.equals("zTXt")) {
455
chunk = readChunk(distream);
456
parse_zTXt_chunk(chunk);
458
chunk = readChunk(distream);
459
// Output the chunk data in raw form
413
} catch (Exception e) {
415
String msg = PropertyUtil.getString("PNGImageDecoder2");
416
throw new RuntimeException(msg);
418
break; // fall through to the bottom
419
} else if (chunkType.equals("bKGD")) {
420
chunk = readChunk(distream);
421
parse_bKGD_chunk(chunk);
422
} else if (chunkType.equals("cHRM")) {
423
chunk = readChunk(distream);
424
parse_cHRM_chunk(chunk);
425
} else if (chunkType.equals("gAMA")) {
426
chunk = readChunk(distream);
427
parse_gAMA_chunk(chunk);
428
} else if (chunkType.equals("hIST")) {
429
chunk = readChunk(distream);
430
parse_hIST_chunk(chunk);
431
} else if (chunkType.equals("iCCP")) {
432
chunk = readChunk(distream);
433
} else if (chunkType.equals("pHYs")) {
434
chunk = readChunk(distream);
435
parse_pHYs_chunk(chunk);
436
} else if (chunkType.equals("sBIT")) {
437
chunk = readChunk(distream);
438
parse_sBIT_chunk(chunk);
439
} else if (chunkType.equals("sRGB")) {
440
chunk = readChunk(distream);
441
parse_sRGB_chunk(chunk);
442
} else if (chunkType.equals("tEXt")) {
443
chunk = readChunk(distream);
444
parse_tEXt_chunk(chunk);
445
} else if (chunkType.equals("tIME")) {
446
chunk = readChunk(distream);
447
parse_tIME_chunk(chunk);
448
} else if (chunkType.equals("tRNS")) {
449
chunk = readChunk(distream);
450
parse_tRNS_chunk(chunk);
451
} else if (chunkType.equals("zTXt")) {
452
chunk = readChunk(distream);
453
parse_zTXt_chunk(chunk);
455
chunk = readChunk(distream);
456
// Output the chunk data in raw form
461
String type = chunk.getTypeString();
462
byte[] data = chunk.getData();
463
if (encodeParam != null) {
464
encodeParam.addPrivateChunk(type, data);
466
if (emitProperties) {
467
String key = "chunk_" + chunkIndex++ + ':' + type;
468
properties.put(key.toLowerCase(), data);
471
} catch (Exception e) {
473
String msg = PropertyUtil.getString("PNGImageDecoder2");
474
throw new RuntimeException(msg);
458
String type = chunk.getTypeString();
459
byte[] data = chunk.getData();
460
if (encodeParam != null) {
461
encodeParam.addPrivateChunk(type, data);
463
if (emitProperties) {
464
String key = "chunk_" + chunkIndex++ + ':' + type;
465
properties.put(key.toLowerCase(Locale.getDefault()), data);
1081
1072
chromaticity = new float[8];
1082
chromaticity[0] = chunk.getInt4(0)/100000.0F;
1083
chromaticity[1] = chunk.getInt4(4)/100000.0F;
1084
chromaticity[2] = chunk.getInt4(8)/100000.0F;
1085
chromaticity[3] = chunk.getInt4(12)/100000.0F;
1086
chromaticity[4] = chunk.getInt4(16)/100000.0F;
1087
chromaticity[5] = chunk.getInt4(20)/100000.0F;
1088
chromaticity[6] = chunk.getInt4(24)/100000.0F;
1089
chromaticity[7] = chunk.getInt4(28)/100000.0F;
1073
chromaticity[0] = chunk.getInt4(0) / 100000.0F;
1074
chromaticity[1] = chunk.getInt4(4) / 100000.0F;
1075
chromaticity[2] = chunk.getInt4(8) / 100000.0F;
1076
chromaticity[3] = chunk.getInt4(12) / 100000.0F;
1077
chromaticity[4] = chunk.getInt4(16) / 100000.0F;
1078
chromaticity[5] = chunk.getInt4(20) / 100000.0F;
1079
chromaticity[6] = chunk.getInt4(24) / 100000.0F;
1080
chromaticity[7] = chunk.getInt4(28) / 100000.0F;
1091
1082
if (encodeParam != null) {
1092
1083
encodeParam.setChromaticity(chromaticity);
1094
1085
if (emitProperties) {
1095
properties.put("white_point_x", new Float(chromaticity[0]));
1096
properties.put("white_point_y", new Float(chromaticity[1]));
1097
properties.put("red_x", new Float(chromaticity[2]));
1098
properties.put("red_y", new Float(chromaticity[3]));
1099
properties.put("green_x", new Float(chromaticity[4]));
1100
properties.put("green_y", new Float(chromaticity[5]));
1101
properties.put("blue_x", new Float(chromaticity[6]));
1102
properties.put("blue_y", new Float(chromaticity[7]));
1086
properties.put("white_point_x", chromaticity[0]);
1087
properties.put("white_point_y", chromaticity[1]);
1088
properties.put("red_x", chromaticity[2]);
1089
properties.put("red_y", chromaticity[3]);
1090
properties.put("green_x", chromaticity[4]);
1091
properties.put("green_y", chromaticity[5]);
1092
properties.put("blue_x", chromaticity[6]);
1093
properties.put("blue_y", chromaticity[7]);
1205
1186
// The presence of an sRGB chunk implies particular
1206
1187
// settings for gamma and chroma.
1207
fileGamma = 45455/100000.0F;
1188
fileGamma = 45455 / 100000.0F;
1209
1190
chromaticity = new float[8];
1210
chromaticity[0] = 31270/10000.0F;
1211
chromaticity[1] = 32900/10000.0F;
1212
chromaticity[2] = 64000/10000.0F;
1213
chromaticity[3] = 33000/10000.0F;
1214
chromaticity[4] = 30000/10000.0F;
1215
chromaticity[5] = 60000/10000.0F;
1216
chromaticity[6] = 15000/10000.0F;
1217
chromaticity[7] = 6000/10000.0F;
1191
chromaticity[0] = 31270 / 10000.0F;
1192
chromaticity[1] = 32900 / 10000.0F;
1193
chromaticity[2] = 64000 / 10000.0F;
1194
chromaticity[3] = 33000 / 10000.0F;
1195
chromaticity[4] = 30000 / 10000.0F;
1196
chromaticity[5] = 60000 / 10000.0F;
1197
chromaticity[6] = 15000 / 10000.0F;
1198
chromaticity[7] = 6000 / 10000.0F;
1219
1200
if (performGammaCorrection) {
1220
1201
// File gamma is 1/2.2
1221
float gamma = fileGamma*(displayExponent/userExponent);
1202
float gamma = fileGamma * (displayExponent / userExponent);
1222
1203
if (encodeParam != null) {
1223
1204
encodeParam.setGamma(gamma);
1224
1205
encodeParam.setChromaticity(chromaticity);
1226
1207
if (emitProperties) {
1227
properties.put("gamma", new Float(gamma));
1228
properties.put("white_point_x", new Float(chromaticity[0]));
1229
properties.put("white_point_y", new Float(chromaticity[1]));
1230
properties.put("red_x", new Float(chromaticity[2]));
1231
properties.put("red_y", new Float(chromaticity[3]));
1232
properties.put("green_x", new Float(chromaticity[4]));
1233
properties.put("green_y", new Float(chromaticity[5]));
1234
properties.put("blue_x", new Float(chromaticity[6]));
1235
properties.put("blue_y", new Float(chromaticity[7]));
1208
properties.put("gamma", gamma);
1209
properties.put("white_point_x", chromaticity[0]);
1210
properties.put("white_point_y", chromaticity[1]);
1211
properties.put("red_x", chromaticity[2]);
1212
properties.put("red_y", chromaticity[3]);
1213
properties.put("green_x", chromaticity[4]);
1214
properties.put("green_y", chromaticity[5]);
1215
properties.put("blue_x", chromaticity[6]);
1216
properties.put("blue_y", chromaticity[7]);
1851
1833
if (!useInterlacing) {
1852
1834
decodePass(theTile, 0, 0, 1, 1, width, height);
1854
decodePass(theTile, 0, 0, 8, 8, (width + 7)/8, (height + 7)/8);
1855
decodePass(theTile, 4, 0, 8, 8, (width + 3)/8, (height + 7)/8);
1856
decodePass(theTile, 0, 4, 4, 8, (width + 3)/4, (height + 3)/8);
1857
decodePass(theTile, 2, 0, 4, 4, (width + 1)/4, (height + 3)/4);
1858
decodePass(theTile, 0, 2, 2, 4, (width + 1)/2, (height + 1)/4);
1859
decodePass(theTile, 1, 0, 2, 2, width/2, (height + 1)/2);
1860
decodePass(theTile, 0, 1, 1, 2, width, height/2);
1836
decodePass(theTile, 0, 0, 8, 8, (width + 7) / 8, (height + 7) / 8);
1837
decodePass(theTile, 4, 0, 8, 8, (width + 3) / 8, (height + 7) / 8);
1838
decodePass(theTile, 0, 4, 4, 8, (width + 3) / 4, (height + 3) / 8);
1839
decodePass(theTile, 2, 0, 4, 4, (width + 1) / 4, (height + 3) / 4);
1840
decodePass(theTile, 0, 2, 2, 4, (width + 1) / 2, (height + 1) / 4);
1841
decodePass(theTile, 1, 0, 2, 2, width / 2, (height + 1) / 2);
1842
decodePass(theTile, 0, 1, 1, 2, width, height / 2);