QOIR: A fast, simple, lossless image file format
I&#x27;m wondering if anybody has tried adding PNG filters on top of QOI[0]. I have an idea of how to do it, and it should be fairly easy to build on top of a QOI encoder&#x2F;decoder, but didn&#x27;t get around to trying it out myself.<p>Per the QOI spec it already has the &quot;SUB&quot; filter built in, in the form of QOI_OP_DIFF. Which is great, because that leaves exactly four PNG filters instead of five (meaning they only need two bits to store): NONE, UP, AVERAGE and PAETH.<p>Here&#x27;s how to add the filters: on a per-line basis, create a transformed line for each filter (so the original pixels, plus UP, AVERAGE and PAETH transformations). Then see how each of the arrays would compress, then pick the best one by <i>treating it as if it was the original pixel input</i>. In a separate array we keep track of which filter we picked for each line.<p>When we&#x27;ve done this for the entire image, we basically have a plain QOI image but if we decompress it the output would show each line in its new transformation. To recover the original image we just have to reverse these transformations bottom-to-top.<p>So we just have to append that array where we kept track of the filters we picked for each line, four picks per byte. Since we know the height of the image we don&#x27;t need to mark the end of this stream. The added size overhead is effectively one byte per four lines. I expect speed overhead would be roughly 4x for encoding (since we brute-force try four transformations), and something very tiny for decoding (since all we need to do in the end is one single in-place pass over all pixels to undo the transformations).<p>Also, because SUB is covered by QOI_OP_DIFF, this approach would implicitly support mixing each of these per-line filters with SUB, so it would be possible to have SUB+UP, SUB+AVERAGE and SUB+PAETH, <i>and</i> it would be on a per-pixel basis. Might be beneficial in some cases.<p>[0] <a href="https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;PNG-Filters.html" rel="nofollow">https:&#x2F;&#x2F;www.w3.org&#x2F;TR&#x2F;PNG-Filters.html</a><p>[1] <a href="https:&#x2F;&#x2F;qoiformat.org&#x2F;qoi-specification.pdf" rel="nofollow">https:&#x2F;&#x2F;qoiformat.org&#x2F;qoi-specification.pdf</a>