Progressive Reading

In this section we will read an animated PNG image and create a regular PNG image out of every frame just as we did in the previous section, but this time we will use the progressive reader. The most notable difference here is the use of png_set_progressive_frame_fn().


An overview of what we're going to do:

  1. Create a png_struct and an info_struct for the animated image (the one we're reading).

  2. Set the usual callback functions for info, row and end.

  3. In a loop, read the animated image READ_BLOCK_LEN bytes at a time (you can change this number to whatever you like) and call png_process_data().

  4. In the info callback, besides the usual things we check to make sure the image has an acTl chunk, record the number of frames (we don't use this number), and set up a couple of frame callback functions using png_set_progressive_frame_fn().

  5. There is nothing APNG-specific in the row callback.

  6. The frame info callback tells us we have the fcTL chunk for the frame. We're not using this information, the functions is here for illustration only.

  7. The frame end callback is called when a frame has finished decoding, so this is where we write the simple PNG:

    1. Create a png_struct and an info_struct for the simple image (the one we're writing).

    2. Copy the standard chunks from the animated to the simple image.

      Part of this step is not done properly in the example, it is to get the contents of the fcTl chunk for the next frame and do the appropriate thing with compositing the new frame over the old one. This is only so in the example because compositing the image is not the point of this tutorial. The APNG spec requires that you do the compositing according to the contents of the fcTl chunk.

    3. Write the simple image info.

    4. Write the image data.

    5. Complete writing the image with the usual png_write_end().

    6. Destroy the png_struct and an info_struct created for the simple image.

  8. We're not interested in the end_callback.