ESPHome 2026.3.0
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
7#include "esphome/core/log.h"
8
10
11static const char *const TAG = "image_decoder.bmp";
12
13int HOT BmpDecoder::decode(uint8_t *buffer, size_t size) {
14 size_t index = 0;
15 if (this->current_index_ == 0) {
16 if (size <= 14) {
17 return 0; // Need more data for file header
18 }
29 // Check if the file is a BMP image
30 if (buffer[0] != 'B' || buffer[1] != 'M') {
31 ESP_LOGE(TAG, "Not a BMP file");
33 }
34
35 // BMP file contains its own size in the header
36 size_t file_size = encode_uint32(buffer[5], buffer[4], buffer[3], buffer[2]);
37 if (this->expected_size_ == 0) {
38 this->expected_size_ = file_size; // Use file header size if not provided
39 }
40 this->data_offset_ = encode_uint32(buffer[13], buffer[12], buffer[11], buffer[10]);
41
42 this->current_index_ = 14;
43 index = 14;
44 }
45 if (this->current_index_ == 14) {
47 return 0; // Need more data for DIB header and color table
48 }
63 this->width_ = encode_uint32(buffer[21], buffer[20], buffer[19], buffer[18]);
64 this->height_ = encode_uint32(buffer[25], buffer[24], buffer[23], buffer[22]);
65 this->bits_per_pixel_ = encode_uint16(buffer[29], buffer[28]);
66 this->compression_method_ = encode_uint32(buffer[33], buffer[32], buffer[31], buffer[30]);
67 this->image_data_size_ = encode_uint32(buffer[37], buffer[36], buffer[35], buffer[34]);
68 this->color_table_entries_ = encode_uint32(buffer[49], buffer[48], buffer[47], buffer[46]);
69
70 switch (this->bits_per_pixel_) {
71 case 1:
72 this->width_bytes_ = (this->width_ + 7) / 8;
73 this->padding_bytes_ = (4 - (this->width_bytes_ % 4)) % 4;
74 break;
75 case 8: {
76 this->width_bytes_ = this->width_;
77 if (this->color_table_entries_ == 0) {
78 this->color_table_entries_ = 256;
79 } else if (this->color_table_entries_ > 256) {
80 ESP_LOGE(TAG, "Too many color table entries: %" PRIu32, this->color_table_entries_);
82 }
83 size_t header_size = encode_uint32(buffer[17], buffer[16], buffer[15], buffer[14]);
84 size_t offset = 14 + header_size;
85
86 this->color_table_ = std::make_unique<uint32_t[]>(this->color_table_entries_);
87
88 for (size_t i = 0; i < this->color_table_entries_; i++) {
89 this->color_table_[i] = encode_uint32(buffer[offset + i * 4 + 3], buffer[offset + i * 4 + 2],
90 buffer[offset + i * 4 + 1], buffer[offset + i * 4]);
91 }
92
93 this->padding_bytes_ = (4 - (this->width_bytes_ % 4)) % 4;
94
95 break;
96 }
97 case 24:
98 this->width_bytes_ = this->width_ * 3;
99 if (this->width_bytes_ % 4 != 0) {
100 this->padding_bytes_ = 4 - (this->width_bytes_ % 4);
101 this->width_bytes_ += this->padding_bytes_;
102 }
103 break;
104 default:
105 ESP_LOGE(TAG, "Unsupported bits per pixel: %d", this->bits_per_pixel_);
107 }
108
109 if (this->compression_method_ != 0) {
110 ESP_LOGE(TAG, "Unsupported compression method: %d", this->compression_method_);
112 }
113
114 if (!this->set_size(this->width_, this->height_)) {
116 }
117 this->current_index_ = this->data_offset_;
118 index = this->data_offset_;
119 }
120 switch (this->bits_per_pixel_) {
121 case 1: {
122 size_t width = static_cast<size_t>(this->width_);
123 while (index < size) {
124 size_t x = this->paint_index_ % width;
125 size_t y = static_cast<size_t>(this->height_ - 1) - (this->paint_index_ / width);
126 size_t remaining_in_row = width - x;
127 uint8_t pixels_in_byte = std::min<size_t>(remaining_in_row, 8);
128 bool end_of_row = remaining_in_row <= 8;
129 size_t needed = 1 + (end_of_row ? this->padding_bytes_ : 0);
130 if (index + needed > size) {
131 this->decoded_bytes_ += index;
132 return index;
133 }
134 uint8_t current_byte = buffer[index];
135 for (uint8_t i = 0; i < pixels_in_byte; i++) {
136 Color c = (current_byte & (1 << (7 - i))) ? display::COLOR_ON : display::COLOR_OFF;
137 this->draw(x + i, y, 1, 1, c);
138 }
139 this->paint_index_ += pixels_in_byte;
140 this->current_index_++;
141 index++;
142 // End of pixel row: skip row padding bytes (4-byte alignment)
143 if (end_of_row && this->padding_bytes_ > 0) {
144 index += this->padding_bytes_;
145 this->current_index_ += this->padding_bytes_;
146 }
147 }
148 break;
149 }
150 case 8: {
151 size_t width = static_cast<size_t>(this->width_);
152 size_t last_col = width - 1;
153 while (index < size) {
154 size_t x = this->paint_index_ % width;
155 size_t y = static_cast<size_t>(this->height_ - 1) - (this->paint_index_ / width);
156 size_t needed = 1 + ((x == last_col) ? this->padding_bytes_ : 0);
157 if (index + needed > size) {
158 this->decoded_bytes_ += index;
159 return index;
160 }
161
162 uint8_t color_index = buffer[index];
163 if (color_index >= this->color_table_entries_) {
164 ESP_LOGE(TAG, "Invalid color index: %u", color_index);
166 }
167
168 uint32_t rgb = this->color_table_[color_index];
169 uint8_t b = rgb & 0xff;
170 uint8_t g = (rgb >> 8) & 0xff;
171 uint8_t r = (rgb >> 16) & 0xff;
172 this->draw(x, y, 1, 1, Color(r, g, b));
173 this->paint_index_++;
174 this->current_index_++;
175 index++;
176 if (x == last_col && this->padding_bytes_ > 0) {
177 index += this->padding_bytes_;
178 this->current_index_ += this->padding_bytes_;
179 }
180 }
181 break;
182 }
183 case 24: {
184 size_t width = static_cast<size_t>(this->width_);
185 size_t last_col = width - 1;
186 while (index < size) {
187 size_t x = this->paint_index_ % width;
188 size_t y = static_cast<size_t>(this->height_ - 1) - (this->paint_index_ / width);
189 size_t needed = 3 + ((x == last_col) ? this->padding_bytes_ : 0);
190 if (index + needed > size) {
191 this->decoded_bytes_ += index;
192 return index;
193 }
194 uint8_t b = buffer[index];
195 uint8_t g = buffer[index + 1];
196 uint8_t r = buffer[index + 2];
197 this->draw(x, y, 1, 1, Color(r, g, b));
198 this->paint_index_++;
199 this->current_index_ += 3;
200 index += 3;
201 if (x == last_col && this->padding_bytes_ > 0) {
202 index += this->padding_bytes_;
203 this->current_index_ += this->padding_bytes_;
204 }
205 }
206 break;
207 }
208 default:
209 ESP_LOGE(TAG, "Unsupported bits per pixel: %d", this->bits_per_pixel_);
211 }
212 this->decoded_bytes_ += size;
213 return size;
214};
215
216} // namespace esphome::runtime_image
217
218#endif // USE_RUNTIME_IMAGE_BMP
std::unique_ptr< uint32_t[]> color_table_
Definition bmp_decoder.h:46
int HOT decode(uint8_t *buffer, size_t size) override
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.
constexpr Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
constexpr Color COLOR_OFF(0, 0, 0, 0)
Turn the pixel OFF.
size_t size
Definition helpers.h:929
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:736
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:728
static void uint32_t
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6