From 8a7713220743ad69edd3d7af41bae0bf256e7c40 Mon Sep 17 00:00:00 2001 From: Patrick Moessler Date: Mon, 10 Jan 2022 01:10:20 +0100 Subject: [PATCH] add image decoder --- img_codec.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 img_codec.py diff --git a/img_codec.py b/img_codec.py new file mode 100644 index 0000000..3343c20 --- /dev/null +++ b/img_codec.py @@ -0,0 +1,34 @@ +from PIL import Image, ImagePalette + + +def load_lut_file(filename): + tbl = {} + flat = bytearray([0]*512*4) + with open(filename, 'r') as f: + for line in f: + reg, data = line.strip().split('=') + reg = int(reg, 16) + # BGRA -> RGBA, ignore alpha + data = bytes.fromhex(data) + data = bytes((data[2], data[1], data[0], 0xFF)) + tbl[reg] = data + flat[reg*4:reg*4+4] = data + return tbl, flat + + +def get_palette(lut, shift=0): + pal = ImagePalette.ImagePalette('RGBA', lut[shift:shift+256*4], 256*4) + return pal + + +def decode_img(src, lut, w, h, shift=0): + dst = Image.frombytes('P', (w, h), src) + pal = get_palette(lut) + dst.putpalette(pal.palette, rawmode=pal.mode) + return dst + + +tbl, lut = load_lut_file('lut.txt') +with open('dump_img1.bin', 'rb') as in_file: + img = decode_img(in_file.read(), lut, 480, 480, 0) + img.save('out.png')