ESPHome  2024.11.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 void ImageDecoder::set_size(int width, int height) {
12  this->image_->resize_(width, height);
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 }
16 
17 void ImageDecoder::draw(int x, int y, int w, int h, const Color &color) {
18  auto width = std::min(this->image_->buffer_width_, static_cast<int>(std::ceil((x + w) * this->x_scale_)));
19  auto height = std::min(this->image_->buffer_height_, static_cast<int>(std::ceil((y + h) * this->y_scale_)));
20  for (int i = x * this->x_scale_; i < width; i++) {
21  for (int j = y * this->y_scale_; j < height; j++) {
22  this->image_->draw_pixel_(i, j, color);
23  }
24  }
25 }
26 
27 uint8_t *DownloadBuffer::data(size_t offset) {
28  if (offset > this->size_) {
29  ESP_LOGE(TAG, "Tried to access beyond download buffer bounds!!!");
30  return this->buffer_;
31  }
32  return this->buffer_ + offset;
33 }
34 
35 size_t DownloadBuffer::read(size_t len) {
36  this->unread_ -= len;
37  if (this->unread_ > 0) {
38  memmove(this->data(), this->data(len), this->unread_);
39  }
40  return this->unread_;
41 }
42 
43 } // namespace online_image
44 } // namespace esphome
bool resize_(int width, int height)
void draw_pixel_(int x, int y, Color color)
Draw a pixel into the buffer.
int buffer_width_
Actual width of the current image.
Definition: online_image.h:138
uint16_t x
Definition: tt21100.cpp:17
int buffer_height_
Actual height of the current image.
Definition: online_image.h:147
uint8_t h
Definition: bl0906.h:209
uint16_t y
Definition: tt21100.cpp:18
void draw(int x, int y, int w, int h, const Color &color)
Draw a rectangle on the display_buffer using the defined color.
uint8_t * data(size_t offset=0)
std::string size_t len
Definition: helpers.h:293
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