src/share/classes/com/sun/imageio/plugins/png/PNGImageReader.java

Print this page




 647     }
 648 
 649     private void readMetadata() throws IIOException {
 650         if (gotMetadata) {
 651             return;
 652         }
 653 
 654         readHeader();
 655 
 656         /*
 657          * Optimization: We can skip the remaining metadata if the
 658          * ignoreMetadata flag is set, and only if this is not a palette
 659          * image (in that case, we need to read the metadata to get the
 660          * tRNS chunk, which is needed for the getImageTypes() method).
 661          */
 662         int colorType = metadata.IHDR_colorType;
 663         if (ignoreMetadata && colorType != PNG_COLOR_PALETTE) {
 664             try {
 665                 while (true) {
 666                     int chunkLength = stream.readInt();






 667                     int chunkType = stream.readInt();
 668 
 669                     if (chunkType == IDAT_TYPE) {
 670                         // We've reached the image data
 671                         stream.skipBytes(-8);
 672                         imageStartPosition = stream.getStreamPosition();
 673                         break;
 674                     } else {
 675                         // Skip the chunk plus the 4 CRC bytes that follow
 676                         stream.skipBytes(chunkLength + 4);
 677                     }
 678                 }
 679             } catch (IOException e) {
 680                 throw new IIOException("Error skipping PNG metadata", e);
 681             }
 682 
 683             gotMetadata = true;
 684             return;
 685         }
 686 
 687         try {
 688             loop: while (true) {
 689                 int chunkLength = stream.readInt();
 690                 int chunkType = stream.readInt();
 691                 int chunkCRC;
 692 
 693                 // verify the chunk length
 694                 if (chunkLength < 0) {
 695                     throw new IIOException("Invalid chunk lenght " + chunkLength);
 696                 };
 697 
 698                 try {
 699                     stream.mark();
 700                     stream.seek(stream.getStreamPosition() + chunkLength);
 701                     chunkCRC = stream.readInt();
 702                     stream.reset();
 703                 } catch (IOException e) {
 704                     throw new IIOException("Invalid chunk length " + chunkLength);
 705                 }
 706 
 707                 switch (chunkType) {
 708                 case IDAT_TYPE:
 709                     // If chunk type is 'IDAT', we've reached the image data.
 710                     stream.skipBytes(-8);
 711                     imageStartPosition = stream.getStreamPosition();
 712                     break loop;
 713                 case PLTE_TYPE:
 714                     parse_PLTE_chunk(chunkLength);
 715                     break;




 647     }
 648 
 649     private void readMetadata() throws IIOException {
 650         if (gotMetadata) {
 651             return;
 652         }
 653 
 654         readHeader();
 655 
 656         /*
 657          * Optimization: We can skip the remaining metadata if the
 658          * ignoreMetadata flag is set, and only if this is not a palette
 659          * image (in that case, we need to read the metadata to get the
 660          * tRNS chunk, which is needed for the getImageTypes() method).
 661          */
 662         int colorType = metadata.IHDR_colorType;
 663         if (ignoreMetadata && colorType != PNG_COLOR_PALETTE) {
 664             try {
 665                 while (true) {
 666                     int chunkLength = stream.readInt();
 667 
 668                     // verify the chunk length first
 669                     if (chunkLength < 0 || chunkLength + 4 < 0) {
 670                         throw new IIOException("Invalid chunk length " + chunkLength);
 671                     }
 672 
 673                     int chunkType = stream.readInt();
 674 
 675                     if (chunkType == IDAT_TYPE) {
 676                         // We've reached the image data
 677                         stream.skipBytes(-8);
 678                         imageStartPosition = stream.getStreamPosition();
 679                         break;
 680                     } else {
 681                         // Skip the chunk plus the 4 CRC bytes that follow
 682                         stream.skipBytes(chunkLength + 4);
 683                     }
 684                 }
 685             } catch (IOException e) {
 686                 throw new IIOException("Error skipping PNG metadata", e);
 687             }
 688 
 689             gotMetadata = true;
 690             return;
 691         }
 692 
 693         try {
 694             loop: while (true) {
 695                 int chunkLength = stream.readInt();
 696                 int chunkType = stream.readInt();
 697                 int chunkCRC;
 698 
 699                 // verify the chunk length
 700                 if (chunkLength < 0) {
 701                     throw new IIOException("Invalid chunk length " + chunkLength);
 702                 };
 703 
 704                 try {
 705                     stream.mark();
 706                     stream.seek(stream.getStreamPosition() + chunkLength);
 707                     chunkCRC = stream.readInt();
 708                     stream.reset();
 709                 } catch (IOException e) {
 710                     throw new IIOException("Invalid chunk length " + chunkLength);
 711                 }
 712 
 713                 switch (chunkType) {
 714                 case IDAT_TYPE:
 715                     // If chunk type is 'IDAT', we've reached the image data.
 716                     stream.skipBytes(-8);
 717                     imageStartPosition = stream.getStreamPosition();
 718                     break loop;
 719                 case PLTE_TYPE:
 720                     parse_PLTE_chunk(chunkLength);
 721                     break;