2015-06-28 23:50:50 -07:00
|
|
|
"""Supplementary scripts for graphics conversion."""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import argparse
|
|
|
|
|
2015-07-11 10:34:26 -07:00
|
|
|
from extras.pokemontools import gfx, lz
|
2015-06-28 23:50:50 -07:00
|
|
|
|
|
|
|
|
|
|
|
# Graphics with inverted tilemaps that aren't covered by filepath_rules.
|
|
|
|
pics = [
|
|
|
|
'gfx/shrink1',
|
|
|
|
'gfx/shrink2',
|
|
|
|
]
|
|
|
|
|
2016-02-17 13:15:18 -08:00
|
|
|
def recursive_read(filename):
|
|
|
|
def recurse(filename_):
|
|
|
|
lines = []
|
|
|
|
for line in open(filename_):
|
|
|
|
if 'include "' in line.lower():
|
|
|
|
lines += recurse(line.split('"')[1])
|
|
|
|
else:
|
|
|
|
lines += [line]
|
|
|
|
return lines
|
|
|
|
lines = recurse(filename)
|
|
|
|
return ''.join(lines)
|
|
|
|
|
2015-11-01 15:06:15 -08:00
|
|
|
base_stats = None
|
2015-08-30 11:06:37 -07:00
|
|
|
def get_base_stats():
|
2015-11-01 15:06:15 -08:00
|
|
|
global base_stats
|
|
|
|
if not base_stats:
|
2016-02-17 13:15:18 -08:00
|
|
|
base_stats = recursive_read('data/base_stats.asm')
|
2015-11-01 15:06:15 -08:00
|
|
|
return base_stats
|
2015-08-30 11:06:37 -07:00
|
|
|
|
|
|
|
def get_pokemon_dimensions(name):
|
2016-02-17 14:12:51 -08:00
|
|
|
try:
|
|
|
|
if name == 'egg':
|
|
|
|
return 5, 5
|
|
|
|
if name == 'questionmark':
|
|
|
|
return 7, 7
|
|
|
|
if name.startswith('unown_'):
|
|
|
|
name = 'unown'
|
|
|
|
base_stats = get_base_stats()
|
|
|
|
start = base_stats.find('\tdb ' + name.upper())
|
|
|
|
start = base_stats.find('\tdn ', start)
|
|
|
|
end = base_stats.find('\n', start)
|
|
|
|
line = base_stats[start:end].replace(',', ' ')
|
|
|
|
w, h = map(int, line.split()[1:3])
|
|
|
|
return w, h
|
|
|
|
except:
|
|
|
|
return 7, 7
|
2015-08-30 11:06:37 -07:00
|
|
|
|
2015-06-28 23:50:50 -07:00
|
|
|
def filepath_rules(filepath):
|
|
|
|
"""Infer attributes of certain graphics by their location in the filesystem."""
|
|
|
|
args = {}
|
|
|
|
|
|
|
|
filedir, filename = os.path.split(filepath)
|
2016-02-17 13:15:18 -08:00
|
|
|
if filedir.startswith('./'):
|
|
|
|
filedir = filedir[2:]
|
|
|
|
|
2015-07-11 10:34:26 -07:00
|
|
|
name, ext = os.path.splitext(filename)
|
2016-02-17 15:08:11 -08:00
|
|
|
if ext == '.lz':
|
|
|
|
name, ext = os.path.splitext(name)
|
2015-06-28 23:50:50 -07:00
|
|
|
|
2015-08-30 11:06:37 -07:00
|
|
|
pokemon_name = ''
|
|
|
|
|
2015-06-28 23:50:50 -07:00
|
|
|
if 'gfx/pics/' in filedir:
|
2016-02-17 13:15:18 -08:00
|
|
|
pokemon_name = filedir.split('/')[-1]
|
2015-08-30 11:06:37 -07:00
|
|
|
if pokemon_name.startswith('unown_'):
|
|
|
|
index = filedir.find(pokemon_name)
|
2015-08-29 10:54:28 -07:00
|
|
|
if index != -1:
|
|
|
|
filedir = filedir[:index + len('unown')] + filedir[index + len('unown_a'):]
|
2015-06-28 23:50:50 -07:00
|
|
|
if name == 'front':
|
|
|
|
args['pal_file'] = os.path.join(filedir, 'normal.pal')
|
|
|
|
args['pic'] = True
|
|
|
|
args['animate'] = True
|
|
|
|
elif name == 'back':
|
|
|
|
args['pal_file'] = os.path.join(filedir, 'shiny.pal')
|
|
|
|
args['pic'] = True
|
|
|
|
|
|
|
|
elif 'gfx/trainers' in filedir:
|
|
|
|
args['pic'] = True
|
|
|
|
|
|
|
|
elif os.path.join(filedir, name) in pics:
|
|
|
|
args['pic'] = True
|
|
|
|
|
|
|
|
if args.get('pal_file'):
|
2015-08-29 10:54:28 -07:00
|
|
|
if os.path.exists(args['pal_file']):
|
|
|
|
args['palout'] = args['pal_file']
|
|
|
|
else:
|
|
|
|
del args['pal_file']
|
2015-06-28 23:50:50 -07:00
|
|
|
|
|
|
|
if args.get('pic'):
|
2015-07-11 10:34:26 -07:00
|
|
|
if ext == '.png':
|
2015-06-28 23:50:50 -07:00
|
|
|
w, h = gfx.png.Reader(filepath).asRGBA8()[:2]
|
|
|
|
w = min(w/8, h/8)
|
|
|
|
args['pic_dimensions'] = w, w
|
2015-08-30 11:06:37 -07:00
|
|
|
elif ext == '.2bpp':
|
2015-08-30 19:11:31 -07:00
|
|
|
if pokemon_name and name == 'front':
|
2015-08-30 11:06:37 -07:00
|
|
|
w, h = get_pokemon_dimensions(pokemon_name)
|
|
|
|
args['pic_dimensions'] = w, w
|
2015-08-30 19:11:31 -07:00
|
|
|
elif pokemon_name and name == 'back':
|
|
|
|
args['pic_dimensions'] = 6, 6
|
2015-08-30 11:06:37 -07:00
|
|
|
else:
|
|
|
|
args['pic_dimensions'] = 7, 7
|
2015-06-28 23:50:50 -07:00
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
def to_1bpp(filename, **kwargs):
|
2016-02-17 15:08:11 -08:00
|
|
|
name, ext = os.path.splitext(filename)
|
2015-06-28 23:50:50 -07:00
|
|
|
if ext == '.1bpp': pass
|
|
|
|
elif ext == '.2bpp': gfx.export_2bpp_to_1bpp(filename, **kwargs)
|
|
|
|
elif ext == '.png': gfx.export_png_to_1bpp(filename, **kwargs)
|
2016-02-17 15:08:11 -08:00
|
|
|
elif ext == '.lz':
|
|
|
|
decompress(filename, **kwargs)
|
|
|
|
to_1bpp(name, **kwargs)
|
2015-06-28 23:50:50 -07:00
|
|
|
|
|
|
|
def to_2bpp(filename, **kwargs):
|
2016-02-17 15:08:11 -08:00
|
|
|
name, ext = os.path.splitext(filename)
|
2015-06-28 23:50:50 -07:00
|
|
|
if ext == '.1bpp': gfx.export_1bpp_to_2bpp(filename, **kwargs)
|
|
|
|
elif ext == '.2bpp': pass
|
|
|
|
elif ext == '.png': gfx.export_png_to_2bpp(filename, **kwargs)
|
2016-02-17 15:08:11 -08:00
|
|
|
elif ext == '.lz':
|
|
|
|
decompress(filename, **kwargs)
|
|
|
|
to_2bpp(name, **kwargs)
|
2015-06-28 23:50:50 -07:00
|
|
|
|
|
|
|
def to_png(filename, **kwargs):
|
2016-02-17 15:08:11 -08:00
|
|
|
name, ext = os.path.splitext(filename)
|
2015-06-28 23:50:50 -07:00
|
|
|
if ext == '.1bpp': gfx.export_1bpp_to_png(filename, **kwargs)
|
|
|
|
elif ext == '.2bpp': gfx.export_2bpp_to_png(filename, **kwargs)
|
|
|
|
elif ext == '.png': pass
|
2016-02-17 15:08:11 -08:00
|
|
|
elif ext == '.lz':
|
|
|
|
decompress(filename, **kwargs)
|
|
|
|
to_png(name, **kwargs)
|
2015-06-28 23:50:50 -07:00
|
|
|
|
|
|
|
def compress(filename, **kwargs):
|
|
|
|
data = open(filename, 'rb').read()
|
2015-07-11 10:34:26 -07:00
|
|
|
lz_data = lz.Compressed(data).output
|
2015-06-28 23:50:50 -07:00
|
|
|
open(filename + '.lz', 'wb').write(bytearray(lz_data))
|
|
|
|
|
|
|
|
def decompress(filename, **kwargs):
|
|
|
|
lz_data = open(filename, 'rb').read()
|
2015-07-11 10:34:26 -07:00
|
|
|
data = lz.Decompressed(lz_data).output
|
2015-06-28 23:50:50 -07:00
|
|
|
name, ext = os.path.splitext(filename)
|
|
|
|
open(name, 'wb').write(bytearray(data))
|
|
|
|
|
|
|
|
|
|
|
|
methods = {
|
|
|
|
'2bpp': to_2bpp,
|
|
|
|
'1bpp': to_1bpp,
|
|
|
|
'png': to_png,
|
|
|
|
'lz': compress,
|
|
|
|
'unlz': decompress,
|
|
|
|
}
|
|
|
|
|
|
|
|
def main(method_name, filenames=None):
|
|
|
|
if filenames is None: filenames = []
|
|
|
|
for filename in filenames:
|
|
|
|
args = filepath_rules(filename)
|
|
|
|
method = methods.get(method_name)
|
2015-07-11 10:34:26 -07:00
|
|
|
if method:
|
|
|
|
method(filename, **args)
|
2015-06-28 23:50:50 -07:00
|
|
|
|
|
|
|
def get_args():
|
|
|
|
ap = argparse.ArgumentParser()
|
|
|
|
ap.add_argument('method_name')
|
|
|
|
ap.add_argument('filenames', nargs='*')
|
|
|
|
args = ap.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(**get_args().__dict__)
|