TPNGImage help

PNG Delphi

About chunks


A Portable Networks Image is made of several information packets called chunks. Some of these are necessary and essencial to allow the image to be displayed, others contain additional information as text or historiograms.

Inside a file, a chunk contains: a network ordered 4 bytes value containing the size for the chunk data; a 4 bytes string containing the chunk name; the data with the length specified before; and for the last also a network ordered 4 bytes unsigned integer containing the chunk crc for the chunk name and data.

ChunkLength: Cardinal;
ChunkName  : Array[0..3] of Char;
ChunkData  : Array[0..ChunkLength - 1] of byte;
ChunkCRC   : Cardinal;

The crc part is important to validate the chunk data, as when the image was saved. The crc is created using the ChunkName and ChunkData only. This storage method allows flexibility to the images, allowing decoders to ignore certain chunks when they don't reconize it.

The chunk name will indicate the chunk content type so it can be decoded. The name should contain 4 ASCII uppercase or lowercase letters. Also the case is sensitive. There are some rules to tell which letter should be lowercased or uppercased.

Letter

 In case it is:

Uppercase

Lowercase

1

The chunk is critical and must be know by the decoder.

It's a secondary chunk and might be ignored.

2

The chunk is part from the official PNG specification.

This is a private chunk with specific purposes.

3

Following PNG specification, this letter must be uppercased.

It's not definied yet when this letter should be lowercased.

4

Depends on the image contents to exist.

May be saved unchanged in case this image is not changed.

 

A valid PNG image must contain a IHDR, one or more sequencial IDAT chunks and in the end a IEND chunk. In case this image requires a palette, the PLTE chunk is also mandatory.

Name

Multiples ok ?

Ordem do chunk

IHDR

No

Must be always the first

PLTE

No

Before IDAT

IDAT

Yes

Multiple must be sequencial

IEND

No

Always the last


 

All the readed chunks are stored in allocated memory by the component. This component implements objects to handle with all the different chunk types and provides ways to read different properties from each chunk. The ancestor class to handle all the chunks is TChunk. Altough there are several differend classes with TChunk as it's ancestor to read specific information such as TChunkIHDR handling the IHDR chunk.

Use the property Chunks from TPNGObject to access all the stored chunks. TPNGObject.Chunks.Count retorna o total número de chunks e TPNGObject.Chunks.Item[i] returns a TChunk object from the position i (should be between 0 and Count - 1).

As you get the returned TChunk, use it's property Name to get the chunk type. You might also use the is operator followed by the class to test (for instance: TPNGObject.Chunks.Item[i] is TChunkIHDR).

Knowing the chunk name, and if there is a class to handle this chunk, you should access this class using typecast, for instance TChunkIHDR(TPNGObject.Chunks.Item[0]). Now you may access this object properties as specified in this help file.