ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
bmp_decoder.cpp
Go to the documentation of this file.
1#include "bmp_decoder.h"
2
3#ifdef USE_RUNTIME_IMAGE_BMP
4
6#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
9
10#include <cinttypes>
11
13
14ESPHOME_LOG_TAG(TAG, "image_decoder.bmp");
15
16// BMP has no alpha. Every pixel is drawn opaque, or a transparent image would hide it
17static constexpr Color OPAQUE_BLACK(0, 0, 0, 0xFF);
18
21 this->bits_per_pixel_ = 0;
22 this->compression_method_ = 0;
23 this->image_data_size_ = 0;
24 this->width_ = 0;
25 this->height_ = 0;
26 this->current_index_ = 0;
27 this->paint_index_ = 0;
28 // color_table_ is deliberately kept allocated so the next decode can reuse it
29 this->color_table_entries_ = 0;
30 this->data_offset_ = 0;
31 this->padding_bytes_ = 0;
32 this->width_bytes_ = 0;
33}
34
35int HOT BmpDecoder::decode(uint8_t *buffer, size_t size) {
36 size_t index = 0;
37 if (this->current_index_ == 0) {
38 if (size <= 14) {
39 return 0; // Need more data for file header
40 }
51 // Check if the file is a BMP image
52 if (buffer[0] != 'B' || buffer[1] != 'M') {
53 ESP_LOGE(TAG, "Not a BMP file");
55 }
56
57 // BMP file contains its own size in the header
58 size_t file_size = encode_uint32(buffer[5], buffer[4], buffer[3], buffer[2]);
59 if (this->expected_size_ == 0) {
60 this->expected_size_ = file_size; // Use file header size if not provided
61 }
62 this->data_offset_ = encode_uint32(buffer[13], buffer[12], buffer[11], buffer[10]);
63
64 this->current_index_ = 14;
65 index = 14;
66 }
67 if (this->current_index_ == 14) {
69 return 0; // Need more data for DIB header and color table
70 }
85 this->width_ = encode_uint32(buffer[21], buffer[20], buffer[19], buffer[18]);
86 this->height_ = encode_uint32(buffer[25], buffer[24], buffer[23], buffer[22]);
87 if (this->width_ <= 0 || this->height_ <= 0) {
88 ESP_LOGE(TAG, "Invalid image dimensions: (%zdx%zd)", this->width_, this->height_);
90 }
91 this->bits_per_pixel_ = encode_uint16(buffer[29], buffer[28]);
92 this->compression_method_ = encode_uint32(buffer[33], buffer[32], buffer[31], buffer[30]);
93 this->image_data_size_ = encode_uint32(buffer[37], buffer[36], buffer[35], buffer[34]);
94 this->color_table_entries_ = encode_uint32(buffer[49], buffer[48], buffer[47], buffer[46]);
95
96 switch (this->bits_per_pixel_) {
97 case 1:
98 this->width_bytes_ = (this->width_ + 7) / 8;
99 this->padding_bytes_ = (4 - (this->width_bytes_ % 4)) % 4;
100 break;
101 case 8: {
102 this->width_bytes_ = this->width_;
103 if (this->color_table_entries_ == 0) {
104 this->color_table_entries_ = 256;
105 } else if (this->color_table_entries_ > 256) {
106 ESP_LOGE(TAG, "Too many color table entries: %" PRIu32, this->color_table_entries_);
108 }
109 size_t header_size = encode_uint32(buffer[17], buffer[16], buffer[15], buffer[14]);
110 size_t offset = 14 + header_size;
111
113 this->color_table_ = std::make_unique<uint32_t[]>(this->color_table_entries_);
115 }
116
117 for (size_t i = 0; i < this->color_table_entries_; i++) {
118 this->color_table_[i] = encode_uint32(buffer[offset + i * 4 + 3], buffer[offset + i * 4 + 2],
119 buffer[offset + i * 4 + 1], buffer[offset + i * 4]);
120 }
121
122 this->padding_bytes_ = (4 - (this->width_bytes_ % 4)) % 4;
123
124 break;
125 }
126 case 24:
127 this->width_bytes_ = this->width_ * 3;
128 if (this->width_bytes_ % 4 != 0) {
129 this->padding_bytes_ = 4 - (this->width_bytes_ % 4);
130 this->width_bytes_ += this->padding_bytes_;
131 }
132 break;
133 default:
134 ESP_LOGE(TAG, "Unsupported bits per pixel: %d", this->bits_per_pixel_);
136 }
137
138 if (this->compression_method_ != 0) {
139 ESP_LOGE(TAG, "Unsupported compression method: %" PRIu32, this->compression_method_);
141 }
142
143 if (!this->set_size(this->width_, this->height_)) {
145 }
146 this->current_index_ = this->data_offset_;
147 index = this->data_offset_;
148 }
149 // A call that has run for DECODE_TIME_LIMIT_MS stops, and the caller passes the rest again later
150 const uint32_t start = millis();
151 uint32_t steps = 0;
152 auto out_of_time = [start, &steps]() {
153 return ++steps % DECODE_TIME_CHECK_STEPS == 0 && millis() - start >= DECODE_TIME_LIMIT_MS;
154 };
155 // Bytes after the last pixel are used up without drawing anything
156 const size_t total_pixels = static_cast<size_t>(this->width_) * this->height_;
157 switch (this->bits_per_pixel_) {
158 case 1: {
159 size_t width = static_cast<size_t>(this->width_);
160 while (index < size && this->paint_index_ < total_pixels) {
161 size_t x = this->paint_index_ % width;
162 size_t y = static_cast<size_t>(this->height_ - 1) - (this->paint_index_ / width);
163 size_t remaining_in_row = width - x;
164 uint8_t pixels_in_byte = std::min<size_t>(remaining_in_row, 8);
165 bool end_of_row = remaining_in_row <= 8;
166 size_t needed = 1 + (end_of_row ? this->padding_bytes_ : 0);
167 if (index + needed > size || out_of_time()) {
168 this->decoded_bytes_ += index;
169 return index;
170 }
171 uint8_t current_byte = buffer[index];
172 for (uint8_t i = 0; i < pixels_in_byte; i++) {
173 Color c = (current_byte & (1 << (7 - i))) ? display::COLOR_ON : OPAQUE_BLACK;
174 this->draw(x + i, y, 1, 1, c);
175 }
176 this->paint_index_ += pixels_in_byte;
177 this->current_index_++;
178 index++;
179 // End of pixel row: skip row padding bytes (4-byte alignment)
180 if (end_of_row && this->padding_bytes_ > 0) {
181 index += this->padding_bytes_;
182 this->current_index_ += this->padding_bytes_;
183 }
184 }
185 break;
186 }
187 case 8: {
188 size_t width = static_cast<size_t>(this->width_);
189 size_t last_col = width - 1;
190 while (index < size && this->paint_index_ < total_pixels) {
191 size_t x = this->paint_index_ % width;
192 size_t y = static_cast<size_t>(this->height_ - 1) - (this->paint_index_ / width);
193 size_t needed = 1 + ((x == last_col) ? this->padding_bytes_ : 0);
194 if (index + needed > size || out_of_time()) {
195 this->decoded_bytes_ += index;
196 return index;
197 }
198
199 uint8_t color_index = buffer[index];
200 if (color_index >= this->color_table_entries_) {
201 ESP_LOGE(TAG, "Invalid color index: %u", color_index);
203 }
204
205 uint32_t rgb = this->color_table_[color_index];
206 uint8_t b = rgb & 0xff;
207 uint8_t g = (rgb >> 8) & 0xff;
208 uint8_t r = (rgb >> 16) & 0xff;
209 this->draw(x, y, 1, 1, Color(r, g, b, 0xFF));
210 this->paint_index_++;
211 this->current_index_++;
212 index++;
213 if (x == last_col && this->padding_bytes_ > 0) {
214 index += this->padding_bytes_;
215 this->current_index_ += this->padding_bytes_;
216 }
217 }
218 break;
219 }
220 case 24: {
221 size_t width = static_cast<size_t>(this->width_);
222 size_t last_col = width - 1;
223 while (index < size && this->paint_index_ < total_pixels) {
224 size_t x = this->paint_index_ % width;
225 size_t y = static_cast<size_t>(this->height_ - 1) - (this->paint_index_ / width);
226 size_t needed = 3 + ((x == last_col) ? this->padding_bytes_ : 0);
227 if (index + needed > size || out_of_time()) {
228 this->decoded_bytes_ += index;
229 return index;
230 }
231 uint8_t b = buffer[index];
232 uint8_t g = buffer[index + 1];
233 uint8_t r = buffer[index + 2];
234 this->draw(x, y, 1, 1, Color(r, g, b, 0xFF));
235 this->paint_index_++;
236 this->current_index_ += 3;
237 index += 3;
238 if (x == last_col && this->padding_bytes_ > 0) {
239 index += this->padding_bytes_;
240 this->current_index_ += this->padding_bytes_;
241 }
242 }
243 break;
244 }
245 default:
246 ESP_LOGE(TAG, "Unsupported bits per pixel: %d", this->bits_per_pixel_);
248 }
249 this->decoded_bytes_ += size;
250 return size;
251};
252
253} // namespace esphome::runtime_image
254
255#endif // USE_RUNTIME_IMAGE_BMP
std::unique_ptr< uint32_t[]> color_table_
Definition bmp_decoder.h:36
int HOT decode(uint8_t *buffer, size_t size) override
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.
uint32_t buffer
Definition gif.cpp:50
constexpr Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
ESPHOME_LOG_TAG(TAG, "image_decoder.bmp")
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
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:1043
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6