<s> __version__ = "<STR_LIT>" <EOL> import Image , ImageFile , ImagePalette <EOL> MODES = { <EOL> ( <NUM_LIT:0> , <NUM_LIT:1> ) : ( "<STR_LIT:1>" , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:8> ) : ( "<STR_LIT:L>" , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:8> ) : ( "<STR_LIT:L>" , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:8> ) : ( "<STR_LIT:P>" , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:3> , <NUM_LIT:8> ) : ( "<STR_LIT>" , <NUM_LIT:3> ) , <EOL> ( <NUM_LIT:4> , <NUM_LIT:8> ) : ( "<STR_LIT>" , <NUM_LIT:4> ) , <EOL> ( <NUM_LIT:7> , <NUM_LIT:8> ) : ( "<STR_LIT:L>" , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:8> , <NUM_LIT:8> ) : ( "<STR_LIT:L>" , <NUM_LIT:1> ) , <EOL> ( <NUM_LIT:9> , <NUM_LIT:8> ) : ( "<STR_LIT>" , <NUM_LIT:3> ) <EOL> } <EOL> def i16 ( c ) : <EOL> return ord ( c [ <NUM_LIT:1> ] ) + ( ord ( c [ <NUM_LIT:0> ] ) << <NUM_LIT:8> ) <EOL> def i32 ( c ) : <EOL> return ord ( c [ <NUM_LIT:3> ] ) + ( ord ( c [ <NUM_LIT:2> ] ) << <NUM_LIT:8> ) + ( ord ( c [ <NUM_LIT:1> ] ) << <NUM_LIT:16> ) + ( ord ( c [ <NUM_LIT:0> ] ) << <NUM_LIT> ) <EOL> def _accept ( prefix ) : <EOL> return prefix [ : <NUM_LIT:4> ] == "<STR_LIT>" <EOL> class PsdImageFile ( ImageFile . ImageFile ) : <EOL> format = "<STR_LIT>" <EOL> format_description = "<STR_LIT>" <EOL> def _open ( self ) : <EOL> read = self . fp . read <EOL> s = read ( <NUM_LIT> ) <EOL> if s [ : <NUM_LIT:4> ] != "<STR_LIT>" or i16 ( s [ <NUM_LIT:4> : ] ) != <NUM_LIT:1> : <EOL> raise SyntaxError , "<STR_LIT>" <EOL> psd_bits = i16 ( s [ <NUM_LIT> : ] ) <EOL> psd_channels = i16 ( s [ <NUM_LIT:12> : ] ) <EOL> psd_mode = i16 ( s [ <NUM_LIT> : ] ) <EOL> mode , channels = MODES [ ( psd_mode , psd_bits ) ] <EOL> if channels > psd_channels : <EOL> raise IOError , "<STR_LIT>" <EOL> self . mode = mode <EOL> self . size = i32 ( s [ <NUM_LIT> : ] ) , i32 ( s [ <NUM_LIT> : ] ) <EOL> size = i32 ( read ( <NUM_LIT:4> ) ) <EOL> if size : <EOL> data = read ( size ) <EOL> if mode == "<STR_LIT:P>" and size == <NUM_LIT> : <EOL> self . palette = ImagePalette . raw ( "<STR_LIT>" , data ) <EOL> self . resources = [ ] <EOL> size = i32 ( read ( <NUM_LIT:4> ) ) <EOL> if size : <EOL> end = self . fp . tell ( ) + size <EOL> while self . fp . tell ( ) < end : <EOL> signature = read ( <NUM_LIT:4> ) <EOL> id = i16 ( read ( <NUM_LIT:2> ) ) <EOL> name = read ( ord ( read ( <NUM_LIT:1> ) ) ) <EOL> if not ( len ( name ) & <NUM_LIT:1> ) : <EOL> read ( <NUM_LIT:1> ) <EOL> data = read ( i32 ( read ( <NUM_LIT:4> ) ) ) <EOL> if ( len ( data ) & <NUM_LIT:1> ) : <EOL> read ( <NUM_LIT:1> ) <EOL> self . resources . append ( ( id , name , data ) ) <EOL> if id == <NUM_LIT> : <EOL> self . info [ "<STR_LIT>" ] = data <EOL> self . layers = [ ] <EOL> size = i32 ( read ( <NUM_LIT:4> ) ) <EOL> if size : <EOL> end = self . fp . tell ( ) + size <EOL> size = i32 ( read ( <NUM_LIT:4> ) ) <EOL> if size : <EOL> self . layers = _layerinfo ( self . fp ) <EOL> self . fp . seek ( end ) <EOL> self . tile = _maketile ( self . fp , mode , ( <NUM_LIT:0> , <NUM_LIT:0> ) + self . size , channels ) <EOL> self . _fp = self . fp <EOL> self . frame = <NUM_LIT:0> <EOL> def seek ( self , layer ) : <EOL> if layer == self . frame : <EOL> return <EOL> try : <EOL> if layer <= <NUM_LIT:0> : <EOL> raise IndexError <EOL> name , mode , bbox , tile = self . layers [ layer - <NUM_LIT:1> ] <EOL> self . mode = mode <EOL> self . tile = tile <EOL> self . frame = layer <EOL> self . fp = self . _fp <EOL> return name , bbox <EOL> except IndexError : <EOL> raise EOFError , "<STR_LIT>" <EOL> def tell ( self ) : <EOL> return self . frame <EOL> def load_prepare ( self ) : <EOL> if not self . im or self . im . mode != self . mode or self . im . size != self . size : <EOL> self . im = Image . core . fill ( self . mode , self . size , <NUM_LIT:0> ) <EOL> if self . mode == "<STR_LIT:P>" : <EOL> Image . Image . load ( self ) <EOL> def _layerinfo ( file ) : <EOL> layers = [ ] <EOL> read = file . read <EOL> for i in range ( abs ( i16 ( read ( <NUM_LIT:2> ) ) ) ) : <EOL> y0 = i32 ( read ( <NUM_LIT:4> ) ) ; x0 = i32 ( read ( <NUM_LIT:4> ) ) <EOL> y1 = i32 ( read ( <NUM_LIT:4> ) ) ; x1 = i32 ( read ( <NUM_LIT:4> ) ) <EOL> info = [ ] <EOL> mode = [ ] <EOL> for i in range ( i16 ( read ( <NUM_LIT:2> ) ) ) : <EOL> type = i16 ( read ( <NUM_LIT:2> ) ) <EOL> if type == <NUM_LIT> : <EOL> m = "<STR_LIT:A>" <EOL> else : <EOL> m = "<STR_LIT>" [ type ] <EOL> mode . append ( m ) <EOL> size = i32 ( read ( <NUM_LIT:4> ) ) <EOL> info . append ( ( m , size ) ) <EOL> mode . sort ( ) <EOL> if mode == [ "<STR_LIT:R>" ] : <EOL> mode = "<STR_LIT:L>" <EOL> elif mode == [ "<STR_LIT:B>" , "<STR_LIT>" , "<STR_LIT:R>" ] : <EOL> mode = "<STR_LIT>" <EOL> elif mode == [ "<STR_LIT:A>" , "<STR_LIT:B>" , "<STR_LIT>" , "<STR_LIT:R>" ] : <EOL> mode = "<STR_LIT>" <EOL> else : <EOL> mode = None <EOL> filler = read ( <NUM_LIT:12> ) <EOL> name = "<STR_LIT>" <EOL> size = i32 ( read ( <NUM_LIT:4> ) ) <EOL> combined = <NUM_LIT:0> <EOL> if size : <EOL> length = i32 ( read ( <NUM_LIT:4> ) ) <EOL> if length : <EOL> mask_y = i32 ( read ( <NUM_LIT:4> ) ) ; mask_x = i32 ( read ( <NUM_LIT:4> ) ) <EOL> mask_h = i32 ( read ( <NUM_LIT:4> ) ) - mask_y ; mask_w = i32 ( read ( <NUM_LIT:4> ) ) - mask_x <EOL> file . seek ( length - <NUM_LIT:16> , <NUM_LIT:1> ) <EOL> combined += length + <NUM_LIT:4> <EOL> length = i32 ( read ( <NUM_LIT:4> ) ) <EOL> if length : <EOL> file . seek ( length , <NUM_LIT:1> ) <EOL> combined += length + <NUM_LIT:4> <EOL> length = ord ( read ( <NUM_LIT:1> ) ) <EOL> if length : <EOL> name = read ( length ) <EOL> combined += length + <NUM_LIT:1> <EOL> file . seek ( size - combined , <NUM_LIT:1> ) <EOL> layers . append ( ( name , mode , ( x0 , y0 , x1 , y1 ) ) ) <EOL> i = <NUM_LIT:0> <EOL> for name , mode , bbox in layers : <EOL> tile = [ ] <EOL> for m in mode : <EOL> t = _maketile ( file , m , bbox , <NUM_LIT:1> ) <EOL> if t : <EOL> tile . extend ( t ) <EOL> layers [ i ] = name , mode , bbox , tile <EOL> i = i + <NUM_LIT:1> <EOL> return layers <EOL> def _maketile ( file , mode , bbox , channels ) : <EOL> tile = None <EOL> read = file . read <EOL> compression = i16 ( read ( <NUM_LIT:2> ) ) <EOL> xsize = bbox [ <NUM_LIT:2> ] - bbox [ <NUM_LIT:0> ] <EOL> ysize = bbox [ <NUM_LIT:3> ] - bbox [ <NUM_LIT:1> ] <EOL> offset = file . tell ( ) <EOL> if compression == <NUM_LIT:0> : <EOL> tile = [ ] <EOL> for channel in range ( channels ) : <EOL> layer = mode [ channel ] <EOL> if mode == "<STR_LIT>" : <EOL> layer = layer + "<STR_LIT>" <EOL> tile . append ( ( "<STR_LIT>" , bbox , offset , layer ) ) <EOL> offset = offset + xsize * ysize <EOL> elif compression == <NUM_LIT:1> : <EOL> i = <NUM_LIT:0> <EOL> tile = [ ] <EOL> bytecount = read ( channels * ysize * <NUM_LIT:2> ) <EOL> offset = file . tell ( ) <EOL> for channel in range ( channels ) : <EOL> layer = mode [ channel ] <EOL> if mode == "<STR_LIT>" : <EOL> layer = layer + "<STR_LIT>" <EOL> tile . append ( <EOL> ( "<STR_LIT>" , bbox , offset , layer ) <EOL> ) <EOL> for y in range ( ysize ) : <EOL> offset = offset + i16 ( bytecount [ i : i + <NUM_LIT:2> ] ) <EOL> i = i + <NUM_LIT:2> <EOL> file . seek ( offset ) <EOL> if offset & <NUM_LIT:1> : <EOL> read ( <NUM_LIT:1> ) <EOL> return tile <EOL> Image . register_open ( "<STR_LIT>" , PsdImageFile , _accept ) <EOL> Image . register_extension ( "<STR_LIT>" , "<STR_LIT>" ) </s>
