Trezor Optimized Image Format

All multibyte integer values are little endian!

Header

offsetlengthnamedescription
0x00003magicTOI
0x00031fmtdata format: f or g (see below)
0x00042widthwidth of the image
0x00062heightheight of the image
0x00084datasizelength of the compressed data
0x000A?datacompressed data (see below)

Format

TOI currently supports 2 variants:

  • f: full-color
  • g: gray-scale

Full-color

For each pixel a 16-bit value is used. First 5 bits are used for red component, next 6 bits are green, final 5 bits are blue:

1514131211109876543210
RRRRRGGGGGGBBBBB

Gray-scale

Each pixel is encoded using a 4-bit value. Each byte contains color of two pixels:

76543210
PoPoPoPoPePePePe

Where Po is odd pixel and Pe is even pixel.

Compression

Pixel data is compressed using DEFLATE algorithm with 10-bit sliding window and no header. This can be achieved with ZLIB library by using the following:

import zlib
z = zlib.compressobj(level=9, wbits=-10)
zdata = z.compress(pixeldata) + z.flush()

Tools

  • toif_convert - tool for converting PNGs into TOI format and back