426 metadata.hIST_present = true; |
426 metadata.hIST_present = true; |
427 } |
427 } |
428 |
428 |
429 private void parse_iCCP_chunk(int chunkLength) throws IOException { |
429 private void parse_iCCP_chunk(int chunkLength) throws IOException { |
430 String keyword = readNullTerminatedString("ISO-8859-1", 80); |
430 String keyword = readNullTerminatedString("ISO-8859-1", 80); |
|
431 int compressedProfileLength = chunkLength - keyword.length() - 2; |
|
432 if (compressedProfileLength <= 0) { |
|
433 throw new IIOException("iCCP chunk length is not proper"); |
|
434 } |
431 metadata.iCCP_profileName = keyword; |
435 metadata.iCCP_profileName = keyword; |
432 |
436 |
433 metadata.iCCP_compressionMethod = stream.readUnsignedByte(); |
437 metadata.iCCP_compressionMethod = stream.readUnsignedByte(); |
434 |
438 |
435 byte[] compressedProfile = |
439 byte[] compressedProfile = |
436 new byte[chunkLength - keyword.length() - 2]; |
440 new byte[compressedProfileLength]; |
437 stream.readFully(compressedProfile); |
441 stream.readFully(compressedProfile); |
438 metadata.iCCP_compressedProfile = compressedProfile; |
442 metadata.iCCP_compressedProfile = compressedProfile; |
439 |
443 |
440 metadata.iCCP_present = true; |
444 metadata.iCCP_present = true; |
441 } |
445 } |
461 readNullTerminatedString("UTF8", maxLen); |
465 readNullTerminatedString("UTF8", maxLen); |
462 metadata.iTXt_translatedKeyword.add(translatedKeyword); |
466 metadata.iTXt_translatedKeyword.add(translatedKeyword); |
463 |
467 |
464 String text; |
468 String text; |
465 pos = stream.getStreamPosition(); |
469 pos = stream.getStreamPosition(); |
466 byte[] b = new byte[(int)(chunkStart + chunkLength - pos)]; |
470 int textLength = (int)(chunkStart + chunkLength - pos); |
|
471 if (textLength <= 0) { |
|
472 throw new IIOException("iTXt chunk length is not proper"); |
|
473 } |
|
474 byte[] b = new byte[textLength]; |
467 stream.readFully(b); |
475 stream.readFully(b); |
468 |
476 |
469 if (compressionFlag == 1) { // Decompress the text |
477 if (compressionFlag == 1) { // Decompress the text |
470 text = new String(inflate(b), "UTF8"); |
478 text = new String(inflate(b), "UTF8"); |
471 } else { |
479 } else { |
513 } |
521 } |
514 |
522 |
515 private void parse_sPLT_chunk(int chunkLength) |
523 private void parse_sPLT_chunk(int chunkLength) |
516 throws IOException, IIOException { |
524 throws IOException, IIOException { |
517 metadata.sPLT_paletteName = readNullTerminatedString("ISO-8859-1", 80); |
525 metadata.sPLT_paletteName = readNullTerminatedString("ISO-8859-1", 80); |
518 chunkLength -= metadata.sPLT_paletteName.length() + 1; |
526 int remainingChunkLength = chunkLength - |
|
527 (metadata.sPLT_paletteName.length() + 1); |
|
528 if (remainingChunkLength <= 0) { |
|
529 throw new IIOException("sPLT chunk length is not proper"); |
|
530 } |
519 |
531 |
520 int sampleDepth = stream.readUnsignedByte(); |
532 int sampleDepth = stream.readUnsignedByte(); |
521 metadata.sPLT_sampleDepth = sampleDepth; |
533 metadata.sPLT_sampleDepth = sampleDepth; |
522 |
534 |
523 int numEntries = chunkLength/(4*(sampleDepth/8) + 2); |
535 int numEntries = remainingChunkLength/(4*(sampleDepth/8) + 2); |
524 metadata.sPLT_red = new int[numEntries]; |
536 metadata.sPLT_red = new int[numEntries]; |
525 metadata.sPLT_green = new int[numEntries]; |
537 metadata.sPLT_green = new int[numEntries]; |
526 metadata.sPLT_blue = new int[numEntries]; |
538 metadata.sPLT_blue = new int[numEntries]; |
527 metadata.sPLT_alpha = new int[numEntries]; |
539 metadata.sPLT_alpha = new int[numEntries]; |
528 metadata.sPLT_frequency = new int[numEntries]; |
540 metadata.sPLT_frequency = new int[numEntries]; |
556 metadata.sRGB_present = true; |
568 metadata.sRGB_present = true; |
557 } |
569 } |
558 |
570 |
559 private void parse_tEXt_chunk(int chunkLength) throws IOException { |
571 private void parse_tEXt_chunk(int chunkLength) throws IOException { |
560 String keyword = readNullTerminatedString("ISO-8859-1", 80); |
572 String keyword = readNullTerminatedString("ISO-8859-1", 80); |
|
573 int textLength = chunkLength - keyword.length() - 1; |
|
574 if (textLength <= 0) { |
|
575 throw new IIOException("tEXt chunk length is not proper"); |
|
576 } |
561 metadata.tEXt_keyword.add(keyword); |
577 metadata.tEXt_keyword.add(keyword); |
562 |
578 |
563 byte[] b = new byte[chunkLength - keyword.length() - 1]; |
579 byte[] b = new byte[textLength]; |
564 stream.readFully(b); |
580 stream.readFully(b); |
565 metadata.tEXt_text.add(new String(b, "ISO-8859-1")); |
581 metadata.tEXt_text.add(new String(b, "ISO-8859-1")); |
566 |
582 |
567 // Check if the text chunk contains image creation time |
583 // Check if the text chunk contains image creation time |
568 if (keyword.equals(PNGMetadata.tEXt_creationTimeKey)) { |
584 if (keyword.equals(PNGMetadata.tEXt_creationTimeKey)) { |
594 } |
610 } |
595 |
611 |
596 // Alpha table may have fewer entries than RGB palette |
612 // Alpha table may have fewer entries than RGB palette |
597 int maxEntries = metadata.PLTE_red.length; |
613 int maxEntries = metadata.PLTE_red.length; |
598 int numEntries = chunkLength; |
614 int numEntries = chunkLength; |
599 if (numEntries > maxEntries) { |
615 if (numEntries > maxEntries && maxEntries > 0) { |
600 processWarningOccurred( |
616 processWarningOccurred( |
601 "tRNS chunk has more entries than prior PLTE chunk, ignoring extras."); |
617 "tRNS chunk has more entries than prior PLTE chunk, ignoring extras."); |
602 numEntries = maxEntries; |
618 numEntries = maxEntries; |
603 } |
619 } |
604 metadata.tRNS_alpha = new byte[numEntries]; |
620 metadata.tRNS_alpha = new byte[numEntries]; |
650 return baos.toByteArray(); |
666 return baos.toByteArray(); |
651 } |
667 } |
652 |
668 |
653 private void parse_zTXt_chunk(int chunkLength) throws IOException { |
669 private void parse_zTXt_chunk(int chunkLength) throws IOException { |
654 String keyword = readNullTerminatedString("ISO-8859-1", 80); |
670 String keyword = readNullTerminatedString("ISO-8859-1", 80); |
|
671 int textLength = chunkLength - keyword.length() - 2; |
|
672 if (textLength <= 0) { |
|
673 throw new IIOException("zTXt chunk length is not proper"); |
|
674 } |
655 metadata.zTXt_keyword.add(keyword); |
675 metadata.zTXt_keyword.add(keyword); |
656 |
676 |
657 int method = stream.readUnsignedByte(); |
677 int method = stream.readUnsignedByte(); |
658 metadata.zTXt_compressionMethod.add(method); |
678 metadata.zTXt_compressionMethod.add(method); |
659 |
679 |
660 byte[] b = new byte[chunkLength - keyword.length() - 2]; |
680 byte[] b = new byte[textLength]; |
661 stream.readFully(b); |
681 stream.readFully(b); |
662 metadata.zTXt_text.add(new String(inflate(b), "ISO-8859-1")); |
682 metadata.zTXt_text.add(new String(inflate(b), "ISO-8859-1")); |
663 |
683 |
664 // Check if the text chunk contains image creation time |
684 // Check if the text chunk contains image creation time |
665 if (keyword.equals(PNGMetadata.tEXt_creationTimeKey)) { |
685 if (keyword.equals(PNGMetadata.tEXt_creationTimeKey)) { |