1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-15 20:19:23 +00:00
trezor-firmware/tools/toi2png

93 lines
2.4 KiB
Plaintext
Raw Normal View History

2016-10-10 11:38:33 +00:00
#!/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]
2017-06-13 14:50:03 +00:00
pix[i * 3 + 0] = (c & 0xF800) >> 8
2016-10-10 11:38:33 +00:00
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):
2017-06-13 14:50:03 +00:00
pix[i * 2 + 0] = data[i] & 0xF0
2016-10-10 11:38:33 +00:00
pix[i * 2 + 1] = (data[i] & 0x0F) << 4
return bytes(pix)
def process_image(ifn, ofn):
2016-10-10 11:38:33 +00:00
2018-07-31 09:35:09 +00:00
data = open(ifn, "rb").read()
2016-10-10 11:38:33 +00:00
2018-07-31 09:35:09 +00:00
if ifn.endswith(".toif"):
if data[:4] != b"TOIf":
print("Unknown TOIF header")
2016-10-10 11:38:33 +00:00
return 1
2018-07-31 09:35:09 +00:00
elif ifn.endswith(".toig"):
if data[:4] != b"TOIg":
print("Unknown TOIG header")
2016-10-10 11:38:33 +00:00
return 2
else:
2018-07-31 09:35:09 +00:00
print("Unsupported format")
2016-10-10 11:38:33 +00:00
return 3
if ofn is None:
2018-07-31 09:35:09 +00:00
ofn = "%s.png" % ifn[:-5]
2016-10-10 11:38:33 +00:00
2018-07-31 09:35:09 +00:00
w, h = struct.unpack("<HH", data[4:8])
2016-10-10 11:38:33 +00:00
2018-07-31 09:35:09 +00:00
print("Opened %s ... %d x %d" % (ifn, w, h))
2016-10-10 11:38:33 +00:00
2018-07-31 09:35:09 +00:00
l = struct.unpack("<I", data[8:12])[0]
2016-10-10 11:38:33 +00:00
data = data[12:]
if len(data) != l:
2018-07-31 09:35:09 +00:00
print("Compressed data length mismatch (%d vs %d)" % (len(data), l))
2016-10-10 11:38:33 +00:00
return 4
data = zlib.decompress(data, -10)
2018-07-31 09:35:09 +00:00
if ifn.endswith(".toif"):
2016-10-10 11:38:33 +00:00
if len(data) != w * h * 2:
2018-07-31 09:35:09 +00:00
print(
"Uncompressed data length mismatch (%d vs %d)" % (len(data), w * h * 2)
)
2016-10-10 11:38:33 +00:00
return 5
pix = process_rgb(w, h, data)
2018-07-31 09:35:09 +00:00
img = Image.frombuffer("RGB", (w, h), pix, "raw", "RGB", 0, 1)
2016-10-10 11:38:33 +00:00
img.save(ofn)
2018-07-31 09:35:09 +00:00
print("Written %s ..." % ofn)
2016-10-10 11:38:33 +00:00
2018-07-31 09:35:09 +00:00
if ifn.endswith(".toig"):
2016-10-10 11:38:33 +00:00
if len(data) != w * h // 2:
2018-07-31 09:35:09 +00:00
print(
"Uncompressed data length mismatch (%d vs %d)" % (len(data), w * h // 2)
)
2016-10-10 11:38:33 +00:00
return 6
pix = process_grayscale(w, h, data)
2018-07-31 09:35:09 +00:00
img = Image.frombuffer("L", (w, h), pix, "raw", "L", 0, 1)
2016-10-10 11:38:33 +00:00
img.save(ofn)
2018-07-31 09:35:09 +00:00
print("Written %s ..." % ofn)
2016-10-10 11:38:33 +00:00
def main():
if len(sys.argv) < 2:
2018-07-31 09:35:09 +00:00
print("Usage: toi2png image.toi[fg] [output]")
2016-10-10 11:38:33 +00:00
return 1
ifn = sys.argv[1]
2018-07-31 09:35:09 +00:00
if not ifn.endswith(".toif") and not ifn.endswith(".toig"):
print("Must provide TOIF/TOIG file")
2016-10-10 11:38:33 +00:00
return 2
ofn = sys.argv[2] if len(sys.argv) > 2 else None
process_image(ifn, ofn)
2016-10-10 11:38:33 +00:00
main()