Header A QOI file consists of a 14-byte header, followed by any number of data “chunks” and an 8-byte end marker. qoi_header { char magic[4]; // magic bytes "qoif" uint32_t width; // image width in pixels (BE) uint32_t height; // image height in pixels (BE) uint8_t channels; // 3 = RGB, 4 = RGBA uint8_t colorspace; // 0 = sRGB with linear alpha // 1 = all channels linear }; The colorspace and channel fields are purely informative. They do not change the way data chunks are encoded.
Encoding Images are encoded row by row, left to right, top to bottom. The decoder and encoder start with {{code|2=yaml| {r: 0, g: 0, b: 0, a: 255} }} as the previous pixel value. An image is complete when all
pixels specified by have been covered. Pixels are encoded as: •
Run-length encoding of the previous pixel () • an index into the array of previously seen pixels () • a difference compared to the previous pixel value in r,g,b ( or ) • Full r,g,b or r,g,b,a values ( or ) The color channels are assumed to not be premultiplied with the alpha channel (“un-premultiplied alpha”). A running (zero-initialized) of previously seen pixel values is maintained by the encoder and decoder. Each pixel that is seen by the encoder and decoder is put into this array at the position formed by a
hash function of the color value. In the encoder, if the pixel value at the index matches the current pixel, this index position is written to the stream as . The hash function for the index is: index_position = (r * 3 + g * 5 + b * 7 + a * 11) % 64 Each chunk starts with a 2- or 8-bit tag, followed by a number of data bits. The bit length of chunks is divisible by 8 - i.e. all chunks are byte aligned. All values encoded in these data bits have the most significant bit on the left. The 8-bit tags have precedence over the 2-bit tags. A decoder must check for the presence of an 8-bit tag first. The byte stream's end is marked with 7 bytes followed by a single byte. The possible chunks are:
• 8-bit tag (254) • 8-bit red channel value • 8-bit green channel value • 8-bit blue channel value The alpha value remains unchanged from the previous pixel.
• 8-bit tag (255) • 8-bit red channel value • 8-bit green channel value • 8-bit blue channel value • 8-bit alpha channel value
• 2-bit tag • 6-bit index into the color index array: A valid encoder must not issue 2 or more consecutive chunks to the same index. should be used instead.
• 2-bit tag • 2-bit red channel difference from the previous pixel • 2-bit green channel difference from the previous pixel • 2-bit blue channel difference from the previous pixel The difference to the current channel values are using a
wraparound operation, so will result in 255, while will result in 0. Values are stored as
unsigned integers with a bias of 2. E.g. −2 is stored as 0 (). 1 is stored as 3 (). The alpha value remains unchanged from the previous pixel.
• 2-bit tag • 6-bit green channel difference from the previous pixel • 4-bit red channel difference minus green channel difference • 4-bit blue channel difference minus green channel difference The green channel is used to indicate the general direction of change and is encoded in 6 bits. The red and blue channels (dr and db) base their diffs off of the green channel difference. I.e.: dr_dg = (cur_px.r - prev_px.r) - (cur_px.g - prev_px.g) db_dg = (cur_px.b - prev_px.b) - (cur_px.g - prev_px.g) The difference to the current channel values are using a wraparound operation, so will result in 253, while will result in 1. Values are stored as unsigned integers with a bias of 32 for the green channel and a bias of 8 for the red and blue channel. The alpha value remains unchanged from the previous pixel.
• 2-bit tag • 6-bit run-length repeating the previous pixel The
run-length is stored with a bias of −1. Note that the runlengths 63 and 64 ( and ) are illegal as they are occupied by the and tags. == History ==