hn.today

Each step of decoding a PNG (2022)

iter.ca3 points0 comments
Screenshot of Each step of decoding a PNG (2022)

A step-by-step walkthrough of decoding a PNG explains the file as a series of named chunks (length, name, data, CRC) and how chunk-name capitalization encodes handling rules (critical/ancillary, public/private, safe-to-copy). A concrete 256×256, 8-bit truecolor example is broken down into its signature, IHDR (width/height/bit depth/color space/interlace), metadata chunks (gAMA, cHRM, pHYs, tIME, bKGD), text entries containing a file path and render time, a 62,801-byte IDAT holding compressed image bytes, and the IEND trailer. Multiple IDAT chunks are concatenated by decoders to support streaming or very large data, and the IDAT payload is ordinary zlib-compressed data rather than any PNG-specific compression.

Decoding proceeds by concatenating IDATs, zlib-decompressing to get filtered scanlines, and then undoing per-line PNG filters to reconstruct pixels; compressed bytes look high-entropy and filters are used to expose patterns to the compressor. Filter usage in the example is listed (None 0, Subtract 23, Up 77, Average 134, Paeth 22). Optional Adam7 interlacing splits the image into seven progressively refined passes that are stored sequentially, producing downscaled subimages during load. A demonstrative bug in Average-filter handling shows how integer overflow mistakes produce visible glitches, illustrating why precise filter behavior matters.

Read on iter.ca0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Web

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.