QOI (image format)
64px | |
Filename extension | .qoi |
---|---|
Magic number | (4 bytes, ASCII) |
Developed by | Dominic Szablewski |
Initial release | 24 November 2021 |
Latest release |
1.0
(5 January 2022 ) |
Type of format | Lossless bitmap image format |
Standard | Specification |
Open format? | Yes |
Website | qoiformat.org |
The Quite OK Image Format (QOI) is a specification for lossless image compression of 24-bit (8 bits per color RGB) or 32-bit (8 bits per color with 8-bit alpha channel RGBA) color raster (bitmapped) images, invented by Dominic Szablewski and first announced November 24th 2021.[1]
Contents
Description
The intended purpose was to create an open source lossless compression method, that was faster and easier to implement than PNG. A third party YouTube video explains how PNG and QOI compression are performed.[2] Figures specified in the announcement and the video claim twenty to fifty times faster encoding, and three to four times faster decoding speed compared to PNG, with similar file sizes.[1] The author has donated the specification to the public domain (CC0).
Software and language support
QOI is supported natively by ImageMagick,[3] IrfanView (as of version 4.60),[4] FFmpeg (as of version 5.1),[5] and GraphicConverter (v11.8+).[6]
Community made plugins are available in GIMP, Paint.NET and XnView MP.[7]
There are also implementations for various languages such as Rust, Python, Java, C++, C# and more.[8] A full list can be found on the project's Git(Hub) repository README.
File format
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
as the previous pixel value. An image is complete when all pixels specified by {r: 0, g: 0, b: 0, a: 255}
have been covered. Pixels are encoded as:width * height
- Run-length encoding of the previous pixel (
)QOI_OP_RUN
- an index into the array of previously seen pixels (
)QOI_OP_INDEX
- a difference compared to the previous pixel value in r,g,b (
orQOI_OP_DIFF
)QOI_OP_LUMA
- Full r,g,b or r,g,b,a values (
orQOI_OP_RGB
)QOI_OP_RGBA
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.array[64]
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:QOI_OP_INDEX
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 0x00
byte.0x01
The possible chunks are:
QOI_OP_RGB
QOI_OP_RGB
Byte[0] | Byte[1] | Byte[2] | Byte[3] | |||||||
---|---|---|---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 |
1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 | red | green | blue |
- 8-bit tag
(254)b11111110
- 8-bit red channel value
- 8-bit green channel value
- 8-bit blue channel value
The alpha value remains unchanged from the previous pixel.
QOI_OP_RGBA
QOI_OP_RGBA
Byte[0] | Byte[1] | Byte[2] | Byte[3] | Byte[4] | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 | 7 .. 0 |
1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | red | green | blue | alpha |
- 8-bit tag
(255)b11111111
- 8-bit red channel value
- 8-bit green channel value
- 8-bit blue channel value
- 8-bit alpha channel value
QOI_OP_INDEX
QOI_OP_INDEX
Byte[0] (Range: 0 .. 63) | |||||||
---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
0 | 0 | index |
- 2-bit tag
b00
- 6-bit index into the color index array:
0..63
A valid encoder must not issue 2 or more consecutive
chunks to the same index. QOI_OP_INDEX
should be used instead.QOI_OP_RUN
QOI_OP_DIFF
QOI_OP_DIFF
Byte[0] (Range: 64 .. 127) | |||||||
---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
0 | 1 | dr | dg | db |
- 2-bit tag
b01
- 2-bit red channel difference from the previous pixel
-2..1
- 2-bit green channel difference from the previous pixel
-2..1
- 2-bit blue channel difference from the previous pixel
-2..1
The difference to the current channel values are using a wraparound operation, so
will result in 255, while 1 - 2
will result in 0.255 + 1
Values are stored as unsigned integers with a bias of 2. E.g. −2 is stored as 0 (
). 1 is stored as 3 (b00
). The alpha value remains unchanged from the previous pixel.b11
QOI_OP_LUMA
QOI_OP_LUMA
Byte[0] (Range: 128 .. 191) | Byte[1] | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
1 | 0 | dg | dr - dg | db - dg |
- 2-bit tag
b10
- 6-bit green channel difference from the previous pixel
-32..31
- 4-bit red channel difference minus green channel difference
-8..7
- 4-bit blue channel difference minus green channel difference
-8..7
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 10 - 13
will result in 1.250 + 7
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.
QOI_OP_RUN
QOI_OP_RUN
Byte[0] (Range: 192 .. 253) | |||||||
---|---|---|---|---|---|---|---|
7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
1 | 1 | run |
- 2-bit tag
b11
- 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 b111110
) are illegal as they are occupied by the b111111
and QOI_OP_RGB
tags.[9]QOI_OP_RGBA
References
- ↑ 1.0 1.1 Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found..
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found.
- ↑ Lua error in package.lua at line 80: module 'strict' not found. This article incorporates text from this source, which is in the public domain.
External links
- Format website: C Source code and benchmark results
- 1 page PDF specification
- GitHub repository (including C implementation)
Lua error in package.lua at line 80: module 'strict' not found.