Skip to main content

PNG Memory Decoder (pngd)

pngd is a lightweight PNG decoder for in-memory buffers. PNG parsing, zlib stream handling, and DEFLATE decompression are implemented inside libx, with no dependency on libpng or an external zlib. It is intended for builds that need to minimize external library dependencies.

Decoded pixels use the XSTAR Surface format directly: each pixel is native-endian premultiplied 0xAARRGGBB. On little-endian systems, the four bytes in memory are B, G, R, A. The header is <libx/pngd.h> and is also included indirectly by <xos/xos.h>.

Format Support​

PNG color typeChannelsSupported bit depths
0Grayscale1, 2, 4, 8, 16
2RGB8, 16
3Indexed color1, 2, 4, 8
4Grayscale + alpha8, 16
6RGBA8, 16

The decoder also supports:

  • non-interlaced and Adam7-interlaced images;
  • all five PNG row filters: None, Sub, Up, Average, and Paeth;
  • indexed-color PLTE palettes and tRNS transparency for grayscale, RGB, and indexed-color images;
  • multiple consecutive IDAT chunks;
  • uncompressed, fixed-Huffman, and dynamic-Huffman DEFLATE blocks;
  • PNG chunk CRC and zlib Adler-32 integrity checks.

Unknown ancillary chunks are skipped, while unknown critical chunks cause decoding to fail. Ancillary metadata such as color profiles, gamma, and text does not affect pixel conversion. APNG animation data is not interpreted; only the default image represented by regular IDAT chunks is decoded.

API​

#include <libx/pngd.h>

int pngd_info(const void * buf, int len, int * width, int * height);
int pngd_decode(const void * buf, int len, void * pixels, int stride);

Both functions return 1 on success and 0 on failure.

pngd_info​

Checks the PNG signature and IHDR, including its CRC, dimensions, compression method, filter method, interlace method, color type, and bit depth. The dimensions are returned through width and height; either output pointer may be NULL.

pngd_info only examines the header. It does not scan later chunks or decompress image data. Success means that the header is acceptable to pngd, not that pngd_decode is guaranteed to succeed.

pngd_decode​

Decodes the complete PNG in buf into caller-provided pixels:

  • len is the PNG buffer size in bytes; it must be positive and representable by int;
  • pixels must have room for at least stride * height bytes;
  • pixels and the start of each row should be 4-byte aligned;
  • stride is the byte distance between adjacent output rows, must be at least width * 4, and should normally be a multiple of four;
  • each pixel is written directly as native-endian premultiplied 0xAARRGGBB;
  • row padding introduced by stride is left untouched.

Low-bit-depth grayscale samples are scaled to 0 through 255. Only the high 8 bits of a 16-bit sample are retained, so output is always 8 bits per channel. RGB is multiplied by alpha; fully transparent pixels are cleared to zero, while fully opaque pixels retain their original color. Input and output buffers should not overlap.

The output buffer may have been partially modified when decoding fails and must be discarded. The API reports only success or failure and does not provide detailed error codes.

Example​

The following function decodes an in-memory PNG into a tightly packed premultiplied-ARGB buffer:

static void * decode_png(const void * buf, int len, int * width, int * height)
{
void * pixels;
int w;
int h;
int stride;

if(!pngd_info(buf, len, &w, &h))
return NULL;
stride = w * 4;
pixels = xos_mem_malloc((size_t)stride * h);
if(!pixels)
return NULL;
if(!pngd_decode(buf, len, pixels, stride))
{
xos_mem_free(pixels);
return NULL;
}
if(width)
*width = w;
if(height)
*height = h;
return pixels;
}

Release the returned buffer with xos_mem_free when it is no longer needed.

Using pngd with a Surface​

When a graphics Surface is required, use surface_alloc_from_buf in most cases:

struct surface_t * s = surface_alloc_from_buf(buf, len);

This function detects PNG input automatically. Because pngd_decode already produces the native-endian premultiplied ARGB representation used by a Surface, no channel swap, alpha premultiplication, or additional pixel conversion is needed after decoding.

Decoding Process and Memory Use​

The decoder first parses and validates chunks, concatenates all IDAT data, performs zlib/DEFLATE decompression, reverses row filtering, and finally converts samples to native-endian premultiplied ARGB in normal row order or Adam7 pass order.

In addition to the caller-provided output buffer, decoding allocates two temporary buffers: one for concatenated compressed data and one for the complete decompressed, filtered scanline stream. Peak additional memory is therefore approximately the total IDAT size plus the decompressed scanline size. Both temporary buffers are released on success and failure.

The decoder rejects zero dimensions, illegal color-type and bit-depth combinations, damaged CRC or Adler-32 values, invalid chunk ordering, malformed DEFLATE data, and images whose RGBA output size exceeds the signed int range.

Limitations​

  • PNG decoding only; encoding is not provided;
  • complete memory buffers only; file-handle and incremental streaming APIs are not provided;
  • fixed native-endian premultiplied-ARGB output; 16-bit precision and alternative pixel formats cannot be retained;
  • no color management and no application of gamma or ICC profiles;
  • no APNG animation-frame support;
  • no detailed error information.