11#ifdef USE_RUNTIME_IMAGE_BMP
14#ifdef USE_RUNTIME_IMAGE_JPEG
17#ifdef USE_RUNTIME_IMAGE_PNG
20#ifdef USE_RUNTIME_IMAGE_QOI
29static constexpr int MAX_IMAGE_DIMENSION = 32767;
30static constexpr int MAX_IMAGE_BPP = 32;
31static_assert((
static_cast<uint64_t
>(MAX_IMAGE_BPP) * MAX_IMAGE_DIMENSION + 7) / 8 * MAX_IMAGE_DIMENSION <=
32 std::numeric_limits<size_t>::max(),
33 "MAX_IMAGE_DIMENSION must keep the worst-case buffer size within size_t");
41 return ((color.
r >> 2) + (color.
g >> 1) + (color.
b >> 2)) & 0x80;
45 image::Image *placeholder,
bool is_big_endian,
int fixed_width,
int fixed_height)
46 : Image(nullptr, 0, 0,
type, transparency, is_big_endian),
48 fixed_width_(fixed_width),
49 fixed_height_(fixed_height),
50 placeholder_(placeholder) {}
56 this->lvgl_widgets_.clear();
70 target_width =
static_cast<int>(width * scale);
71 target_height =
static_cast<int>(height * scale);
87 ESP_LOGE(TAG,
"Buffer not allocated!");
91 ESP_LOGE(TAG,
"Tried to paint a pixel (%d,%d) outside the image!",
x,
y);
95 switch (this->
type_) {
99 auto bitno = 0x80 >> (
pos % 8u);
108 uint8_t bits = (color.
r >> 7) << 2 | (color.
g >> 7) << 1 | (color.
b >> 7);
110 bits = bits << 1 | (color.
w >> 7);
113 uint8_t *
byte = this->
buffer_ + bitpos / 8;
115 const uint8_t shift = 16 - this->bpp_ - bitpos % 8;
116 const uint16_t mask = ((1u << this->
bpp_) - 1) << shift;
117 const uint16_t
value = bits << shift;
118 byte[0] = (
byte[0] & ~(mask >> 8)) | (
value >> 8);
120 byte[1] = (
byte[1] & ~(mask & 0xFF)) | (
value & 0xFF);
127 auto gray =
static_cast<uint8_t
>((54 * color.
r + 183 * color.
g + 19 * color.
b) >> 8);
132 if (color.
w < 0x80) {
144 Color mapped_color = color;
146 uint16_t rgb565 = display::ColorUtil::color_to_565(mapped_color);
148 this->
buffer_[pos + 0] =
static_cast<uint8_t
>((rgb565 >> 8) & 0xFF);
149 this->
buffer_[pos + 1] =
static_cast<uint8_t
>(rgb565 & 0xFF);
151 this->
buffer_[pos + 0] =
static_cast<uint8_t
>(rgb565 & 0xFF);
152 this->
buffer_[pos + 1] =
static_cast<uint8_t
>((rgb565 >> 8) & 0xFF);
162 Color mapped_color = color;
164 this->
buffer_[pos + 0] = mapped_color.
b;
165 this->
buffer_[pos + 1] = mapped_color.
g;
166 this->
buffer_[pos + 2] = mapped_color.
r;
177 if (color.
g == 1 && color.
r == 0 && color.
b == 0) {
180 if (color.
w < 0x80) {
194 return Image::get_lv_image_dsc();
200 if (std::find(this->
lvgl_widgets_.begin(), this->lvgl_widgets_.end(), obj) != this->lvgl_widgets_.end())
207 auto *image =
static_cast<RuntimeImage *
>(lv_event_get_user_data(event));
209 widgets.erase(std::remove(widgets.begin(), widgets.end(), lv_event_get_target_obj(event)), widgets.end());
219 for (
size_t i = 0; i < widgets.size();) {
220 lv_obj_t *obj = widgets[i];
221 if (lv_image_get_src(obj) == dsc) {
223 ::lv_image_set_src(obj, dsc);
227 widgets.erase(widgets.begin() + i);
237 Image::draw(
x,
y, display, color_on, color_off);
247 ESP_LOGW(TAG,
"Decoding already in progress");
258 ESP_LOGD(TAG,
"Decoder format mismatch: current: %s, new: %s",
273 int result = this->
decoder_->prepare(expected_size);
275 ESP_LOGE(TAG,
"Failed to prepare decoder: %d", result);
284 ESP_LOGE(TAG,
"No decoder initialized");
298 ESP_LOGE(TAG,
"Decoder cannot read a source");
301 return this->
decoder_->begin_source(source);
306 ESP_LOGE(TAG,
"No decoder initialized");
309 const int result = this->
decoder_->decode_step();
322 if (this->
buffer_ !=
nullptr) {
405 ESP_LOGE(TAG,
"An external buffer cannot be double buffered");
412 ESP_LOGE(TAG,
"Refusing an invalid external buffer for %dx%d", width, height);
434 ESP_LOGE(TAG,
"Image decoded to %dx%d, but the external buffer is %dx%d", width, height, this->
buffer_width_,
443 ESP_LOGE(TAG,
"Refusing to allocate buffer for invalid image dimensions %dx%d", width, height);
452 ESP_LOGD(TAG,
"Allocating buffer: %dx%d, %zu bytes", width, height, new_size);
457 ESP_LOGE(TAG,
"Failed to allocate %zu bytes. Largest free block: %zu", new_size,
463 memset(this->
buffer_, 0, new_size);
473 if (width <= 0 || height <= 0 || width > MAX_IMAGE_DIMENSION || height > MAX_IMAGE_DIMENSION) {
478 return static_cast<size_t>(width) * height * 3;
480 return (
static_cast<size_t>(this->
get_bpp()) * width + 7u) / 8u * height;
488#ifdef USE_RUNTIME_IMAGE_BMP
490 return make_unique<BmpDecoder>(
this);
492#ifdef USE_RUNTIME_IMAGE_JPEG
494 return make_unique<JpegDecoder>(
this);
496#ifdef USE_RUNTIME_IMAGE_PNG
498 return make_unique<PngDecoder>(
this);
500#ifdef USE_RUNTIME_IMAGE_QOI
502 return make_unique<QoiDecoder>(
this);
505 ESP_LOGE(TAG,
"Image format could not be determined; set `format:` explicitly in the configuration");
An STL allocator that uses SPI or internal RAM.
void deallocate(T *p, size_t n)
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
const uint8_t * data_start_
bool has_transparency() const
ImageType get_type() const
Transparency transparency_
virtual lv_image_dsc_t * get_lv_image_dsc()
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
Input that a decoder reads itself as it goes, so the whole image never has to be in memory at once.
A dynamic image that can be loaded and decoded at runtime.
int decode_step()
Decode a little more from the source; see ImageDecoder::decode_step().
static void lvgl_widget_deleted(lv_event_t *event)
bool begin_decode(size_t expected_size=0, ImageFormat format=AUTO)
Begin decoding an image.
bool is_decoding() const
Check if decoding is currently in progress.
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
size_t get_buffer_size(int width, int height) const
Get the buffer size in bytes needed for a picture of the given dimensions.
int buffer_width_
Actual width of the current image.
int begin_source(DecodeSource *source)
Decode from a source the decoder reads itself, in place of feed_data().
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.
void abort_decode()
End a decode that will not be finished.
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 its memory, ending any decode session.
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.
bool decoder_knows_end() const
True while decoding and the decoder can tell a complete image from a cut-off one.
bool is_double_buffered_() const
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_
bool progressive_display_
lv_image_dsc_t * get_lv_image_dsc() override
Until an image is loaded, the placeholder's descriptor if there is one.
std::vector< lv_obj_t * > lvgl_widgets_
Widgets given this image. One moved to another image is dropped at the next refresh.
void draw_pixel(int x, int y, const Color &color)
std::unique_ptr< ImageDecoder > create_decoder_(ImageFormat format)
Create decoder instance for the requested format.
int buffer_height_
Actual height of the current image.
uint8_t * front_buffer_
With double buffering, the buffer on show, width_ by height_. buffer_ is then the one decoded into.
bool decoder_supports_source() const
Whether the active decoder can read a DecodeSource itself (see begin_source()).
bool set_external_buffer(uint8_t *buffer, int width, int height)
Decode into a buffer the caller owns, instead of one allocated here.
void refresh_lvgl_()
Bring the LVGL descriptor up to date after the image shown changed, and give it again to the widgets ...
void map_chroma_key(Color &color)
void add_lvgl_widget(lv_obj_t *obj) override
bool external_buffer_
Whether buffer_ belongs to the caller, so it must not be freed or resized here.
@ TRANSPARENCY_ALPHA_CHANNEL
@ TRANSPARENCY_CHROMA_KEY
@ DECODE_ERROR_UNSUPPORTED_FORMAT
@ DECODE_ERROR_INVALID_TYPE
bool is_color_on(const Color &color)
ESPHOME_LOG_TAG(TAG, "image_decoder.bmp")
ImageFormat
Image format types that can be decoded dynamically.
@ AUTO
Format is supplied per decode, e.g.
const LogString * get_format_name(ImageFormat format)
Human-readable name for a format; "UNKNOWN" if not recognized.
ESPHOME_ALWAYS_INLINE constexpr void assign_bits(T &value, std::type_identity_t< T > mask, bool on)
Set the bits of mask in value when on is true, clear them otherwise.