ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
image_decoder.cpp
Go to the documentation of this file.
1#include "image_decoder.h"
2#include "runtime_image.h"
4#include "esphome/core/log.h"
5#include <algorithm>
6
8
9ESPHOME_LOG_TAG(TAG, "image_decoder");
10
11bool ImageDecoder::set_size(int width, int height) {
12 if (!this->resize_image_(width, height))
13 return false;
14 this->prepare_draw_(width, height);
15 return true;
16}
17
18bool ImageDecoder::resize_image_(int width, int height) {
19 this->size_valid_ = this->image_->resize(width, height) > 0;
20 return this->size_valid_;
21}
22
23void ImageDecoder::prepare_draw_(int width, int height) {
24 this->draw_width_ = width;
25 this->draw_height_ = height;
27 if (!this->direct_rgb565_)
28 return;
29 if ((this->x_boundaries_.capacity() < static_cast<size_t>(width + 1) && !this->x_boundaries_.try_init(width + 1)) ||
30 (this->y_boundaries_.capacity() < static_cast<size_t>(height + 1) && !this->y_boundaries_.try_init(height + 1))) {
31 ESP_LOGW(TAG, "No memory to draw straight into the buffer; drawing pixel by pixel");
32 this->direct_rgb565_ = false;
33 // Free the table that did fit, as nothing uses it
34 this->x_boundaries_.release();
35 this->y_boundaries_.release();
36 return;
37 }
38 this->x_boundaries_.clear();
39 this->y_boundaries_.clear();
40 const int buffer_width = this->image_->get_buffer_width();
41 const int buffer_height = this->image_->get_buffer_height();
42 // The same boundaries as draw() uses, so both paths paint the same pixels
43 for (int x = 0; x <= width; x++) {
44 this->x_boundaries_.push_back(static_cast<uint16_t>(scale_boundary(x, width, buffer_width)));
45 }
46 for (int y = 0; y <= height; y++) {
47 this->y_boundaries_.push_back(static_cast<uint16_t>(scale_boundary(y, height, buffer_height)));
48 }
49}
50
51void ImageDecoder::draw_rgb565_(int x, int y, int w, int h, uint16_t rgb565) {
52 const int x_end = std::min(x + w, this->draw_width_);
53 const int y_end = std::min(y + h, this->draw_height_);
54 x = std::max(x, 0);
55 y = std::max(y, 0);
56 if (x >= x_end || y >= y_end)
57 return;
58 // A block of source pixels lands on one block of the buffer
59 const uint16_t x1 = this->x_boundaries_[x];
60 const uint16_t x2 = this->x_boundaries_[x_end];
61 const uint16_t y1 = this->y_boundaries_[y];
62 const uint16_t y2 = this->y_boundaries_[y_end];
63 const bool big_endian = this->image_is_big_endian_();
64 const uint8_t first = big_endian ? rgb565 >> 8 : rgb565 & 0xFF;
65 const uint8_t second = big_endian ? rgb565 & 0xFF : rgb565 >> 8;
66 uint8_t *const buffer = this->image_buffer_();
67 const uint32_t buffer_width = this->image_->get_buffer_width();
68 for (uint32_t row = y1; row < y2; row++) {
69 uint8_t *destination = buffer + (row * buffer_width + x1) * 2;
70 for (uint32_t col = x1; col < x2; col++) {
71 destination[0] = first;
72 destination[1] = second;
73 destination += 2;
74 }
75 }
76}
77
78void ImageDecoder::draw(int x, int y, int w, int h, const Color &color) {
79 if (!this->size_valid_) {
80 return;
81 }
82 if (this->direct_rgb565_) {
83 this->draw_rgb565_(x, y, w, h, display::ColorUtil::color_to_565(color));
84 return;
85 }
86 // When shrinking, each target pixel is painted once, by the first source pixel that reaches it, rather than by every
87 // pixel that lands on it. Integer maths: the S3 has no double FPU.
88 const int buffer_width = this->image_->get_buffer_width();
89 const int buffer_height = this->image_->get_buffer_height();
90 int x1 = x;
91 int y1 = y;
92 int x2 = x + w;
93 int y2 = y + h;
94 if (buffer_width != this->draw_width_ || buffer_height != this->draw_height_) {
95 x1 = scale_boundary(x, this->draw_width_, buffer_width);
96 y1 = scale_boundary(y, this->draw_height_, buffer_height);
97 x2 = scale_boundary(x + w, this->draw_width_, buffer_width);
98 y2 = scale_boundary(y + h, this->draw_height_, buffer_height);
99 }
100 x2 = std::min(buffer_width, x2);
101 y2 = std::min(buffer_height, y2);
102 for (int i = x1; i < x2; i++) {
103 for (int j = y1; j < y2; j++) {
104 this->image_->draw_pixel(i, j, color);
105 }
106 }
107}
108
109uint8_t *ImageDecoder::image_buffer_() const { return this->image_->buffer_; }
111
112} // namespace esphome::runtime_image
uint8_t h
Definition bl0906.h:2
size_t capacity() const
Definition helpers.h:847
void push_back(const T &value)
Add element without bounds checking Caller must ensure sufficient capacity was allocated via init() S...
Definition helpers.h:796
bool has_transparency() const
Definition image.h:43
ImageType get_type() const
Definition image.cpp:309
FixedVector< uint16_t > x_boundaries_
void prepare_draw_(int width, int height)
Draw a source of width x height over the whole buffer from now on.
void draw_rgb565_(int x, int y, int w, int h, uint16_t rgb565)
Paint a block of source pixels in one color straight into the buffer. Only while uses_direct_rgb565()...
bool resize_image_(int width, int height)
Resize the image for a width x height source, without preparing to draw. prepare_draw_() must follow.
uint8_t * image_buffer_() const
The image's pixel buffer, for decoders that write their output straight into it.
void draw(int x, int y, int w, int h, const Color &color)
Fill a rectangle on the display_buffer using the defined color.
FixedVector< uint16_t > y_boundaries_
bool set_size(int width, int height)
Request the image to be resized once the actual dimensions are known.
int resize(int width, int height)
Resize the image buffer to the requested dimensions.
void draw_pixel(int x, int y, const Color &color)
uint8_t second
uint32_t buffer
Definition gif.cpp:50
@ IMAGE_TYPE_RGB565
Definition image.h:15
ESPHOME_LOG_TAG(TAG, "image_decoder.bmp")
int scale_boundary(int n, int source, int target)
The first of target pixels that pixel n of source paints, rounded up: ceil(n * target / source).
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6