PNGdec

repository·master·Indexed 42 days ago

https://github.com/bitbank2/pngdec

A lightweight, embedded-friendly PNG decoding library for microcontrollers. Designed for minimal RAM usage (requiring at least 48K free RAM) and self-containment, it can operate without heap memory management (malloc/free). It supports line-by-line decoding via callbacks, provides a C and C++ interface, and includes a PNGDisplay helper class for integration with bb_spi_lcd, bb_epaper, and FastEPD display libraries.

Tokens
1.1K
Snippets
2
Records
6
Agent score
5%

What's inside PNGdec

  1. Overview of PNGdec

    master

    PNGdec is an 'embedded-friendly' (Arduino-compatible) PNG image decoding library designed specifically for microcontrollers with limited resources. It is optimized to run on any MCU with at least 48K of free RAM and is designed to be self-contained without external dependencies, including the ability to operate without malloc/free (heap memory management).

    Key features include:

    • Memory Efficiency: Minimal RAM usage and support for decoding images line-by-line via a callback function.
    • Flexible Decoding: Supports decoding to a user-supplied buffer or using a callback for line-by-line processing.
    • Format Support: Supports all standard PNG options except interlacing (which is excluded to save RAM).
    • LCD Integration: Includes a PNGDisplay helper class for use with the bb_spi_lcd library and functions to convert pixel formats to RGB565 for LCD displays.
    • Performance: Offers an option to disable zlib's internal CRC check to improve decoding speed by 10-30%.
    • Dual Interface: Available as an Arduino-style C++ library class or as straight C.
  2. Setup PNGDisplay with a display library

    master

    Before including or using PNGDisplay, you must include one of the following display libraries to ensure compatibility:

    • bb_spi_lcd (defined by SPI_LCD_H)
    • bb_epaper (defined by __BB_EPAPER__)
    • FastEPD (defined by __FASTEPD_H__)

    If none of these are present, the compiler will throw an error.

  3. Retrieve PNG metadata with getPNGInfo

    master

    Use getPNGInfo to extract the dimensions and bit depth of a PNG file or buffer without rendering it to a display.

    Methods

    • int getPNGInfo(int *width, int *height, int *bpp, const void *pData, int iDataSize): Get info from a memory buffer.
    • int getPNGInfo(int *width, int *height, int *bpp, const char *fname): Get info from a file.
    • int getPNGInfo_LFS(int *width, int *height, int *bpp, const char *fname): Get info from LittleFS.

    Parameters

    • width: Pointer to an integer where the image width will be stored.
    • height: Pointer to an integer where the image height will be stored.
    • bpp: Pointer to an integer where the bits per pixel will be stored.
    • pData / fname: The source data or file path.

    Returns an error code on failure.

  4. Load a PNG onto a display using PNGDisplay

    master

    The PNGDisplay class provides helper methods to decode and draw PNG images directly to supported display objects. Depending on the library included, you can load images from memory (data buffers) or from a file system (SD or LittleFS).

    Available Methods

    For SPI LCD (BB_SPI_LCD)

    • int loadPNG(BB_SPI_LCD *pLCD, int x, int y, const void *pData, int iDataSize, uint32_t bgColor = 0xffffffff): Load from a memory buffer.
    • int loadPNG(BB_SPI_LCD *pLCD, int x, int y, const char *fname, uint32_t bgColor = 0xffffffff): Load from a file.
    • int loadPNG_LFS(BB_SPI_LCD *pLCD, int x, int y, const char *fname, uint32_t bgColor = 0xffffffff): Load from LittleFS.

    For FastEPD (FASTEPD)

    • int loadPNG(FASTEPD *pDisplay, int x, int y, const void *pData, int iDataSize, uint32_t bgColor = 0xffffffff)
    • int loadPNG(FASTEPD *pDisplay, int x, int y, const char *fname, uint32_t bgColor = 0xffffffff)
    • int loadPNG_LFS(FASTEPD *pDisplay, int x, int y, const char *fname, uint32_t bgColor = 0xffffffff)

    For BBEPAPER (BBEPAPER)

    • int loadPNG(BBEPAPER *pDisplay, int x, int y, const void *pData, int iDataSize, uint32_t bgColor = 0xffffffff)
    • int loadPNG(BBEPAPER *pDisplay, int x, int y, const char *fname, uint32_t bgColor = 0xffffffff)
    • int loadPNG_LFS(BBEPAPER *pDisplay, int x, int y, const char *fname, uint32_t bgColor = 0xffffffff)

    Parameters

    • x, y: The drawing coordinates on the display.
    • pData: Pointer to the PNG data buffer.
    • iDataSize: Size of the data buffer in bytes.
    • fname: Path to the PNG file.
    • bgColor: Background color (default 0xffffffff).

    Returns the number of bytes processed or an error code.