mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-27 00:28:10 +00:00
introduce TOI grayscale 4-bit format
This commit is contained in:
parent
cf4078543b
commit
9558ac1830
BIN
assets/lock.png
Normal file
BIN
assets/lock.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 710 B |
BIN
assets/lock.toi
Normal file
BIN
assets/lock.toi
Normal file
Binary file not shown.
@ -5,11 +5,15 @@ import struct
|
|||||||
import zlib
|
import zlib
|
||||||
|
|
||||||
if len(sys.argv) < 2:
|
if len(sys.argv) < 2:
|
||||||
print('Usage png2toi image.png')
|
print('Usage png2toi image.png [mode]')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
ifn = sys.argv[1]
|
ifn = sys.argv[1]
|
||||||
|
|
||||||
|
gray = False
|
||||||
|
if len(sys.argv) >= 3 and sys.argv[2] == 'g':
|
||||||
|
gray = True
|
||||||
|
|
||||||
if not ifn.endswith('.png'):
|
if not ifn.endswith('.png'):
|
||||||
print('Must provide PNG file')
|
print('Must provide PNG file')
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
@ -18,22 +22,40 @@ im = Image.open(ifn)
|
|||||||
w, h = im.size
|
w, h = im.size
|
||||||
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
|
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
|
||||||
|
|
||||||
if not im.mode == 'RGB':
|
if not gray:
|
||||||
print('PNG file must use RGB mode')
|
if not im.mode == 'RGB':
|
||||||
sys.exit(3)
|
print('PNG file must use RGB mode')
|
||||||
|
sys.exit(3)
|
||||||
|
else:
|
||||||
|
if not im.mode == 'L':
|
||||||
|
print('PNG file must use grayscale mode')
|
||||||
|
sys.exit(3)
|
||||||
|
if w % 2 > 0:
|
||||||
|
print('PNG file must have width divisible by 2')
|
||||||
|
sys.exit(4)
|
||||||
|
|
||||||
pix = im.load()
|
pix = im.load()
|
||||||
|
|
||||||
ofn = '%s.toi' % ifn[:-4]
|
ofn = '%s.toi' % ifn[:-4]
|
||||||
with open(ofn, 'wb') as f:
|
with open(ofn, 'wb') as f:
|
||||||
f.write(bytes('TOIa', 'ascii'))
|
if not gray:
|
||||||
|
f.write(bytes('TOIa', 'ascii'))
|
||||||
|
else:
|
||||||
|
f.write(bytes('TOIg', 'ascii'))
|
||||||
f.write(struct.pack('>HH', w, h))
|
f.write(struct.pack('>HH', w, h))
|
||||||
data = bytes()
|
data = bytes()
|
||||||
for j in range(h):
|
if not gray:
|
||||||
for i in range(w):
|
for j in range(h):
|
||||||
r, g, b = pix[i, j]
|
for i in range(w):
|
||||||
c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
|
r, g, b = pix[i, j]
|
||||||
data += struct.pack('>H', c)
|
c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
|
||||||
|
data += struct.pack('>H', c)
|
||||||
|
else:
|
||||||
|
for j in range(h):
|
||||||
|
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)
|
||||||
z = zlib.compressobj(level=9, wbits=10)
|
z = zlib.compressobj(level=9, wbits=10)
|
||||||
zdata = z.compress(data) + z.flush()
|
zdata = z.compress(data) + z.flush()
|
||||||
zdata = zdata[2:-4] # strip header and checksum
|
zdata = zdata[2:-4] # strip header and checksum
|
||||||
|
Loading…
Reference in New Issue
Block a user