ESPHome  2024.11.0
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 (size < 256 && size < this->download_size_ - this->decoded_bytes_) {
53  ESP_LOGD(TAG, "Waiting for data");
54  return 0;
55  }
56  auto fed = pngle_feed(this->pngle_, buffer, size);
57  if (fed < 0) {
58  ESP_LOGE(TAG, "Error decoding image: %s", pngle_error(this->pngle_));
59  } else {
60  this->decoded_bytes_ += fed;
61  }
62  return fed;
63 }
64 
65 } // namespace online_image
66 } // namespace esphome
67 
68 #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