1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-26 01:18:28 +00:00

tools: added toi2png tool

This commit is contained in:
Pavol Rusnak 2016-10-10 13:38:33 +02:00
parent cae5fbe434
commit f8bf6d8cff
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
10 changed files with 93 additions and 6 deletions

BIN
assets/experiment1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

BIN
assets/experiment2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

BIN
assets/experiment3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

Binary file not shown.

BIN
assets/experiment7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

View File

@ -18,10 +18,10 @@ def process_rgb(w, h, pix):
def process_grayscale(w, h, pix): def process_grayscale(w, h, pix):
data = bytes() data = bytes()
for j in range(h): for j in range(h):
for i in range(w // 2): for i in range(w // 2):
l1, l2 = pix[i * 2, j], pix[i * 2 + 1, j] l1, l2 = pix[i * 2, j], pix[i * 2 + 1, j]
c = (l1 & 0xF0) | (l2 >> 4) c = (l1 & 0xF0) | (l2 >> 4)
data += struct.pack('>B', c) data += struct.pack('>B', c)
return data return data
@ -55,9 +55,9 @@ def process_image(ifn):
with open(ofn, 'wb') as f: with open(ofn, 'wb') as f:
if im.mode == 'RGB': if im.mode == 'RGB':
f.write(bytes('TOIf', 'ascii')) f.write(b'TOIf')
else: else:
f.write(bytes('TOIg', 'ascii')) f.write(b'TOIg')
f.write(struct.pack('<HH', w, h)) f.write(struct.pack('<HH', w, h))
f.write(struct.pack('<I', len(zdata))) f.write(struct.pack('<I', len(zdata)))
f.write(zdata) f.write(zdata)

87
tools/toi2png Executable file
View File

@ -0,0 +1,87 @@
#!/usr/bin/env python3
from PIL import Image
import sys
import struct
import zlib
def process_rgb(w, h, data):
pix = bytearray(w * h * 3)
for i in range(w * h):
c = (data[i * 2] << 8) + data[i * 2 + 1]
pix[i * 3 ] = (c & 0xF800) >> 8
pix[i * 3 + 1] = (c & 0x07C0) >> 3
pix[i * 3 + 2] = (c & 0x001F) << 3
return bytes(pix)
def process_grayscale(w, h, data):
pix = bytearray(w * h)
for i in range(w * h // 2):
pix[i * 2 ] = data[i] & 0xF0
pix[i * 2 + 1] = (data[i] & 0x0F) << 4
return bytes(pix)
def process_image(ifn):
data = open(ifn, 'rb').read()
if ifn.endswith('.toif'):
if data[:4] != b'TOIf':
print('Unknown TOIF header')
return 1
elif ifn.endswith('.toig'):
if data[:4] != b'TOIg':
print('Unknown TOIG header')
return 2
else:
print('Unsupported format')
return 3
ofn = '%s.png' % ifn[:-5]
w, h = struct.unpack('<HH', data[4:8])
print('Opened %s ... %d x %d' % (ifn, w, h))
l = struct.unpack('<I', data[8:12])[0]
data = data[12:]
if len(data) != l:
print('Compressed data length mismatch (%d vs %d)' % (len(data), l))
return 4
data = zlib.decompress(data, -10)
if ifn.endswith('.toif'):
if len(data) != w * h * 2:
print('Uncompressed data length mismatch (%d vs %d)' % (len(data), w * h * 2))
return 5
pix = process_rgb(w, h, data)
img = Image.frombuffer('RGB', (w, h), pix, 'raw', 'RGB', 0, 1)
img.save(ofn)
print('Written %s ...' % ofn)
if ifn.endswith('.toig'):
if len(data) != w * h // 2:
print('Uncompressed data length mismatch (%d vs %d)' % (len(data), w * h // 2))
return 6
pix = process_grayscale(w, h, data)
img = Image.frombuffer('L', (w, h), pix, 'raw', 'L', 0, 1)
img.save(ofn)
print('Written %s ...' % ofn)
def main():
if len(sys.argv) < 2:
print('Usage toi2png image.toi[fg]')
return 1
ifn = sys.argv[1]
if not ifn.endswith('.toif') and not ifn.endswith('.toig'):
print('Must provide TOIF/TOIG file')
return 2
process_image(ifn)
main()