ESPHome 2026.3.0-dev
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
9static const char *const TAG = "image_decoder.png";
10
11namespace esphome::runtime_image {
12
21static void init_callback(pngle_t *pngle, uint32_t w, uint32_t h) {
22 PngDecoder *decoder = (PngDecoder *) pngle_get_user_data(pngle);
23 decoder->set_size(w, h);
24}
25
37static void draw_callback(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint8_t rgba[4]) {
38 PngDecoder *decoder = (PngDecoder *) pngle_get_user_data(pngle);
39 Color color(rgba[0], rgba[1], rgba[2], rgba[3]);
40 decoder->draw(x, y, w, h, color);
41
42 // Feed watchdog periodically to avoid triggering during long decode operations.
43 // Feed every 1024 pixels to balance efficiency and responsiveness.
44 uint32_t pixels = w * h;
45 decoder->increment_pixels_decoded(pixels);
46 if ((decoder->get_pixels_decoded() % 1024) < pixels) {
47 App.feed_wdt();
48 }
49}
50
52 {
53 pngle_t *pngle = this->allocator_.allocate(1, PNGLE_T_SIZE);
54 if (!pngle) {
55 ESP_LOGE(TAG, "Failed to allocate memory for PNGLE engine!");
56 return;
57 }
58 memset(pngle, 0, PNGLE_T_SIZE);
59 pngle_reset(pngle);
60 this->pngle_ = pngle;
61 }
62}
63
65 if (this->pngle_) {
66 pngle_reset(this->pngle_);
67 this->allocator_.deallocate(this->pngle_, PNGLE_T_SIZE);
68 }
69}
70
71int PngDecoder::prepare(size_t expected_size) {
72 ImageDecoder::prepare(expected_size);
73 if (!this->pngle_) {
74 ESP_LOGE(TAG, "PNG decoder engine not initialized!");
76 }
77 pngle_set_user_data(this->pngle_, this);
78 pngle_set_init_callback(this->pngle_, init_callback);
79 pngle_set_draw_callback(this->pngle_, draw_callback);
80 return 0;
81}
82
83int HOT PngDecoder::decode(uint8_t *buffer, size_t size) {
84 if (!this->pngle_) {
85 ESP_LOGE(TAG, "PNG decoder engine not initialized!");
87 }
88 // PNG can be decoded progressively, but wait for a reasonable chunk
90 ESP_LOGD(TAG, "Waiting for more data");
91 return 0;
92 }
93 auto fed = pngle_feed(this->pngle_, buffer, size);
94 if (fed < 0) {
95 ESP_LOGE(TAG, "Error decoding image: %s", pngle_error(this->pngle_));
96 } else {
97 this->decoded_bytes_ += fed;
98 }
99 return fed;
100}
101
102} // namespace esphome::runtime_image
103
104#endif // USE_RUNTIME_IMAGE_PNG
uint8_t h
Definition bl0906.h:2
void feed_wdt(uint32_t time=0)
void deallocate(T *p, size_t n)
Definition helpers.h:1723
T * allocate(size_t n)
Definition helpers.h:1685
Class to abstract decoding different image formats.
virtual int prepare(size_t expected_size)
Initialize the decoder.
int HOT decode(uint8_t *buffer, size_t size) override
PngDecoder(RuntimeImage *image)
Construct a new PNG Decoder object.
RAMAllocator< pngle_t > allocator_
Definition png_decoder.h:32
int prepare(size_t expected_size) override
A dynamic image that can be loaded and decoded at runtime.
size_t size
Definition helpers.h:729
Application App
Global storage of Application pointer - only one Application can exist.
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6