ESPHome 2026.4.0-dev
Loading...
Searching...
No Matches
runtime_image.cpp
Go to the documentation of this file.
1#include "runtime_image.h"
2#include "image_decoder.h"
3#include "esphome/core/log.h"
5#include <algorithm>
6#include <cstring>
7
8#ifdef USE_RUNTIME_IMAGE_BMP
9#include "bmp_decoder.h"
10#endif
11#ifdef USE_RUNTIME_IMAGE_JPEG
12#include "jpeg_decoder.h"
13#endif
14#ifdef USE_RUNTIME_IMAGE_PNG
15#include "png_decoder.h"
16#endif
17
18namespace esphome::runtime_image {
19
20static const char *const TAG = "runtime_image";
21
22inline bool is_color_on(const Color &color) {
23 // This produces the most accurate monochrome conversion, but is slightly slower.
24 // return (0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b) > 127;
25
26 // Approximation using fast integer computations; produces acceptable results
27 // Equivalent to 0.25 * R + 0.5 * G + 0.25 * B
28 return ((color.r >> 2) + (color.g >> 1) + (color.b >> 2)) & 0x80;
29}
30
32 image::Image *placeholder, bool is_big_endian, int fixed_width, int fixed_height)
33 : Image(nullptr, 0, 0, type, transparency),
34 format_(format),
35 fixed_width_(fixed_width),
36 fixed_height_(fixed_height),
37 placeholder_(placeholder),
38 is_big_endian_(is_big_endian) {}
39
41
42int RuntimeImage::resize(int width, int height) {
43 // Use fixed dimensions if specified (0 means auto-resize)
44 int target_width = this->fixed_width_ ? this->fixed_width_ : width;
45 int target_height = this->fixed_height_ ? this->fixed_height_ : height;
46
47 // When both fixed dimensions are set, scale uniformly to preserve aspect ratio
48 if (this->fixed_width_ && this->fixed_height_ && width > 0 && height > 0) {
49 float scale =
50 std::min(static_cast<float>(this->fixed_width_) / width, static_cast<float>(this->fixed_height_) / height);
51 target_width = static_cast<int>(width * scale);
52 target_height = static_cast<int>(height * scale);
53 }
54
55 size_t result = this->resize_buffer_(target_width, target_height);
56 if (result > 0 && this->progressive_display_) {
57 // Update display dimensions for progressive display
58 this->width_ = this->buffer_width_;
59 this->height_ = this->buffer_height_;
60 this->data_start_ = this->buffer_;
61 }
62 return result;
63}
64
65void RuntimeImage::draw_pixel(int x, int y, const Color &color) {
66 if (!this->buffer_) {
67 ESP_LOGE(TAG, "Buffer not allocated!");
68 return;
69 }
70 if (x < 0 || y < 0 || x >= this->buffer_width_ || y >= this->buffer_height_) {
71 ESP_LOGE(TAG, "Tried to paint a pixel (%d,%d) outside the image!", x, y);
72 return;
73 }
74
75 switch (this->type_) {
77 const uint32_t width_8 = ((this->buffer_width_ + 7u) / 8u) * 8u;
78 uint32_t pos = x + y * width_8;
79 auto bitno = 0x80 >> (pos % 8u);
80 pos /= 8u;
81 auto on = is_color_on(color);
82 if (this->has_transparency() && color.w < 0x80)
83 on = false;
84 if (on) {
85 this->buffer_[pos] |= bitno;
86 } else {
87 this->buffer_[pos] &= ~bitno;
88 }
89 break;
90 }
92 uint32_t pos = this->get_position_(x, y);
93 auto gray = static_cast<uint8_t>(0.2125 * color.r + 0.7154 * color.g + 0.0721 * color.b);
95 if (gray == 1) {
96 gray = 0;
97 }
98 if (color.w < 0x80) {
99 gray = 1;
100 }
102 if (color.w != 0xFF)
103 gray = color.w;
104 }
105 this->buffer_[pos] = gray;
106 break;
107 }
109 const size_t pos = (x + y * this->buffer_width_) * 2;
110 Color mapped_color = color;
111 this->map_chroma_key(mapped_color);
112 uint16_t rgb565 = display::ColorUtil::color_to_565(mapped_color);
113 if (this->is_big_endian_) {
114 this->buffer_[pos + 0] = static_cast<uint8_t>((rgb565 >> 8) & 0xFF);
115 this->buffer_[pos + 1] = static_cast<uint8_t>(rgb565 & 0xFF);
116 } else {
117 this->buffer_[pos + 0] = static_cast<uint8_t>(rgb565 & 0xFF);
118 this->buffer_[pos + 1] = static_cast<uint8_t>((rgb565 >> 8) & 0xFF);
119 }
121 const size_t alpha_pos = pos / 2 + this->buffer_width_ * this->buffer_height_ * 2;
122 this->buffer_[alpha_pos] = color.w;
123 }
124 break;
125 }
127 uint32_t pos = this->get_position_(x, y);
128 Color mapped_color = color;
129 this->map_chroma_key(mapped_color);
130 this->buffer_[pos + 0] = mapped_color.r;
131 this->buffer_[pos + 1] = mapped_color.g;
132 this->buffer_[pos + 2] = mapped_color.b;
134 this->buffer_[pos + 3] = color.w;
135 }
136 break;
137 }
138 }
139}
140
143 if (color.g == 1 && color.r == 0 && color.b == 0) {
144 color.g = 0;
145 }
146 if (color.w < 0x80) {
147 color.r = 0;
148 color.g = this->type_ == image::IMAGE_TYPE_RGB565 ? 4 : 1;
149 color.b = 0;
150 }
151 }
152}
153
154void RuntimeImage::draw(int x, int y, display::Display *display, Color color_on, Color color_off) {
155 if (this->data_start_) {
156 // If we have a complete image, use the base class draw method
157 Image::draw(x, y, display, color_on, color_off);
158 } else if (this->placeholder_) {
159 // Show placeholder while the runtime image is not available
160 this->placeholder_->draw(x, y, display, color_on, color_off);
161 }
162 // If no image is loaded and no placeholder, nothing to draw
163}
164
165bool RuntimeImage::begin_decode(size_t expected_size) {
166 if (this->decoder_) {
167 ESP_LOGW(TAG, "Decoding already in progress");
168 return false;
169 }
170
171 this->decoder_ = this->create_decoder_();
172 if (!this->decoder_) {
173 ESP_LOGE(TAG, "Failed to create decoder for format %d", this->format_);
174 return false;
175 }
176
177 this->total_size_ = expected_size;
178 this->decoded_bytes_ = 0;
179
180 // Initialize decoder
181 int result = this->decoder_->prepare(expected_size);
182 if (result < 0) {
183 ESP_LOGE(TAG, "Failed to prepare decoder: %d", result);
184 this->decoder_ = nullptr;
185 return false;
186 }
187
188 return true;
189}
190
191int RuntimeImage::feed_data(uint8_t *data, size_t len) {
192 if (!this->decoder_) {
193 ESP_LOGE(TAG, "No decoder initialized");
194 return -1;
195 }
196
197 int consumed = this->decoder_->decode(data, len);
198 if (consumed > 0) {
199 this->decoded_bytes_ += consumed;
200 }
201
202 return consumed;
203}
204
206 if (!this->decoder_) {
207 return false;
208 }
209
210 // Finalize the image for display
211 if (!this->progressive_display_) {
212 // Only now make the image visible
213 this->width_ = this->buffer_width_;
214 this->height_ = this->buffer_height_;
215 this->data_start_ = this->buffer_;
216 }
217
218 // Clean up decoder
219 this->decoder_ = nullptr;
220
221 ESP_LOGD(TAG, "Decoding complete: %dx%d, %zu bytes", this->width_, this->height_, this->decoded_bytes_);
222 return true;
223}
224
226 if (!this->decoder_) {
227 return false;
228 }
229 return this->decoder_->is_finished();
230}
231
233 this->release_buffer_();
234 // Reset decoder separately — release() can be called from within the decoder
235 // (via set_size -> resize -> resize_buffer_), so we must not destroy the decoder here.
236 // The decoder lifecycle is managed by begin_decode()/end_decode().
237 this->decoder_ = nullptr;
238}
239
241 if (this->buffer_) {
242 ESP_LOGV(TAG, "Releasing buffer of size %zu", this->get_buffer_size_(this->buffer_width_, this->buffer_height_));
243 RAMAllocator<uint8_t> allocator;
244 allocator.deallocate(this->buffer_, this->get_buffer_size_(this->buffer_width_, this->buffer_height_));
245 this->buffer_ = nullptr;
246 this->data_start_ = nullptr;
247 this->width_ = 0;
248 this->height_ = 0;
249 this->buffer_width_ = 0;
250 this->buffer_height_ = 0;
251#ifdef USE_LVGL
252 memset(&this->dsc_, 0, sizeof(this->dsc_));
253#endif
254 }
255}
256
257size_t RuntimeImage::resize_buffer_(int width, int height) {
258 size_t new_size = this->get_buffer_size_(width, height);
259
260 if (this->buffer_ && this->buffer_width_ == width && this->buffer_height_ == height) {
261 // Buffer already allocated with correct size
262 return new_size;
263 }
264
265 // Release old buffer if dimensions changed
266 if (this->buffer_) {
267 this->release_buffer_();
268 }
269
270 ESP_LOGD(TAG, "Allocating buffer: %dx%d, %zu bytes", width, height, new_size);
271 RAMAllocator<uint8_t> allocator;
272 this->buffer_ = allocator.allocate(new_size);
273
274 if (!this->buffer_) {
275 ESP_LOGE(TAG, "Failed to allocate %zu bytes. Largest free block: %zu", new_size,
276 allocator.get_max_free_block_size());
277 return 0;
278 }
279
280 // Clear buffer
281 memset(this->buffer_, 0, new_size);
282
283 this->buffer_width_ = width;
284 this->buffer_height_ = height;
285
286 return new_size;
287}
288
289size_t RuntimeImage::get_buffer_size_(int width, int height) const {
291 // Add extra alpha channel for RGB565 with alpha
292 return width * height * 3;
293 }
294 return (this->get_bpp() * width + 7u) / 8u * height;
295}
296
297int RuntimeImage::get_position_(int x, int y) const { return (x + y * this->buffer_width_) * this->get_bpp() / 8; }
298
299std::unique_ptr<ImageDecoder> RuntimeImage::create_decoder_() {
300 switch (this->format_) {
301#ifdef USE_RUNTIME_IMAGE_BMP
302 case BMP:
303 return make_unique<BmpDecoder>(this);
304#endif
305#ifdef USE_RUNTIME_IMAGE_JPEG
306 case JPEG:
307 return make_unique<JpegDecoder>(this);
308#endif
309#ifdef USE_RUNTIME_IMAGE_PNG
310 case PNG:
311 return make_unique<PngDecoder>(this);
312#endif
313 default:
314 ESP_LOGE(TAG, "Unsupported image format: %d", this->format_);
315 return nullptr;
316 }
317}
318
319} // namespace esphome::runtime_image
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2153
void deallocate(T *p, size_t n)
Definition helpers.h:2208
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:2236
T * allocate(size_t n)
Definition helpers.h:2170
static uint16_t color_to_565(Color color, ColorOrder color_order=ColorOrder::COLOR_ORDER_RGB)
const uint8_t * data_start_
Definition image.h:55
int get_bpp() const
Definition image.h:34
bool has_transparency() const
Definition image.h:41
ImageType type_
Definition image.h:54
ImageType get_type() const
Definition image.cpp:227
Transparency transparency_
Definition image.h:56
lv_img_dsc_t dsc_
Definition image.h:60
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
Definition image.cpp:9
size_t get_buffer_size_(int width, int height) const
Get the buffer size in bytes for given dimensions.
bool is_big_endian_
Whether the image is stored in big-endian format.
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
int buffer_width_
Actual width of the current image.
bool end_decode()
Complete the decoding process.
int get_position_(int x, int y) const
Get the position in the buffer for a pixel.
const int fixed_height_
Fixed height requested on configuration, or 0 if not specified.
void release_buffer_()
Release only the image buffer without resetting the decoder.
int resize(int width, int height)
Resize the image buffer to the requested dimensions.
int feed_data(uint8_t *data, size_t len)
Feed data to the decoder.
const int fixed_width_
Fixed width requested on configuration, or 0 if not specified.
bool is_decode_finished() const
Check if the decoder has finished processing all data.
void release()
Release the image buffer and free memory.
image::Image * placeholder_
Placeholder image to show when the runtime image is not available.
RuntimeImage(ImageFormat format, image::ImageType type, image::Transparency transparency, image::Image *placeholder=nullptr, bool is_big_endian=false, int fixed_width=0, int fixed_height=0)
Construct a new RuntimeImage object.
const ImageFormat format_
The image format this RuntimeImage is configured to decode.
size_t resize_buffer_(int width, int height)
Resize the image buffer to the requested dimensions.
std::unique_ptr< ImageDecoder > decoder_
std::unique_ptr< ImageDecoder > create_decoder_()
Create decoder instance for the image's format.
void draw_pixel(int x, int y, const Color &color)
int buffer_height_
Actual height of the current image.
bool begin_decode(size_t expected_size=0)
Begin decoding an image.
uint16_t type
@ TRANSPARENCY_ALPHA_CHANNEL
Definition image.h:22
@ TRANSPARENCY_CHROMA_KEY
Definition image.h:21
@ IMAGE_TYPE_GRAYSCALE
Definition image.h:14
@ IMAGE_TYPE_BINARY
Definition image.h:13
@ IMAGE_TYPE_RGB565
Definition image.h:16
@ IMAGE_TYPE_RGB
Definition image.h:15
bool is_color_on(const Color &color)
ImageFormat
Image format types that can be decoded dynamically.
const char int const __FlashStringHelper * format
Definition log.h:74
std::string size_t len
Definition helpers.h:1045
size_t size_t pos
Definition helpers.h:1082
static void uint32_t
uint8_t w
Definition color.h:42
uint8_t g
Definition color.h:34
uint8_t b
Definition color.h:38
uint8_t r
Definition color.h:30
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6