ESPHome  2025.2.0
image_decoder.cpp
Go to the documentation of this file.
1 #include "image_decoder.h"
2 #include "online_image.h"
3 
4 #include "esphome/core/log.h"
5 
6 namespace esphome {
7 namespace online_image {
8 
9 static const char *const TAG = "online_image.decoder";
10 
11 bool ImageDecoder::set_size(int width, int height) {
12  bool success = this->image_->resize_(width, height) > 0;
13  this->x_scale_ = static_cast<double>(this->image_->buffer_width_) / width;
14  this->y_scale_ = static_cast<double>(this->image_->buffer_height_) / height;
15  return success;
16 }
17 
18 void ImageDecoder::draw(int x, int y, int w, int h, const Color &color) {
19  auto width = std::min(this->image_->buffer_width_, static_cast<int>(std::ceil((x + w) * this->x_scale_)));
20  auto height = std::min(this->image_->buffer_height_, static_cast<int>(std::ceil((y + h) * this->y_scale_)));
21  for (int i = x * this->x_scale_; i < width; i++) {
22  for (int j = y * this->y_scale_; j < height; j++) {
23  this->image_->draw_pixel_(i, j, color);
24  }
25  }
26 }
27 
28 DownloadBuffer::DownloadBuffer(size_t size) : size_(size) {
29  this->buffer_ = this->allocator_.allocate(size);
30  this->reset();
31  if (!this->buffer_) {
32  ESP_LOGE(TAG, "Initial allocation of download buffer failed!");
33  this->size_ = 0;
34  }
35 }
36 
37 uint8_t *DownloadBuffer::data(size_t offset) {
38  if (offset > this->size_) {
39  ESP_LOGE(TAG, "Tried to access beyond download buffer bounds!!!");
40  return this->buffer_;
41  }
42  return this->buffer_ + offset;
43 }
44 
45 size_t DownloadBuffer::read(size_t len) {
46  this->unread_ -= len;
47  if (this->unread_ > 0) {
48  memmove(this->data(), this->data(len), this->unread_);
49  }
50  return this->unread_;
51 }
52 
53 size_t DownloadBuffer::resize(size_t size) {
54  if (this->size_ >= size) {
55  // Avoid useless reallocations; if the buffer is big enough, don't reallocate.
56  return this->size_;
57  }
58  this->allocator_.deallocate(this->buffer_, this->size_);
59  this->buffer_ = this->allocator_.allocate(size);
60  this->reset();
61  if (this->buffer_) {
62  this->size_ = size;
63  return size;
64  } else {
65  ESP_LOGE(TAG, "allocation of %zu bytes failed. Biggest block in heap: %zu Bytes", size,
67  this->size_ = 0;
68  return 0;
69  }
70 }
71 
72 } // namespace online_image
73 } // namespace esphome
void draw_pixel_(int x, int y, Color color)
Draw a pixel into the buffer.
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition: helpers.h:748
int buffer_width_
Actual width of the current image.
Definition: online_image.h:166
uint16_t x
Definition: tt21100.cpp:17
T * allocate(size_t n)
Definition: helpers.h:703
int buffer_height_
Actual height of the current image.
Definition: online_image.h:175
uint8_t h
Definition: bl0906.h:209
uint16_t y
Definition: tt21100.cpp:18
RAMAllocator< uint8_t > allocator_
size_t resize_(int width, int height)
Resize the image buffer to the requested dimensions.
void deallocate(T *p, size_t n)
Definition: helpers.h:720
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.
uint8_t * data(size_t offset=0)
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