TPNGImage help

PNG Delphi

Introduction to transparency


PNG is not just a replacement for compuserve GIF, it goes further than that. Portable Network Graphics is a new generation format, supporting partially transparency images. This means that pixels are blended with the background pixels when drawing, feature supported by this new PNG Delphi version. Also this means that pre-calculations are made to blend the image pixel and the background pixel in order to partial transparency to happen.
The PNG Delphi 1.4 library also provides access to the transparency data in order to allow image editors to have access to this feature.

How does the transparency works in PNG ?

There are two different methods for storing transparency information, one for 24bits, 48bits and grayscale and the other for palette (8 bits or less). When working with 24bits or more, the transparency information is stored next to each pixel red, green and blue values, and then stored in a different memory space by PNG Delphi. In the second way, all the transparency information is stored in a chunk called tRNS containing transparency information for each palette entry.

So, in order to have access to transparency data, you should verify the color mode being used. To do so, access the bitdepth property from the IHDR chunk, if the returns COLOR_PALETTE constant, you should check for the tRNS chunk, otherwise use the AlphaScanline array (from the main TPNGObject).

Transparency data

In booth method, transparency is a single byte (in PNG Delphi implementation), containing information for the associated pixel. This byte contains values between 0 and 255. 0 value means that the associated pixel is completly transparent (the background pixel is intact), and 255 value means that the background pixel is replaced by the image pixel. For instance, if the transparency contains a value like 128, the image pixel is blended with the background pixel and resulting pixel contains half of the background pixel plus half of the image pixel.

Detecting transparency mode

It is real simple to detect the transparency mode for the current image. Just check the BitDepth property from the IHDR chunk, accessed by bitdepth property from the header (direct access to the IHDR chunk) property:

if Header.BitDepth <> COLOR_PALETTE then
  ... First way
else
  ... Second way;