ESPHome 2026.3.0
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 uint32_t pos = this->get_position_(x, y);
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 this->buffer_[pos + 2] = color.w;
122 }
123 break;
124 }
126 uint32_t pos = this->get_position_(x, y);
127 Color mapped_color = color;
128 this->map_chroma_key(mapped_color);
129 this->buffer_[pos + 0] = mapped_color.r;
130 this->buffer_[pos + 1] = mapped_color.g;
131 this->buffer_[pos + 2] = mapped_color.b;
133 this->buffer_[pos + 3] = color.w;
134 }
135 break;
136 }
137 }
138}
139
142 if (color.g == 1 && color.r == 0 && color.b == 0) {
143 color.g = 0;
144 }
145 if (color.w < 0x80) {
146 color.r = 0;
147 color.g = this->type_ == image::IMAGE_TYPE_RGB565 ? 4 : 1;
148 color.b = 0;
149 }
150 }
151}
152
153void RuntimeImage::draw(int x, int y, display::Display *display, Color color_on, Color color_off) {
154 if (this->data_start_) {
155 // If we have a complete image, use the base class draw method
156 Image::draw(x, y, display, color_on, color_off);
157 } else if (this->placeholder_) {
158 // Show placeholder while the runtime image is not available
159 this->placeholder_->draw(x, y, display, color_on, color_off);
160 }
161 // If no image is loaded and no placeholder, nothing to draw
162}
163
164bool RuntimeImage::begin_decode(size_t expected_size) {
165 if (this->decoder_) {
166 ESP_LOGW(TAG, "Decoding already in progress");
167 return false;
168 }
169
170 this->decoder_ = this->create_decoder_();
171 if (!this->decoder_) {
172 ESP_LOGE(TAG, "Failed to create decoder for format %d", this->format_);
173 return false;
174 }
175
176 this->total_size_ = expected_size;
177 this->decoded_bytes_ = 0;
178
179 // Initialize decoder
180 int result = this->decoder_->prepare(expected_size);
181 if (result < 0) {
182 ESP_LOGE(TAG, "Failed to prepare decoder: %d", result);
183 this->decoder_ = nullptr;
184 return false;
185 }
186
187 return true;
188}
189
190int RuntimeImage::feed_data(uint8_t *data, size_t len) {
191 if (!this->decoder_) {
192 ESP_LOGE(TAG, "No decoder initialized");
193 return -1;
194 }
195
196 int consumed = this->decoder_->decode(data, len);
197 if (consumed > 0) {
198 this->decoded_bytes_ += consumed;
199 }
200
201 return consumed;
202}
203
205 if (!this->decoder_) {
206 return false;
207 }
208
209 // Finalize the image for display
210 if (!this->progressive_display_) {
211 // Only now make the image visible
212 this->width_ = this->buffer_width_;
213 this->height_ = this->buffer_height_;
214 this->data_start_ = this->buffer_;
215 }
216
217 // Clean up decoder
218 this->decoder_ = nullptr;
219
220 ESP_LOGD(TAG, "Decoding complete: %dx%d, %zu bytes", this->width_, this->height_, this->decoded_bytes_);
221 return true;
222}
223
225 if (!this->decoder_) {
226 return false;
227 }
228 return this->decoder_->is_finished();
229}
230
232 this->release_buffer_();
233 // Reset decoder separately — release() can be called from within the decoder
234 // (via set_size -> resize -> resize_buffer_), so we must not destroy the decoder here.
235 // The decoder lifecycle is managed by begin_decode()/end_decode().
236 this->decoder_ = nullptr;
237}
238
240 if (this->buffer_) {
241 ESP_LOGV(TAG, "Releasing buffer of size %zu", this->get_buffer_size_(this->buffer_width_, this->buffer_height_));
242 RAMAllocator<uint8_t> allocator;
243 allocator.deallocate(this->buffer_, this->get_buffer_size_(this->buffer_width_, this->buffer_height_));
244 this->buffer_ = nullptr;
245 this->data_start_ = nullptr;
246 this->width_ = 0;
247 this->height_ = 0;
248 this->buffer_width_ = 0;
249 this->buffer_height_ = 0;
250 }
251}
252
253size_t RuntimeImage::resize_buffer_(int width, int height) {
254 size_t new_size = this->get_buffer_size_(width, height);
255
256 if (this->buffer_ && this->buffer_width_ == width && this->buffer_height_ == height) {
257 // Buffer already allocated with correct size
258 return new_size;
259 }
260
261 // Release old buffer if dimensions changed
262 if (this->buffer_) {
263 this->release_buffer_();
264 }
265
266 ESP_LOGD(TAG, "Allocating buffer: %dx%d, %zu bytes", width, height, new_size);
267 RAMAllocator<uint8_t> allocator;
268 this->buffer_ = allocator.allocate(new_size);
269
270 if (!this->buffer_) {
271 ESP_LOGE(TAG, "Failed to allocate %zu bytes. Largest free block: %zu", new_size,
272 allocator.get_max_free_block_size());
273 return 0;
274 }
275
276 // Clear buffer
277 memset(this->buffer_, 0, new_size);
278
279 this->buffer_width_ = width;
280 this->buffer_height_ = height;
281
282 return new_size;
283}
284
285size_t RuntimeImage::get_buffer_size_(int width, int height) const {
286 return (this->get_bpp() * width + 7u) / 8u * height;
287}
288
289int RuntimeImage::get_position_(int x, int y) const { return (x + y * this->buffer_width_) * this->get_bpp() / 8; }
290
291std::unique_ptr<ImageDecoder> RuntimeImage::create_decoder_() {
292 switch (this->format_) {
293#ifdef USE_RUNTIME_IMAGE_BMP
294 case BMP:
295 return make_unique<BmpDecoder>(this);
296#endif
297#ifdef USE_RUNTIME_IMAGE_JPEG
298 case JPEG:
299 return make_unique<JpegDecoder>(this);
300#endif
301#ifdef USE_RUNTIME_IMAGE_PNG
302 case PNG:
303 return make_unique<PngDecoder>(this);
304#endif
305 default:
306 ESP_LOGE(TAG, "Unsupported image format: %d", this->format_);
307 return nullptr;
308 }
309}
310
311} // namespace esphome::runtime_image
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:1899
void deallocate(T *p, size_t n)
Definition helpers.h:1954
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:1982
T * allocate(size_t n)
Definition helpers.h:1916
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
Transparency transparency_
Definition image.h:56
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:892
size_t size_t pos
Definition helpers.h:929
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