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 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_get_buffer_raise(args[3], &bufinfo, MP_BUFFER_READ);
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"));
}
mp_int_t w = (data[4] << 8) | data[5];
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)) {
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;
}
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 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)) {
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 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;
}
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():
col = 0
# hawcons gesture
f = open('playground/tap_64.toi', 'r')
f = open('playground/tap_64.toig', 'r')
while True:
col %= 0xff
@ -71,6 +71,8 @@ def tap_to_confirm():
MIN_COLOR = 0x00
MAX_COLOR = 0xB0
f = open('playground/tap_64.toig', 'r')
_background = ui.rgbcolor(255, 255, 255)
x = math.pi
while True:
@ -100,7 +102,7 @@ def on_read():
def run():
# pipe.init('../pipe', on_read)
msg.set_notify(on_read)
# msg.set_notify(on_read)
loop.call_soon(perf_info)
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)
def read():
raise NotImplemented
raise NotImplementedError
return Msg.receive()
def set_notify(_on_read):
raise NotImplemented
raise NotImplementedError

View File

@ -4,61 +4,79 @@ import sys
import struct
import zlib
if len(sys.argv) < 2:
print('Usage png2toi image.png [mode]')
sys.exit(1)
ifn = sys.argv[1]
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))
def process_rgb(w, h, pix):
data = bytes()
if not gray:
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)
else:
return data
def process_grayscale(w, h, pix):
data = bytes()
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)
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]
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)))
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()