diff options
author | yenatch <yenatch@gmail.com> | 2014-03-23 18:40:34 -0400 |
---|---|---|
committer | yenatch <yenatch@gmail.com> | 2014-03-23 19:30:25 -0400 |
commit | 12d8bb11e82ba44ee6a04a52497381833a7b5924 (patch) | |
tree | 87afe3c0b8cba123132812e7988d2783e1aa8428 /pokemontools/gfx.py | |
parent | 1129d71e2ccd3bfe4d635cf7d280a300c0338b09 (diff) |
gfx: Read image metadata from filenames where applicable.
png has some incompatibilities with 2bpp. Among them:
- 2bpp is tiled, and can be arranged arbitrarily
- 2bpp does not have associated palette data
- png must be rectangular
- png must have specific dimensions
The requirements for an image vary, and are unwritten. One conversion method will not work for all images.
To resolve these conflicts metadata can be attached to images and related data.
So as to not create additional files without changing the file content, the filename is used.
Usage: metadata is defined as pseudoextensions.
.w<width>
- force width to <width> px
.h<height>
- force height to <height> px
.t<num>
- pad the image by <num> tiles so the resulting png can be rectangular
- remove said tiles when converting back to 2bpp
Example:
gfx/title/logo.w160.t4.{lz,2bpp,png}
- width 160px
- pad by 4 tiles
Diffstat (limited to 'pokemontools/gfx.py')
-rw-r--r-- | pokemontools/gfx.py | 70 |
1 files changed, 62 insertions, 8 deletions
diff --git a/pokemontools/gfx.py b/pokemontools/gfx.py index 04ccac5..3db8818 100644 --- a/pokemontools/gfx.py +++ b/pokemontools/gfx.py @@ -1220,27 +1220,72 @@ def png_to_rgb(palette): return output +def read_filename_arguments(filename): + int_args = { + 'w': 'width', + 'h': 'height', + 't': 'tile_padding', + } + parsed_arguments = {} + arguments = os.path.splitext(filename)[0].split('.')[1:] + for argument in arguments: + arg = argument[0] + param = argument[1:] + if param.isdigit(): + arg = int_args.get(arg, False) + if arg: + parsed_arguments[arg] = int(param) + return parsed_arguments + + +def export_2bpp_to_png(filein, fileout=None, pal_file=None, height=0, width=0, tile_padding=0): -def export_2bpp_to_png(filein, fileout=None, pal_file=None, height=0, width=0): if fileout == None: fileout = os.path.splitext(filein)[0] + '.png' + image = open(filein, 'rb').read() + arguments = { + 'width': width, + 'height': height, + 'pal_file': pal_file, + 'tile_padding': tile_padding, + } + arguments.update(read_filename_arguments(filein)) + if pal_file == None: if os.path.exists(os.path.splitext(fileout)[0]+'.pal'): pal_file = os.path.splitext(fileout)[0]+'.pal' - width, height, palette, greyscale, bitdepth, px_map = convert_2bpp_to_png(image, width=width, height=height, pal_file=pal_file) - - w = png.Writer(width, height, palette=palette, compression=9, greyscale=greyscale, bitdepth=bitdepth) + result = convert_2bpp_to_png( + image, + width=arguments['width'], + height=arguments['height'], + pal_file=arguments['pal_file'], + tile_padding=arguments['tile_padding'] + ) + width, height, palette, greyscale, bitdepth, px_map = result + + w = png.Writer( + width, + height, + palette=palette, + compression=9, + greyscale=greyscale, + bitdepth=bitdepth + ) with open(fileout, 'wb') as f: w.write(f, px_map) -def convert_2bpp_to_png(image, width=0, height=0, pal_file=None): +def convert_2bpp_to_png(image, width=0, height=0, pal_file=None, tile_padding=0): """ Convert a planar 2bpp graphic to png. """ + + # Pad the image by a given number of tiles if asked. + image += chr(0) * 0x10 * tile_padding + num_pixels = len(image) * 4 assert num_pixels > 0, 'empty image!' @@ -1283,8 +1328,14 @@ def convert_2bpp_to_png(image, width=0, height=0, pal_file=None): return width, height, palette, greyscale, bitdepth, px_map -def export_png_to_2bpp(filein, fileout=None, palout=None): - image, palette = png_to_2bpp(filein) +def export_png_to_2bpp(filein, fileout=None, palout=None, tile_padding=0): + + arguments = { + 'tile_padding': tile_padding, + } + arguments.update(read_filename_arguments(filein)) + + image, palette = png_to_2bpp(filein, arguments['tile_padding']) if fileout == None: fileout = os.path.splitext(filein)[0] + '.2bpp' @@ -1317,7 +1368,7 @@ def get_image_padding(width, height, wstep=8, hstep=8): return padding -def png_to_2bpp(filein): +def png_to_2bpp(filein, tile_padding=0): """ Convert a png image to planar 2bpp. """ @@ -1408,6 +1459,9 @@ def png_to_2bpp(filein): top += (quad /2 & 1) << (7 - bit) image += [bottom, top] + # Remove any tile padding used to make the png rectangular. + image = image[:-tile_padding * 0x10] + return image, palette |