<s> from __future__ import print_function <EOL> """<STR_LIT>""" <EOL> __version__ = "<STR_LIT>" <EOL> import itertools <EOL> import math <EOL> import operator <EOL> import struct <EOL> import sys <EOL> import warnings <EOL> import zlib <EOL> from array import array <EOL> from functools import reduce <EOL> try : <EOL> import cpngfilters as pngfilters <EOL> except ImportError : <EOL> pass <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _signature = struct . pack ( '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> , <NUM_LIT:10> ) <EOL> _adam7 = ( ( <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:8> , <NUM_LIT:8> ) , <EOL> ( <NUM_LIT:4> , <NUM_LIT:0> , <NUM_LIT:8> , <NUM_LIT:8> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:4> , <NUM_LIT:8> ) , <EOL> ( <NUM_LIT:2> , <NUM_LIT:0> , <NUM_LIT:4> , <NUM_LIT:4> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:4> ) , <EOL> ( <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:2> ) , <EOL> ( <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:2> ) ) <EOL> def group ( s , n ) : <EOL> return list ( zip ( * [ iter ( s ) ] * n ) ) <EOL> def isarray ( x ) : <EOL> return isinstance ( x , array ) <EOL> def tostring ( row ) : <EOL> return row . tostring ( ) <EOL> def interleave_planes ( ipixels , apixels , ipsize , apsize ) : <EOL> """<STR_LIT>""" <EOL> itotal = len ( ipixels ) <EOL> atotal = len ( apixels ) <EOL> newtotal = itotal + atotal <EOL> newpsize = ipsize + apsize <EOL> out = array ( ipixels . typecode ) <EOL> out . extend ( ipixels ) <EOL> out . extend ( apixels ) <EOL> for i in range ( ipsize ) : <EOL> out [ i : newtotal : newpsize ] = ipixels [ i : itotal : ipsize ] <EOL> for i in range ( apsize ) : <EOL> out [ i + ipsize : newtotal : newpsize ] = apixels [ i : atotal : apsize ] <EOL> return out <EOL> def check_palette ( palette ) : <EOL> """<STR_LIT>""" <EOL> if palette is None : <EOL> return None <EOL> p = list ( palette ) <EOL> if not ( <NUM_LIT:0> < len ( p ) <= <NUM_LIT> ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> seen_triple = False <EOL> for i , t in enumerate ( p ) : <EOL> if len ( t ) not in ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % i ) <EOL> if len ( t ) == <NUM_LIT:3> : <EOL> seen_triple = True <EOL> if seen_triple and len ( t ) == <NUM_LIT:4> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % i ) <EOL> for x in t : <EOL> if int ( x ) != x or not ( <NUM_LIT:0> <= x <= <NUM_LIT:255> ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % i ) <EOL> return p <EOL> def check_sizes ( size , width , height ) : <EOL> """<STR_LIT>""" <EOL> if not size : <EOL> return width , height <EOL> if len ( size ) != <NUM_LIT:2> : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> if width is not None and width != size [ <NUM_LIT:0> ] : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> % ( size [ <NUM_LIT:0> ] , width ) ) <EOL> if height is not None and height != size [ <NUM_LIT:1> ] : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> % ( size [ <NUM_LIT:1> ] , height ) ) <EOL> return size <EOL> def check_color ( c , greyscale , which ) : <EOL> """<STR_LIT>""" <EOL> if c is None : <EOL> return c <EOL> if greyscale : <EOL> try : <EOL> len ( c ) <EOL> except TypeError : <EOL> c = ( c , ) <EOL> if len ( c ) != <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> which ) <EOL> if not isinteger ( c [ <NUM_LIT:0> ] ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % which ) <EOL> else : <EOL> if not ( len ( c ) == <NUM_LIT:3> and <EOL> isinteger ( c [ <NUM_LIT:0> ] ) and <EOL> isinteger ( c [ <NUM_LIT:1> ] ) and <EOL> isinteger ( c [ <NUM_LIT:2> ] ) ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % which ) <EOL> return c <EOL> class Error ( Exception ) : <EOL> def __str__ ( self ) : <EOL> return self . __class__ . __name__ + '<STR_LIT>' + '<STR_LIT:U+0020>' . join ( self . args ) <EOL> class FormatError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class ChunkError ( FormatError ) : <EOL> pass <EOL> class Writer : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , width = None , height = None , <EOL> size = None , <EOL> greyscale = False , <EOL> alpha = False , <EOL> bitdepth = <NUM_LIT:8> , <EOL> palette = None , <EOL> transparent = None , <EOL> background = None , <EOL> gamma = None , <EOL> compression = None , <EOL> interlace = False , <EOL> bytes_per_sample = None , <EOL> planes = None , <EOL> colormap = None , <EOL> maxval = None , <EOL> chunk_limit = <NUM_LIT:2> ** <NUM_LIT:20> , <EOL> x_pixels_per_unit = None , <EOL> y_pixels_per_unit = None , <EOL> unit_is_meter = False ) : <EOL> """<STR_LIT>""" <EOL> width , height = check_sizes ( size , width , height ) <EOL> del size <EOL> if width <= <NUM_LIT:0> or height <= <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not isinteger ( width ) or not isinteger ( height ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if width > <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> or height > <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if alpha and transparent is not None : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> if bytes_per_sample is not None : <EOL> warnings . warn ( '<STR_LIT>' , <EOL> DeprecationWarning ) <EOL> if bytes_per_sample not in ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT:1> , <NUM_LIT:2> ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> bitdepth = int ( <NUM_LIT:8> * bytes_per_sample ) <EOL> del bytes_per_sample <EOL> if not isinteger ( bitdepth ) or bitdepth < <NUM_LIT:1> or <NUM_LIT:16> < bitdepth : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> bitdepth ) <EOL> self . rescale = None <EOL> palette = check_palette ( palette ) <EOL> if palette : <EOL> if bitdepth not in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:8> ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if transparent is not None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if alpha : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if greyscale : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> if alpha or not greyscale : <EOL> if bitdepth not in ( <NUM_LIT:8> , <NUM_LIT:16> ) : <EOL> targetbitdepth = ( <NUM_LIT:8> , <NUM_LIT:16> ) [ bitdepth > <NUM_LIT:8> ] <EOL> self . rescale = ( bitdepth , targetbitdepth ) <EOL> bitdepth = targetbitdepth <EOL> del targetbitdepth <EOL> else : <EOL> assert greyscale <EOL> assert not alpha <EOL> if bitdepth not in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:16> ) : <EOL> if bitdepth > <NUM_LIT:8> : <EOL> targetbitdepth = <NUM_LIT:16> <EOL> elif bitdepth == <NUM_LIT:3> : <EOL> targetbitdepth = <NUM_LIT:4> <EOL> else : <EOL> assert bitdepth in ( <NUM_LIT:5> , <NUM_LIT:6> , <NUM_LIT:7> ) <EOL> targetbitdepth = <NUM_LIT:8> <EOL> self . rescale = ( bitdepth , targetbitdepth ) <EOL> bitdepth = targetbitdepth <EOL> del targetbitdepth <EOL> if bitdepth < <NUM_LIT:8> and ( alpha or not greyscale and not palette ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> if bitdepth > <NUM_LIT:8> and palette : <EOL> raise ValueError ( <EOL> "<STR_LIT>" ) <EOL> transparent = check_color ( transparent , greyscale , '<STR_LIT>' ) <EOL> background = check_color ( background , greyscale , '<STR_LIT>' ) <EOL> self . width = width <EOL> self . height = height <EOL> self . transparent = transparent <EOL> self . background = background <EOL> self . gamma = gamma <EOL> self . greyscale = bool ( greyscale ) <EOL> self . alpha = bool ( alpha ) <EOL> self . colormap = bool ( palette ) <EOL> self . bitdepth = int ( bitdepth ) <EOL> self . compression = compression <EOL> self . chunk_limit = chunk_limit <EOL> self . interlace = bool ( interlace ) <EOL> self . palette = palette <EOL> self . x_pixels_per_unit = x_pixels_per_unit <EOL> self . y_pixels_per_unit = y_pixels_per_unit <EOL> self . unit_is_meter = bool ( unit_is_meter ) <EOL> self . color_type = <NUM_LIT:4> * self . alpha + <NUM_LIT:2> * ( not greyscale ) + <NUM_LIT:1> * self . colormap <EOL> assert self . color_type in ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:6> ) <EOL> self . color_planes = ( <NUM_LIT:3> , <NUM_LIT:1> ) [ self . greyscale or self . colormap ] <EOL> self . planes = self . color_planes + self . alpha <EOL> self . psize = ( self . bitdepth / <NUM_LIT:8> ) * self . planes <EOL> def make_palette ( self ) : <EOL> """<STR_LIT>""" <EOL> p = array ( '<STR_LIT:B>' ) <EOL> t = array ( '<STR_LIT:B>' ) <EOL> for x in self . palette : <EOL> p . extend ( x [ <NUM_LIT:0> : <NUM_LIT:3> ] ) <EOL> if len ( x ) > <NUM_LIT:3> : <EOL> t . append ( x [ <NUM_LIT:3> ] ) <EOL> p = tostring ( p ) <EOL> t = tostring ( t ) <EOL> if t : <EOL> return p , t <EOL> return p , None <EOL> def write ( self , outfile , rows ) : <EOL> """<STR_LIT>""" <EOL> if self . interlace : <EOL> fmt = '<STR_LIT>' [ self . bitdepth > <NUM_LIT:8> ] <EOL> a = array ( fmt , itertools . chain ( * rows ) ) <EOL> return self . write_array ( outfile , a ) <EOL> nrows = self . write_passes ( outfile , rows ) <EOL> if nrows != self . height : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % <EOL> ( nrows , self . height ) ) <EOL> def write_passes ( self , outfile , rows , packed = False ) : <EOL> """<STR_LIT>""" <EOL> outfile . write ( _signature ) <EOL> write_chunk ( outfile , b'<STR_LIT>' , <EOL> struct . pack ( "<STR_LIT>" , self . width , self . height , <EOL> self . bitdepth , self . color_type , <EOL> <NUM_LIT:0> , <NUM_LIT:0> , self . interlace ) ) <EOL> if self . gamma is not None : <EOL> write_chunk ( outfile , b'<STR_LIT>' , <EOL> struct . pack ( "<STR_LIT>" , int ( round ( self . gamma * <NUM_LIT> ) ) ) ) <EOL> if self . rescale : <EOL> write_chunk ( outfile , b'<STR_LIT>' , <EOL> struct . pack ( '<STR_LIT>' % self . planes , <EOL> * [ self . rescale [ <NUM_LIT:0> ] ] * self . planes ) ) <EOL> if self . palette : <EOL> p , t = self . make_palette ( ) <EOL> write_chunk ( outfile , b'<STR_LIT>' , p ) <EOL> if t : <EOL> write_chunk ( outfile , b'<STR_LIT>' , t ) <EOL> if self . transparent is not None : <EOL> if self . greyscale : <EOL> write_chunk ( outfile , b'<STR_LIT>' , <EOL> struct . pack ( "<STR_LIT>" , * self . transparent ) ) <EOL> else : <EOL> write_chunk ( outfile , b'<STR_LIT>' , <EOL> struct . pack ( "<STR_LIT>" , * self . transparent ) ) <EOL> if self . background is not None : <EOL> if self . greyscale : <EOL> write_chunk ( outfile , b'<STR_LIT>' , <EOL> struct . pack ( "<STR_LIT>" , * self . background ) ) <EOL> else : <EOL> write_chunk ( outfile , b'<STR_LIT>' , <EOL> struct . pack ( "<STR_LIT>" , * self . background ) ) <EOL> if self . x_pixels_per_unit is not None and self . y_pixels_per_unit is not None : <EOL> tup = ( self . x_pixels_per_unit , self . y_pixels_per_unit , int ( self . unit_is_meter ) ) <EOL> write_chunk ( outfile , b'<STR_LIT>' , struct . pack ( "<STR_LIT>" , * tup ) ) <EOL> if self . compression is not None : <EOL> compressor = zlib . compressobj ( self . compression ) <EOL> else : <EOL> compressor = zlib . compressobj ( ) <EOL> data = array ( '<STR_LIT:B>' ) <EOL> if self . bitdepth == <NUM_LIT:8> or packed : <EOL> extend = data . extend <EOL> elif self . bitdepth == <NUM_LIT:16> : <EOL> def extend ( sl ) : <EOL> fmt = '<STR_LIT>' % len ( sl ) <EOL> data . extend ( array ( '<STR_LIT:B>' , struct . pack ( fmt , * sl ) ) ) <EOL> else : <EOL> assert self . bitdepth < <NUM_LIT:8> <EOL> spb = int ( <NUM_LIT:8> / self . bitdepth ) <EOL> def extend ( sl ) : <EOL> a = array ( '<STR_LIT:B>' , sl ) <EOL> l = float ( len ( a ) ) <EOL> extra = math . ceil ( l / float ( spb ) ) * spb - l <EOL> a . extend ( [ <NUM_LIT:0> ] * int ( extra ) ) <EOL> l = group ( a , spb ) <EOL> l = [ reduce ( lambda x , y : <EOL> ( x << self . bitdepth ) + y , e ) for e in l ] <EOL> data . extend ( l ) <EOL> if self . rescale : <EOL> oldextend = extend <EOL> factor = float ( <NUM_LIT:2> ** self . rescale [ <NUM_LIT:1> ] - <NUM_LIT:1> ) / float ( <NUM_LIT:2> ** self . rescale [ <NUM_LIT:0> ] - <NUM_LIT:1> ) <EOL> def extend ( sl ) : <EOL> oldextend ( [ int ( round ( factor * x ) ) for x in sl ] ) <EOL> enumrows = enumerate ( rows ) <EOL> del rows <EOL> data . append ( <NUM_LIT:0> ) <EOL> i , row = next ( enumrows ) <EOL> try : <EOL> extend ( row ) <EOL> except : <EOL> def wrapmapint ( f ) : <EOL> return lambda sl : f ( [ int ( x ) for x in sl ] ) <EOL> extend = wrapmapint ( extend ) <EOL> del wrapmapint <EOL> extend ( row ) <EOL> for i , row in enumrows : <EOL> data . append ( <NUM_LIT:0> ) <EOL> extend ( row ) <EOL> if len ( data ) > self . chunk_limit : <EOL> compressed = compressor . compress ( tostring ( data ) ) <EOL> if len ( compressed ) : <EOL> write_chunk ( outfile , b'<STR_LIT>' , compressed ) <EOL> del data [ : ] <EOL> if len ( data ) : <EOL> compressed = compressor . compress ( tostring ( data ) ) <EOL> else : <EOL> compressed = b'<STR_LIT>' <EOL> flushed = compressor . flush ( ) <EOL> if len ( compressed ) or len ( flushed ) : <EOL> write_chunk ( outfile , b'<STR_LIT>' , compressed + flushed ) <EOL> write_chunk ( outfile , b'<STR_LIT>' ) <EOL> return i + <NUM_LIT:1> <EOL> def write_array ( self , outfile , pixels ) : <EOL> """<STR_LIT>""" <EOL> if self . interlace : <EOL> self . write_passes ( outfile , self . array_scanlines_interlace ( pixels ) ) <EOL> else : <EOL> self . write_passes ( outfile , self . array_scanlines ( pixels ) ) <EOL> def write_packed ( self , outfile , rows ) : <EOL> """<STR_LIT>""" <EOL> if self . rescale : <EOL> raise Error ( "<STR_LIT>" % <EOL> self . rescale [ <NUM_LIT:0> ] ) <EOL> return self . write_passes ( outfile , rows , packed = True ) <EOL> def convert_pnm ( self , infile , outfile ) : <EOL> """<STR_LIT>""" <EOL> if self . interlace : <EOL> pixels = array ( '<STR_LIT:B>' ) <EOL> pixels . fromfile ( infile , <EOL> ( self . bitdepth / <NUM_LIT:8> ) * self . color_planes * <EOL> self . width * self . height ) <EOL> self . write_passes ( outfile , self . array_scanlines_interlace ( pixels ) ) <EOL> else : <EOL> self . write_passes ( outfile , self . file_scanlines ( infile ) ) <EOL> def convert_ppm_and_pgm ( self , ppmfile , pgmfile , outfile ) : <EOL> """<STR_LIT>""" <EOL> pixels = array ( '<STR_LIT:B>' ) <EOL> pixels . fromfile ( ppmfile , <EOL> ( self . bitdepth / <NUM_LIT:8> ) * self . color_planes * <EOL> self . width * self . height ) <EOL> apixels = array ( '<STR_LIT:B>' ) <EOL> apixels . fromfile ( pgmfile , <EOL> ( self . bitdepth / <NUM_LIT:8> ) * <EOL> self . width * self . height ) <EOL> pixels = interleave_planes ( pixels , apixels , <EOL> ( self . bitdepth / <NUM_LIT:8> ) * self . color_planes , <EOL> ( self . bitdepth / <NUM_LIT:8> ) ) <EOL> if self . interlace : <EOL> self . write_passes ( outfile , self . array_scanlines_interlace ( pixels ) ) <EOL> else : <EOL> self . write_passes ( outfile , self . array_scanlines ( pixels ) ) <EOL> def file_scanlines ( self , infile ) : <EOL> """<STR_LIT>""" <EOL> vpr = self . width * self . planes <EOL> row_bytes = vpr <EOL> if self . bitdepth > <NUM_LIT:8> : <EOL> assert self . bitdepth == <NUM_LIT:16> <EOL> row_bytes *= <NUM_LIT:2> <EOL> fmt = '<STR_LIT>' % vpr <EOL> def line ( ) : <EOL> return array ( '<STR_LIT:H>' , struct . unpack ( fmt , infile . read ( row_bytes ) ) ) <EOL> else : <EOL> def line ( ) : <EOL> scanline = array ( '<STR_LIT:B>' , infile . read ( row_bytes ) ) <EOL> return scanline <EOL> for y in range ( self . height ) : <EOL> yield line ( ) <EOL> def array_scanlines ( self , pixels ) : <EOL> """<STR_LIT>""" <EOL> vpr = self . width * self . planes <EOL> stop = <NUM_LIT:0> <EOL> for y in range ( self . height ) : <EOL> start = stop <EOL> stop = start + vpr <EOL> yield pixels [ start : stop ] <EOL> def array_scanlines_interlace ( self , pixels ) : <EOL> """<STR_LIT>""" <EOL> fmt = '<STR_LIT>' [ self . bitdepth > <NUM_LIT:8> ] <EOL> vpr = self . width * self . planes <EOL> for xstart , ystart , xstep , ystep in _adam7 : <EOL> if xstart >= self . width : <EOL> continue <EOL> ppr = int ( math . ceil ( ( self . width - xstart ) / float ( xstep ) ) ) <EOL> row_len = ppr * self . planes <EOL> for y in range ( ystart , self . height , ystep ) : <EOL> if xstep == <NUM_LIT:1> : <EOL> offset = y * vpr <EOL> yield pixels [ offset : offset + vpr ] <EOL> else : <EOL> row = array ( fmt ) <EOL> row . extend ( pixels [ <NUM_LIT:0> : row_len ] ) <EOL> offset = y * vpr + xstart * self . planes <EOL> end_offset = ( y + <NUM_LIT:1> ) * vpr <EOL> skip = self . planes * xstep <EOL> for i in range ( self . planes ) : <EOL> row [ i : : self . planes ] = pixels [ offset + i : end_offset : skip ] <EOL> yield row <EOL> def write_chunk ( outfile , tag , data = b'<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> outfile . write ( struct . pack ( "<STR_LIT>" , len ( data ) ) ) <EOL> outfile . write ( tag ) <EOL> outfile . write ( data ) <EOL> checksum = zlib . crc32 ( tag ) <EOL> checksum = zlib . crc32 ( data , checksum ) <EOL> checksum &= <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> <EOL> outfile . write ( struct . pack ( "<STR_LIT>" , checksum ) ) <EOL> def write_chunks ( out , chunks ) : <EOL> """<STR_LIT>""" <EOL> out . write ( _signature ) <EOL> for chunk in chunks : <EOL> write_chunk ( out , * chunk ) <EOL> def filter_scanline ( type , line , fo , prev = None ) : <EOL> """<STR_LIT>""" <EOL> assert <NUM_LIT:0> <= type < <NUM_LIT:5> <EOL> out = array ( '<STR_LIT:B>' , [ type ] ) <EOL> def sub ( ) : <EOL> ai = - fo <EOL> for x in line : <EOL> if ai >= <NUM_LIT:0> : <EOL> x = ( x - line [ ai ] ) & <NUM_LIT> <EOL> out . append ( x ) <EOL> ai += <NUM_LIT:1> <EOL> def up ( ) : <EOL> for i , x in enumerate ( line ) : <EOL> x = ( x - prev [ i ] ) & <NUM_LIT> <EOL> out . append ( x ) <EOL> def average ( ) : <EOL> ai = - fo <EOL> for i , x in enumerate ( line ) : <EOL> if ai >= <NUM_LIT:0> : <EOL> x = ( x - ( ( line [ ai ] + prev [ i ] ) >> <NUM_LIT:1> ) ) & <NUM_LIT> <EOL> else : <EOL> x = ( x - ( prev [ i ] >> <NUM_LIT:1> ) ) & <NUM_LIT> <EOL> out . append ( x ) <EOL> ai += <NUM_LIT:1> <EOL> def paeth ( ) : <EOL> ai = - fo <EOL> for i , x in enumerate ( line ) : <EOL> a = <NUM_LIT:0> <EOL> b = prev [ i ] <EOL> c = <NUM_LIT:0> <EOL> if ai >= <NUM_LIT:0> : <EOL> a = line [ ai ] <EOL> c = prev [ ai ] <EOL> p = a + b - c <EOL> pa = abs ( p - a ) <EOL> pb = abs ( p - b ) <EOL> pc = abs ( p - c ) <EOL> if pa <= pb and pa <= pc : <EOL> Pr = a <EOL> elif pb <= pc : <EOL> Pr = b <EOL> else : <EOL> Pr = c <EOL> x = ( x - Pr ) & <NUM_LIT> <EOL> out . append ( x ) <EOL> ai += <NUM_LIT:1> <EOL> if not prev : <EOL> if type == <NUM_LIT:2> : <EOL> type = <NUM_LIT:0> <EOL> elif type == <NUM_LIT:3> : <EOL> prev = [ <NUM_LIT:0> ] * len ( line ) <EOL> elif type == <NUM_LIT:4> : <EOL> type = <NUM_LIT:1> <EOL> if type == <NUM_LIT:0> : <EOL> out . extend ( line ) <EOL> elif type == <NUM_LIT:1> : <EOL> sub ( ) <EOL> elif type == <NUM_LIT:2> : <EOL> up ( ) <EOL> elif type == <NUM_LIT:3> : <EOL> average ( ) <EOL> else : <EOL> paeth ( ) <EOL> return out <EOL> def from_array ( a , mode = None , info = { } ) : <EOL> """<STR_LIT>""" <EOL> info = dict ( info ) <EOL> bitdepth = None <EOL> try : <EOL> if mode . startswith ( '<STR_LIT:L>' ) : <EOL> gotmode = '<STR_LIT:L>' <EOL> mode = mode [ <NUM_LIT:1> : ] <EOL> elif mode . startswith ( '<STR_LIT>' ) : <EOL> gotmode = '<STR_LIT>' <EOL> mode = mode [ <NUM_LIT:3> : ] <EOL> else : <EOL> raise Error ( ) <EOL> if mode . startswith ( '<STR_LIT:A>' ) : <EOL> gotmode += '<STR_LIT:A>' <EOL> mode = mode [ <NUM_LIT:1> : ] <EOL> while mode . startswith ( '<STR_LIT:;>' ) : <EOL> mode = mode [ <NUM_LIT:1> : ] <EOL> if mode : <EOL> try : <EOL> bitdepth = int ( mode ) <EOL> except ( TypeError , ValueError ) : <EOL> raise Error ( ) <EOL> except Error : <EOL> raise Error ( "<STR_LIT>" ) <EOL> mode = gotmode <EOL> if bitdepth : <EOL> if info . get ( '<STR_LIT>' ) and bitdepth != info [ '<STR_LIT>' ] : <EOL> raise Error ( "<STR_LIT>" % <EOL> ( bitdepth , info [ '<STR_LIT>' ] ) ) <EOL> info [ '<STR_LIT>' ] = bitdepth <EOL> if '<STR_LIT:size>' in info : <EOL> for dimension , axis in [ ( '<STR_LIT:width>' , <NUM_LIT:0> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] : <EOL> if dimension in info : <EOL> if info [ dimension ] != info [ '<STR_LIT:size>' ] [ axis ] : <EOL> raise Error ( <EOL> "<STR_LIT>" % <EOL> ( dimension , axis ) ) <EOL> info [ '<STR_LIT:width>' ] , info [ '<STR_LIT>' ] = info [ '<STR_LIT:size>' ] <EOL> if '<STR_LIT>' not in info : <EOL> try : <EOL> l = len ( a ) <EOL> except TypeError : <EOL> raise Error ( <EOL> "<STR_LIT>" ) <EOL> info [ '<STR_LIT>' ] = l <EOL> if '<STR_LIT>' in info : <EOL> if bool ( info [ '<STR_LIT>' ] ) != ( '<STR_LIT:L>' in mode ) : <EOL> raise Error ( "<STR_LIT>" ) <EOL> info [ '<STR_LIT>' ] = '<STR_LIT:L>' in mode <EOL> if '<STR_LIT>' in info : <EOL> if bool ( info [ '<STR_LIT>' ] ) != ( '<STR_LIT:A>' in mode ) : <EOL> raise Error ( "<STR_LIT>" ) <EOL> info [ '<STR_LIT>' ] = '<STR_LIT:A>' in mode <EOL> planes = len ( mode ) <EOL> if '<STR_LIT>' in info : <EOL> if info [ '<STR_LIT>' ] != planes : <EOL> raise Error ( "<STR_LIT>" ) <EOL> a , t = itertools . tee ( a ) <EOL> row = next ( t ) <EOL> del t <EOL> try : <EOL> row [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> threed = True <EOL> testelement = row [ <NUM_LIT:0> ] <EOL> except ( IndexError , TypeError ) : <EOL> threed = False <EOL> testelement = row <EOL> if '<STR_LIT:width>' not in info : <EOL> if threed : <EOL> width = len ( row ) <EOL> else : <EOL> width = len ( row ) // planes <EOL> info [ '<STR_LIT:width>' ] = width <EOL> if threed : <EOL> a = ( itertools . chain . from_iterable ( x ) for x in a ) <EOL> if '<STR_LIT>' not in info : <EOL> try : <EOL> dtype = testelement . dtype <EOL> except AttributeError : <EOL> try : <EOL> bitdepth = <NUM_LIT:8> * testelement . itemsize <EOL> except AttributeError : <EOL> bitdepth = <NUM_LIT:8> <EOL> else : <EOL> if dtype . kind == '<STR_LIT:b>' : <EOL> bitdepth = <NUM_LIT:1> <EOL> else : <EOL> bitdepth = <NUM_LIT:8> * dtype . itemsize <EOL> info [ '<STR_LIT>' ] = bitdepth <EOL> for thing in '<STR_LIT>' . split ( ) : <EOL> assert thing in info <EOL> return Image ( a , info ) <EOL> fromarray = from_array <EOL> class Image : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , rows , info ) : <EOL> """<STR_LIT>""" <EOL> self . rows = rows <EOL> self . info = info <EOL> def save ( self , file ) : <EOL> """<STR_LIT>""" <EOL> w = Writer ( ** self . info ) <EOL> try : <EOL> file . write <EOL> def close ( ) : pass <EOL> except AttributeError : <EOL> file = open ( file , '<STR_LIT:wb>' ) <EOL> def close ( ) : file . close ( ) <EOL> try : <EOL> w . write ( file , self . rows ) <EOL> finally : <EOL> close ( ) <EOL> class _readable : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , buf ) : <EOL> self . buf = buf <EOL> self . offset = <NUM_LIT:0> <EOL> def read ( self , n ) : <EOL> r = self . buf [ self . offset : self . offset + n ] <EOL> if isarray ( r ) : <EOL> r = r . tostring ( ) <EOL> self . offset += n <EOL> return r <EOL> try : <EOL> str ( b'<STR_LIT>' , '<STR_LIT:ascii>' ) <EOL> except TypeError : <EOL> as_str = str <EOL> else : <EOL> def as_str ( x ) : <EOL> return str ( x , '<STR_LIT:ascii>' ) <EOL> class Reader : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , _guess = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if ( ( _guess is not None and len ( kw ) != <NUM_LIT:0> ) or <EOL> ( _guess is None and len ( kw ) != <NUM_LIT:1> ) ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . signature = None <EOL> self . transparent = None <EOL> self . atchunk = None <EOL> if _guess is not None : <EOL> if isarray ( _guess ) : <EOL> kw [ "<STR_LIT>" ] = _guess <EOL> elif isinstance ( _guess , str ) : <EOL> kw [ "<STR_LIT:filename>" ] = _guess <EOL> elif hasattr ( _guess , '<STR_LIT>' ) : <EOL> kw [ "<STR_LIT:file>" ] = _guess <EOL> if "<STR_LIT:filename>" in kw : <EOL> self . file = open ( kw [ "<STR_LIT:filename>" ] , "<STR_LIT:rb>" ) <EOL> elif "<STR_LIT:file>" in kw : <EOL> self . file = kw [ "<STR_LIT:file>" ] <EOL> elif "<STR_LIT>" in kw : <EOL> self . file = _readable ( kw [ "<STR_LIT>" ] ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def chunk ( self , seek = None , lenient = False ) : <EOL> """<STR_LIT>""" <EOL> self . validate_signature ( ) <EOL> while True : <EOL> if not self . atchunk : <EOL> self . atchunk = self . chunklentype ( ) <EOL> length , type = self . atchunk <EOL> self . atchunk = None <EOL> data = self . file . read ( length ) <EOL> if len ( data ) != length : <EOL> raise ChunkError ( '<STR_LIT>' <EOL> % ( type , length ) ) <EOL> checksum = self . file . read ( <NUM_LIT:4> ) <EOL> if len ( checksum ) != <NUM_LIT:4> : <EOL> raise ChunkError ( '<STR_LIT>' % type ) <EOL> if seek and type != seek : <EOL> continue <EOL> verify = zlib . crc32 ( type ) <EOL> verify = zlib . crc32 ( data , verify ) <EOL> verify &= <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> <EOL> verify = struct . pack ( '<STR_LIT>' , verify ) <EOL> if checksum != verify : <EOL> ( a , ) = struct . unpack ( '<STR_LIT>' , checksum ) <EOL> ( b , ) = struct . unpack ( '<STR_LIT>' , verify ) <EOL> message = "<STR_LIT>" % ( type , a , b ) <EOL> if lenient : <EOL> warnings . warn ( message , RuntimeWarning ) <EOL> else : <EOL> raise ChunkError ( message ) <EOL> return type , data <EOL> def chunks ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> t , v = self . chunk ( ) <EOL> yield t , v <EOL> if t == b'<STR_LIT>' : <EOL> break <EOL> def undo_filter ( self , filter_type , scanline , previous ) : <EOL> """<STR_LIT>""" <EOL> result = scanline <EOL> if filter_type == <NUM_LIT:0> : <EOL> return result <EOL> if filter_type not in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> raise FormatError ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> fu = max ( <NUM_LIT:1> , self . psize ) <EOL> if not previous : <EOL> previous = array ( '<STR_LIT:B>' , [ <NUM_LIT:0> ] * len ( scanline ) ) <EOL> def sub ( ) : <EOL> """<STR_LIT>""" <EOL> ai = <NUM_LIT:0> <EOL> for i in range ( fu , len ( result ) ) : <EOL> x = scanline [ i ] <EOL> a = result [ ai ] <EOL> result [ i ] = ( x + a ) & <NUM_LIT> <EOL> ai += <NUM_LIT:1> <EOL> def up ( ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( len ( result ) ) : <EOL> x = scanline [ i ] <EOL> b = previous [ i ] <EOL> result [ i ] = ( x + b ) & <NUM_LIT> <EOL> def average ( ) : <EOL> """<STR_LIT>""" <EOL> ai = - fu <EOL> for i in range ( len ( result ) ) : <EOL> x = scanline [ i ] <EOL> if ai < <NUM_LIT:0> : <EOL> a = <NUM_LIT:0> <EOL> else : <EOL> a = result [ ai ] <EOL> b = previous [ i ] <EOL> result [ i ] = ( x + ( ( a + b ) >> <NUM_LIT:1> ) ) & <NUM_LIT> <EOL> ai += <NUM_LIT:1> <EOL> def paeth ( ) : <EOL> """<STR_LIT>""" <EOL> ai = - fu <EOL> for i in range ( len ( result ) ) : <EOL> x = scanline [ i ] <EOL> if ai < <NUM_LIT:0> : <EOL> a = c = <NUM_LIT:0> <EOL> else : <EOL> a = result [ ai ] <EOL> c = previous [ ai ] <EOL> b = previous [ i ] <EOL> p = a + b - c <EOL> pa = abs ( p - a ) <EOL> pb = abs ( p - b ) <EOL> pc = abs ( p - c ) <EOL> if pa <= pb and pa <= pc : <EOL> pr = a <EOL> elif pb <= pc : <EOL> pr = b <EOL> else : <EOL> pr = c <EOL> result [ i ] = ( x + pr ) & <NUM_LIT> <EOL> ai += <NUM_LIT:1> <EOL> ( None , <EOL> pngfilters . undo_filter_sub , <EOL> pngfilters . undo_filter_up , <EOL> pngfilters . undo_filter_average , <EOL> pngfilters . undo_filter_paeth ) [ filter_type ] ( fu , scanline , previous , result ) <EOL> return result <EOL> def deinterlace ( self , raw ) : <EOL> """<STR_LIT>""" <EOL> vpr = self . width * self . planes <EOL> fmt = '<STR_LIT>' [ self . bitdepth > <NUM_LIT:8> ] <EOL> a = array ( fmt , [ <NUM_LIT:0> ] * vpr * self . height ) <EOL> source_offset = <NUM_LIT:0> <EOL> for xstart , ystart , xstep , ystep in _adam7 : <EOL> if xstart >= self . width : <EOL> continue <EOL> recon = None <EOL> ppr = int ( math . ceil ( ( self . width - xstart ) / float ( xstep ) ) ) <EOL> row_size = int ( math . ceil ( self . psize * ppr ) ) <EOL> for y in range ( ystart , self . height , ystep ) : <EOL> filter_type = raw [ source_offset ] <EOL> source_offset += <NUM_LIT:1> <EOL> scanline = raw [ source_offset : source_offset + row_size ] <EOL> source_offset += row_size <EOL> recon = self . undo_filter ( filter_type , scanline , recon ) <EOL> flat = self . serialtoflat ( recon , ppr ) <EOL> if xstep == <NUM_LIT:1> : <EOL> assert xstart == <NUM_LIT:0> <EOL> offset = y * vpr <EOL> a [ offset : offset + vpr ] = flat <EOL> else : <EOL> offset = y * vpr + xstart * self . planes <EOL> end_offset = ( y + <NUM_LIT:1> ) * vpr <EOL> skip = self . planes * xstep <EOL> for i in range ( self . planes ) : <EOL> a [ offset + i : end_offset : skip ] = flat [ i : : self . planes ] <EOL> return a <EOL> def iterboxed ( self , rows ) : <EOL> """<STR_LIT>""" <EOL> def asvalues ( raw ) : <EOL> """<STR_LIT>""" <EOL> if self . bitdepth == <NUM_LIT:8> : <EOL> return array ( '<STR_LIT:B>' , raw ) <EOL> if self . bitdepth == <NUM_LIT:16> : <EOL> raw = tostring ( raw ) <EOL> return array ( '<STR_LIT:H>' , struct . unpack ( '<STR_LIT>' % ( len ( raw ) // <NUM_LIT:2> ) , raw ) ) <EOL> assert self . bitdepth < <NUM_LIT:8> <EOL> width = self . width <EOL> spb = <NUM_LIT:8> // self . bitdepth <EOL> out = array ( '<STR_LIT:B>' ) <EOL> mask = <NUM_LIT:2> ** self . bitdepth - <NUM_LIT:1> <EOL> shifts = [ self . bitdepth * i <EOL> for i in reversed ( list ( range ( spb ) ) ) ] <EOL> for o in raw : <EOL> out . extend ( [ mask & ( o >> i ) for i in shifts ] ) <EOL> return out [ : width ] <EOL> return map ( asvalues , rows ) <EOL> def serialtoflat ( self , bytes , width = None ) : <EOL> """<STR_LIT>""" <EOL> if self . bitdepth == <NUM_LIT:8> : <EOL> return bytes <EOL> if self . bitdepth == <NUM_LIT:16> : <EOL> bytes = tostring ( bytes ) <EOL> return array ( '<STR_LIT:H>' , <EOL> struct . unpack ( '<STR_LIT>' % ( len ( bytes ) // <NUM_LIT:2> ) , bytes ) ) <EOL> assert self . bitdepth < <NUM_LIT:8> <EOL> if width is None : <EOL> width = self . width <EOL> spb = <NUM_LIT:8> // self . bitdepth <EOL> out = array ( '<STR_LIT:B>' ) <EOL> mask = <NUM_LIT:2> ** self . bitdepth - <NUM_LIT:1> <EOL> shifts = list ( map ( self . bitdepth . __mul__ , reversed ( list ( range ( spb ) ) ) ) ) <EOL> l = width <EOL> for o in bytes : <EOL> out . extend ( [ ( mask & ( o >> s ) ) for s in shifts ] [ : l ] ) <EOL> l -= spb <EOL> if l <= <NUM_LIT:0> : <EOL> l = width <EOL> return out <EOL> def iterstraight ( self , raw ) : <EOL> """<STR_LIT>""" <EOL> rb = self . row_bytes <EOL> a = array ( '<STR_LIT:B>' ) <EOL> recon = None <EOL> for some in raw : <EOL> a . extend ( some ) <EOL> while len ( a ) >= rb + <NUM_LIT:1> : <EOL> filter_type = a [ <NUM_LIT:0> ] <EOL> scanline = a [ <NUM_LIT:1> : rb + <NUM_LIT:1> ] <EOL> del a [ : rb + <NUM_LIT:1> ] <EOL> recon = self . undo_filter ( filter_type , scanline , recon ) <EOL> yield recon <EOL> if len ( a ) != <NUM_LIT:0> : <EOL> raise FormatError ( <EOL> '<STR_LIT>' ) <EOL> assert len ( a ) == <NUM_LIT:0> <EOL> def validate_signature ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . signature : <EOL> return <EOL> self . signature = self . file . read ( <NUM_LIT:8> ) <EOL> if self . signature != _signature : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> def preamble ( self , lenient = False ) : <EOL> """<STR_LIT>""" <EOL> self . validate_signature ( ) <EOL> while True : <EOL> if not self . atchunk : <EOL> self . atchunk = self . chunklentype ( ) <EOL> if self . atchunk is None : <EOL> raise FormatError ( <EOL> '<STR_LIT>' ) <EOL> if self . atchunk [ <NUM_LIT:1> ] == b'<STR_LIT>' : <EOL> return <EOL> self . process_chunk ( lenient = lenient ) <EOL> def chunklentype ( self ) : <EOL> """<STR_LIT>""" <EOL> x = self . file . read ( <NUM_LIT:8> ) <EOL> if not x : <EOL> return None <EOL> if len ( x ) != <NUM_LIT:8> : <EOL> raise FormatError ( <EOL> '<STR_LIT>' ) <EOL> length , type = struct . unpack ( '<STR_LIT>' , x ) <EOL> if length > <NUM_LIT:2> ** <NUM_LIT> - <NUM_LIT:1> : <EOL> raise FormatError ( '<STR_LIT>' % ( type , length ) ) <EOL> return length , type <EOL> def process_chunk ( self , lenient = False ) : <EOL> """<STR_LIT>""" <EOL> type , data = self . chunk ( lenient = lenient ) <EOL> method = '<STR_LIT>' + as_str ( type ) <EOL> m = getattr ( self , method , None ) <EOL> if m : <EOL> m ( data ) <EOL> def _process_IHDR ( self , data ) : <EOL> if len ( data ) != <NUM_LIT> : <EOL> raise FormatError ( '<STR_LIT>' ) <EOL> ( self . width , self . height , self . bitdepth , self . color_type , <EOL> self . compression , self . filter , <EOL> self . interlace ) = struct . unpack ( "<STR_LIT>" , data ) <EOL> check_bitdepth_colortype ( self . bitdepth , self . color_type ) <EOL> if self . compression != <NUM_LIT:0> : <EOL> raise Error ( "<STR_LIT>" % self . compression ) <EOL> if self . filter != <NUM_LIT:0> : <EOL> raise FormatError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . filter ) <EOL> if self . interlace not in ( <NUM_LIT:0> , <NUM_LIT:1> ) : <EOL> raise FormatError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % self . interlace ) <EOL> colormap = bool ( self . color_type & <NUM_LIT:1> ) <EOL> greyscale = not ( self . color_type & <NUM_LIT:2> ) <EOL> alpha = bool ( self . color_type & <NUM_LIT:4> ) <EOL> color_planes = ( <NUM_LIT:3> , <NUM_LIT:1> ) [ greyscale or colormap ] <EOL> planes = color_planes + alpha <EOL> self . colormap = colormap <EOL> self . greyscale = greyscale <EOL> self . alpha = alpha <EOL> self . color_planes = color_planes <EOL> self . planes = planes <EOL> self . psize = float ( self . bitdepth ) / float ( <NUM_LIT:8> ) * planes <EOL> if int ( self . psize ) == self . psize : <EOL> self . psize = int ( self . psize ) <EOL> self . row_bytes = int ( math . ceil ( self . width * self . psize ) ) <EOL> self . plte = None <EOL> self . trns = None <EOL> self . sbit = None <EOL> def _process_PLTE ( self , data ) : <EOL> if self . plte : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> self . plte = data <EOL> if len ( data ) % <NUM_LIT:3> != <NUM_LIT:0> : <EOL> raise FormatError ( <EOL> "<STR_LIT>" ) <EOL> if len ( data ) > ( <NUM_LIT:2> ** self . bitdepth ) * <NUM_LIT:3> : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> if len ( data ) == <NUM_LIT:0> : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> def _process_bKGD ( self , data ) : <EOL> try : <EOL> if self . colormap : <EOL> if not self . plte : <EOL> warnings . warn ( <EOL> "<STR_LIT>" ) <EOL> self . background = struct . unpack ( '<STR_LIT:B>' , data ) <EOL> else : <EOL> self . background = struct . unpack ( "<STR_LIT>" % self . color_planes , <EOL> data ) <EOL> except struct . error : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> def _process_tRNS ( self , data ) : <EOL> self . trns = data <EOL> if self . colormap : <EOL> if not self . plte : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> else : <EOL> if len ( data ) > len ( self . plte ) / <NUM_LIT:3> : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> else : <EOL> if self . alpha : <EOL> raise FormatError ( <EOL> "<STR_LIT>" % <EOL> self . color_type ) <EOL> try : <EOL> self . transparent = struct . unpack ( "<STR_LIT>" % self . color_planes , data ) <EOL> except struct . error : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> def _process_gAMA ( self , data ) : <EOL> try : <EOL> self . gamma = struct . unpack ( "<STR_LIT>" , data ) [ <NUM_LIT:0> ] / <NUM_LIT> <EOL> except struct . error : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> def _process_sBIT ( self , data ) : <EOL> self . sbit = data <EOL> if ( self . colormap and len ( data ) != <NUM_LIT:3> or <EOL> not self . colormap and len ( data ) != self . planes ) : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> def _process_pHYs ( self , data ) : <EOL> self . phys = data <EOL> fmt = "<STR_LIT>" <EOL> if len ( data ) != struct . calcsize ( fmt ) : <EOL> raise FormatError ( "<STR_LIT>" ) <EOL> self . x_pixels_per_unit , self . y_pixels_per_unit , unit = struct . unpack ( fmt , data ) <EOL> self . unit_is_meter = bool ( unit ) <EOL> def read ( self , lenient = False ) : <EOL> """<STR_LIT>""" <EOL> def iteridat ( ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> try : <EOL> type , data = self . chunk ( lenient = lenient ) <EOL> except ValueError as e : <EOL> raise ChunkError ( e . args [ <NUM_LIT:0> ] ) <EOL> if type == b'<STR_LIT>' : <EOL> break <EOL> if type != b'<STR_LIT>' : <EOL> continue <EOL> if self . colormap and not self . plte : <EOL> warnings . warn ( "<STR_LIT>" ) <EOL> yield data <EOL> def iterdecomp ( idat ) : <EOL> """<STR_LIT>""" <EOL> d = zlib . decompressobj ( ) <EOL> for data in idat : <EOL> yield array ( '<STR_LIT:B>' , d . decompress ( data ) ) <EOL> yield array ( '<STR_LIT:B>' , d . flush ( ) ) <EOL> self . preamble ( lenient = lenient ) <EOL> raw = iterdecomp ( iteridat ( ) ) <EOL> if self . interlace : <EOL> raw = array ( '<STR_LIT:B>' , itertools . chain ( * raw ) ) <EOL> arraycode = '<STR_LIT>' [ self . bitdepth > <NUM_LIT:8> ] <EOL> pixels = map ( lambda * row : array ( arraycode , row ) , <EOL> * [ iter ( self . deinterlace ( raw ) ) ] * self . width * self . planes ) <EOL> else : <EOL> pixels = self . iterboxed ( self . iterstraight ( raw ) ) <EOL> meta = dict ( ) <EOL> for attr in '<STR_LIT>' . split ( ) : <EOL> meta [ attr ] = getattr ( self , attr ) <EOL> meta [ '<STR_LIT:size>' ] = ( self . width , self . height ) <EOL> for attr in '<STR_LIT>' . split ( ) : <EOL> a = getattr ( self , attr , None ) <EOL> if a is not None : <EOL> meta [ attr ] = a <EOL> if self . plte : <EOL> meta [ '<STR_LIT>' ] = self . palette ( ) <EOL> return self . width , self . height , pixels , meta <EOL> def read_flat ( self ) : <EOL> """<STR_LIT>""" <EOL> x , y , pixel , meta = self . read ( ) <EOL> arraycode = '<STR_LIT>' [ meta [ '<STR_LIT>' ] > <NUM_LIT:8> ] <EOL> pixel = array ( arraycode , itertools . chain ( * pixel ) ) <EOL> return x , y , pixel , meta <EOL> def palette ( self , alpha = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not self . plte : <EOL> raise FormatError ( <EOL> "<STR_LIT>" ) <EOL> plte = group ( array ( '<STR_LIT:B>' , self . plte ) , <NUM_LIT:3> ) <EOL> if self . trns or alpha == '<STR_LIT>' : <EOL> trns = array ( '<STR_LIT:B>' , self . trns or '<STR_LIT>' ) <EOL> trns . extend ( [ <NUM_LIT:255> ] * ( len ( plte ) - len ( trns ) ) ) <EOL> plte = list ( map ( operator . add , plte , group ( trns , <NUM_LIT:1> ) ) ) <EOL> return plte <EOL> def asDirect ( self ) : <EOL> """<STR_LIT>""" <EOL> self . preamble ( ) <EOL> if not self . colormap and not self . trns and not self . sbit : <EOL> return self . read ( ) <EOL> x , y , pixels , meta = self . read ( ) <EOL> if self . colormap : <EOL> meta [ '<STR_LIT>' ] = False <EOL> meta [ '<STR_LIT>' ] = bool ( self . trns ) <EOL> meta [ '<STR_LIT>' ] = <NUM_LIT:8> <EOL> meta [ '<STR_LIT>' ] = <NUM_LIT:3> + bool ( self . trns ) <EOL> plte = self . palette ( ) <EOL> def iterpal ( pixels ) : <EOL> for row in pixels : <EOL> row = [ plte [ x ] for x in row ] <EOL> yield array ( '<STR_LIT:B>' , itertools . chain ( * row ) ) <EOL> pixels = iterpal ( pixels ) <EOL> elif self . trns : <EOL> it = self . transparent <EOL> maxval = <NUM_LIT:2> ** meta [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> planes = meta [ '<STR_LIT>' ] <EOL> meta [ '<STR_LIT>' ] = True <EOL> meta [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> typecode = '<STR_LIT>' [ meta [ '<STR_LIT>' ] > <NUM_LIT:8> ] <EOL> def itertrns ( pixels ) : <EOL> for row in pixels : <EOL> row = group ( row , planes ) <EOL> opa = map ( it . __ne__ , row ) <EOL> opa = map ( maxval . __mul__ , opa ) <EOL> opa = list ( zip ( opa ) ) <EOL> yield array ( typecode , <EOL> itertools . chain ( * map ( operator . add , row , opa ) ) ) <EOL> pixels = itertrns ( pixels ) <EOL> targetbitdepth = None <EOL> if self . sbit : <EOL> sbit = struct . unpack ( '<STR_LIT>' % len ( self . sbit ) , self . sbit ) <EOL> targetbitdepth = max ( sbit ) <EOL> if targetbitdepth > meta [ '<STR_LIT>' ] : <EOL> raise Error ( '<STR_LIT>' % <EOL> ( sbit , self . bitdepth ) ) <EOL> if min ( sbit ) <= <NUM_LIT:0> : <EOL> raise Error ( '<STR_LIT>' % sbit ) <EOL> if targetbitdepth == meta [ '<STR_LIT>' ] : <EOL> targetbitdepth = None <EOL> if targetbitdepth : <EOL> shift = meta [ '<STR_LIT>' ] - targetbitdepth <EOL> meta [ '<STR_LIT>' ] = targetbitdepth <EOL> def itershift ( pixels ) : <EOL> for row in pixels : <EOL> yield [ p >> shift for p in row ] <EOL> pixels = itershift ( pixels ) <EOL> return x , y , pixels , meta <EOL> def asFloat ( self , maxval = <NUM_LIT:1.0> ) : <EOL> """<STR_LIT>""" <EOL> x , y , pixels , info = self . asDirect ( ) <EOL> sourcemaxval = <NUM_LIT:2> ** info [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> del info [ '<STR_LIT>' ] <EOL> info [ '<STR_LIT>' ] = float ( maxval ) <EOL> factor = float ( maxval ) / float ( sourcemaxval ) <EOL> def iterfloat ( ) : <EOL> for row in pixels : <EOL> yield [ factor * p for p in row ] <EOL> return x , y , iterfloat ( ) , info <EOL> def _as_rescale ( self , get , targetbitdepth ) : <EOL> """<STR_LIT>""" <EOL> width , height , pixels , meta = get ( ) <EOL> maxval = <NUM_LIT:2> ** meta [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> targetmaxval = <NUM_LIT:2> ** targetbitdepth - <NUM_LIT:1> <EOL> factor = float ( targetmaxval ) / float ( maxval ) <EOL> meta [ '<STR_LIT>' ] = targetbitdepth <EOL> def iterscale ( ) : <EOL> for row in pixels : <EOL> yield [ int ( round ( x * factor ) ) for x in row ] <EOL> if maxval == targetmaxval : <EOL> return width , height , pixels , meta <EOL> else : <EOL> return width , height , iterscale ( ) , meta <EOL> def asRGB8 ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _as_rescale ( self . asRGB , <NUM_LIT:8> ) <EOL> def asRGBA8 ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _as_rescale ( self . asRGBA , <NUM_LIT:8> ) <EOL> def asRGB ( self ) : <EOL> """<STR_LIT>""" <EOL> width , height , pixels , meta = self . asDirect ( ) <EOL> if meta [ '<STR_LIT>' ] : <EOL> raise Error ( "<STR_LIT>" ) <EOL> if not meta [ '<STR_LIT>' ] : <EOL> return width , height , pixels , meta <EOL> meta [ '<STR_LIT>' ] = False <EOL> typecode = '<STR_LIT>' [ meta [ '<STR_LIT>' ] > <NUM_LIT:8> ] <EOL> def iterrgb ( ) : <EOL> for row in pixels : <EOL> a = array ( typecode , [ <NUM_LIT:0> ] ) * <NUM_LIT:3> * width <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> a [ i : : <NUM_LIT:3> ] = row <EOL> yield a <EOL> return width , height , iterrgb ( ) , meta <EOL> def asRGBA ( self ) : <EOL> """<STR_LIT>""" <EOL> width , height , pixels , meta = self . asDirect ( ) <EOL> if meta [ '<STR_LIT>' ] and not meta [ '<STR_LIT>' ] : <EOL> return width , height , pixels , meta <EOL> typecode = '<STR_LIT>' [ meta [ '<STR_LIT>' ] > <NUM_LIT:8> ] <EOL> maxval = <NUM_LIT:2> ** meta [ '<STR_LIT>' ] - <NUM_LIT:1> <EOL> maxbuffer = struct . pack ( '<STR_LIT:=>' + typecode , maxval ) * <NUM_LIT:4> * width <EOL> def newarray ( ) : <EOL> return array ( typecode , maxbuffer ) <EOL> if meta [ '<STR_LIT>' ] and meta [ '<STR_LIT>' ] : <EOL> def convert ( ) : <EOL> for row in pixels : <EOL> a = newarray ( ) <EOL> pngfilters . convert_la_to_rgba ( row , a ) <EOL> yield a <EOL> elif meta [ '<STR_LIT>' ] : <EOL> def convert ( ) : <EOL> for row in pixels : <EOL> a = newarray ( ) <EOL> pngfilters . convert_l_to_rgba ( row , a ) <EOL> yield a <EOL> else : <EOL> assert not meta [ '<STR_LIT>' ] and not meta [ '<STR_LIT>' ] <EOL> def convert ( ) : <EOL> for row in pixels : <EOL> a = newarray ( ) <EOL> pngfilters . convert_rgb_to_rgba ( row , a ) <EOL> yield a <EOL> meta [ '<STR_LIT>' ] = True <EOL> meta [ '<STR_LIT>' ] = False <EOL> return width , height , convert ( ) , meta <EOL> def check_bitdepth_colortype ( bitdepth , colortype ) : <EOL> """<STR_LIT>""" <EOL> if bitdepth not in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:16> ) : <EOL> raise FormatError ( "<STR_LIT>" % bitdepth ) <EOL> if colortype not in ( <NUM_LIT:0> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:6> ) : <EOL> raise FormatError ( "<STR_LIT>" % colortype ) <EOL> if colortype & <NUM_LIT:1> and bitdepth > <NUM_LIT:8> : <EOL> raise FormatError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( bitdepth , colortype ) ) <EOL> if bitdepth < <NUM_LIT:8> and colortype not in ( <NUM_LIT:0> , <NUM_LIT:3> ) : <EOL> raise FormatError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( bitdepth , colortype ) ) <EOL> def isinteger ( x ) : <EOL> try : <EOL> return int ( x ) == x <EOL> except ( TypeError , ValueError ) : <EOL> return False <EOL> try : <EOL> pngfilters <EOL> except NameError : <EOL> class pngfilters ( object ) : <EOL> def undo_filter_sub ( filter_unit , scanline , previous , result ) : <EOL> """<STR_LIT>""" <EOL> ai = <NUM_LIT:0> <EOL> for i in range ( filter_unit , len ( result ) ) : <EOL> x = scanline [ i ] <EOL> a = result [ ai ] <EOL> result [ i ] = ( x + a ) & <NUM_LIT> <EOL> ai += <NUM_LIT:1> <EOL> undo_filter_sub = staticmethod ( undo_filter_sub ) <EOL> def undo_filter_up ( filter_unit , scanline , previous , result ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( len ( result ) ) : <EOL> x = scanline [ i ] <EOL> b = previous [ i ] <EOL> result [ i ] = ( x + b ) & <NUM_LIT> <EOL> undo_filter_up = staticmethod ( undo_filter_up ) <EOL> def undo_filter_average ( filter_unit , scanline , previous , result ) : <EOL> """<STR_LIT>""" <EOL> ai = - filter_unit <EOL> for i in range ( len ( result ) ) : <EOL> x = scanline [ i ] <EOL> if ai < <NUM_LIT:0> : <EOL> a = <NUM_LIT:0> <EOL> else : <EOL> a = result [ ai ] <EOL> b = previous [ i ] <EOL> result [ i ] = ( x + ( ( a + b ) >> <NUM_LIT:1> ) ) & <NUM_LIT> <EOL> ai += <NUM_LIT:1> <EOL> undo_filter_average = staticmethod ( undo_filter_average ) <EOL> def undo_filter_paeth ( filter_unit , scanline , previous , result ) : <EOL> """<STR_LIT>""" <EOL> ai = - filter_unit <EOL> for i in range ( len ( result ) ) : <EOL> x = scanline [ i ] <EOL> if ai < <NUM_LIT:0> : <EOL> a = c = <NUM_LIT:0> <EOL> else : <EOL> a = result [ ai ] <EOL> c = previous [ ai ] <EOL> b = previous [ i ] <EOL> p = a + b - c <EOL> pa = abs ( p - a ) <EOL> pb = abs ( p - b ) <EOL> pc = abs ( p - c ) <EOL> if pa <= pb and pa <= pc : <EOL> pr = a <EOL> elif pb <= pc : <EOL> pr = b <EOL> else : <EOL> pr = c <EOL> result [ i ] = ( x + pr ) & <NUM_LIT> <EOL> ai += <NUM_LIT:1> <EOL> undo_filter_paeth = staticmethod ( undo_filter_paeth ) <EOL> def convert_la_to_rgba ( row , result ) : <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> result [ i : : <NUM_LIT:4> ] = row [ <NUM_LIT:0> : : <NUM_LIT:2> ] <EOL> result [ <NUM_LIT:3> : : <NUM_LIT:4> ] = row [ <NUM_LIT:1> : : <NUM_LIT:2> ] <EOL> convert_la_to_rgba = staticmethod ( convert_la_to_rgba ) <EOL> def convert_l_to_rgba ( row , result ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> result [ i : : <NUM_LIT:4> ] = row <EOL> convert_l_to_rgba = staticmethod ( convert_l_to_rgba ) <EOL> def convert_rgb_to_rgba ( row , result ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> result [ i : : <NUM_LIT:4> ] = row [ i : : <NUM_LIT:3> ] <EOL> convert_rgb_to_rgba = staticmethod ( convert_rgb_to_rgba ) <EOL> def read_pam_header ( infile ) : <EOL> """<STR_LIT>""" <EOL> header = dict ( ) <EOL> while True : <EOL> l = infile . readline ( ) . strip ( ) <EOL> if l == b'<STR_LIT>' : <EOL> break <EOL> if not l : <EOL> raise EOFError ( '<STR_LIT>' ) <EOL> if l [ <NUM_LIT:0> ] == b'<STR_LIT:#>' : <EOL> continue <EOL> l = l . split ( None , <NUM_LIT:1> ) <EOL> if l [ <NUM_LIT:0> ] not in header : <EOL> header [ l [ <NUM_LIT:0> ] ] = l [ <NUM_LIT:1> ] <EOL> else : <EOL> header [ l [ <NUM_LIT:0> ] ] += b'<STR_LIT:U+0020>' + l [ <NUM_LIT:1> ] <EOL> required = [ b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ] <EOL> WIDTH , HEIGHT , DEPTH , MAXVAL = required <EOL> present = [ x for x in required if x in header ] <EOL> if len ( present ) != len ( required ) : <EOL> raise Error ( '<STR_LIT>' ) <EOL> width = int ( header [ WIDTH ] ) <EOL> height = int ( header [ HEIGHT ] ) <EOL> depth = int ( header [ DEPTH ] ) <EOL> maxval = int ( header [ MAXVAL ] ) <EOL> if ( width <= <NUM_LIT:0> or <EOL> height <= <NUM_LIT:0> or <EOL> depth <= <NUM_LIT:0> or <EOL> maxval <= <NUM_LIT:0> ) : <EOL> raise Error ( <EOL> '<STR_LIT>' ) <EOL> return '<STR_LIT>' , width , height , depth , maxval <EOL> def read_pnm_header ( infile , supported = ( b'<STR_LIT>' , b'<STR_LIT>' ) ) : <EOL> """<STR_LIT>""" <EOL> type = infile . read ( <NUM_LIT:3> ) . rstrip ( ) <EOL> if type not in supported : <EOL> raise NotImplementedError ( '<STR_LIT>' % type ) <EOL> if type == b'<STR_LIT>' : <EOL> return read_pam_header ( infile ) <EOL> expected = <NUM_LIT:4> <EOL> pbm = ( b'<STR_LIT>' , b'<STR_LIT>' ) <EOL> if type in pbm : <EOL> expected = <NUM_LIT:3> <EOL> header = [ type ] <EOL> def getc ( ) : <EOL> c = infile . read ( <NUM_LIT:1> ) <EOL> if not c : <EOL> raise Error ( '<STR_LIT>' ) <EOL> return c <EOL> c = getc ( ) <EOL> while True : <EOL> while c . isspace ( ) : <EOL> c = getc ( ) <EOL> while c == '<STR_LIT:#>' : <EOL> while c not in b'<STR_LIT>' : <EOL> c = getc ( ) <EOL> if not c . isdigit ( ) : <EOL> raise Error ( '<STR_LIT>' % c ) <EOL> token = b'<STR_LIT>' <EOL> while c . isdigit ( ) : <EOL> token += c <EOL> c = getc ( ) <EOL> header . append ( int ( token ) ) <EOL> if len ( header ) == expected : <EOL> break <EOL> while c == '<STR_LIT:#>' : <EOL> while c not in '<STR_LIT>' : <EOL> c = getc ( ) <EOL> if not c . isspace ( ) : <EOL> raise Error ( '<STR_LIT>' % c ) <EOL> if type in pbm : <EOL> header . append ( <NUM_LIT:1> ) <EOL> depth = ( <NUM_LIT:1> , <NUM_LIT:3> ) [ type == b'<STR_LIT>' ] <EOL> return header [ <NUM_LIT:0> ] , header [ <NUM_LIT:1> ] , header [ <NUM_LIT:2> ] , depth , header [ <NUM_LIT:3> ] <EOL> def write_pnm ( file , width , height , pixels , meta ) : <EOL> """<STR_LIT>""" <EOL> bitdepth = meta [ '<STR_LIT>' ] <EOL> maxval = <NUM_LIT:2> ** bitdepth - <NUM_LIT:1> <EOL> planes = meta [ '<STR_LIT>' ] <EOL> assert planes in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) <EOL> if planes in ( <NUM_LIT:1> , <NUM_LIT:3> ) : <EOL> if <NUM_LIT:1> == planes : <EOL> fmt = '<STR_LIT>' <EOL> else : <EOL> fmt = '<STR_LIT>' <EOL> header = '<STR_LIT>' % ( fmt , width , height , maxval ) <EOL> if planes in ( <NUM_LIT:2> , <NUM_LIT:4> ) : <EOL> if <NUM_LIT:2> == planes : <EOL> tupltype = '<STR_LIT>' <EOL> else : <EOL> tupltype = '<STR_LIT>' <EOL> header = ( '<STR_LIT>' <EOL> '<STR_LIT>' % <EOL> ( width , height , planes , maxval , tupltype ) ) <EOL> file . write ( header . encode ( '<STR_LIT:ascii>' ) ) <EOL> vpr = planes * width <EOL> fmt = '<STR_LIT>' % vpr <EOL> if maxval > <NUM_LIT> : <EOL> fmt = fmt + '<STR_LIT:H>' <EOL> else : <EOL> fmt = fmt + '<STR_LIT:B>' <EOL> for row in pixels : <EOL> file . write ( struct . pack ( fmt , * row ) ) <EOL> file . flush ( ) <EOL> def color_triple ( color ) : <EOL> """<STR_LIT>""" <EOL> if color . startswith ( '<STR_LIT:#>' ) and len ( color ) == <NUM_LIT:4> : <EOL> return ( int ( color [ <NUM_LIT:1> ] , <NUM_LIT:16> ) , <EOL> int ( color [ <NUM_LIT:2> ] , <NUM_LIT:16> ) , <EOL> int ( color [ <NUM_LIT:3> ] , <NUM_LIT:16> ) ) <EOL> if color . startswith ( '<STR_LIT:#>' ) and len ( color ) == <NUM_LIT:7> : <EOL> return ( int ( color [ <NUM_LIT:1> : <NUM_LIT:3> ] , <NUM_LIT:16> ) , <EOL> int ( color [ <NUM_LIT:3> : <NUM_LIT:5> ] , <NUM_LIT:16> ) , <EOL> int ( color [ <NUM_LIT:5> : <NUM_LIT:7> ] , <NUM_LIT:16> ) ) <EOL> elif color . startswith ( '<STR_LIT:#>' ) and len ( color ) == <NUM_LIT> : <EOL> return ( int ( color [ <NUM_LIT:1> : <NUM_LIT:5> ] , <NUM_LIT:16> ) , <EOL> int ( color [ <NUM_LIT:5> : <NUM_LIT:9> ] , <NUM_LIT:16> ) , <EOL> int ( color [ <NUM_LIT:9> : <NUM_LIT> ] , <NUM_LIT:16> ) ) <EOL> def _add_common_options ( parser ) : <EOL> """<STR_LIT>""" <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> default = False , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , type = "<STR_LIT:float>" , metavar = "<STR_LIT:value>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT:-c>" , "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , type = "<STR_LIT:int>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> return parser <EOL> def _main ( argv ) : <EOL> """<STR_LIT>""" <EOL> from optparse import OptionParser <EOL> version = '<STR_LIT>' + __version__ <EOL> parser = OptionParser ( version = version ) <EOL> parser . set_usage ( "<STR_LIT>" ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , default = False , <EOL> action = '<STR_LIT:store_true>' , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , <EOL> action = "<STR_LIT:store>" , type = "<STR_LIT:string>" , metavar = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> _add_common_options ( parser ) <EOL> ( options , args ) = parser . parse_args ( args = argv [ <NUM_LIT:1> : ] ) <EOL> if options . transparent is not None : <EOL> options . transparent = color_triple ( options . transparent ) <EOL> if options . background is not None : <EOL> options . background = color_triple ( options . background ) <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> infilename = '<STR_LIT:->' <EOL> infile = sys . stdin <EOL> elif len ( args ) == <NUM_LIT:1> : <EOL> infilename = args [ <NUM_LIT:0> ] <EOL> infile = open ( infilename , '<STR_LIT:rb>' ) <EOL> else : <EOL> parser . error ( "<STR_LIT>" ) <EOL> outfile = sys . stdout <EOL> if sys . platform == "<STR_LIT:win32>" : <EOL> import msvcrt , os <EOL> msvcrt . setmode ( sys . stdout . fileno ( ) , os . O_BINARY ) <EOL> if options . read_png : <EOL> png = Reader ( file = infile ) <EOL> width , height , pixels , meta = png . asDirect ( ) <EOL> write_pnm ( outfile , width , height , pixels , meta ) <EOL> else : <EOL> format , width , height , depth , maxval = read_pnm_header ( infile , ( b'<STR_LIT>' , b'<STR_LIT>' , b'<STR_LIT>' ) ) <EOL> greyscale = depth <= <NUM_LIT:2> <EOL> pamalpha = depth in ( <NUM_LIT:2> , <NUM_LIT:4> ) <EOL> supported = [ <NUM_LIT:2> ** x - <NUM_LIT:1> for x in range ( <NUM_LIT:1> , <NUM_LIT> ) ] <EOL> try : <EOL> mi = supported . index ( maxval ) <EOL> except ValueError : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' % <EOL> ( maxval , str ( supported ) ) ) <EOL> bitdepth = mi + <NUM_LIT:1> <EOL> writer = Writer ( width , height , <EOL> greyscale = greyscale , <EOL> bitdepth = bitdepth , <EOL> interlace = options . interlace , <EOL> transparent = options . transparent , <EOL> background = options . background , <EOL> alpha = bool ( pamalpha or options . alpha ) , <EOL> gamma = options . gamma , <EOL> compression = options . compression ) <EOL> if options . alpha : <EOL> pgmfile = open ( options . alpha , '<STR_LIT:rb>' ) <EOL> format , awidth , aheight , adepth , amaxval = read_pnm_header ( pgmfile , '<STR_LIT>' ) <EOL> if amaxval != '<STR_LIT>' : <EOL> raise NotImplementedError ( <EOL> '<STR_LIT>' % amaxval ) <EOL> if ( awidth , aheight ) != ( width , height ) : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( infilename , width , height , <EOL> options . alpha , awidth , aheight ) ) <EOL> writer . convert_ppm_and_pgm ( infile , pgmfile , outfile ) <EOL> else : <EOL> writer . convert_pnm ( infile , outfile ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> try : <EOL> _main ( sys . argv ) <EOL> except Error as e : <EOL> print ( e , file = sys . stderr ) </s>
