ESPHome 2025.5.2
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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
6namespace esphome {
7namespace online_image {
8
9static const char *const TAG = "online_image.decoder";
10
11bool 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
18void 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
28DownloadBuffer::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
37uint8_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
45size_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
53size_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
uint8_t h
Definition bl0906.h:2
void deallocate(T *p, size_t n)
Definition helpers.h:742
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:770
T * allocate(size_t n)
Definition helpers.h:704
size_t unread_
Total number of downloaded bytes not yet read.
uint8_t * data(size_t offset=0)
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 resize_(int width, int height)
Resize the image buffer to the requested dimensions.
void draw_pixel_(int x, int y, Color color)
Draw a pixel into the buffer.
int buffer_width_
Actual width of the current image.
int buffer_height_
Actual height of the current image.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:302
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6