tw88xx_utils/img_codec.py

35 lines
941 B
Python
Raw Permalink Normal View History

2022-01-10 01:10:20 +01:00
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')