Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> You have some wiggle room but chunks have a specific order. For example, the image metadata chunk has to appear before the pixel data chunk.

Probably to make streaming easier. If you know the size early, HTML layout can anticipate the data that should eventually follow.



Yes, the PNG chunk ordering constraints ( https://www.w3.org/TR/2003/REC-PNG-20031110/#5ChunkOrdering ) were crafted to streamline streaming.

Having read the source code of libpng and several others, I see that as chunks come in, their data is parsed and put into a fixed-size structure that contains all the possible relevant metadata. By the time image decoding starts, all the critical information like bit depth, palette, transparency, etc. have been handled.

Likewise, the CRC-32 is at the end of the block and not at the beginning because you can save an epsilon of memory. When the checksum is at the end, all you have to do when reading is to keep a running checksum in one variable, then allocate a second variable after the block is ended to check that the actual checksum matches the declared checksum. If the checksum is at the beginning, then you first have to store that in a variable, then have a second variable to compute a running checksum.

(Actually, there's an even sneakier way to handle a postfix checksum with only one variable, where you incorporate it into the checksum calculation and then confirm that it matches some fixed reference value.)


Not only that, but so the decoder knows how much memory to allocate.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: