1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +00:00

add data size to toi format header

This commit is contained in:
Pavol Rusnak 2016-04-25 18:53:40 +02:00
parent f8713bad4d
commit b41c1c3e4d
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
13 changed files with 87 additions and 58 deletions

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.5 KiB

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

BIN
assets/trezor.toig Normal file

Binary file not shown.

View File

@ -1,3 +1,4 @@
#TREZOR OS documentation #TREZOR OS documentation
* [TREZOR OS API](api.md) * [TREZOR OS API](api.md)
* [TREZOR OS Bootloader](bootloader.md)

View File

@ -203,15 +203,19 @@ STATIC mp_obj_t mod_TrezorUi_Display_image(size_t n_args, const mp_obj_t *args)
mp_buffer_info_t bufinfo; mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ); mp_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
uint8_t *data = bufinfo.buf; uint8_t *data = bufinfo.buf;
if (bufinfo.len < 8 || memcmp(data, "TOIa", 4) != 0) { if (bufinfo.len < 8 || memcmp(data, "TOIf", 4) != 0) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid image format")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid image format"));
} }
mp_int_t w = (data[4] << 8) | data[5]; mp_int_t w = (data[4] << 8) | data[5];
mp_int_t h = (data[6] << 8) | data[7]; mp_int_t h = (data[6] << 8) | data[7];
mp_int_t datalen = (data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11];
if ((x < 0) || (y < 0) || (x + w > RESX) || (y + h > RESY)) { if ((x < 0) || (y < 0) || (x + w > RESX) || (y + h > RESY)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Out of bounds")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Out of bounds"));
} }
display_image(x, y, w, h, data + 8, bufinfo.len - 8); if (datalen != bufinfo.len - 12) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid size of data"));
}
display_image(x, y, w, h, data + 12, bufinfo.len - 12);
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_image_obj, 4, 4, mod_TrezorUi_Display_image); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_image_obj, 4, 4, mod_TrezorUi_Display_image);
@ -228,12 +232,16 @@ STATIC mp_obj_t mod_TrezorUi_Display_icon(size_t n_args, const mp_obj_t *args) {
} }
mp_int_t w = (data[4] << 8) | data[5]; mp_int_t w = (data[4] << 8) | data[5];
mp_int_t h = (data[6] << 8) | data[7]; mp_int_t h = (data[6] << 8) | data[7];
mp_int_t datalen = (data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11];
if ((x < 0) || (y < 0) || (x + w > RESX) || (y + h > RESY)) { if ((x < 0) || (y < 0) || (x + w > RESX) || (y + h > RESY)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Out of bounds")); nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Out of bounds"));
} }
if (datalen != bufinfo.len - 12) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid size of data"));
}
mp_int_t fgcolor = mp_obj_get_int(args[4]); mp_int_t fgcolor = mp_obj_get_int(args[4]);
mp_int_t bgcolor = mp_obj_get_int(args[5]); mp_int_t bgcolor = mp_obj_get_int(args[5]);
display_icon(x, y, w, h, data + 8, bufinfo.len - 8, fgcolor, bgcolor); display_icon(x, y, w, h, data + 12, bufinfo.len - 12, fgcolor, bgcolor);
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_icon_obj, 6, 6, mod_TrezorUi_Display_icon); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUi_Display_icon_obj, 6, 6, mod_TrezorUi_Display_icon);

View File

@ -29,7 +29,7 @@ def perf_info():
def animate(): def animate():
col = 0 col = 0
# hawcons gesture # hawcons gesture
f = open('playground/tap_64.toi', 'r') f = open('playground/tap_64.toig', 'r')
while True: while True:
col %= 0xff col %= 0xff
@ -71,6 +71,8 @@ def tap_to_confirm():
MIN_COLOR = 0x00 MIN_COLOR = 0x00
MAX_COLOR = 0xB0 MAX_COLOR = 0xB0
f = open('playground/tap_64.toig', 'r')
_background = ui.rgbcolor(255, 255, 255) _background = ui.rgbcolor(255, 255, 255)
x = math.pi x = math.pi
while True: while True:
@ -100,7 +102,7 @@ def on_read():
def run(): def run():
# pipe.init('../pipe', on_read) # pipe.init('../pipe', on_read)
msg.set_notify(on_read) # msg.set_notify(on_read)
loop.call_soon(perf_info) loop.call_soon(perf_info)
loop.call_soon(tap_to_confirm()) loop.call_soon(tap_to_confirm())

Binary file not shown.

BIN
src/playground/tap_64.toig Normal file

Binary file not shown.

View File

@ -21,8 +21,8 @@ else:
return Msg.send(msg) return Msg.send(msg)
def read(): def read():
raise NotImplemented raise NotImplementedError
return Msg.receive() return Msg.receive()
def set_notify(_on_read): def set_notify(_on_read):
raise NotImplemented raise NotImplementedError

View File

@ -4,61 +4,79 @@ import sys
import struct import struct
import zlib import zlib
if len(sys.argv) < 2:
print('Usage png2toi image.png [mode]')
sys.exit(1)
ifn = sys.argv[1] def process_rgb(w, h, pix):
gray = False
if len(sys.argv) >= 3 and sys.argv[2] == 'g':
gray = True
if not ifn.endswith('.png'):
print('Must provide PNG file')
sys.exit(2)
im = Image.open(ifn)
w, h = im.size
print('Opened %s ... %d x %d @ %s' % (ifn, w, h, im.mode))
if not gray:
if not im.mode == 'RGB':
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()
ofn = '%s.toi' % ifn[:-4]
with open(ofn, 'wb') as f:
if not gray:
f.write(bytes('TOIa', 'ascii'))
else:
f.write(bytes('TOIg', 'ascii'))
f.write(struct.pack('>HH', w, h))
data = bytes() data = bytes()
if not gray: for j in range(h):
for j in range(h): for i in range(w):
for i in range(w): r, g, b = pix[i, j]
r, g, b = pix[i, j] c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3)
c = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | ((b & 0xF8) >> 3) data += struct.pack('>H', c)
data += struct.pack('>H', c) return data
else:
for j in range(h):
def process_grayscale(w, h, pix):
data = bytes()
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)
z = zlib.compressobj(level=9, wbits=10) return data
zdata = z.compress(data) + z.flush()
zdata = zdata[2:-4] # strip header and checksum
f.write(zdata)
print('Written %s ... %d bytes' % (ofn, 4 + 4 + len(zdata)))
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]
else:
ofn = '%s.toig' % ifn[:-4]
with open(ofn, 'wb') as f:
if im.mode == 'RGB':
f.write(bytes('TOIf', 'ascii'))
else:
f.write(bytes('TOIg', 'ascii'))
f.write(struct.pack('>HH', w, h))
if im.mode == 'RGB':
data = process_rgb(w, h, pix)
else:
data = process_grayscale(w, h, pix)
z = zlib.compressobj(level=9, wbits=10)
zdata = z.compress(data) + z.flush()
zdata = zdata[2:-4] # strip header and checksum
f.write(struct.pack('>I', len(zdata)))
f.write(zdata)
print('Written %s ... %d bytes' % (ofn, 4 + 4 + len(zdata)))
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)
main()