<s> import sys , os , argparse , glob , re , tempfile , shutil <EOL> from klei import textureconverter , atlas , imgutil , util <EOL> from PIL import Image <EOL> from properties import ImageProperties <EOL> def is_power2 ( num ) : <EOL> return num != <NUM_LIT:0> and ( ( num & ( num - <NUM_LIT:1> ) ) == <NUM_LIT:0> ) <EOL> def ExportImage ( filename ) : <EOL> path , base_name = os . path . split ( filename ) <EOL> base_name , ext = os . path . splitext ( base_name ) <EOL> path = os . path . abspath ( path ) <EOL> dest_filename = os . path . join ( path , base_name + "<STR_LIT>" ) <EOL> if util . IsAnyFileNewer ( filename , dest_filename ) or args . force : <EOL> im = Image . open ( filename ) <EOL> sz = im . size <EOL> print ( "<STR_LIT>" + filename + "<STR_LIT>" + dest_filename ) <EOL> assert sz [ <NUM_LIT:0> ] % <NUM_LIT:4> == <NUM_LIT:0> and sz [ <NUM_LIT:1> ] % <NUM_LIT:4> == <NUM_LIT:0> , "<STR_LIT>" + filename + "<STR_LIT>" <EOL> skipmip = not is_power2 ( sz [ <NUM_LIT:0> ] ) or not is_power2 ( sz [ <NUM_LIT:1> ] ) <EOL> textureconverter . Convert ( src_filenames = filename , dest_filename = dest_filename , <EOL> force = args . force , texture_format = args . textureformat , platform = args . platform , <EOL> generate_mips = not skipmip , no_premultiply = im . mode == '<STR_LIT>' , ignore_exceptions = args . ignoreexceptions ) <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , type = str , nargs = '<STR_LIT:*>' ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' ) <EOL> parser . add_argument ( '<STR_LIT>' , default = '<STR_LIT:data>' ) <EOL> args = parser . parse_args ( ) <EOL> in_images = args . images <EOL> ROOT = os . path . abspath ( "<STR_LIT>" ) <EOL> class BBox : <EOL> x = <NUM_LIT:0> <EOL> y = <NUM_LIT:0> <EOL> w = <NUM_LIT:1> <EOL> h = <NUM_LIT:1> <EOL> def process_file_to_atlas ( ) : <EOL> for image in in_images : <EOL> path , base_name = os . path . split ( image ) <EOL> image_base_name , image_ext = os . path . splitext ( base_name ) <EOL> dest_filename = os . path . join ( path , image_base_name + "<STR_LIT>" ) <EOL> xml_filename = os . path . join ( path , image_base_name + "<STR_LIT>" ) <EOL> working_dir = os . path . join ( tempfile . gettempdir ( ) , image_base_name ) <EOL> try : <EOL> if os . path . exists ( working_dir ) : <EOL> shutil . rmtree ( working_dir ) <EOL> os . makedirs ( working_dir ) <EOL> border_size = <NUM_LIT:0> <EOL> images = [ ] <EOL> im = imgutil . OpenImage ( image , size = None , border_size = border_size ) <EOL> im . name = image_base_name + "<STR_LIT>" <EOL> images . append ( im ) <EOL> sz = im . size <EOL> npot = not is_power2 ( sz [ <NUM_LIT:0> ] ) or not is_power2 ( sz [ <NUM_LIT:1> ] ) <EOL> textureformat = '<STR_LIT>' <EOL> sys . stderr . write ( "<STR_LIT>" + image + "<STR_LIT>" + str ( npot ) + "<STR_LIT:\n>" ) <EOL> atlas_data = atlas . Atlas ( images , image_base_name , minimize_num_textures = True , ignore_exceptions = args . ignoreexceptions , force_square = args . square ) <EOL> assert len ( atlas_data ) == <NUM_LIT:1> <EOL> page = atlas_data [ <NUM_LIT:0> ] <EOL> mip_filenames = [ ] <EOL> for mip_idx in range ( len ( page . mips ) ) : <EOL> mip = page . mips [ mip_idx ] <EOL> mip_filename = mip . name + "<STR_LIT>" . format ( mip_idx ) <EOL> full_mip_filename = os . path . join ( working_dir , mip_filename ) <EOL> mip_filenames . append ( full_mip_filename ) <EOL> mip . im . save ( full_mip_filename ) <EOL> textureconverter . Convert ( <EOL> src_filenames = mip_filenames , dest_filename = dest_filename , <EOL> platform = args . platform , texture_format = textureformat , force = args . force , <EOL> ignore_exceptions = args . ignoreexceptions , generate_mips = True ) <EOL> tex_filename = os . path . relpath ( dest_filename , ROOT ) . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) <EOL> xml = atlas . GenerateXMLTree ( tex_filename , page . mips [ <NUM_LIT:0> ] . im . size , page . bboxes , border_size + <NUM_LIT:0.5> ) <EOL> xml . write ( xml_filename ) <EOL> finally : <EOL> if os . path . exists ( working_dir ) : <EOL> shutil . rmtree ( working_dir ) <EOL> process_file_to_atlas ( ) <EOL> def process_dirs_to_atlas ( ) : <EOL> for dirname in [ d for d in os . listdir ( '<STR_LIT:.>' ) if os . path . isdir ( d ) and not d in excluded_dirs ] : <EOL> filenames = glob . glob ( os . path . join ( dirname , "<STR_LIT>" ) ) <EOL> dest_filename = os . path . join ( out_path , dirname + "<STR_LIT>" ) <EOL> xml_filename = os . path . join ( out_path , dirname + "<STR_LIT>" ) <EOL> if util . IsAnyFileNewer ( filenames , [ dest_filename , xml_filename ] ) or args . force : <EOL> print ( "<STR_LIT>" + dirname ) <EOL> working_dir = os . path . join ( tempfile . gettempdir ( ) , dirname ) <EOL> try : <EOL> if os . path . exists ( working_dir ) : <EOL> shutil . rmtree ( working_dir ) <EOL> os . makedirs ( working_dir ) <EOL> size = None <EOL> border_size = <NUM_LIT:0> <EOL> if dirname in ImageProperties : <EOL> width = ImageProperties [ dirname ] . get ( "<STR_LIT:width>" , None ) <EOL> height = ImageProperties [ dirname ] . get ( "<STR_LIT>" , None ) <EOL> border_size = ImageProperties [ dirname ] . get ( "<STR_LIT>" , <NUM_LIT:0> ) <EOL> if width != None and height != None : <EOL> size = ( width , height ) <EOL> images = [ ] <EOL> for filename in filenames : <EOL> im = imgutil . OpenImage ( filename , size = size , border_size = border_size ) <EOL> filename , ext = os . path . splitext ( os . path . basename ( filename ) ) <EOL> im . name = filename + "<STR_LIT>" <EOL> images . append ( im ) <EOL> atlas_data = atlas . Atlas ( images , dirname , minimize_num_textures = True , ignore_exceptions = args . ignoreexceptions , force_square = args . square ) <EOL> assert len ( atlas_data ) == <NUM_LIT:1> <EOL> page = atlas_data [ <NUM_LIT:0> ] <EOL> mip_filenames = [ ] <EOL> for mip_idx in range ( len ( page . mips ) ) : <EOL> mip = page . mips [ mip_idx ] <EOL> mip_filename = mip . name + "<STR_LIT>" . format ( mip_idx ) <EOL> full_mip_filename = os . path . join ( working_dir , mip_filename ) <EOL> mip_filenames . append ( full_mip_filename ) <EOL> mip . im . save ( full_mip_filename ) <EOL> textureconverter . Convert ( <EOL> src_filenames = mip_filenames , dest_filename = dest_filename , <EOL> platform = args . platform , texture_format = args . textureformat , force = args . force , <EOL> ignore_exceptions = args . ignoreexceptions ) <EOL> tex_filename = os . path . relpath ( dest_filename , ROOT ) . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) <EOL> xml = atlas . GenerateXMLTree ( tex_filename , page . mips [ <NUM_LIT:0> ] . im . size , page . bboxes , border_size + <NUM_LIT:0.5> ) <EOL> xml . write ( xml_filename ) <EOL> finally : <EOL> if os . path . exists ( working_dir ) : <EOL> shutil . rmtree ( working_dir ) </s>
