Decoding a PNG, byte by byte, from chunk headers to Adam7 interlacing

Each step of decoding a PNG (2022)

A walkthrough of the full PNG decoding pipeline: how chunks are structured and named, why the IDAT data is concatenated and decompressed with plain zlib, how per-line filters are undone, and how Adam7 interlacing splits an image into seven progressive passes. The example image's filter usage is tallied, and a deliberately introduced overflow bug shows what breaks when the Average filter is mishandled.

It looks like random noise, and that means that the compression algorithm did a good job.
  1. akersten

    I clicked "Show metadata chunks" and I don't understand the Critical vs Ancillary distinction. The Gamma, Color Space Information, Physical Dimensions, and Background Color sections all have a lowercase letter for their header. Aren't those critical to displaying the image properly? Meanwhile the Trailer IEND has an uppercase letter, indicating the zero-length outro byte is somehow more essential than all the color info?

    Same question for safe/unsafe to copy. "Do programs that modify the PNG data need to understand this chunk?" Somehow, they need to understand the Last Modification Date before modifying the image, but no, they don't need to understand the Physical Dimensions(?)

  2. ranger_danger

    I think it would be a neat idea (if it doesn't exist already) to have a site with a collection of similar pages like this for many different formats.

More from this day

2026-10-18