ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
png_decoder.cpp
Go to the documentation of this file.
1#include "png_decoder.h"
2#ifdef USE_RUNTIME_IMAGE_PNG
3
7#include "esphome/core/log.h"
8
9#include <algorithm>
10
11ESPHOME_LOG_TAG(TAG, "image_decoder.png");
12
13namespace esphome::runtime_image {
14
15// Compressed bytes handed to pngle at a time
16static constexpr size_t FEED_PIECE_SIZE = 64;
17
26static void init_callback(pngle_t *pngle, uint32_t w, uint32_t h) {
27 PngDecoder *decoder = (PngDecoder *) pngle_get_user_data(pngle);
28 decoder->set_size(w, h);
29}
30
42static void draw_callback(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint8_t rgba[4]) {
43 PngDecoder *decoder = (PngDecoder *) pngle_get_user_data(pngle);
44 Color color(rgba[0], rgba[1], rgba[2], rgba[3]);
45 decoder->draw(x, y, w, h, color);
46
47 // Feed watchdog periodically to avoid triggering during long decode operations.
48 // Feed every 1024 pixels to balance efficiency and responsiveness.
49 uint32_t pixels = w * h;
50 decoder->increment_pixels_decoded(pixels);
51 if ((decoder->get_pixels_decoded() % 1024) < pixels) {
52 App.feed_wdt();
53 }
54}
55
57 {
58 RAMAllocator<pngle_t> allocator;
59 pngle_t *pngle = allocator.allocate(1, PNGLE_T_SIZE);
60 if (!pngle) {
61 ESP_LOGE(TAG, "Failed to allocate memory for PNGLE engine!");
62 return;
63 }
64 memset(pngle, 0, PNGLE_T_SIZE);
65 this->pngle_ = pngle;
66 pngle_reset(this->pngle_);
67 }
68}
69
71 if (this->pngle_) {
72 pngle_reset(this->pngle_);
73 RAMAllocator<pngle_t> allocator;
74 allocator.deallocate(this->pngle_, PNGLE_T_SIZE);
75 }
76}
77
78int PngDecoder::prepare(size_t expected_size) {
79 // Check before the base prepare() so a failure never leaves an active session
80 if (!this->pngle_) {
81 ESP_LOGE(TAG, "PNG decoder engine not initialized!");
83 }
84 ImageDecoder::prepare(expected_size);
85 pngle_set_user_data(this->pngle_, this);
86 pngle_set_init_callback(this->pngle_, init_callback);
87 pngle_set_draw_callback(this->pngle_, draw_callback);
88 return 0;
89}
90
91int HOT PngDecoder::decode(uint8_t *buffer, size_t size) {
92 if (!this->pngle_) {
93 ESP_LOGE(TAG, "PNG decoder engine not initialized!");
95 }
96 // PNG can be decoded progressively, but wait for a reasonable chunk
98 ESP_LOGD(TAG, "Waiting for more data");
99 return 0;
100 }
101 // A few compressed bytes can hold many rows of pixels, so feed small pieces and stop once the call has run for
102 // DECODE_TIME_LIMIT_MS; the caller passes the rest again later
103 const uint32_t start = millis();
104 size_t total = 0;
105 while (total < size) {
106 const size_t piece = std::min(FEED_PIECE_SIZE, size - total);
107 const int fed = pngle_feed(this->pngle_, buffer + total, piece);
108 if (fed < 0) {
109 ESP_LOGE(TAG, "Error decoding image: %s", pngle_error(this->pngle_));
111 }
112 if (!this->size_valid_)
114 this->decoded_bytes_ += fed;
115 total += fed;
116 // pngle takes less than the piece when a chunk header or CRC runs past its end; that is no reason to stop
117 if (fed == 0 || millis() - start >= DECODE_TIME_LIMIT_MS)
118 break;
119 }
120 return total;
121}
122
123} // namespace esphome::runtime_image
124
125#endif // USE_RUNTIME_IMAGE_PNG
uint8_t h
Definition bl0906.h:2
void feed_wdt()
Feed the task watchdog.
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2396
void deallocate(T *p, size_t n)
Definition helpers.h:2453
T * allocate(size_t n)
Definition helpers.h:2423
Class to abstract decoding different image formats.
virtual int prepare(size_t expected_size)
Initialize the decoder, starting a new decoding session.
int HOT decode(uint8_t *buffer, size_t size) override
PngDecoder(RuntimeImage *image)
Construct a new PNG Decoder object.
int prepare(size_t expected_size) override
A dynamic image that can be loaded and decoded at runtime.
uint32_t buffer
Definition gif.cpp:50
static void int int int w
Definition display.cpp:62
uint16_t size
Definition helpers.cpp:34
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
ESPHOME_LOG_TAG(TAG, "image_decoder.png")
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6