add image decoder
This commit is contained in:
parent
a87c81d15a
commit
8a77132207
1 changed files with 34 additions and 0 deletions
34
img_codec.py
Normal file
34
img_codec.py
Normal file
|
@ -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')
|
Loading…
Reference in a new issue