APNG: Implementing animation in the PNG format
APNG comes as a modification of the well-known PNG format, but with additional support for animation. It supports 24-bit images and 8-bit transparency, having a much better quality then the classic GIF format.
An APNG file contains ordinary PNG frames, with two additional data chunks: aDAT (animation data chunks) and fCTL(the frame control chunk). Any PNG decoder should be able to decode the first frame of the APNG file as an ordinary PNG image. Any APNG data file must be subjected to certain rules:
- The size of the entire APNG stream is the same as the size of the first frame.
- In order for the APNG file to be valid, an aCTL chunk must appear before any IDAT chunks
- Also, an fCTL chunk must appear before any IDAT chunks
- The second and subsequent frames are encoded as aDAT chunks, containing fCTL info and the IDAT sections of the PNG images.
The aCTL chunk contains:
byte
0 num_iterations (unsigned int) the number of times the APNG is looped. 0 means infinite.
It is an ancillary chunk, as defined by the PNG specification. As it is written in the rule 2, it must be inserted before an IDAT chunk. If the num_iterations variable is different from 0, then the APNG will stop at the last frame, after performing the last iteration.
The aDAT chunk is composed of:
byte
0 sequence_number (unsigned int) Sequence number of the aDAT chunk, starting with 0
4 remaining stream data
It contains a stream of frames, which are to be used for the animation. The sequence number is initialized at 0 and increments for each aDAT chunk. The stream data consists from IHDR, one or more IDATA and an fCTL, which symbolizes the end of the frame and contains information for the next one.
The fCTL chunk is formed like this:
Format:
byte
0 x_offset (unsigned int) X position at which to render the following frame
4 y_offset (unsigned int) Y position at which to render the following frame
8 delay_num (unsigned short) Frame delay fraction numerator
10 delay_den (unsigned short) Frame delay fraction denominator
12 render_op (byte) Type of frame area rendering for this frame
The X and Y offset variables provide the information about the image region that would be rendered. Delay_num and delay_den are the delay numerator and the delay denumerator variables.
