Nibnib: Image compression with extreme decompression speed
Nibnib is an image compression format designed specifically for its exceptional decompression speed. Originating as an experiment to test image quality and performance on an RP2350 microcontroller, nibnib is often used in low-bit depth images and palette lookup scenarios. With a limited budget of around 6 cycles per pixel, the format manages to decode two pixels simultaneously from a single byte of input, resulting in 4bpp compression.
The decoding process is straightforward and deterministic, with each decoded pixel being doubled up before fetching a byte from the input stream to add to the pixel's value, ultimately yielding two 16bpp pixels in RGB 565 format.
Encoding in nibnib is a more complex process, involving a search through possible encoded images to find one that closely matches the original. The current encoder is significantly slower than the decoding process, which allows for greater optimization in the decoding phase. Two main aspects of the search are the choice of palette and the selection of codewords.
The palette used in nibnib is a cube of delta values, with a block of random values added for added flexibility. This structure ensures proper handling of edge cases without risking overflow. The encoding loop attempts all 256 palette entries, choosing the one with the minimum error and distributing it using Floyd-Steinberg weights. Subsequent pixels are based on the source pixel value plus weighted errors from previous iterations.
The format is relatively simple, consisting of 2 bytes for width and height, followed by 256 32-bit palette entries, and then the image rows in order. Each row starts with two RGB 565 pixels, followed by ((width - 2) / 2) bytes indexing the palette. The row stride is rounded up to a multiple of 4 bytes. Currently, the format does not support additional metadata, as it is primarily intended for tight integration with the decoder.
Despite its simplicity, nibnib's performance is impressive, offering 4bpp compression with extreme decompression speed, making it an ideal choice for scenarios where speed is a critical factor.
Rewritten by urgent.news; may contain errors — check the original.