diff options
author | lonkaars <loek@pipeframe.xyz> | 2023-03-22 18:34:53 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2023-03-22 18:34:53 +0100 |
commit | 1bb8b7eda56de815bcfc9c6f8b07df399cf400a0 (patch) | |
tree | 0711d750eb6eb18d1009a1c1208eb9da51b1d68c | |
parent | 1cd0285b22755a21cbc367213a143ec33d298c95 (diff) |
fix endianness in `tilepack` and add `tilemap2png` script for previewing finished tilemap
-rwxr-xr-x | scripts/tilemap2png | 47 | ||||
-rwxr-xr-x | scripts/tilepack | 4 |
2 files changed, 49 insertions, 2 deletions
diff --git a/scripts/tilemap2png b/scripts/tilemap2png new file mode 100755 index 0000000..e37bfab --- /dev/null +++ b/scripts/tilemap2png @@ -0,0 +1,47 @@ +#!/bin/python3 +# read packed tilemap file from stdin and output png on stdout + +import sys +import io +import struct +from PIL import Image + +color_map = ( + (0x00, 0x00, 0x00), + (0x24, 0x24, 0x24), + (0x49, 0x49, 0x49), + (0x6d, 0x6d, 0x6d), + (0x92, 0x92, 0x92), + (0xb6, 0xb6, 0xb6), + (0xda, 0xda, 0xda), + (0xff, 0xff, 0xff), +) + +buf = [] +for byte in sys.stdin.buffer.read(): + buf.append(byte) + +size_horizontal = 16 * 16 +size_vertical = ((len(buf) // 104) // 16 + 1) * 16 +img = Image.new('RGB', (size_horizontal, size_vertical)) + +i = 0 +sprite_idx = 0 +for x in range(0, len(buf), 2): + word = 0x0000 + word |= buf[x] + word |= buf[x+1] << 8 + + for p in range(5): + img.putpixel(((i % 16) + (16 * (sprite_idx % 16)), (i // 16) + (16 * (sprite_idx // 16))), color_map[(word >> (p * 3)) & 0b111]) + i += 1 + if i > 0xff: + i = 0 + sprite_idx += 1 + break + +with io.BytesIO() as out: + img.save(out, format="PNG") + sys.stdout.buffer.write(out.getvalue()) + sys.stdout.buffer.flush() + diff --git a/scripts/tilepack b/scripts/tilepack index aff417d..a07cbb6 100755 --- a/scripts/tilepack +++ b/scripts/tilepack @@ -11,8 +11,8 @@ for byte in sys.stdin.buffer.read(): pixel_idx = i % 5 word |= (byte & 0b111) << 3 * pixel_idx - if i % 5 == 4 or i == 0xff: - sys.stdout.buffer.write(struct.pack('!H', word)) + if pixel_idx == 4 or i == 0xff: + sys.stdout.buffer.write(struct.pack('<H', word)) word = 0x0000 i += 1 |