ESPHome 2025.9.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
6#include "esphome/core/log.h"
7
8static const char *const TAG = "online_image.png";
9
10namespace esphome {
11namespace online_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
44 {
45 pngle_t *pngle = this->allocator_.allocate(1, PNGLE_T_SIZE);
46 if (!pngle) {
47 ESP_LOGE(TAG, "Failed to allocate memory for PNGLE engine!");
48 return;
49 }
50 memset(pngle, 0, PNGLE_T_SIZE);
51 pngle_reset(pngle);
52 this->pngle_ = pngle;
53 }
54}
55
57 if (this->pngle_) {
58 pngle_reset(this->pngle_);
59 this->allocator_.deallocate(this->pngle_, PNGLE_T_SIZE);
60 }
61}
62
63int PngDecoder::prepare(size_t download_size) {
64 ImageDecoder::prepare(download_size);
65 if (!this->pngle_) {
66 ESP_LOGE(TAG, "PNG decoder engine not initialized!");
68 }
69 pngle_set_user_data(this->pngle_, this);
70 pngle_set_init_callback(this->pngle_, init_callback);
71 pngle_set_draw_callback(this->pngle_, draw_callback);
72 return 0;
73}
74
75int HOT PngDecoder::decode(uint8_t *buffer, size_t size) {
76 if (!this->pngle_) {
77 ESP_LOGE(TAG, "PNG decoder engine not initialized!");
79 }
80 if (size < 256 && size < this->download_size_ - this->decoded_bytes_) {
81 ESP_LOGD(TAG, "Waiting for data");
82 return 0;
83 }
84 auto fed = pngle_feed(this->pngle_, buffer, size);
85 if (fed < 0) {
86 ESP_LOGE(TAG, "Error decoding image: %s", pngle_error(this->pngle_));
87 } else {
88 this->decoded_bytes_ += fed;
89 }
90 return fed;
91}
92
93} // namespace online_image
94} // namespace esphome
95
96#endif // USE_ONLINE_IMAGE_PNG_SUPPORT
uint8_t h
Definition bl0906.h:2
void deallocate(T *p, size_t n)
Definition helpers.h:876
T * allocate(size_t n)
Definition helpers.h:838
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:43
int HOT decode(uint8_t *buffer, size_t size) override
Definition png_image.cpp:75
int prepare(size_t download_size) override
Definition png_image.cpp:63
RAMAllocator< pngle_t > allocator_
Definition png_image.h:29
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6