1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-21 05:48:23 +00:00
trezor-firmware/tools/png2toi

94 lines
2.4 KiB
Plaintext
Raw Normal View History

2016-03-24 22:00:38 +00:00
#!/usr/bin/env python3
from PIL import Image
import sys
import struct
import zlib
2016-04-25 16:53:40 +00:00
def process_rgb(w, h, pix):
2016-03-24 22:00:38 +00:00
data = bytes()
2016-04-25 16:53:40 +00:00
for j in range(h):
for i in range(w):
r, g, b = pix[i, j]
c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
data += struct.pack('>H', c)
return data
def process_grayscale(w, h, pix):
data = bytes()
for j in range(h):
2016-10-10 11:38:33 +00:00
for i in range(w // 2):
l1, l2 = pix[i * 2, j], pix[i * 2 + 1, j]
c = (l1 & 0xF0) | (l2 >> 4)
data += struct.pack('>B', c)
2016-04-25 16:53:40 +00:00
return data
def process_image(ifn):
im = Image.open(ifn)
w, h = im.size
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
if im.mode == 'RGB':
print('Detected RGB mode')
elif im.mode == 'L':
if w % 2 > 0:
print('PNG file must have width divisible by 2')
return 3
print('Detected GRAYSCALE mode')
else:
print('Unknown mode:', im.mode)
return 4
pix = im.load()
if im.mode == 'RGB':
ofn = '%s.toif' % ifn[:-4]
2016-04-27 16:45:00 +00:00
pixeldata = process_rgb(w, h, pix)
2016-04-25 16:53:40 +00:00
else:
ofn = '%s.toig' % ifn[:-4]
2016-04-27 16:45:00 +00:00
pixeldata = process_grayscale(w, h, pix)
z = zlib.compressobj(level=9, wbits=10)
zdata = z.compress(pixeldata) + z.flush()
zdata = zdata[2:-4] # strip header and checksum
2016-04-25 16:53:40 +00:00
with open(ofn, 'wb') as f:
if im.mode == 'RGB':
2016-10-10 11:38:33 +00:00
f.write(b'TOIf')
2016-04-25 16:53:40 +00:00
else:
2016-10-10 11:38:33 +00:00
f.write(b'TOIg')
2016-04-27 16:45:00 +00:00
f.write(struct.pack('<HH', w, h))
f.write(struct.pack('<I', len(zdata)))
f.write(zdata)
print('Written %s ... %d bytes' % (ofn, 4 + 4 + len(zdata)))
with open(ofn + '.h', 'wt') as f:
f.write('static const uint8_t toi_%s[] = {\n' % ifn[:-4])
2016-04-25 16:53:40 +00:00
if im.mode == 'RGB':
2016-04-27 16:45:00 +00:00
f.write(" 'T', 'O', 'I', 'f',\n")
2016-04-25 16:53:40 +00:00
else:
2016-04-27 16:45:00 +00:00
f.write(" 'T', 'O', 'I', 'g',\n")
f.write(' (uint16_t)%d, (uint16_t)%d,\n' % (w, h))
f.write(' (uint32_t)%d,\n' % len(zdata))
f.write(' ')
for b in zdata:
f.write(' 0x%02x,' % b)
f.write('\n};\n')
print('Written %s ...' % (ofn + '.h'))
2016-04-25 16:53:40 +00:00
def main():
if len(sys.argv) < 2:
print('Usage png2toi image.png')
return 1
ifn = sys.argv[1]
if not ifn.endswith('.png'):
print('Must provide PNG file')
return 2
process_image(ifn)
2016-03-24 22:00:38 +00:00
2016-04-25 16:53:40 +00:00
main()