<s> '''<STR_LIT>''' <EOL> import json <EOL> from struct import pack <EOL> from pprint import pprint <EOL> from subprocess import Popen <EOL> from PIL import Image <EOL> from argparse import ArgumentParser <EOL> from sys import exit <EOL> from os . path import join , exists , dirname , basename <EOL> from os import environ , unlink <EOL> class Tool ( object ) : <EOL> def __init__ ( self , options ) : <EOL> super ( Tool , self ) . __init__ ( ) <EOL> self . options = options <EOL> self . source_fn = options . image <EOL> self . dest_dir = options . dir or dirname ( options . image ) <EOL> @ property <EOL> def tex_fn ( self ) : <EOL> fn = basename ( self . source_fn ) . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] + '<STR_LIT>' <EOL> return join ( self . dest_dir , fn ) <EOL> def compress ( self ) : <EOL> pass <EOL> def nearest_pow2 ( self , v ) : <EOL> v -= <NUM_LIT:1> <EOL> v |= v >> <NUM_LIT:1> <EOL> v |= v >> <NUM_LIT:2> <EOL> v |= v >> <NUM_LIT:4> <EOL> v |= v >> <NUM_LIT:8> <EOL> v |= v >> <NUM_LIT:16> <EOL> return v + <NUM_LIT:1> <EOL> def runcmd ( self , cmd ) : <EOL> print ( '<STR_LIT>' . format ( '<STR_LIT:U+0020>' . join ( cmd ) ) ) <EOL> Popen ( cmd ) . communicate ( ) <EOL> def write_tex ( self , data , fmt , image_size , texture_size , mipmap = False , <EOL> formatinfo = None ) : <EOL> infos = { <EOL> '<STR_LIT>' : len ( data ) , <EOL> '<STR_LIT>' : image_size , <EOL> '<STR_LIT>' : texture_size , <EOL> '<STR_LIT>' : mipmap , <EOL> '<STR_LIT>' : fmt } <EOL> if formatinfo : <EOL> infos [ '<STR_LIT>' ] = formatinfo <EOL> header = json . dumps ( infos , indent = <NUM_LIT:0> , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT::>' ) ) <EOL> header = header . replace ( '<STR_LIT:\n>' , '<STR_LIT>' ) <EOL> with open ( self . tex_fn , '<STR_LIT:wb>' ) as fd : <EOL> fd . write ( '<STR_LIT>' ) <EOL> fd . write ( pack ( '<STR_LIT:I>' , len ( header ) ) ) <EOL> fd . write ( header ) <EOL> fd . write ( data ) <EOL> print ( '<STR_LIT>' . format ( self . tex_fn ) ) <EOL> pprint ( infos ) <EOL> @ staticmethod <EOL> def run ( ) : <EOL> parser = ArgumentParser ( <EOL> description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = bool , default = False , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , default = None , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , choices = [ '<STR_LIT>' , '<STR_LIT>' ] , <EOL> help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT:image>' , type = str , <EOL> help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> if args . format == '<STR_LIT>' : <EOL> PvrtcTool ( args ) . compress ( ) <EOL> elif args . format == '<STR_LIT>' : <EOL> Etc1Tool ( args ) . compress ( ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> class Etc1Tool ( Tool ) : <EOL> def __init__ ( self , options ) : <EOL> super ( Etc1Tool , self ) . __init__ ( options ) <EOL> self . etc1tool = None <EOL> self . locate_etc1tool ( ) <EOL> def locate_etc1tool ( self ) : <EOL> search_directories = [ environ . get ( '<STR_LIT>' , '<STR_LIT:/>' ) ] <EOL> search_directories += environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT::>' ) <EOL> for directory in search_directories : <EOL> fn = join ( directory , '<STR_LIT>' ) <EOL> if not exists ( fn ) : <EOL> fn = join ( directory , '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not exists ( fn ) : <EOL> continue <EOL> print ( '<STR_LIT>' . format ( directory ) ) <EOL> self . etc1tool = fn <EOL> return <EOL> if self . etc1tool is None : <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> def compress ( self ) : <EOL> image = Image . open ( self . source_fn ) <EOL> w , h = image . size <EOL> print ( '<STR_LIT>' . format ( * image . size ) ) <EOL> w2 = self . nearest_pow2 ( w ) <EOL> h2 = self . nearest_pow2 ( h ) <EOL> print ( '<STR_LIT>' . format ( w2 , h2 ) ) <EOL> raw_tex_fn = self . tex_fn + '<STR_LIT>' <EOL> cmd = [ self . etc1tool , self . source_fn , '<STR_LIT>' , '<STR_LIT>' , <EOL> raw_tex_fn ] <EOL> try : <EOL> self . runcmd ( cmd ) <EOL> with open ( raw_tex_fn , '<STR_LIT:rb>' ) as fd : <EOL> data = fd . read ( ) <EOL> finally : <EOL> if exists ( raw_tex_fn ) : <EOL> unlink ( raw_tex_fn ) <EOL> self . write_tex ( data , '<STR_LIT>' , ( w , h ) , ( w2 , h2 ) , self . options . mipmap ) <EOL> class PvrtcTool ( Tool ) : <EOL> def __init__ ( self , options ) : <EOL> super ( PvrtcTool , self ) . __init__ ( options ) <EOL> self . texturetool = None <EOL> self . locate_texturetool ( ) <EOL> def locate_texturetool ( self ) : <EOL> search_directories = [ <EOL> ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> '<STR_LIT>' ] <EOL> search_directories += environ . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT::>' ) <EOL> for directory in search_directories : <EOL> fn = join ( directory , '<STR_LIT>' ) <EOL> if not exists ( fn ) : <EOL> continue <EOL> print ( '<STR_LIT>' . format ( directory ) ) <EOL> self . texturetool = fn <EOL> return <EOL> print ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> exit ( <NUM_LIT:1> ) <EOL> def compress ( self ) : <EOL> image = Image . open ( self . source_fn ) <EOL> w , h = image . size <EOL> print ( '<STR_LIT>' . format ( * image . size ) ) <EOL> w2 = self . nearest_pow2 ( w ) <EOL> h2 = self . nearest_pow2 ( h ) <EOL> print ( '<STR_LIT>' . format ( w2 , h2 ) ) <EOL> s2 = max ( w2 , h2 ) <EOL> print ( '<STR_LIT>' . format ( s2 ) ) <EOL> ext = self . source_fn . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ - <NUM_LIT:1> ] <EOL> tmpfile = '<STR_LIT>' . format ( ext ) <EOL> image = image . resize ( ( s2 , s2 ) ) <EOL> image . save ( tmpfile ) <EOL> raw_tex_fn = self . tex_fn + '<STR_LIT>' <EOL> cmd = [ self . texturetool ] <EOL> if self . options . mipmap : <EOL> cmd += [ '<STR_LIT>' ] <EOL> cmd += [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , raw_tex_fn , '<STR_LIT>' , '<STR_LIT>' , tmpfile ] <EOL> try : <EOL> self . runcmd ( cmd ) <EOL> with open ( raw_tex_fn , '<STR_LIT:rb>' ) as fd : <EOL> data = fd . read ( ) <EOL> finally : <EOL> if exists ( raw_tex_fn ) : <EOL> unlink ( raw_tex_fn ) <EOL> self . write_tex ( data , '<STR_LIT>' , ( w , h ) , ( s2 , s2 ) , <EOL> self . options . mipmap ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> Tool . run ( ) </s>
