Overview
The pngdec library allows you to decode PNG images from files or memory buffers. It supports PNG features including transparency, interlacing, multiple color spaces, and various bit depths.Before using pngdec functions, you must load the SYSMODULE_PNGDEC system module using
sysModuleLoad().Error Codes
| Code | Value | Description |
|---|---|---|
PNGDEC_ERROR_OK | 0x00 | Operation successful |
PNGDEC_ERROR_HEADER | 0x80611201 | Invalid PNG header |
PNGDEC_ERROR_STREAM_FORMAT | 0x80611202 | Invalid stream format |
PNGDEC_ERROR_ARG | 0x80611203 | Invalid argument |
PNGDEC_ERROR_SEQ | 0x80611204 | Invalid sequence |
PNGDEC_ERROR_BUSY | 0x80611205 | Decoder is busy |
PNGDEC_ERROR_FATAL | 0x80611206 | Fatal error |
PNGDEC_ERROR_OPEN_FILE | 0x80611207 | Failed to open file |
PNGDEC_ERROR_SPU_UNSUPPORT | 0x80611208 | SPU acceleration unsupported |
PNGDEC_ERROR_SPU_ERROR | 0x80611209 | SPU error |
PNGDEC_ERROR_CB_PARAM | 0x8061120a | Invalid callback parameter |
Color Spaces
Supported color space formats for PNG decoding.
PNGDEC_GRAYSCALE(1) - Grayscale outputPNGDEC_RGB(2) - RGB color outputPNGDEC_PALETTE(4) - Palette-indexed colorPNGDEC_GRAYSCALE_ALPHA(9) - Grayscale with alpha channelPNGDEC_RGBA(10) - RGBA color outputPNGDEC_ARGB(20) - ARGB color output
Stream Selection
Input source selection for PNG data.
PNGDEC_FILE(0) - Decode from filePNGDEC_BUFFER(1) - Decode from memory buffer
Interlace Modes
PNG interlacing methods.
PNGDEC_NO_INTERLACE(0) - Standard sequential imagePNGDEC_ADAM7_INTERLACE(1) - Adam7 interlaced image
Core Functions
pngDecCreate
Pointer to receive the decoder handle.
Input parameters for decoder creation.
Enable SPU acceleration (PNGDEC_SPU_THREAD_ENABLE or PNGDEC_SPU_THREAD_DISABLE).
PPU thread priority.
SPU thread priority (if SPU enabled).
Custom memory allocation function (optional, can be NULL).
Argument passed to malloc function.
Custom memory free function (optional, can be NULL).
Argument passed to free function.
Output parameters from decoder creation.
Decoder library version.
PNGDEC_ERROR_OK on success, error code otherwise.
pngDecOpen
Decoder handle from pngDecCreate.
Pointer to receive the stream subhandle.
Source configuration for the PNG data.
Input stream selection (PNGDEC_FILE or PNGDEC_BUFFER).
File path (if stream_sel is PNGDEC_FILE).
Offset in file to start reading.
Size of PNG data in file.
Pointer to PNG data in memory (if stream_sel is PNGDEC_BUFFER).
Size of PNG data in memory.
Enable SPU acceleration for this stream.
Information about the opened stream.
Amount of memory allocated for initialization.
PNGDEC_ERROR_OK on success, error code otherwise.
pngDecReadHeader
Decoder handle.
Stream subhandle from pngDecOpen.
Pointer to receive image information.
Image width in pixels.
Image height in pixels.
Number of color components.
Original color space of the PNG image.
Bit depth per channel (1, 2, 4, 8, or 16).
Interlace method used (PNGDEC_NO_INTERLACE or PNGDEC_ADAM7_INTERLACE).
Information about chunks present in the PNG.
PNGDEC_ERROR_OK on success, error code otherwise.
pngDecSetParameter
Decoder handle.
Stream subhandle.
Input decoding parameters.
Pointer to command variable for decoder control (can be NULL).
Output scanline order (PNGDEC_TOP_TO_BOTTOM or PNGDEC_BOTTOM_TO_TOP).
Desired output color space.
Desired output bit depth.
Pixel packing flag (PNGDEC_1BYTE_PER_NPIXEL or PNGDEC_1BYTE_PER_1PIXEL).
Alpha channel handling (PNGDEC_STREAM_ALPHA or PNGDEC_FIX_ALPHA).
Fixed alpha value if alpha_select is PNGDEC_FIX_ALPHA.
Output parameters after configuration.
Width of output in bytes (including padding).
Output image width in pixels.
Output image height in pixels.
Number of output color components.
Actual output bit depth.
Actual output mode being used.
Actual output color space.
Memory required for decoding.
PNGDEC_ERROR_OK on success, error code otherwise.
pngDecDecodeData
Decoder handle.
Stream subhandle.
Pointer to output buffer for decoded image data.
Data control parameters.
Bytes per line in output buffer (for custom stride).
PNGDEC_ERROR_OK on success, error code otherwise.
pngDecClose
Decoder handle.
Stream subhandle to close.
PNGDEC_ERROR_OK on success, error code otherwise.
pngDecDestroy
Decoder handle to destroy.
PNGDEC_ERROR_OK on success, error code otherwise.
Helper Functions
pngLoadFromFile
Path to the PNG file.
PNGDEC_ERROR_OK on success, error code otherwise.
pngLoadFromBuffer
Pointer to PNG data in memory.
Size of PNG data in bytes.
Pointer to receive the decoded image data (same structure as pngLoadFromFile).
PNGDEC_ERROR_OK on success, error code otherwise.
Usage Example
Simple Usage Example
For basic use cases, the helper functions provide a simpler interface:Extended API
The pngdec library also provides extended functions for advanced use cases:pngDecExtOpen()- Opens a stream with callback support for streaming datapngDecExtReadHeader()- Reads header with extended informationpngDecExtSetParameter()- Sets parameters with extended options (line mode, progressive)pngDecExtDecodeData()- Decodes with display callback support for progressive rendering
Transparency Handling
PNG images can contain transparency information in several ways:Alpha Channel
Alpha Channel
For RGBA and Grayscale+Alpha images, the alpha channel is decoded directly when using
PNGDEC_RGBA or PNGDEC_ARGB output modes with PNGDEC_STREAM_ALPHA.tRNS Chunk
tRNS Chunk
For images with a tRNS chunk (transparent color), the decoder automatically converts the transparent color to alpha values in the output.
Fixed Alpha
Fixed Alpha
You can use
PNGDEC_FIX_ALPHA to ignore the PNG’s transparency and use a fixed alpha value for all pixels.