From 5aa13db815f6fa9018c1d437445e90c3bd497b7c Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Thu, 22 May 2025 13:40:53 +1200 Subject: [PATCH] [online_image] Allocate pngle manually to potentially use psram (#8354) Co-authored-by: Keith Burzinski --- esphome/components/online_image/__init__.py | 2 +- esphome/components/online_image/png_image.cpp | 22 ++++++++++++++++++- esphome/components/online_image/png_image.h | 8 ++++--- platformio.ini | 2 +- 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/esphome/components/online_image/__init__.py b/esphome/components/online_image/__init__.py index 6b69bc240b..55b9037176 100644 --- a/esphome/components/online_image/__init__.py +++ b/esphome/components/online_image/__init__.py @@ -75,7 +75,7 @@ class PNGFormat(Format): def actions(self): cg.add_define("USE_ONLINE_IMAGE_PNG_SUPPORT") - cg.add_library("pngle", "1.0.2") + cg.add_library("pngle", "1.1.0") IMAGE_FORMATS = { diff --git a/esphome/components/online_image/png_image.cpp b/esphome/components/online_image/png_image.cpp index fc5fb554bf..2038d09ed0 100644 --- a/esphome/components/online_image/png_image.cpp +++ b/esphome/components/online_image/png_image.cpp @@ -34,12 +34,32 @@ static void init_callback(pngle_t *pngle, uint32_t w, uint32_t h) { * @param h The height of the rectangle to draw. * @param rgba The color to paint the rectangle in. */ -static void draw_callback(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t rgba[4]) { +static void draw_callback(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint8_t rgba[4]) { PngDecoder *decoder = (PngDecoder *) pngle_get_user_data(pngle); Color color(rgba[0], rgba[1], rgba[2], rgba[3]); decoder->draw(x, y, w, h, color); } +PngDecoder::PngDecoder(OnlineImage *image) : ImageDecoder(image) { + { + pngle_t *pngle = this->allocator_.allocate(1, PNGLE_T_SIZE); + if (!pngle) { + ESP_LOGE(TAG, "Failed to allocate memory for PNGLE engine!"); + return; + } + memset(pngle, 0, PNGLE_T_SIZE); + pngle_reset(pngle); + this->pngle_ = pngle; + } +} + +PngDecoder::~PngDecoder() { + if (this->pngle_) { + pngle_reset(this->pngle_); + this->allocator_.deallocate(this->pngle_, PNGLE_T_SIZE); + } +} + int PngDecoder::prepare(size_t download_size) { ImageDecoder::prepare(download_size); if (!this->pngle_) { diff --git a/esphome/components/online_image/png_image.h b/esphome/components/online_image/png_image.h index 39f445c588..46519f8ef4 100644 --- a/esphome/components/online_image/png_image.h +++ b/esphome/components/online_image/png_image.h @@ -1,7 +1,8 @@ #pragma once -#include "image_decoder.h" #include "esphome/core/defines.h" +#include "esphome/core/helpers.h" +#include "image_decoder.h" #ifdef USE_ONLINE_IMAGE_PNG_SUPPORT #include @@ -18,13 +19,14 @@ class PngDecoder : public ImageDecoder { * * @param display The image to decode the stream into. */ - PngDecoder(OnlineImage *image) : ImageDecoder(image), pngle_(pngle_new()) {} - ~PngDecoder() override { pngle_destroy(this->pngle_); } + PngDecoder(OnlineImage *image); + ~PngDecoder() override; int prepare(size_t download_size) override; int HOT decode(uint8_t *buffer, size_t size) override; protected: + RAMAllocator allocator_; pngle_t *pngle_; }; diff --git a/platformio.ini b/platformio.ini index abea62a4ca..06a0666d67 100644 --- a/platformio.ini +++ b/platformio.ini @@ -40,7 +40,7 @@ lib_deps = wjtje/qr-code-generator-library@1.7.0 ; qr_code functionpointer/arduino-MLX90393@1.0.2 ; mlx90393 pavlodn/HaierProtocol@0.9.31 ; haier - kikuchan98/pngle@1.0.2 ; online_image + kikuchan98/pngle@1.1.0 ; online_image ; Using the repository directly, otherwise ESP-IDF can't use the library https://github.com/bitbank2/JPEGDEC.git#ca1e0f2 ; online_image ; This is using the repository until a new release is published to PlatformIO