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:
Create a png_struct and an info_struct for the animated image (the one we're reading).
Set the usual callback functions for info, row and end.
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().
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().
There is nothing APNG-specific in the row callback.
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.
The frame end callback is called when a frame has finished decoding, so this is where we write the simple PNG:
Create a png_struct and an info_struct for the simple image (the one we're writing).
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.
Write the simple image info.
Write the image data.
Complete writing the image with the usual png_write_end().
Destroy the png_struct and an info_struct created for the simple image.
We're not interested in the end_callback.