ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
png_image.cpp
Go to the documentation of this file.
1#include "png_image.h"
2#ifdef USE_ONLINE_IMAGE_PNG_SUPPORT
3
7#include "esphome/core/log.h"
8
9static const char *const TAG = "online_image.png";
10
11namespace esphome {
12namespace online_image {
13
22static void init_callback(pngle_t *pngle, uint32_t w, uint32_t h) {
23 PngDecoder *decoder = (PngDecoder *) pngle_get_user_data(pngle);
24 decoder->set_size(w, h);
25}
26
38static void draw_callback(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint8_t rgba[4]) {
39 PngDecoder *decoder = (PngDecoder *) pngle_get_user_data(pngle);
40 Color color(rgba[0], rgba[1], rgba[2], rgba[3]);
41 decoder->draw(x, y, w, h, color);
42
43 // Feed watchdog periodically to avoid triggering during long decode operations.
44 // Feed every 1024 pixels to balance efficiency and responsiveness.
45 uint32_t pixels = w * h;
46 decoder->increment_pixels_decoded(pixels);
47 if ((decoder->get_pixels_decoded() % 1024) < pixels) {
48 App.feed_wdt();
49 }
50}
51
53 {
54 pngle_t *pngle = this->allocator_.allocate(1, PNGLE_T_SIZE);
55 if (!pngle) {
56 ESP_LOGE(TAG, "Failed to allocate memory for PNGLE engine!");
57 return;
58 }
59 memset(pngle, 0, PNGLE_T_SIZE);
60 pngle_reset(pngle);
61 this->pngle_ = pngle;
62 }
63}
64
66 if (this->pngle_) {
67 pngle_reset(this->pngle_);
68 this->allocator_.deallocate(this->pngle_, PNGLE_T_SIZE);
69 }
70}
71
72int PngDecoder::prepare(size_t download_size) {
73 ImageDecoder::prepare(download_size);
74 if (!this->pngle_) {
75 ESP_LOGE(TAG, "PNG decoder engine not initialized!");
77 }
78 pngle_set_user_data(this->pngle_, this);
79 pngle_set_init_callback(this->pngle_, init_callback);
80 pngle_set_draw_callback(this->pngle_, draw_callback);
81 return 0;
82}
83
84int HOT PngDecoder::decode(uint8_t *buffer, size_t size) {
85 if (!this->pngle_) {
86 ESP_LOGE(TAG, "PNG decoder engine not initialized!");
88 }
89 if (size < 256 && size < this->download_size_ - this->decoded_bytes_) {
90 ESP_LOGD(TAG, "Waiting for 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 online_image
103} // namespace esphome
104
105#endif // USE_ONLINE_IMAGE_PNG_SUPPORT
uint8_t h
Definition bl0906.h:2
void feed_wdt(uint32_t time=0)
void deallocate(T *p, size_t n)
Definition helpers.h:1332
T * allocate(size_t n)
Definition helpers.h:1294
Class to abstract decoding different image formats.
virtual int prepare(size_t download_size)
Initialize the decoder.
Download an image from a given URL, and decode it using the specified decoder.
PngDecoder(OnlineImage *image)
Construct a new PNG Decoder object.
Definition png_image.cpp:52
int HOT decode(uint8_t *buffer, size_t size) override
Definition png_image.cpp:84
int prepare(size_t download_size) override
Definition png_image.cpp:72
RAMAllocator< pngle_t > allocator_
Definition png_image.h:32
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
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