JPEGDecoder Arduino Library

repository·master·Indexed 42 days ago

https://github.com/bodmer/jpegdecoder

An Arduino library for decoding 24-bit JPEG files and rendering them onto TFT displays. It supports storage via SD cards, PROGMEM FLASH arrays, and LittleFS on RP2040, ESP8266, and ESP32. Compatible with processors such as Arduino Due, ESP32, ESP8266, and STM32. Requires drivers that support setWindow() and pushColor() functions, similar to Adafruit_GFX.

Tokens
1.1K
Snippets
2
Records
8
Agent score
4%

What's inside JPEGDecoder

  1. Overview of Arduino JPEGDecoder

    master

    The JPEGDecoder library allows for rendering JPEG files onto TFT displays. It supports images stored on an SD card, in program memory (FLASH) arrays, or in the LittleFS Flash filing system (on RP2040, ESP8266, and ESP32).

    Key Constraints & Requirements:

    • Format: Jpeg files must be in 24-bit format. 8-bit JPEGs are not supported.
    • Progressive JPEGs: Not supported (requires too much memory).
    • RAM Requirements: Decompression requires significant RAM. It is recommended to use processors with more RAM than an Arduino UNO (e.g., Arduino Due, ESP32, ESP8266, or STM32). The Arduino Mega is not recommended due to potential RAM shortages during decoding.
    • AVR Array Limits: For AVR processors (like Mega2560), the maximum size for a memory array is 32,767 bytes.
  2. Supported Hardware and File Systems

    master

    The library is compatible with several architectures and storage methods:

    Supported Processors:

    • Arduino Due
    • ESP32
    • ESP8266 (e.g., NodeMCU 1.0)
    • STM32-based boards
    • RP2040

    Supported Storage & File Systems:

    • SD Card: Uses the standard SD library or SdFat (recommended for Due to allow bit-banged SPI on pins 50, 51, and 52).
    • Flash/Program Memory: PROGMEM arrays.
    • Flash Filing Systems: LittleFS (on ESP8266, ESP32, and RP2040). For ESP8266, ensure you use Arduino IDE Board Core 2.3.0 or later to avoid conflicts between SPIFFS and SD libraries.
  3. Adapting JPEGDecoder for different TFT displays

    master

    The library is designed to work with displays using drivers like HX8357B, HX8357C, and ILI9481. To use the library with other TFT drivers or graphics libraries, ensure your driver/library supports the following functions (similar to the Adafruit_GFX library):

    • setWindow() or SetAddrWindow()
    • pushColor()
  4. Configure JPEGDecoder via User_Config.h or PlatformIO

    master

    The library uses a configuration header User_Config.h to manage settings. If you are using PlatformIO, you can bypass this file by defining JPEGDECODER_SETUP_LOADED in your platformio.ini to provide your own settings.

    Key configuration behaviors include:

    • SD Library Selection: You can choose between the standard SD.h or SdFat.h by defining LOAD_SD_LIBRARY or LOAD_SDFAT_LIBRARY respectively.
    • Flash File System: On ESP8266, ESP32, or RP2040, the library automatically enables LOAD_FLASH_FS to support LittleFS (aliased as SPIFFS).
  5. Decode JPEG images from different sources

    master

    The JPEGDecoder class provides several methods to initiate decoding depending on where your JPEG data is stored. The decoding process populates public members like width, height, and pImage with the resulting pixel data.

    Decode from Memory Array

    Use decodeArray to decode a JPEG stored in a byte array (e.g., in PROGMEM).

    int result = JpegDec.decodeArray(my_jpg_array, array_size);

    Decode from Flash File System (LittleFS/SPIFFS)

    Use decodeFsFile to decode files from the internal flash file system.

    int result = JpegDec.decodeFsFile("/image.jpg");

    Decode from SD Card

    If LOAD_SD_LIBRARY or LOAD_SDFAT_LIBRARY is defined, use decodeSdFile to decode files from an SD card.

    int result = JpegDec.decodeSdFile("/sd/image.jpg");
    int result = JpegDec.decodeArray(my_jpg_array, array_size);
    int result = JpegDec.decodeFsFile("/image.jpg");
    int result = JpegDec.decodeSdFile("/sd/image.jpg");
  6. Access decoded image properties and pixel data

    master

    After a successful call to a decode* method, the JPEGDecoder instance (typically the global JpegDec) holds the following public information:

    • width: The width of the decoded image in pixels.
    • height: The height of the decoded image in pixels.
    • pImage: A pointer to the decoded pixel data (16-bit format).
    • comps: Number of color components.
    • MCUWidth / MCUHeight: Dimensions of a Minimum Coded Unit (MCU).
    • MCUSPerRow / MCUSPerCol: Number of MCUs across the width and height of the image.