Sans-I/O image codecs for z2d surfaces: BMP, PNG, JPEG, GIF, TGA, XBM, XPM, Netpbm, PCX, ICO and WebP, with an optional sandbox for decoders

ICC: carry a colour profile from one file to another

A profile says what the numbers in an image mean. This library does nothing with that --- z2d has no colour management, so there is nothing here that could apply one --- but it can stop losing it. A program converting a JPEG to a PNG should hand back a file that still says what its colours mean, and one that silently dropped the profile would turn a wide-gamut photograph into a washed-out one the first time it passed through. So the profile comes out as bytes and goes back in as bytes, unread. `DecodeOptions.icc` is a list to append to, `EncodeOptions.icc` is bytes to carry, and `Limits.max_icc_bytes` bounds what a length field can ask for. A profile over the budget is dropped and the image still decodes: a profile too large is a reason to lose the profile, not the photograph. A caller who asks for no profile allocates nothing for one. `src/icc.zig` belongs to neither codec because the thing it knows does not. JPEG has no segment large enough --- a profile can be megabytes and an `APP2` holds 65533 bytes --- so the ICC specification's own Annex B defines how to split one across several, which makes the splitting ICC's business and not JPEG's. PNG has `iCCP`, one chunk of deflated profile whatever the size. BMP is not covered, and the reason is structural rather than an omission: a `BITMAPV5HEADER` gives an *offset* to the profile rather than the profile, and writers put it after the pixel data, so reaching it means knowing where the cursor is in a stream already read past. This decoder only ever seeks forward. A profile that is not one is dropped on the way in and refused on the way out. Unlike EXIF this is passed *on* rather than read, so carrying a malformed profile through would put a malformed profile in somebody else's file. Two checks decide, both from the profile's own header: its size field must be the size it actually is, which catches a truncated profile and one reassembled from pieces that went missing, and bytes 36 to 40 must be `acsp`. Pieces have to arrive in order. libjpeg sorts them, and can, because it holds every marker in memory before looking at any; a decoder reading a stream once cannot leave a gap for a piece that has not come. No encoder writes them out of order, and one that did would have its profile dropped rather than reassembled wrongly. libjpeg and libpng agree. `tools/check_samples.py` now writes a 3KB profile and a 140KB one --- the second split across three `APP2` segments --- into a baseline JPEG, a progressive JPEG and a PNG, and Pillow reads all six back byte for byte. There is a fuzz target for the reassembler, whose properties are the three that matter for something an attacker writes: it terminates, it never exceeds the budget however the pieces are numbered, and anything it hands back is a profile. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01MdXttGjXJxdbBPZQQNyR6h


Author jcollie.dev
Co-author Claude Opus 5 (1M context) Date Commit Parent ee6835ff