ESPHome 2026.9.0-dev
Loading...
Searching...
No Matches
image_decoder.h
Go to the documentation of this file.
1#pragma once
3#include "image_format.h"
4
6
13
14constexpr const char *decode_error_to_string(int error) {
15 switch (error) {
17 return "Invalid type";
19 return "Unsupported format";
21 return "Out of memory";
23 return "Internal decoder error";
24 default:
25 return "Unknown error";
26 }
27}
28
29class RuntimeImage;
30
35 public:
43 virtual ~ImageDecoder() = default;
44
46 ImageFormat get_format() const { return this->format_; }
47
49 bool is_active() const { return this->active_; }
50
56 virtual void reset() {
57 this->active_ = false;
58 this->expected_size_ = 0;
59 this->decoded_bytes_ = 0;
60 this->size_valid_ = true;
61 this->x_scale_ = 1.0;
62 this->y_scale_ = 1.0;
63 }
64
71 virtual int prepare(size_t expected_size) {
72 this->reset();
73 this->expected_size_ = expected_size;
74 this->active_ = true;
75 return 0;
76 }
77
89 virtual int decode(uint8_t *buffer, size_t size) = 0;
90
99 bool set_size(int width, int height);
100
113 void draw(int x, int y, int w, int h, const Color &color);
114
121 virtual bool is_finished() const {
122 if (this->expected_size_ > 0) {
123 return this->decoded_bytes_ >= this->expected_size_;
124 }
125 // If size is unknown, derived classes should override this
126 return false;
127 }
128
129 protected:
130 double x_scale_ = 1.0;
131 double y_scale_ = 1.0;
133 size_t expected_size_ = 0; // Expected data size (0 if unknown)
134 size_t decoded_bytes_ = 0; // Bytes processed so far
136 bool active_ = false; // A decoding session is in progress
137 bool size_valid_ = true; // Last set_size() result; draw() no-ops while false
138};
139
140} // namespace esphome::runtime_image
uint8_t h
Definition bl0906.h:2
Class to abstract decoding different image formats.
virtual void reset()
Reset the decoder state, ending any decoding session.
ImageDecoder(RuntimeImage *image, ImageFormat format)
Construct a new Image Decoder object.
bool is_active() const
Check if a decoding session is in progress (prepare() called, reset() not yet).
virtual bool is_finished() const
Check if the decoder has finished processing.
virtual int decode(uint8_t *buffer, size_t size)=0
Decode a part of the image.
void draw(int x, int y, int w, int h, const Color &color)
Fill a rectangle on the display_buffer using the defined color.
ImageFormat get_format() const
Get the image format handled by this decoder.
bool set_size(int width, int height)
Request the image to be resized once the actual dimensions are known.
virtual int prepare(size_t expected_size)
Initialize the decoder, starting a new decoding session.
A dynamic image that can be loaded and decoded at runtime.
const char * format
ImageFormat
Image format types that can be decoded dynamically.
constexpr const char * decode_error_to_string(int error)
uint16_t size
Definition helpers.cpp:25
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6