ESPHome  2024.10.2
image_decoder.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/defines.h"
3 #include "esphome/core/color.h"
4 
5 namespace esphome {
6 namespace online_image {
7 
8 class OnlineImage;
9 
13 class ImageDecoder {
14  public:
20  ImageDecoder(OnlineImage *image) : image_(image) {}
21  virtual ~ImageDecoder() = default;
22 
28  virtual void prepare(uint32_t download_size) { this->download_size_ = download_size; }
29 
41  virtual int decode(uint8_t *buffer, size_t size);
42 
50  void set_size(int width, int height);
51 
64  void draw(int x, int y, int w, int h, const Color &color);
65 
66  bool is_finished() const { return this->decoded_bytes_ == this->download_size_; }
67 
68  protected:
70  // Initializing to 1, to ensure it is different than initial "decoded_bytes_".
71  // Will be overwritten anyway once the download size is known.
72  uint32_t download_size_ = 1;
73  uint32_t decoded_bytes_ = 0;
74  double x_scale_ = 1.0;
75  double y_scale_ = 1.0;
76 };
77 
79  public:
80  DownloadBuffer(size_t size) : size_(size) {
81  this->buffer_ = this->allocator_.allocate(size);
82  this->reset();
83  }
84 
85  virtual ~DownloadBuffer() { this->allocator_.deallocate(this->buffer_, this->size_); }
86 
87  uint8_t *data(size_t offset = 0);
88 
89  uint8_t *append() { return this->data(this->unread_); }
90 
91  size_t unread() const { return this->unread_; }
92  size_t size() const { return this->size_; }
93  size_t free_capacity() const { return this->size_ - this->unread_; }
94 
95  size_t read(size_t len);
96  size_t write(size_t len) {
97  this->unread_ += len;
98  return this->unread_;
99  }
100 
101  void reset() { this->unread_ = 0; }
102 
103  protected:
105  uint8_t *buffer_;
106  size_t size_;
108  size_t unread_;
109 };
110 
111 } // namespace online_image
112 } // namespace esphome
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
Download an image from a given URL, and decode it using the specified decoder.
Definition: online_image.h:37
Class to abstract decoding different image formats.
Definition: image_decoder.h:13
ImageDecoder(OnlineImage *image)
Construct a new Image Decoder object.
Definition: image_decoder.h:20
virtual int decode(uint8_t *buffer, size_t size)
Decode a part of the image.
uint16_t reset
Definition: ina226.h:39
void draw(int x, int y, int w, int h, const Color &color)
Draw a rectangle on the display_buffer using the defined color.
ExternalRAMAllocator< uint8_t > allocator_
size_t unread_
Total number of downloaded bytes not yet read.
std::string size_t len
Definition: helpers.h:292
void set_size(int width, int height)
Request the image to be resized once the actual dimensions are known.
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7