Yunqa • The Delphi Inspiration

Delphi Components and Applications

User Tools

Site Tools



YuPng implements libpng for Delphi, statically linked, no DLLs needed. It includes a TGraphic descendant with ICC color management, chromaticities, and gamma correction.

YuPNG – libpng for Delphi, no DLLs

YuPng brings libpng to Delphi, with its complete API of functions, types, and constants. libpng is the official PNG reference library. libpng supports almost all PNG features, is extensible, and extensively tested. YuPng links libpng statically, no DLLs needed.

The TYuPngImage class desecends from TGraphic. It loads and draws PNG images with support for embedded ICC color profile conversion, gamma correction, and chromaticities. Features:

  • ICC (International Color Consortium) profile 2.0 and 4.4 color management based on Little CMS.
  • All allowed PNG color types supported: RGB color, palette, and grayscale with 1, 2, 4, 8, and 16-bit bit depths, respectively.
  • Alpha transparency.
  • Progress callback with correct percentages, including interlaced PNG images.

Of course, all required code for TYuPngImage is statically linked into executable binaries and needs no DLLs.

Next to TYuPngImage, the complete libpng API is available for custom implementations to read and write, modify and create PNG images.

Demo projects, including a YuPng Image Viewer with thumbnails preview are included in the download.

What's different in YuPng and TYuPngImage?

YuPng came into being to make up for the deficiencies and bugs of Delphi's TPngImage, for a demonstration of the most notorious ones see below.

All examples were created by Delphi 11.2 Alexandria, the most recent Delphi at the time of this writing. The left and right images are drawn on a checker board pattern to show their transparent pixels. The center image points out pixel differences with a red color overlay.

  • Embedded ICC color profiles go unnoticed in TPngImage. TYuPngImage reads the iCCP chunk and applies the proper color conversions. In this example, red and blue are swapped.
    TPngImage ignores iCCP chunk and displays wrong colors.
  • In this example, failure to apply ICC color profiles leads to negative rendering of grayscale images. The correct gradient is white on the left and black on the right.
    TPngImage ignores iCCP chunk in grayscale PNG.
  • Primary chromaticities and white point are not handled in TPngImage. TYuPngImage reads the cHRM chunk and applies the red, green, and blue chromaticities and the referenced white point. In this example, red and green are swapped.
    TPngImage fails to apply cHRM chromaticities.
  • 2-bit grayscale images show wrong colors in TPngImage. The differences are in stainted shades of gray, whereas the lightest color should be a clean white.
    TPngImage outputs wrong gray levels in 2-bit PNG.
  • 2-bit grayscale can have 7 colors in TPngImage, where only 4 colors are possible. The artistic diff-pattern hints at an algorithmic problem. TYuPngImage is less imaginative and sticks the correct 4 colors.
    TYuPngImage shows 4 correct colors in 2-bit grayscale. TPngImage shows 7 colors, 3 more than possible.
  • Transparency ignored for 16-bit grayscale in TPngImage so the checker board does not shine through.
    TYuPngImage shows correct transparncy where TPngImage prints wrong opacity.
  • Transparency ignored for 16-bit color as well in TPngImage.
    TYuPngImage shows correct transparncy where TPngImage shows wrong opaqe color.
  • Access Violation and memory leak on invalid IHDR data in TPngImage. Older Delphis are more prone to AV than newer ones, but even the current Delphi 11.2 Alexandria is affected. TYuPngImage issues a proper error message and does not leak memory, in all Delphi versions.
    TYuPngImage shows invalid IHDR error where TPngImage crashes with access violation.

Minimum Code Example

{ Add the YuPngImage unit once to any uses clause 
  to register TYuPngImage as the PNG handler. }
  Graphics, // For TPicture.
  YuPngImage; // Registers TYuPngImage.
procedure LoadPng;
  pic: TPicture;
  pic := TPicture.Create;
    { Add code here to do something with the picture. }

3rd Party Problems Detected During YuPng Development

products/png/index.txt · Last modified: 2022/12/19 13:11 by