2#ifdef USE_RUNTIME_IMAGE_JPEG
16 if (pixels ==
nullptr) {
23 for (
int source_y = 0; source_y <
h; source_y++) {
24 const int absolute_y =
y + source_y;
25 if (absolute_y < 0 || absolute_y >= this->
draw_height_) {
30 for (
int source_x = 0; source_x < w; source_x++) {
31 const int absolute_x =
x + source_x;
32 if (absolute_x < 0 || absolute_x >= this->
draw_width_) {
37 const uint8_t *pixel = pixels + (source_y * w + source_x) * 2;
38 for (uint16_t destination_y = y1; destination_y < y2; destination_y++) {
39 uint8_t *destination =
buffer + (destination_y * buffer_width + x1) * 2;
40 for (uint16_t destination_x = x1; destination_x < x2; destination_x++) {
41 destination[0] = pixel[first];
42 destination[1] = pixel[1 - first];
56static int draw_callback(JPEGDRAW *jpeg) {
59 if (decoder ==
nullptr) {
60 ESP_LOGE(TAG,
"Decoder pointer is null!");
66 if (decoder->uses_direct_rgb565()) {
67 decoder->draw_rgb565(jpeg->x, jpeg->y, jpeg->iWidth, jpeg->iHeight,
68 reinterpret_cast<const uint8_t *
>(jpeg->pPixels));
72 size_t height =
static_cast<size_t>(jpeg->iHeight);
73 size_t width =
static_cast<size_t>(jpeg->iWidth);
74 for (
size_t y = 0;
y < height;
y++) {
75 for (
size_t x = 0;
x < width;
x++) {
78 Color color(rg[1], rg[0], ba[1], ba[0]);
80 decoder->draw(jpeg->x +
x, jpeg->y +
y, 1, 1, color);
88 if (this->
jpeg_ ==
nullptr) {
89 ESP_LOGE(TAG,
"Failed to allocate memory for the JPEG decoder");
95 if (this->
jpeg_ ==
nullptr) {
96 ESP_LOGE(TAG,
"JPEG decoder engine not initialized!");
103 if (this->
jpeg_ ==
nullptr) {
104 ESP_LOGE(TAG,
"JPEG decoder engine not initialized!");
110 ESP_LOGV(TAG,
"Download not complete. Size: %zu/%zu",
size, this->expected_size_);
117 if (!this->
jpeg_->openRAM(buffer,
size, draw_callback)) {
118 ESP_LOGE(TAG,
"Could not open image for decoding: %d", this->
jpeg_->getLastError());
121 const int err = this->
start_();
125 if (!this->
jpeg_->decodeBegin(0, 0, this->scale_option_()))
126 return this->
fail_();
128 while ((result = this->
jpeg_->decodeRows(1)) == JPEG_DECODE_MORE)
131 return this->
fail_();
133 this->
jpeg_->close();
138 auto jpeg_type = this->
jpeg_->getJPEGType();
139 if (jpeg_type == JPEG_MODE_INVALID) {
140 ESP_LOGE(TAG,
"Unsupported JPEG image");
143 ESP_LOGD(TAG,
"Image size: %d x %d, bpp: %d", this->
jpeg_->getWidth(), this->jpeg_->getHeight(),
144 this->jpeg_->getBpp());
146 this->
jpeg_->setUserPointer(
this);
147 const int width = this->
jpeg_->getWidth();
148 const int height = this->
jpeg_->getHeight();
158 while (this->
scale_ < JPEG_SCALE_EIGHTH && width / (this->
scale_ * 2) >= buffer_width &&
159 height / (this->
scale_ * 2) >= buffer_height) {
162 const int decoded_width = (width + this->
scale_ - 1) / this->
scale_;
163 const int decoded_height = (height + this->
scale_ - 1) / this->
scale_;
165 ESP_LOGD(TAG,
"Decoding at 1/%d size, %d x %d", this->
scale_, decoded_width, decoded_height);
171 this->
jpeg_->setPixelType(RGB565_LITTLE_ENDIAN);
173 this->
jpeg_->setPixelType(RGB8888);
180 const int error = this->
jpeg_->getLastError();
181 this->
jpeg_->close();
184 ESP_LOGE(TAG,
"Reading the image failed");
187 ESP_LOGE(TAG,
"Error while decoding: %d", error);
189 case JPEG_ERROR_MEMORY:
191 case JPEG_UNSUPPORTED_FEATURE:
193 case JPEG_INVALID_FILE:
194 case JPEG_INVALID_PARAMETER:
196 case JPEG_DECODE_ERROR:
204 if (this->
source_ !=
nullptr) {
205 this->
jpeg_->close();
212 auto *decoder =
static_cast<JpegDecoder *
>(file->fHandle);
216 decoder->source_failed_ =
true;
224 auto *decoder =
static_cast<JpegDecoder *
>(file->fHandle);
226 decoder->source_failed_ =
true;
234 if (this->
jpeg_ ==
nullptr) {
235 ESP_LOGE(TAG,
"JPEG decoder engine not initialized!");
239 ESP_LOGE(TAG,
"Image size %zu cannot be read from a source", this->
expected_size_);
242 if (!source->
seek(0)) {
243 ESP_LOGE(TAG,
"Could not read the image");
250 ESP_LOGD(TAG,
"Could not open image for decoding: %d", this->
jpeg_->getLastError());
254 const int err = this->
start_();
256 this->
jpeg_->close();
260 if (!this->
jpeg_->decodeBegin(0, 0, this->scale_option_()))
261 return this->
fail_();
271 return this->
fail_();
272 if (result == JPEG_DECODE_MORE)
274 if (
result == JPEG_DECODE_NEED_DATA)
277 return this->
fail_();
279 this->
jpeg_->close();
void feed_wdt_if_watched()
Feed from code that may run off the loop task (a long download or image decode): on ESP32 only a task...
An STL allocator that uses SPI or internal RAM.
Input that a decoder reads itself as it goes, so the whole image never has to be in memory at once.
virtual int32_t read(uint8_t *buf, uint32_t len)=0
Read up to len bytes at the current position.
virtual bool seek(uint32_t position)=0
Move to a position counted from the start. Returns false on error.
Class to abstract decoding different image formats.
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.
virtual void reset()
Reset the decoder state, ending any decoding session.
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.
FixedVector< uint16_t > y_boundaries_
virtual int prepare(size_t expected_size)
Initialize the decoder, starting a new decoding session.
bool image_is_big_endian_() const
Image decoder specialization for JPEG images.
RAMUniquePtr< JPEGDEC > jpeg_
About 18 KB of decoder state: PSRAM first, nullptr if out of memory.
int prepare(size_t expected_size) override
int begin_source(DecodeSource *source) override
void draw_rgb565(int x, int y, int w, int h, const uint8_t *pixels)
int start_()
Set up an opened image for decoding. Returns 0, or a DecodeError.
static int32_t source_read(JPEGFILE *file, uint8_t *buf, int32_t len)
JpegDecoder(RuntimeImage *image)
Construct a new JPEG Decoder object.
int fail_()
The DecodeError for a failed read of the source or JPEGDEC's last error, closing the image.
int HOT decode(uint8_t *buffer, size_t size) override
int HOT decode_step() override
Decodes one row of MCUs (8 or 16 pixel rows), or waits part way through one for more of the source.
static int32_t source_seek(JPEGFILE *file, int32_t position)
static void source_close(void *handle)
A dynamic image that can be loaded and decoded at runtime.
int get_buffer_height() const
int get_buffer_width() const
ESPHOME_LOG_TAG(TAG, "image_decoder.jpeg")
@ DECODE_ERROR_INTERNAL_DECODER_ERROR
@ DECODE_ERROR_OUT_OF_MEMORY
@ DECODE_ERROR_UNSUPPORTED_FORMAT
@ DECODE_ERROR_INVALID_TYPE
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
Application App
Global storage of Application pointer - only one Application can exist.