2#ifdef USE_RUNTIME_IMAGE_JPEG
12#include "esp_task_wdt.h"
20 if (pixels ==
nullptr) {
27 for (
int source_y = 0; source_y <
h; source_y++) {
28 const int absolute_y =
y + source_y;
29 if (absolute_y < 0 || absolute_y >= this->
draw_height_) {
34 for (
int source_x = 0; source_x < w; source_x++) {
35 const int absolute_x =
x + source_x;
36 if (absolute_x < 0 || absolute_x >= this->
draw_width_) {
41 const uint8_t *pixel = pixels + (source_y * w + source_x) * 2;
42 for (uint16_t destination_y = y1; destination_y < y2; destination_y++) {
43 uint8_t *destination =
buffer + (destination_y * buffer_width + x1) * 2;
44 for (uint16_t destination_x = x1; destination_x < x2; destination_x++) {
45 destination[0] = pixel[first];
46 destination[1] = pixel[1 - first];
56static void feed_watchdog() {
58 if (esp_task_wdt_status(
nullptr) == ESP_OK) {
72static int draw_callback(JPEGDRAW *jpeg) {
73 JpegDecoder *decoder = (JpegDecoder *) jpeg->pUser;
75 if (decoder ==
nullptr) {
76 ESP_LOGE(TAG,
"Decoder pointer is null!");
81 if (decoder->uses_direct_rgb565()) {
82 decoder->draw_rgb565(jpeg->x, jpeg->y, jpeg->iWidth, jpeg->iHeight,
83 reinterpret_cast<const uint8_t *
>(jpeg->pPixels));
87 size_t height =
static_cast<size_t>(jpeg->iHeight);
88 size_t width =
static_cast<size_t>(jpeg->iWidth);
89 for (
size_t y = 0;
y < height;
y++) {
90 for (
size_t x = 0;
x < width;
x++) {
93 Color color(rg[1], rg[0], ba[1], ba[0]);
95 decoder->draw(jpeg->x +
x, jpeg->y +
y, 1, 1, color);
103 if (this->
jpeg_ ==
nullptr) {
104 ESP_LOGE(TAG,
"Failed to allocate memory for the JPEG decoder");
110 if (this->
jpeg_ ==
nullptr) {
111 ESP_LOGE(TAG,
"JPEG decoder engine not initialized!");
118 if (this->
jpeg_ ==
nullptr) {
119 ESP_LOGE(TAG,
"JPEG decoder engine not initialized!");
125 ESP_LOGV(TAG,
"Download not complete. Size: %zu/%zu",
size, this->expected_size_);
132 if (!this->
jpeg_->openRAM(buffer,
size, draw_callback)) {
133 ESP_LOGE(TAG,
"Could not open image for decoding: %d", this->
jpeg_->getLastError());
136 const int err = this->
start_();
140 if (!this->
jpeg_->decodeBegin(0, 0, this->scale_option_()))
141 return this->
fail_();
143 while ((result = this->
jpeg_->decodeRows(1)) == JPEG_DECODE_MORE)
146 return this->
fail_();
148 this->
jpeg_->close();
153 auto jpeg_type = this->
jpeg_->getJPEGType();
154 if (jpeg_type == JPEG_MODE_INVALID) {
155 ESP_LOGE(TAG,
"Unsupported JPEG image");
158 ESP_LOGD(TAG,
"Image size: %d x %d, bpp: %d", this->
jpeg_->getWidth(), this->jpeg_->getHeight(),
159 this->jpeg_->getBpp());
161 this->
jpeg_->setUserPointer(
this);
162 const int width = this->
jpeg_->getWidth();
163 const int height = this->
jpeg_->getHeight();
173 while (this->
scale_ < JPEG_SCALE_EIGHTH && width / (this->
scale_ * 2) >= buffer_width &&
174 height / (this->
scale_ * 2) >= buffer_height) {
177 const int decoded_width = (width + this->
scale_ - 1) / this->
scale_;
178 const int decoded_height = (height + this->
scale_ - 1) / this->
scale_;
180 ESP_LOGD(TAG,
"Decoding at 1/%d size, %d x %d", this->
scale_, decoded_width, decoded_height);
186 this->
jpeg_->setPixelType(RGB565_LITTLE_ENDIAN);
188 this->
jpeg_->setPixelType(RGB8888);
195 const int error = this->
jpeg_->getLastError();
196 this->
jpeg_->close();
199 ESP_LOGE(TAG,
"Reading the image failed");
202 ESP_LOGE(TAG,
"Error while decoding: %d", error);
204 case JPEG_ERROR_MEMORY:
206 case JPEG_UNSUPPORTED_FEATURE:
208 case JPEG_INVALID_FILE:
209 case JPEG_INVALID_PARAMETER:
211 case JPEG_DECODE_ERROR:
219 if (this->
source_ !=
nullptr) {
220 this->
jpeg_->close();
227 auto *decoder =
static_cast<JpegDecoder *
>(file->fHandle);
231 decoder->source_failed_ =
true;
239 auto *decoder =
static_cast<JpegDecoder *
>(file->fHandle);
241 decoder->source_failed_ =
true;
249 if (this->
jpeg_ ==
nullptr) {
250 ESP_LOGE(TAG,
"JPEG decoder engine not initialized!");
254 ESP_LOGE(TAG,
"Image size %zu cannot be read from a source", this->
expected_size_);
257 if (!source->
seek(0)) {
258 ESP_LOGE(TAG,
"Could not read the image");
265 ESP_LOGD(TAG,
"Could not open image for decoding: %d", this->
jpeg_->getLastError());
269 const int err = this->
start_();
271 this->
jpeg_->close();
275 if (!this->
jpeg_->decodeBegin(0, 0, this->scale_option_()))
276 return this->
fail_();
283 const int result = this->
jpeg_->decodeRows(1);
286 return this->
fail_();
287 if (result == JPEG_DECODE_MORE)
289 if (result == JPEG_DECODE_NEED_DATA)
292 return this->
fail_();
294 this->
jpeg_->close();
void feed_wdt()
Feed the task watchdog.
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.