From fbc5ee87d2a5b6c56f535563c28b422354df17b7 Mon Sep 17 00:00:00 2001 From: Daniel Thompson Date: Tue, 28 Jan 2020 21:17:41 +0000 Subject: [PATCH] tools: rle_encode: Implement proper argument parsing --- tools/rle_encode.py | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/tools/rle_encode.py b/tools/rle_encode.py index aea7d40..c8c2de6 100755 --- a/tools/rle_encode.py +++ b/tools/rle_encode.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import argparse import sys from PIL import Image @@ -32,6 +33,20 @@ def encode(im): return (im.width, im.height, bytes(rle)) +def render_c(image, fname): + print(f'// 1-bit RLE, generated from {fname}, {len(image[2])} bytes') + print('static const uint8_t rle[] = {\n ', end='') + i = 0 + for rl in image[2]: + print(f' {hex(rl)},', end='') + + i += 1 + if i == 12: + print('\n ', end='') + i = 0 + + print('\n};') + def decode_to_ascii(image): (sx, sy, rle) = image data = bytearray(2*sx) @@ -59,9 +74,26 @@ def decode_to_ascii(image): # Check the image is the correct length assert(dp == 0) -image = encode(Image.open(sys.argv[1])) -# This is kinda cool for testing but let's leave this disabled until we add -# proper argument processing! -#decode_to_ascii(image) -print(f'# 1-bit RLE, generated from {sys.argv[1]}') -print(image) + + +parser = argparse.ArgumentParser(description='RLE encoder tool.') +parser.add_argument('files', nargs='+', + help='files to be encoded') +parser.add_argument('--ascii', action='store_true', + help='Run the resulting image(s) through an ascii art decoder') +parser.add_argument('--c', action='store_true', + help='Render the output as C instead of python') +args = parser.parse_args() + +for fname in args.files: + image = encode(Image.open(fname)) + + if args.c: + render_c(image, fname) + else: + print(f'# 1-bit RLE, generated from {fname}, {len(image[2])} bytes') + print(f'rle = {image}') + + if args.ascii: + print() + decode_to_ascii(image)