public static Path2D readPath ( final ImageInputStream stream ) throws IOException { notNull ( stream , "stream" ) ; int magic = readMagic ( stream ) ; if ( magic == PSD . RESOURCE_TYPE ) { // This is a PSD Image Resource Block, we can parse directly return buildPathFromPhotoshopResources ( stream ) ; } else if ( magic == PSD . SIGNATURE_8BPS ) { // PSD version // 4 byte magic, 2 byte version, 6 bytes reserved, 2 byte channels, // 4 byte height, 4 byte width, 2 byte bit depth, 2 byte mode stream . skipBytes ( 26 ) ; // 4 byte color mode data length + n byte color mode data long colorModeLen = stream . readUnsignedInt ( ) ; stream . skipBytes ( colorModeLen ) ; // 4 byte image resources length long imageResourcesLen = stream . readUnsignedInt ( ) ; // Image resources return buildPathFromPhotoshopResources ( new SubImageInputStream ( stream , imageResourcesLen ) ) ; } else if ( magic >>> 16 == JPEG . SOI && ( magic & 0xff00 ) == 0xff00 ) { // JPEG version Map < Integer , java . util . List < String > > segmentIdentifiers = new LinkedHashMap <> ( ) ; segmentIdentifiers . put ( JPEG . APP13 , singletonList ( "Photoshop 3.0" ) ) ; List < JPEGSegment > photoshop = JPEGSegmentUtil . readSegments ( stream , segmentIdentifiers ) ; if ( ! photoshop . isEmpty ( ) ) { return buildPathFromPhotoshopResources ( new MemoryCacheImageInputStream ( photoshop . get ( 0 ) . data ( ) ) ) ; } } else if ( magic >>> 16 == TIFF . BYTE_ORDER_MARK_BIG_ENDIAN && ( magic & 0xffff ) == TIFF . TIFF_MAGIC || magic >>> 16 == TIFF . BYTE_ORDER_MARK_LITTLE_ENDIAN && ( magic & 0xffff ) == TIFF . TIFF_MAGIC << 8 ) { // TIFF version CompoundDirectory IFDs = ( CompoundDirectory ) new TIFFReader ( ) . read ( stream ) ; Directory directory = IFDs . getDirectory ( 0 ) ; Entry photoshop = directory . getEntryById ( TIFF . TAG_PHOTOSHOP ) ; if ( photoshop != null ) { return buildPathFromPhotoshopResources ( new ByteArrayImageInputStream ( ( byte [ ] ) photoshop . getValue ( ) ) ) ; } } // Unknown file format, or no path found return null ; }
Reads the clipping path from the given input stream if any . Supports PSD JPEG and TIFF as container formats for Photoshop resources or a bare PSD Image Resource Block .