ESPHome  2024.12.2
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/helpers.h"
7 #include "esphome/core/log.h"
8 
9 static const char *const TAG = "online_image.png";
10 
11 namespace esphome {
12 namespace online_image {
13 
22 static 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 
38 static void draw_callback(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, 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 
44 void PngDecoder::prepare(uint32_t download_size) {
45  ImageDecoder::prepare(download_size);
46  pngle_set_user_data(this->pngle_, this);
47  pngle_set_init_callback(this->pngle_, init_callback);
48  pngle_set_draw_callback(this->pngle_, draw_callback);
49 }
50 
51 int HOT PngDecoder::decode(uint8_t *buffer, size_t size) {
52  if (!this->pngle_) {
53  ESP_LOGE(TAG, "PNG decoder engine not initialized!");
54  return -1;
55  }
56  if (size < 256 && size < this->download_size_ - this->decoded_bytes_) {
57  ESP_LOGD(TAG, "Waiting for data");
58  return 0;
59  }
60  auto fed = pngle_feed(this->pngle_, buffer, size);
61  if (fed < 0) {
62  ESP_LOGE(TAG, "Error decoding image: %s", pngle_error(this->pngle_));
63  } else {
64  this->decoded_bytes_ += fed;
65  }
66  return fed;
67 }
68 
69 } // namespace online_image
70 } // namespace esphome
71 
72 #endif // USE_ONLINE_IMAGE_PNG_SUPPORT
virtual void prepare(uint32_t download_size)
Initialize the decoder.
Definition: image_decoder.h:28
uint16_t x
Definition: tt21100.cpp:17
uint8_t h
Definition: bl0906.h:209
uint16_t y
Definition: tt21100.cpp:18
int HOT decode(uint8_t *buffer, size_t size) override
Definition: png_image.cpp:51
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void prepare(uint32_t download_size) override
Definition: png_image.cpp:44