<s> '''<STR_LIT>''' <EOL> import struct <EOL> from array import array <EOL> KNOWN_FORMATS = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> from kivy . compat import PY2 <EOL> from kivy . logger import Logger <EOL> from kivy . core . image import ImageLoaderBase , ImageData , ImageLoader <EOL> Debug = False <EOL> class ImageLoaderGIF ( ImageLoaderBase ) : <EOL> '''<STR_LIT>''' <EOL> @ staticmethod <EOL> def extensions ( ) : <EOL> '''<STR_LIT>''' <EOL> return ( '<STR_LIT>' , ) <EOL> def load ( self , filename ) : <EOL> try : <EOL> try : <EOL> im = GifDecoder ( open ( filename , '<STR_LIT:rb>' ) . read ( ) ) <EOL> except UnicodeEncodeError : <EOL> if PY2 : <EOL> im = GifDecoder ( open ( filename . encode ( '<STR_LIT:utf8>' ) , '<STR_LIT:rb>' ) . read ( ) ) <EOL> except : <EOL> Logger . warning ( '<STR_LIT>' % filename ) <EOL> raise <EOL> if Debug : <EOL> print ( im . print_info ( ) ) <EOL> img_data = [ ] <EOL> ls_width = im . ls_width <EOL> ls_height = im . ls_height <EOL> im_images = im . images <EOL> im_palette = im . palette <EOL> pixel_map = array ( '<STR_LIT:B>' , [ <NUM_LIT:0> ] * ( ls_width * ls_height * <NUM_LIT:4> ) ) <EOL> for img in im_images : <EOL> palette = img . palette if img . local_color_table_flag else im_palette <EOL> have_transparent_color = img . has_transparent_color <EOL> transparent_color = img . transparent_color <EOL> draw_method_replace = <NUM_LIT:1> if ( ( img . draw_method == '<STR_LIT:replace>' ) or <EOL> ( img . draw_method == '<STR_LIT>' ) ) else <NUM_LIT:0> <EOL> pixels = img . pixels <EOL> img_height = img . height <EOL> img_width = img . width <EOL> left = img . left <EOL> top = img . top <EOL> if img_height > ls_height or img_width > ls_width or top > ls_height or left > ls_width : <EOL> Logger . warning ( '<STR_LIT>' % <EOL> len ( img_data ) ) <EOL> img_height = ls_height <EOL> img_width = ls_width <EOL> left = top = <NUM_LIT:0> <EOL> tmp_top = ( ls_height - ( img_height + top ) ) <EOL> img_width_plus_left = ( img_width + left ) <EOL> ls_width_multiply_4 = ls_width * <NUM_LIT:4> <EOL> left_multiply_4 = left * <NUM_LIT:4> <EOL> img_data_append = img_data . append <EOL> while img_height > <NUM_LIT:0> : <EOL> i = left <EOL> img_height -= <NUM_LIT:1> <EOL> x = ( img_height * img_width ) - left <EOL> rgba_pos = ( tmp_top * ls_width_multiply_4 ) + ( left_multiply_4 ) <EOL> tmp_top += <NUM_LIT:1> <EOL> while i < img_width_plus_left : <EOL> try : <EOL> ( r , g , b ) = palette [ pixels [ x + i ] ] <EOL> except : <EOL> rgba_pos += <NUM_LIT:4> <EOL> i += <NUM_LIT:1> <EOL> continue <EOL> if ( r , g , b ) != ( <NUM_LIT:255> , <NUM_LIT:0> , <NUM_LIT:255> ) : <EOL> if have_transparent_color : <EOL> if transparent_color == pixels [ x + i ] : <EOL> if draw_method_replace : <EOL> pixel_map [ rgba_pos + <NUM_LIT:3> ] = <NUM_LIT:0> <EOL> rgba_pos += <NUM_LIT:4> <EOL> i += <NUM_LIT:1> <EOL> continue <EOL> rgba_pos += <NUM_LIT:4> <EOL> i += <NUM_LIT:1> <EOL> continue <EOL> ( pixel_map [ rgba_pos ] , pixel_map [ rgba_pos + <NUM_LIT:1> ] , <EOL> pixel_map [ rgba_pos + <NUM_LIT:2> ] ) = ( r , g , b ) <EOL> pixel_map [ rgba_pos + <NUM_LIT:3> ] = <NUM_LIT:255> <EOL> rgba_pos += <NUM_LIT:4> <EOL> i += <NUM_LIT:1> <EOL> if PY2 : <EOL> img_data_append ( ImageData ( ls_width , ls_height , <EOL> '<STR_LIT>' , pixel_map . tostring ( ) , flip_vertical = False ) ) <EOL> else : <EOL> img_data_append ( ImageData ( ls_width , ls_height , <EOL> '<STR_LIT>' , pixel_map . tobytes ( ) , flip_vertical = False ) ) <EOL> if draw_method_replace : <EOL> pixel_map = array ( '<STR_LIT:B>' , [ <NUM_LIT:0> ] * ( ls_width * ls_height * <NUM_LIT:4> ) ) <EOL> self . filename = filename <EOL> return img_data <EOL> class Gif ( object ) : <EOL> '''<STR_LIT>''' <EOL> FMT_HEADER = '<STR_LIT>' <EOL> FMT_IMGDESC = '<STR_LIT>' <EOL> IMAGE_SEPARATOR = <NUM_LIT> <EOL> EXTENSION_INTRODUCER = <NUM_LIT> <EOL> GIF_TRAILER = <NUM_LIT> <EOL> LABEL_GRAPHIC_CONTROL = <NUM_LIT> <EOL> LABEL_COMMENT = <NUM_LIT> <EOL> LABEL_PLAINTEXT = <NUM_LIT> <EOL> FMT_EXT_GRAPHIC_CONTROL = '<STR_LIT>' <EOL> def __init__ ( self , data , debug ) : <EOL> self . data = data <EOL> self . pointer = <NUM_LIT:0> <EOL> self . header = '<STR_LIT>' <EOL> self . ls_width = <NUM_LIT:0> <EOL> self . ls_height = <NUM_LIT:0> <EOL> self . flags = <NUM_LIT:0> <EOL> self . color_resolution = <NUM_LIT:0> <EOL> self . sort_flag = <NUM_LIT:0> <EOL> self . color_table_flag = <NUM_LIT:0> <EOL> self . global_color_table_size = <NUM_LIT:0> <EOL> self . background_color = <NUM_LIT:0> <EOL> self . aspect_ratio = <NUM_LIT:0> <EOL> self . palette = [ ( x , x , x ) for x in range ( <NUM_LIT:0> , <NUM_LIT> ) ] <EOL> self . images = [ ] <EOL> self . debug_enabled = False <EOL> return <EOL> def pop ( self , data , length = <NUM_LIT:1> ) : <EOL> '''<STR_LIT>''' <EOL> start = self . pointer <EOL> end = self . pointer + length <EOL> self . pointer += length <EOL> return data [ start : end ] <EOL> def pops ( self , format , data ) : <EOL> '''<STR_LIT>''' <EOL> size = struct . calcsize ( format ) <EOL> return struct . unpack ( format , self . pop ( data , size ) ) <EOL> def print_info ( self ) : <EOL> '''<STR_LIT>''' <EOL> print ( "<STR_LIT>" % self . header ) <EOL> print ( "<STR_LIT>" % self . ls_width ) <EOL> print ( "<STR_LIT>" % self . ls_height ) <EOL> print ( "<STR_LIT>" % repr ( self . flags ) ) <EOL> print ( "<STR_LIT:U+0020>" * <NUM_LIT:6> , "<STR_LIT>" % self . color_resolution ) <EOL> print ( "<STR_LIT:U+0020>" * <NUM_LIT:6> , "<STR_LIT>" % self . sort_flag ) <EOL> print ( "<STR_LIT:U+0020>" * <NUM_LIT:6> , "<STR_LIT>" % self . color_table_flag ) <EOL> print ( "<STR_LIT:U+0020>" * <NUM_LIT> , "<STR_LIT>" % <EOL> ( self . global_color_table_size , self . global_color_table_size * <NUM_LIT:3> ) ) <EOL> print ( "<STR_LIT>" % self . background_color ) <EOL> print ( "<STR_LIT>" % self . aspect_ratio ) <EOL> def new_image ( self , header = None ) : <EOL> '''<STR_LIT>''' <EOL> image = ImageDescriptor ( self , header ) <EOL> self . images . append ( image ) <EOL> return image <EOL> class ImageDescriptor ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , parent , header = None ) : <EOL> self . parent = parent <EOL> self . codesize = <NUM_LIT:0> <EOL> self . lzwcode = '<STR_LIT>' <EOL> self . pixels = [ ] <EOL> self . left = self . top = <NUM_LIT:0> <EOL> self . width = parent . ls_width <EOL> self . height = parent . ls_height <EOL> self . flags = [ False for x in range ( <NUM_LIT:8> ) ] <EOL> self . local_color_table_flag = False <EOL> self . interlace_flag = False <EOL> self . sort_flag = False <EOL> self . local_color_table_size = <NUM_LIT:0> <EOL> self . draw_method = '<STR_LIT:replace>' <EOL> self . transparent_color = - <NUM_LIT:1> <EOL> self . has_transparent_color = <NUM_LIT:0> <EOL> self . palette = [ ] <EOL> if header : <EOL> self . setup_header ( header ) <EOL> def setup_header ( self , header ) : <EOL> '''<STR_LIT>''' <EOL> self . left = header [ <NUM_LIT:0> ] <EOL> self . top = header [ <NUM_LIT:1> ] <EOL> self . width = header [ <NUM_LIT:2> ] <EOL> self . height = header [ <NUM_LIT:3> ] <EOL> self . flags = get_bits ( header [ <NUM_LIT:4> ] ) <EOL> self . local_color_table_flag = self . flags [ <NUM_LIT:7> ] <EOL> self . interlace_flag = self . flags [ <NUM_LIT:6> ] <EOL> self . sort_flag = self . flags [ <NUM_LIT:5> ] <EOL> self . local_color_table_size = <NUM_LIT:2> ** ( pack_bits ( self . flags [ : <NUM_LIT:3> ] ) + <NUM_LIT:1> ) <EOL> if self . local_color_table_flag : <EOL> if Debug : <EOL> print ( '<STR_LIT>' ) <EOL> self . palette = self . parent . get_color_table ( <EOL> self . local_color_table_size * <NUM_LIT:3> ) <EOL> def get_header ( self ) : <EOL> '''<STR_LIT>''' <EOL> flags = [ False for x in range ( <NUM_LIT:8> ) ] <EOL> flags [ <NUM_LIT:7> ] = self . local_color_table_flag <EOL> flags [ <NUM_LIT:6> ] = self . interlace_flag <EOL> flags [ <NUM_LIT:5> ] = self . sort_flag <EOL> flags [ <NUM_LIT:2> ] , flags [ <NUM_LIT:1> ] , flags [ <NUM_LIT:0> ] = get_bits ( len ( self . palette ) , bits = <NUM_LIT:3> ) <EOL> return ( self . left , self . top , self . width , self . height , pack_bits ( flags ) ) <EOL> header = property ( fget = get_header ) <EOL> class GifDecoder ( Gif ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , data , debug = False ) : <EOL> Gif . __init__ ( self , data , debug ) <EOL> self . fill ( ) <EOL> def fill ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . pointer = <NUM_LIT:0> <EOL> data = self . pops ( Gif . FMT_HEADER , self . data ) <EOL> self . header = data [ <NUM_LIT:0> ] <EOL> self . ls_width = data [ <NUM_LIT:1> ] <EOL> self . ls_height = data [ <NUM_LIT:2> ] <EOL> self . background_color = data [ <NUM_LIT:4> ] <EOL> self . aspect_ratio = data [ <NUM_LIT:5> ] <EOL> self . flags = get_bits ( data [ <NUM_LIT:3> ] ) <EOL> self . color_table_flag = self . flags [ <NUM_LIT:7> ] <EOL> self . sort_flag = self . flags [ <NUM_LIT:3> ] <EOL> self . color_resolution = pack_bits ( self . flags [ <NUM_LIT:4> : <NUM_LIT:7> ] ) <EOL> self . global_color_table_size = <NUM_LIT:2> ** ( pack_bits ( self . flags [ : <NUM_LIT:3> ] ) + <NUM_LIT:1> ) <EOL> if self . color_table_flag : <EOL> size = ( self . global_color_table_size ) * <NUM_LIT:3> <EOL> self . palette = self . get_color_table ( size ) <EOL> else : <EOL> self . palette = [ ( x , x , x ) for x in range ( <NUM_LIT> ) ] <EOL> image = None <EOL> self_data = self . data <EOL> self_pops = self . pops <EOL> Gif_IMAGE_SEPARATOR = Gif . IMAGE_SEPARATOR <EOL> Gif_FMT_IMGDESC = Gif . FMT_IMGDESC <EOL> self_new_image = self . new_image <EOL> self_pop = self . pop <EOL> self_debug_enabled = self . debug_enabled <EOL> self_lzw_decode = self . lzw_decode <EOL> Gif_EXTENSION_INTRODUCER = Gif . EXTENSION_INTRODUCER <EOL> Gif_GIF_TRAILER = Gif . GIF_TRAILER <EOL> Gif_LABEL_GRAPHIC_CONTROL = Gif . LABEL_GRAPHIC_CONTROL <EOL> trans_color = <NUM_LIT:0> <EOL> has_transparent_color = <NUM_LIT:0> <EOL> drw_method = '<STR_LIT:replace>' <EOL> while True : <EOL> try : <EOL> nextbyte = self_pops ( '<STR_LIT>' , self_data ) [ <NUM_LIT:0> ] <EOL> except : <EOL> nextbyte = <NUM_LIT> <EOL> if nextbyte == Gif_IMAGE_SEPARATOR : <EOL> descriptor = self_pops ( Gif_FMT_IMGDESC , self_data ) <EOL> image = self_new_image ( descriptor ) <EOL> image . transparent_color = trans_color <EOL> image . has_transparent_color = has_transparent_color <EOL> image . draw_method = drw_method <EOL> image . codesize = self_pops ( '<STR_LIT>' , self_data ) [ <NUM_LIT:0> ] <EOL> image . lzwcode = b'<STR_LIT>' <EOL> image_lzwcode = image . lzwcode <EOL> table_size = image . local_color_table_size if image . local_color_table_flag and self . global_color_table_size < image . local_color_table_size else self . global_color_table_size <EOL> while True : <EOL> try : <EOL> blocksize = self_pops ( '<STR_LIT>' , self_data ) [ <NUM_LIT:0> ] <EOL> except : <EOL> break <EOL> if blocksize == <NUM_LIT:0> : <EOL> break <EOL> lzwdata = self_pop ( self_data , blocksize ) <EOL> image_lzwcode = b'<STR_LIT>' . join ( ( image_lzwcode , lzwdata ) ) <EOL> if self_debug_enabled : <EOL> print ( '<STR_LIT>' , len ( image_lzwcode ) ) <EOL> image . lzwcode = image_lzwcode <EOL> image . pixels = self_lzw_decode ( image . lzwcode , image . codesize , <EOL> table_size ) <EOL> elif nextbyte == Gif_EXTENSION_INTRODUCER : <EOL> pass <EOL> elif nextbyte == Gif_GIF_TRAILER : <EOL> return <EOL> elif nextbyte == Gif_LABEL_GRAPHIC_CONTROL : <EOL> nextbyte = self_pops ( '<STR_LIT>' , self_data ) [ <NUM_LIT:0> ] <EOL> drw_bits = ( get_bits ( self_pops ( '<STR_LIT>' , self_data ) [ <NUM_LIT:0> ] ) ) <EOL> has_transparent_color = drw_bits [ <NUM_LIT:0> ] <EOL> if drw_bits [ <NUM_LIT:2> : <NUM_LIT:5> ] == array ( '<STR_LIT:B>' , [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> ] ) : <EOL> drw_method = '<STR_LIT:replace>' <EOL> elif ( drw_bits [ <NUM_LIT:2> : <NUM_LIT:5> ] ) == array ( '<STR_LIT:B>' , [ <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:0> ] ) : <EOL> drw_method = '<STR_LIT>' <EOL> else : <EOL> drw_method = '<STR_LIT>' <EOL> nextbyte = self_pops ( '<STR_LIT>' , self_data ) [ <NUM_LIT:0> ] <EOL> nextbyte = self_pops ( '<STR_LIT>' , self_data ) [ <NUM_LIT:0> ] <EOL> nextbyte = self_pops ( '<STR_LIT>' , self_data ) [ <NUM_LIT:0> ] <EOL> trans_color = nextbyte <EOL> pass <EOL> else : <EOL> pass <EOL> def string_to_bits ( self , string ) : <EOL> '''<STR_LIT>''' <EOL> ordarray = array ( '<STR_LIT:B>' , string ) <EOL> bits = array ( '<STR_LIT:B>' ) <EOL> bits_append = bits . append <EOL> _get_bits = get_bits <EOL> for byte in ordarray : <EOL> list ( map ( bits_append , _get_bits ( byte ) ) ) <EOL> return bits <EOL> def readable ( bool_list ) : <EOL> '''<STR_LIT>''' <EOL> return [ int ( x ) for x in bool_list ] <EOL> def bits_to_int ( self , bits ) : <EOL> '''<STR_LIT>''' <EOL> c = <NUM_LIT:1> <EOL> i = <NUM_LIT:0> <EOL> for bit in bits : <EOL> if bit : <EOL> i += <NUM_LIT:2> ** ( c - <NUM_LIT:1> ) <EOL> c += <NUM_LIT:1> <EOL> return i <EOL> def get_color_table ( self , size ) : <EOL> '''<STR_LIT>''' <EOL> raw_color_table = self . pops ( "<STR_LIT>" % size , self . data ) <EOL> pos = <NUM_LIT:0> <EOL> palette = [ ] <EOL> palette_append = palette . append <EOL> while pos + <NUM_LIT:3> < ( size + <NUM_LIT:1> ) : <EOL> red = raw_color_table [ pos ] <EOL> green = raw_color_table [ pos + <NUM_LIT:1> ] <EOL> blue = raw_color_table [ pos + <NUM_LIT:2> ] <EOL> palette_append ( ( red , green , blue ) ) <EOL> pos += <NUM_LIT:3> <EOL> return palette <EOL> def lzw_decode ( self , input , initial_codesize , color_table_size ) : <EOL> '''<STR_LIT>''' <EOL> string_table = { } <EOL> output = array ( '<STR_LIT:B>' ) <EOL> output_append = output . append <EOL> output_extend = output . extend <EOL> old = '<STR_LIT>' <EOL> index = <NUM_LIT:0> <EOL> bits = self . string_to_bits ( input ) <EOL> self . bitpointer = <NUM_LIT:0> <EOL> codesize = initial_codesize + <NUM_LIT:1> <EOL> clearcode , end_of_info = color_table_size , color_table_size + <NUM_LIT:1> <EOL> if Debug : <EOL> print ( '<STR_LIT>' % codesize ) <EOL> print ( '<STR_LIT>' % ( clearcode , end_of_info ) ) <EOL> def pop ( size , _bits ) : <EOL> '''<STR_LIT>''' <EOL> start = self . bitpointer <EOL> end = self . bitpointer = start + size <EOL> return _bits [ start : end ] <EOL> def clear ( ) : <EOL> '''<STR_LIT>''' <EOL> string_table . clear ( ) <EOL> for index in range ( color_table_size ) : <EOL> string_table [ index ] = chr ( index ) <EOL> index = end_of_info + <NUM_LIT:1> <EOL> return index <EOL> index = clear ( ) <EOL> bits = bits [ codesize : ] <EOL> self_bits_to_int = self . bits_to_int <EOL> code = self_bits_to_int ( pop ( codesize , bits ) ) <EOL> if code in string_table : <EOL> output_append ( ord ( string_table [ code ] ) ) <EOL> else : <EOL> Logger . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( code , codesize ) ) <EOL> string_table [ code ] = string_table [ <NUM_LIT:0> ] <EOL> output_append ( ord ( string_table [ code ] ) ) <EOL> old = string_table [ code ] <EOL> bitlen = len ( bits ) <EOL> while self . bitpointer < bitlen : <EOL> code = self_bits_to_int ( pop ( codesize , bits ) ) <EOL> if code == clearcode : <EOL> index = clear ( ) <EOL> codesize = initial_codesize + <NUM_LIT:1> <EOL> code = self_bits_to_int ( pop ( codesize , bits ) ) <EOL> if code in string_table : <EOL> output_append ( ord ( string_table [ code ] ) ) <EOL> else : <EOL> Logger . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( code , codesize ) ) <EOL> string_table [ code ] = string_table [ <NUM_LIT:0> ] <EOL> output_append ( ord ( string_table [ code ] ) ) <EOL> old = string_table [ code ] <EOL> continue <EOL> elif code == end_of_info : <EOL> break <EOL> if code in string_table : <EOL> c = string_table [ code ] <EOL> string_table [ index ] = '<STR_LIT>' . join ( ( old , c [ <NUM_LIT:0> ] ) ) <EOL> else : <EOL> c = '<STR_LIT>' . join ( ( old , old [ <NUM_LIT:0> ] ) ) <EOL> string_table [ code ] = c <EOL> index += <NUM_LIT:1> <EOL> old = c <EOL> output_extend ( list ( map ( ord , c ) ) ) <EOL> if index == <NUM_LIT:2> ** codesize : <EOL> codesize += <NUM_LIT:1> <EOL> if codesize == <NUM_LIT> : <EOL> codesize = <NUM_LIT:12> <EOL> if self . debug_enabled : <EOL> print ( '<STR_LIT>' % len ( output ) ) <EOL> return output <EOL> def get_bits ( flags , reverse = False , bits = <NUM_LIT:8> ) : <EOL> '''<STR_LIT>''' <EOL> mybits = ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:4> , <NUM_LIT:8> , <NUM_LIT:16> , <NUM_LIT:32> , <NUM_LIT:64> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) [ : bits ] <EOL> rev_num = <NUM_LIT:1> <EOL> if reverse : <EOL> rev_num = - <NUM_LIT:1> <EOL> ret = array ( '<STR_LIT:B>' ) <EOL> ret_append = ret . append <EOL> for bit in mybits [ : : rev_num ] : <EOL> ret_append ( flags & bit != <NUM_LIT:0> ) <EOL> return ret <EOL> def pack_bits ( bits ) : <EOL> '''<STR_LIT>''' <EOL> packed = <NUM_LIT:0> <EOL> level = <NUM_LIT:0> <EOL> for bit in bits : <EOL> if bit : <EOL> packed += <NUM_LIT:2> ** level <EOL> level += <NUM_LIT:1> <EOL> return packed <EOL> ImageLoader . register ( ImageLoaderGIF ) </s>
