732 parse_tRNS_chunk(chunkLength);
733 // After parsing tRNS chunk we will skip 4 CRC bytes
734 stream.skipBytes(4);
735 } else {
736 // Skip the chunk plus the 4 CRC bytes that follow
737 stream.skipBytes(chunkLength + 4);
738 }
739 }
740 } catch (IOException e) {
741 throw new IIOException("Error skipping PNG metadata", e);
742 }
743
744 gotMetadata = true;
745 return;
746 }
747
748 try {
749 loop: while (true) {
750 int chunkLength = stream.readInt();
751 int chunkType = stream.readInt();
752 int chunkCRC;
753
754 // verify the chunk length
755 if (chunkLength < 0) {
756 throw new IIOException("Invalid chunk length " + chunkLength);
757 };
758
759 try {
760 stream.mark();
761 stream.seek(stream.getStreamPosition() + chunkLength);
762 chunkCRC = stream.readInt();
763 stream.reset();
764 } catch (IOException e) {
765 throw new IIOException("Invalid chunk length " + chunkLength);
766 }
767
768 switch (chunkType) {
769 case IDAT_TYPE:
770 // If chunk type is 'IDAT', we've reached the image data.
771 if (imageStartPosition == -1L) {
772 /*
773 * The PNG specification mandates that if colorType is
774 * PNG_COLOR_PALETTE then the PLTE chunk should appear
775 * before the first IDAT chunk.
776 */
777 if (colorType == PNG_COLOR_PALETTE &&
778 !(metadata.PLTE_present))
779 {
780 throw new IIOException("Required PLTE chunk"
781 + " missing");
782 }
783 /*
|
732 parse_tRNS_chunk(chunkLength);
733 // After parsing tRNS chunk we will skip 4 CRC bytes
734 stream.skipBytes(4);
735 } else {
736 // Skip the chunk plus the 4 CRC bytes that follow
737 stream.skipBytes(chunkLength + 4);
738 }
739 }
740 } catch (IOException e) {
741 throw new IIOException("Error skipping PNG metadata", e);
742 }
743
744 gotMetadata = true;
745 return;
746 }
747
748 try {
749 loop: while (true) {
750 int chunkLength = stream.readInt();
751 int chunkType = stream.readInt();
752 // Initialize chunkCRC, value assigned has no significance
753 int chunkCRC = -1;
754
755 // verify the chunk length
756 if (chunkLength < 0) {
757 throw new IIOException("Invalid chunk length " + chunkLength);
758 };
759
760 try {
761 /*
762 * As per PNG specification all chunks should have
763 * 4 byte CRC. But there are some images where
764 * CRC is not present/corrupt for IEND chunk.
765 * And these type of images are supported by other
766 * decoders. So as soon as we hit chunk type
767 * for IEND chunk stop reading metadata.
768 */
769 if (chunkType != IEND_TYPE) {
770 stream.mark();
771 stream.seek(stream.getStreamPosition() + chunkLength);
772 chunkCRC = stream.readInt();
773 stream.reset();
774 }
775 } catch (IOException e) {
776 throw new IIOException("Invalid chunk length " + chunkLength);
777 }
778
779 switch (chunkType) {
780 case IDAT_TYPE:
781 // If chunk type is 'IDAT', we've reached the image data.
782 if (imageStartPosition == -1L) {
783 /*
784 * The PNG specification mandates that if colorType is
785 * PNG_COLOR_PALETTE then the PLTE chunk should appear
786 * before the first IDAT chunk.
787 */
788 if (colorType == PNG_COLOR_PALETTE &&
789 !(metadata.PLTE_present))
790 {
791 throw new IIOException("Required PLTE chunk"
792 + " missing");
793 }
794 /*
|