.ima container format


The container format for a simple image is,

Magic number a0 49 4d 41h32 bits
Metadata
Image start tag C000000Ch32 bits
Image width32 bits
Image height 32 bits
Color depth8 bits
Number of channels8 bits
Compression algorithm16 bits
Synchronization marker 1632 bits
CRC32 bits
Image content
Synchronization marker 12832 bits
CRC32 bits

Numbers are stored in little-endian format. In the header, everything except the magic number, synchronization marker and CRC is stored in binary-complement format and a ffh byte is inserted after any byte with the value 0, as seen on disk, to avoid creating an artificial synchronization marker. The format for a synchronization marker is 00 00 80 xxh. Outside of a synchronization marker or a CRC, a 1 bit is inserted after any 22 consecutive 0 bits starting at a byte boundary.

The format for metadata is a 32 bits tag followed by the data. The low-order 8 bits indicate how many bytes of data follow the tag:
0 - 127tag is followed by 0 to 127 bytes of data
128tag is followed by a 0-ended text string
129tag is followed by 32 bits indicating how many bytes of binary data follow
130tag is followed by 32 bits indicating how many bytes of text follow

The high-order 2 bits indicate how important the tag is:
00000000h - 7fffffffhtag can be skipped and is user-editable
80000000h - 9fffffffhtag can help and is user-editable
a0000000h - bfffffffhtag can help and is not user-editable
c0000000h - ffffffffhtag is essential and is not user-editable

The format for user-editable numbers is that in each byte, bit 7 indicates if this is the last byte (1) or (0) if the number continues. If bit 7 is 1, bit 6 is the sign. So an 8 bits number would be coded as,
xxxxxxx0 x00000?1
A 16 bits number would be coded as,
xxxxxxx0 xxxxxxx0 xx0000?1
128 would be coded as,
00000000 10000001
-128 would be coded as,
00000000 10000011

The format for an animation is,

Header
Synchronization marker 1632 bits
CRC32 bits
First frame of first group
Synchronization marker 6432 bits
CRC32 bits
Second frame of first group
Synchronization marker 6432 bits
CRC32 bits
.........................................................
Last frame of first group
Synchronization marker 9632 bits
CRC32 bits
Header of second group of frames
Synchronization marker 1632 bits
CRC32 bits
First frame of second group
.........................................................
Last frame of last group
Synchronization marker 11232 bits
CRC32 bits
Additional metadata, seek pointers... (optional)
Synchronization marker 12832 bits
CRC32 bits

The libima image codec is here. 1