From cef3385c2bee0b098a7795548345a9281ace008e Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Wed, 26 Jul 2023 08:39:37 -0700 Subject: Add support for paletted sprites. --- gfx-iso/asset/mkasset.py | 127 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 90 insertions(+), 37 deletions(-) (limited to 'gfx-iso/asset/mkasset.py') diff --git a/gfx-iso/asset/mkasset.py b/gfx-iso/asset/mkasset.py index b4e335f..3ca8a1d 100644 --- a/gfx-iso/asset/mkasset.py +++ b/gfx-iso/asset/mkasset.py @@ -165,44 +165,57 @@ def get_num_cols(image, sprite_width): return 0 -def get_sprite_sheet_rows(input_filepath, sprite_width, sprite_height): +def get_sprite_sheet_rows(im, sprite_width, sprite_height): """Gets the individual rows of a sprite sheet. The input sprite sheet can have any number of rows. - Returns a list of lists [[sprite bytes]], one inner list for the columns in - each row. + Returns a list of lists [[sprite]], one inner list for the columns in each + row. """ - with Image.open(input_filepath) as im: - # Sprite sheet's width and height must be integer multiples of the - # sprite's width and height. - assert (im.width % sprite_width == 0) - assert (im.height % sprite_height == 0) + # Sprite sheet's width and height must be integer multiples of the + # sprite's width and height. + assert (im.width % sprite_width == 0) + assert (im.height % sprite_height == 0) - num_rows = im.height // sprite_height + num_rows = im.height // sprite_height - rows = [] - for row in range(num_rows): - # Get the number of columns. - upper = row * sprite_height - lower = (row + 1) * sprite_height - whole_row = im.crop((0, upper, im.width, lower)) - num_cols = get_num_cols(whole_row, sprite_width) - assert (num_cols > 0) - - # Crop the row into N columns. - cols = [] - for i in range(num_cols): - left = i * sprite_width - right = (i + 1) * sprite_width - sprite = im.crop((left, upper, right, lower)) - cols.append(sprite) - - sprite_bytes = [sprite.convert('RGBA').tobytes() for sprite in cols] - assert (len(sprite_bytes) == num_cols) - rows.append(sprite_bytes) - - return rows + rows = [] + for row in range(num_rows): + # Get the number of columns. + upper = row * sprite_height + lower = (row + 1) * sprite_height + whole_row = im.crop((0, upper, im.width, lower)) + num_cols = get_num_cols(whole_row, sprite_width) + assert (num_cols > 0) + + # Crop the row into N columns. + cols = [] + for i in range(num_cols): + left = i * sprite_width + right = (i + 1) * sprite_width + sprite = im.crop((left, upper, right, lower)) + cols.append(sprite) + + assert (len(cols) == num_cols) + rows.append(cols) + + return rows + + +def make_image_from_rows(rows, sprite_width, sprite_height): + """Concatenate the rows into a single RGBA image.""" + im_width = sprite_width * max(len(row) for row in rows) + im_height = len(rows) * sprite_height + im = Image.new('RGBA', (im_width, im_height)) + y = 0 + for row in rows: + x = 0 + for sprite in row: + im.paste(sprite.convert('RGBA'), (x, y)) + x += sprite_width + y += sprite_height + return im def convert_sprite_sheet(input_file_paths, sprite_width, sprite_height, @@ -217,25 +230,65 @@ def convert_sprite_sheet(input_file_paths, sprite_width, sprite_height, sprite sheets. """ rows = [] + for input_filepath in input_file_paths: + with Image.open(input_filepath) as sprite_sheet: + rows.extend( + get_sprite_sheet_rows(sprite_sheet, sprite_width, + sprite_height)) - for sprite_sheet in input_file_paths: - rows.extend( - get_sprite_sheet_rows(sprite_sheet, sprite_width, sprite_height)) + im = make_image_from_rows(rows, sprite_width, sprite_height) + im = im.convert(mode="P", palette=Image.ADAPTIVE, colors=256) + + # The sprite data in 'rows' is no longer needed. + # Keep just the number of columns per row. + rows = [len(row) for row in rows] with open(output_filepath, 'bw') as output: output.write(ctypes.c_uint16(sprite_width)) output.write(ctypes.c_uint16(sprite_height)) output.write(ctypes.c_uint16(len(rows))) + # Write palette. + # getpalette() returns 256 colors, but the palette might use less than + # that. getcolors() returns the number of unique colors. + # getpalette() also returns a flattened list, which is why we must *4. + num_colours = len(im.getcolors()) + colours = im.getpalette(rawmode="RGBA")[:4 * num_colours] + palette = [] + for i in range(0, 4 * num_colours, 4): + palette.append((colours[i], colours[i + 1], colours[i + 2], + colours[i + 3])) + + output.write(ctypes.c_uint16(len(palette))) + output.write(bytearray(colours)) + print(f"Sprite width: {sprite_width}") print(f"Sprite height: {sprite_height}") print(f"Rows: {len(rows)}") + print(f"Colours: {len(palette)}") + + # print("Palette") + # for i, colour in enumerate(palette): + # print(f"{i}: {colour}") - for sprites in rows: - output.write(ctypes.c_uint16(len(sprites))) - for sprite_bytes in sprites: + for row, num_columns in enumerate(rows): + output.write(ctypes.c_uint16(num_columns)) + upper = row * sprite_height + lower = (row + 1) * sprite_height + for col in range(num_columns): + left = col * sprite_width + right = (col + 1) * sprite_width + sprite = im.crop((left, upper, right, lower)) + sprite_bytes = sprite.tobytes() + + assert (len(sprite_bytes) == sprite_width * sprite_height) output.write(sprite_bytes) + # if (row == 0) and (col == 0): + # print(f"Sprite: ({len(sprite_bytes)})") + # print(list(sprite_bytes)) + # sprite.save("out.png") + def main(): parser = argparse.ArgumentParser() -- cgit v1.2.3