ESPHome  2025.2.0
image_decoder.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/color.h"
3 
4 namespace esphome {
5 namespace online_image {
6 
7 enum DecodeError : int {
11 };
12 
13 class OnlineImage;
14 
18 class ImageDecoder {
19  public:
25  ImageDecoder(OnlineImage *image) : image_(image) {}
26  virtual ~ImageDecoder() = default;
27 
34  virtual int prepare(size_t download_size) {
35  this->download_size_ = download_size;
36  return 0;
37  }
38 
50  virtual int decode(uint8_t *buffer, size_t size) = 0;
51 
60  bool set_size(int width, int height);
61 
74  void draw(int x, int y, int w, int h, const Color &color);
75 
76  bool is_finished() const { return this->decoded_bytes_ == this->download_size_; }
77 
78  protected:
80  // Initializing to 1, to ensure it is distinguishable from initial "decoded_bytes_".
81  // Will be overwritten anyway once the download size is known.
82  size_t download_size_ = 1;
83  size_t decoded_bytes_ = 0;
84  double x_scale_ = 1.0;
85  double y_scale_ = 1.0;
86 };
87 
89  public:
90  DownloadBuffer(size_t size);
91 
92  virtual ~DownloadBuffer() { this->allocator_.deallocate(this->buffer_, this->size_); }
93 
94  uint8_t *data(size_t offset = 0);
95 
96  uint8_t *append() { return this->data(this->unread_); }
97 
98  size_t unread() const { return this->unread_; }
99  size_t size() const { return this->size_; }
100  size_t free_capacity() const { return this->size_ - this->unread_; }
101 
102  size_t read(size_t len);
103  size_t write(size_t len) {
104  this->unread_ += len;
105  return this->unread_;
106  }
107 
108  void reset() { this->unread_ = 0; }
109 
110  size_t resize(size_t size);
111 
112  protected:
113  RAMAllocator<uint8_t> allocator_{};
114  uint8_t *buffer_;
115  size_t size_;
117  size_t unread_;
118 };
119 
120 } // namespace online_image
121 } // namespace esphome
uint16_t x
Definition: tt21100.cpp:17
virtual int prepare(size_t download_size)
Initialize the decoder.
Definition: image_decoder.h:34
uint8_t h
Definition: bl0906.h:209
uint16_t y
Definition: tt21100.cpp:18
virtual int decode(uint8_t *buffer, size_t size)=0
Decode a part of the image.
Download an image from a given URL, and decode it using the specified decoder.
Definition: online_image.h:39
Class to abstract decoding different image formats.
Definition: image_decoder.h:18
ImageDecoder(OnlineImage *image)
Construct a new Image Decoder object.
Definition: image_decoder.h:25
void draw(int x, int y, int w, int h, const Color &color)
Fill a rectangle on the display_buffer using the defined color.
bool set_size(int width, int height)
Request the image to be resized once the actual dimensions are known.
size_t unread_
Total number of downloaded bytes not yet read.
std::string size_t len
Definition: helpers.h:301
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7