Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PNG: What is the benefit of using multiple IDAT-Chunks?

Tags:

png

I would like to know what the benefit of using multiple IDAT-Chunks inside a PNG Image is.

The PNG documentation says

There may be multiple IDAT chunks; if so, they shall appear consecutively with no other intervening chunks. The compressed datastream is then the concatenation of the contents of the data fields of all the IDAT chunks.

I can't imagine it's because of the maximum size (2^32 bytes) of the data-block inside the chunk.

like image 500
vigri Avatar asked Apr 08 '15 11:04

vigri


1 Answers

Recall that all PNG chunks (including IDAT chunks) have a prefix with the chunk length. To put all the compressed stream in a single huge IDAT chunk would cause these two inconveniences:

  • On the encoder side: the compressor doesn't know the total compressed data size until it has finished the compression. Then, it would need to buffer the full compressed data in memory before writing the chunk prefix.

  • On the decoder side: it depends on how chunk decoding is implemented; if it buffers each chunk in memory (allocating the space given by the chunk length prefix) and, after filling it and checking the CRC, it passes the content to the uncompressor, then, again, having a single huge IDAT chunk would be a memory hog.

Considering this, I believe that use rather small IDAT chunks (say, 16KB or 64KB) should be recommended practice. The overhead (12 bytes per chunk, less than 1/5000 if len=64KB) is negligible.

like image 71
leonbloy Avatar answered Mar 03 '23 01:03

leonbloy