build-a-png-codec / lesson-07.md
Lesson 07 · The PNG container

Describing a PNG

Time to see the container work end to end. Today you compose the signature check, the chunk walk, and the IHDR parse into one function that summarizes a PNG and rejects a bad one clearly.

The goal

Summarize a PNG as its dimensions, color type, and the ordered list of its chunk types, and return a clear error for a bad signature.

Start here - the target
TO DO
Scenario: Producing a one-line description of a PNG
Givena valid PNG whose IHDR says 8 by 4, color type 6, with chunks IHDR, IDAT, IEND
Whenit is described
Thenthe summary reports width 8, height 4, color type 6, and chunk types [IHDR IDAT IEND] in order
Anda stream that fails the signature check returns an error naming the problem, rather than a summary
Background

This is the first payoff. Everything the chapter built - recognizing the signature, walking chunks to IEND, and reading IHDR - snaps together into a single Describe that takes raw PNG bytes and tells you what the file is: its size, its color type, and the sequence of chunks it carries. That sequence (IHDR, then one or more IDAT, then IEND, with an optional PLTE) is the skeleton every later chapter fills in.

Just as important is the failure path. Feed Describe a stream with the wrong signature and it returns a clear error instead of a summary or a crash. A codec that fails loudly on bad input is worth more than one that guesses, and you will lean on this habit through the whole project. With the container solved, the next chapter earns the right to trust these chunks by checking their CRCs.

Make it work
func Describe(b []byte) (string, error) {
chunks, err := Chunks(b) // signature + walk
if err != nil { return "", err }
hdr := parseIHDR(chunks[0].Data) // IHDR is always first
// format width, height, color type, and the type list
}
CheckpointDONE
The container reads and describes a real PNG, and refuses a broken one. Commit and stop here.