A word on PNG
PNG stands for Portable Network Graphics. This is an image file format described in the RFC 2083 document which this post is largely based on. Any software writing or reading PNG files must implement the mandatory aspects of this RFC. The PNG file is made of a signature (8 bytes identical for each PNG file) and chunks. Interpreting the chunks allows the software to render or otherwise use the image.
Two PNG file reading approaches
While interpreting PNG files, there are two opposite approaches discussed in chapters 13 and 14 of PNG The Definitive Guide Paperback:
Non-progressive means that the whole PNG file is interpreted and validated before it is being used. Progressive means that as soon as information is available, it is being used for example to display the image. As explained in the book, both approaches are valid. Also, the RFC doesn’t forbid any of the approaches. So, let’s compare them.
Non-progressive reading of a PNG file
Non-progressive reading of a PNG file is quite easy to implement. Each step of the program will be executed sequentially. Each having the whole image data of interest available. This also means that more memory may be required as the whole relevant image data is needed at each step.
In addition, if a file has an invalid chunk for example at the end, depending on the implementation choices on error handling, the whole file will be in error.
The program is faster to check the file, so, it is more appropriate if it is the main duty of the program to validate the file.
Finally, non-progressive reading also means that no part of the image is displayed as long as the whole image is not read which means that the image is available for example for viewing only later.
Progressive reading of a PNG file
Progressive reading of the PNG file compared to non progressive reading allows a faster access to the image. Whether it is for display or for any other use, it may be desirable to have a faster access to the partial file content than waiting for the whole file to be interpreted. This was the whole idea behind the PNG ADAM7 interlacing. The passes allow a progressive display of the file with more details as the file is being interpreted. To support this, the interlaced PNG file is usually larger than the non-interlaced PNG equivalent version. Though, this size difference can sometimes be opposite as explained here. Also, globally, a progressive reading of the whole file should take less computation time as the program should go through the whole file only once.
Progressive PNG file reading allows a more resilient access to the image than non-progressive reading. Indeed, if the reading program first validates the whole file and finds an error, the whole file may be considered as erroneous and thus discarded. Whereas, in the progressive case, the chunks that went through are already used. In the interlaced case, this may lead to a pretty good approximation of the overall image depending on the number of passes that went through without error.
Progressive reading allows for a smaller memory footprint. Indeed, handling the file chunk by chunk as they are read makes it less necessary to allocate memory.
A progressive reading program is more likely to be complex to implement and understand. If the purpose of the program is educational focused on the PNG format for example, the progressive reading may not be the most appropriate solution.
Advantages of both approaches
The following table lists the advantages of each approach compared to the other.
|Smaller memory footprint||Easier to implement|
|More error resilient||Faster validation of the whole file|
|Faster use of the file|
Typical use of progressive or non-progressive reading of PNG
Progressive reading of PNG files should typically be preferred in the following cases:
- Progressive display or use of the file is desirable. When PNG format was established, internet connections were usually much slower than they are in 2019. Web pages with images may require 1 minute to load completely as mentioned in PNG The Definitive Guide Paperback chapter 14. In 2019, it is very rare to wait more than 10 seconds for a web page to completely load. This faster internet connectivity makes the progressive reading less necessary than it was before. Yet, for an internet browser and user interfaces in general, it is still highly desirable to see the file as it loads.
- In very minimal embedded systems, the memory footprint needs to be as small as possible. Progressive loading clearly makes sense then.
- PNG file transmission or generation may not always be reliable. If so, errors are more likely to occur while decoding the file. If a partial display is better than nothing, progressive reading of the PNG file is a better option.
Non-progressive reading of PNG files should typically be preferred in the following cases:
- Non-progressive PNG reading is easier to implement. Therefore, it is better if the purpose of the program is teaching the PNG format for example.
- Validation can be done faster through non-progressive PNG reading. Thus, this is better if the main purpose of the program is fast validation of the PNG file.
Other readings on PNG file progressive reading
Leave a comment
Thanks for your visit. I would be delighted to know what you think of this explanation on the progressive reading of PNG files through the comments section below.