1
0
Fork 0

tools: rle_encode: Implement proper argument parsing

This commit is contained in:
Daniel Thompson 2020-01-28 21:17:41 +00:00
parent 6dfc95764f
commit fbc5ee87d2

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import argparse
import sys import sys
from PIL import Image from PIL import Image
@ -32,6 +33,20 @@ def encode(im):
return (im.width, im.height, bytes(rle)) 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): def decode_to_ascii(image):
(sx, sy, rle) = image (sx, sy, rle) = image
data = bytearray(2*sx) data = bytearray(2*sx)
@ -59,9 +74,26 @@ def decode_to_ascii(image):
# Check the image is the correct length # Check the image is the correct length
assert(dp == 0) 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! parser = argparse.ArgumentParser(description='RLE encoder tool.')
#decode_to_ascii(image) parser.add_argument('files', nargs='+',
print(f'# 1-bit RLE, generated from {sys.argv[1]}') help='files to be encoded')
print(image) 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)