Every QOI file starts with the same 14 bytes. All multi-byte numbers in the format are big-endian. There is only one version of the format, and the header has no version field.
Fields
| Offset | Size | Type | Name | Description |
|---|---|---|---|---|
| 0 | 4 | ASCII | magic | The text qoif: 71 6F 69 66. |
| 4 | 4 | u32 | width | Image width in pixels. |
| 8 | 4 | u32 | height | Image height in pixels. |
| 12 | 1 | u8 | channels | See Channels. |
| 13 | 1 | u8 | colorspace | See Colour space. |
Channels
| Value | Meaning |
|---|---|
| 3 | RGB |
| 4 | RGBA |
The value does not change how chunks are coded. An RGB file still carries a running alpha value, which stays at 255 unless a chunk changes it.
Colour space
| Value | Meaning |
|---|---|
| 0 | sRGB colour channels with linear alpha |
| 1 | All channels linear |
The specification calls this byte informative: it is saved in the header but does not affect how chunks are encoded or decoded.
Alpha
The specification says: “The color channels are assumed to not be premultiplied with
the alpha channel (‘un-premultiplied alpha’).” So the r, g and b stored in a chunk are
the colour itself, not the colour multiplied by alpha, whatever the channels or colour
space bytes say. The same sentence is in the comment at the top of qoi.h.
(Sources: the Specification Version 1.0 of 2022.01.05 from qoiformat.org, read with
PDFKit, and qoi.h from the phoboslab/qoi repository, read as raw text.)
Limits
The format itself sets no maximum beyond the 32-bit width and height. The reference
implementation, qoi.h, refuses images of more than 400,000,000 pixels
(QOI_PIXELS_MAX), which keeps the worst case of five bytes per pixel under
about 2 GB. That limit belongs to the library, not to the file format.
What the reference decoder accepts
The specification states no validity rules beyond the field values above; the
following are what qoi_decode in qoi.h does. The decoder is
the reference, so for a file to be read by it these are the rules:
- The data must be at least 22 bytes (the 14-byte header plus the 8-byte end marker), or it returns nothing.
- It returns nothing if the magic is not
qoif, the width or the height is 0,channelsis below 3 or above 4, orcolorspaceis above 1. - It returns nothing if
height ≥ 400,000,000 ÷ width(integer division), the pixel cap above; so the cap applies to the product with that rounding. - The channels the caller asks for (0, 3 or 4; any other value is refused) set the output layout; 0 means use the file’s. The file’s channels value does not change how the chunks are read.
- The decoder stops reading chunks 8 bytes before the end of the data and never compares those 8 bytes with the end marker, so a wrong end marker is not detected. Chunks left over after width × height pixels are ignored.
- When the chunk data runs out before all pixels are produced, there is no error: each remaining pixel is set to the previous pixel’s value (and the colour index is not updated). A truncated image therefore decodes to a full-size image whose tail repeats the last decoded pixel.
- A
QOI_OP_RGB,QOI_OP_RGBAorQOI_OP_LUMAchunk is not checked against the end of the data: a chunk that starts just before the end marker reads its remaining bytes from the end marker.
Source: qoi_decode, qoi.h, phoboslab/qoi master, read as raw text.
Example
A 2 by 1 RGBA image in sRGB:
00000000 71 6F 69 66 00 00 00 02 00 00 00 01 04 00
The rest of this file is taken apart at the end of the Chunks page.