A QOI-Plane file has a 25 byte header, compatible with Gamut QOIX.
struct qoix_header_t { char magic[4]; // magic bytes "qoix" uint32_t width; // image width in pixels (BE) uint32_t height; // image height in pixels (BE) uint8_t version_; // Major version of QOIX format. uint8_t channels; // 1 = 8-bit luminance 2 = luminance + alpha (3 and 4 indicate QOI2AVG codec, see qoi2avg.d) uint8_t bitdepth; // 8 = this qoiplane codec is always 8-bit (10 indicates QOI-10 codec, see qoi10b.d) uint8_t colorspace; // 0 = sRGB with linear alpha, 1 = all channels linear uint8_t compression; // 0 = none, 1 = LZ4 float pixelAspectRatio; // -1 = unknown, else Pixel Aspect Ratio float resolutionX; // -1 = unknown, else physical resolution in DPI };
The decoder and encoder start with {l: 0} as the previous pixel value. Pixels are either encoded as - a run of the previous pixel - a difference to the previous pixel value - full luminance value
Each chunk starts with a tag, followed by a number of data bits. The bit length of chunks is divisible by 4 - i.e. all chunks are nibble aligned. All values encoded in these data bits have the most significant bit on the left. The last nibble needs to be 0xf.
The byte stream's end is marked with 4 0xff bytes.
Encoding:
QOIPLANE_DIFF1 0xxx => diff -4..+3 vs average of rounded up left pixel and top pixel QOIPLANE_DIFF2 100x xxxx => diff -16..15 vs average of rounded up left pixel and top pixel QOIPLANE_ADIFF 1011 xxxx => diff -7..+7 in alpha channel QOIPLANE_LA 1011 0000 xxxx xxxx aaaa aaaa => encode direct full values QOIPLANE_DIRECT 1010 xxxx xxxx => direct value If channels == 2 and the last opcode is not a QOIPLANE_ADIFF then QOIPLANE_DIRECT encodes an alpha value. QOIPLANE_REPEAT1 11xx => repeat 1 to 3 times the last pixel QOIPLANE_REPEAT2 1111 xxxx xxxx => repeat 4 to 258 times a pixel. (1111 1111 1111 disallowed, indicates end of stream)
A QOI-inspired codec for 8-bit greyscale images.
Because the input is 8-bit, we are forced to split bytes in nibbles.
Incompatible adaptation of QOI format - https://phoboslab.org
-- LICENSE: The MIT License(MIT) Copyright(c) 2021 Dominic Szablewski (original QOI format) Copyright(c) 2022 Guillaume Piolat (QOI-plane variant for 8-bit greyscale and greyscale + alpha images). Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -- Documentation This library provides the following functions; - qoiplane_decode -- decode the raw bytes of a QOI-plane image from memory - qoiplane_encode -- encode an rgba buffer into a QOI-plane image in memory