ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
qoi_decoder.cpp
Go to the documentation of this file.
1#include "qoi_decoder.h"
2
3#ifdef USE_RUNTIME_IMAGE_QOI
4
6#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
9
10#include <algorithm>
11#include <cinttypes>
12
13namespace esphome::runtime_image {
14
15ESPHOME_LOG_TAG(TAG, "image_decoder.qoi");
16
17constexpr uint8_t QOI_OP_RGB = 0b11111110;
18constexpr uint8_t QOI_OP_RGBA = 0b11111111;
19constexpr uint8_t QOI_OP_INDEX = 0b00000000; // 00xxxxxx
20constexpr uint8_t QOI_OP_DIFF = 0b01000000; // 01xxxxxx
21constexpr uint8_t QOI_OP_LUMA = 0b10000000; // 10xxxxxx
22constexpr uint8_t QOI_OP_RUN = 0b11000000; // 11xxxxxx
23
24constexpr uint8_t QOI_RGB_CHUNK_SIZE = 4;
25constexpr uint8_t QOI_RGBA_CHUNK_SIZE = 5;
26constexpr uint8_t QOI_LUMA_CHUNK_SIZE = 2;
27
28constexpr uint8_t QOI_MASK_OP = 0b11000000;
29constexpr uint8_t QOI_MASK_VALUE = 0b00111111;
30
31constexpr size_t QOI_HEADER_SIZE = 14;
32constexpr size_t QOI_COLOR_TABLE_SIZE = 64;
33
34inline size_t qoi_color_table_index(const Color &color) {
35 // QOI color hash function: (r * 3 + g * 5 + b * 7 + a * 11) % 64
36 return (color.r * 3 + color.g * 5 + color.b * 7 + color.w * 11) &
37 63; // modulo 64 is equivalent to bitwise AND with 63 (0b00111111)
38}
39
42 this->current_index_ = 0;
43 this->paint_index_ = 0;
44 this->width_ = 0;
45 this->height_ = 0;
46 this->bits_per_pixel_ = 0;
47 this->last_pixel_ = Color(0, 0, 0, 255);
48 if (this->color_table_) {
49 std::fill_n(this->color_table_.get(), QOI_COLOR_TABLE_SIZE, Color());
50 }
51}
52
53int HOT QoiDecoder::decode(uint8_t *buffer, size_t size) {
54 size_t index = 0;
55 if (this->current_index_ == 0) {
56 if (size < QOI_HEADER_SIZE) {
57 return 0; // Need more data for file header
58 }
59
67 // Check if the file is a QOI image
68 if (buffer[0] != 'q' || buffer[1] != 'o' || buffer[2] != 'i' || buffer[3] != 'f') {
69 ESP_LOGE(TAG, "Not a QOI file");
71 }
72
73 this->width_ = encode_uint32(buffer[4], buffer[5], buffer[6], buffer[7]);
74 this->height_ = encode_uint32(buffer[8], buffer[9], buffer[10], buffer[11]);
75 if (this->width_ == 0 || this->height_ == 0) {
76 ESP_LOGE(TAG, "Invalid image dimensions: (%zux%zu)", this->width_, this->height_);
78 }
79 uint8_t channels = buffer[12];
80 if (channels < 3 || channels > 4) {
81 ESP_LOGE(TAG, "Unsupported number of channels: %d", channels);
83 }
84 this->bits_per_pixel_ = channels * 8;
85 uint8_t colorspace = buffer[13];
86 if (colorspace > 1) {
87 ESP_LOGE(TAG, "Unsupported colorspace value: %d", colorspace);
89 }
90 ESP_LOGD(TAG, "QOI image header: width=%zu, height=%zu, channels=%d, colorspace=%d", this->width_, this->height_,
91 channels, colorspace);
92
93 if (!this->color_table_) {
94 this->color_table_ = std::make_unique<Color[]>(QOI_COLOR_TABLE_SIZE);
95 }
96
97 if (!this->set_size(this->width_, this->height_)) {
99 }
100
102 index = QOI_HEADER_SIZE;
103 } // Current_index == 0
104
105 Color color;
106 const size_t total_pixels = this->width_ * this->height_;
107 // A call that has run for DECODE_TIME_LIMIT_MS stops, and the caller passes the rest again later
108 const uint32_t start = millis();
109 uint32_t steps = 0;
110 while (index < size && this->paint_index_ < total_pixels) {
111 if (++steps % DECODE_TIME_CHECK_STEPS == 0 && millis() - start >= DECODE_TIME_LIMIT_MS)
112 break;
113 color = this->last_pixel_;
114 uint8_t byte = buffer[index];
115 if (byte == QOI_OP_RGB) {
116 if (size < index + QOI_RGB_CHUNK_SIZE) {
117 break; // Need more data for RGB chunk
118 }
119 index++;
120 color.r = buffer[index++];
121 color.g = buffer[index++];
122 color.b = buffer[index++];
123 this->draw(this->paint_index_ % this->width_, this->paint_index_ / this->width_, 1, 1, color);
124 this->paint_index_++;
125 } else if (byte == QOI_OP_RGBA) {
126 if (size < index + QOI_RGBA_CHUNK_SIZE) {
127 break; // Need more data for RGBA chunk
128 }
129 index++;
130 color.r = buffer[index++];
131 color.g = buffer[index++];
132 color.b = buffer[index++];
133 color.w = buffer[index++];
134 this->draw(this->paint_index_ % this->width_, this->paint_index_ / this->width_, 1, 1, color);
135 this->paint_index_++;
136 } else if ((byte & QOI_MASK_OP) == QOI_OP_RUN) {
137 // QOI run chunk
138 size_t run_length = (byte & QOI_MASK_VALUE) + 1; // run length is encoded in the lower 6 bits, plus one
139 // Draw the part of the run in each row it covers at once
140 while (run_length > 0) {
141 const size_t x = this->paint_index_ % this->width_;
142 const size_t count = std::min(run_length, this->width_ - x);
143 this->draw(x, this->paint_index_ / this->width_, count, 1, color);
144 this->paint_index_ += count;
145 run_length -= count;
146 }
147 index++;
148 } else if ((byte & QOI_MASK_OP) == QOI_OP_LUMA) {
149 if (size < index + QOI_LUMA_CHUNK_SIZE) {
150 break; // Need more data for LUMA chunk
151 }
152 index++;
153 uint8_t byte2 = buffer[index++];
154 uint8_t delta_g = (byte & QOI_MASK_VALUE) - 32;
155 color.r += delta_g - 8 + ((byte2 >> 4) & 0x0f);
156 color.g += delta_g;
157 color.b += delta_g - 8 + (byte2 & 0x0f);
158
159 this->draw(this->paint_index_ % this->width_, this->paint_index_ / this->width_, 1, 1, color);
160 this->paint_index_++;
161 } else if ((byte & QOI_MASK_OP) == QOI_OP_DIFF) {
162 color.r += ((byte >> 4) & 0x03) - 2;
163 color.g += ((byte >> 2) & 0x03) - 2;
164 color.b += (byte & 0x03) - 2;
165
166 this->draw(this->paint_index_ % this->width_, this->paint_index_ / this->width_, 1, 1, color);
167 this->paint_index_++;
168 index++;
169 } else if ((byte & QOI_MASK_OP) == QOI_OP_INDEX) {
170 color = this->color_table_[byte];
171 this->draw(this->paint_index_ % this->width_, this->paint_index_ / this->width_, 1, 1, color);
172 this->paint_index_++;
173 index++;
174 }
175 this->last_pixel_ = color;
176 this->color_table_[qoi_color_table_index(color)] = color;
177 }
178 if (this->paint_index_ >= total_pixels)
179 index = size; // All that follows the last pixel is the end marker
180 this->decoded_bytes_ += index;
181 return index;
182}
183
184} // namespace esphome::runtime_image
185
186#endif // USE_RUNTIME_IMAGE_QOI
virtual void reset()
Reset the decoder state, ending any decoding session.
void draw(int x, int y, int w, int h, const Color &color)
Fill a rectangle on the display_buffer using the defined color.
bool set_size(int width, int height)
Request the image to be resized once the actual dimensions are known.
int HOT decode(uint8_t *buffer, size_t size) override
std::unique_ptr< Color[]> color_table_
Definition qoi_decoder.h:35
unsigned count
Definition gif.cpp:51
uint32_t buffer
Definition gif.cpp:50
constexpr uint8_t QOI_LUMA_CHUNK_SIZE
constexpr uint8_t QOI_RGB_CHUNK_SIZE
ESPHOME_LOG_TAG(TAG, "image_decoder.bmp")
size_t qoi_color_table_index(const Color &color)
constexpr uint8_t QOI_OP_RUN
constexpr uint8_t QOI_OP_RGBA
constexpr uint8_t QOI_RGBA_CHUNK_SIZE
constexpr uint8_t QOI_OP_DIFF
constexpr uint8_t QOI_MASK_VALUE
constexpr uint8_t QOI_OP_LUMA
constexpr uint8_t QOI_MASK_OP
constexpr size_t QOI_HEADER_SIZE
constexpr size_t QOI_COLOR_TABLE_SIZE
constexpr uint8_t QOI_OP_RGB
constexpr uint8_t QOI_OP_INDEX
uint16_t size
Definition helpers.cpp:34
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:1051
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t
uint8_t w
Definition color.h:48
uint8_t g
Definition color.h:40
uint8_t b
Definition color.h:44
uint8_t r
Definition color.h:36
uint16_t x
Definition tt21100.cpp:5