ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
image.cpp
Go to the documentation of this file.
1#include "image.h"
2
3#include "esphome/core/hal.h"
5
6namespace esphome::image {
7
8void Image::draw(int x, int y, display::Display *display, Color color_on, Color color_off) {
9 int img_x0 = 0;
10 int img_y0 = 0;
11 int w = width_;
12 int h = height_;
13
14 auto clipping = display->get_clipping();
15 if (clipping.is_set()) {
16 if (clipping.x > x)
17 img_x0 += clipping.x - x;
18 if (clipping.y > y)
19 img_y0 += clipping.y - y;
20 if (w > clipping.x2() - x)
21 w = clipping.x2() - x;
22 if (h > clipping.y2() - y)
23 h = clipping.y2() - y;
24 }
25
26 switch (type_) {
27 case IMAGE_TYPE_BINARY: {
28 for (int img_x = img_x0; img_x < w; img_x++) {
29 for (int img_y = img_y0; img_y < h; img_y++) {
30 if (this->get_binary_pixel_(img_x, img_y)) {
31 display->draw_pixel_at(x + img_x, y + img_y, color_on);
32 } else if (!this->transparency_) {
33 display->draw_pixel_at(x + img_x, y + img_y, color_off);
34 }
35 }
36 }
37 break;
38 }
40 for (int img_x = img_x0; img_x < w; img_x++) {
41 for (int img_y = img_y0; img_y < h; img_y++) {
42 const uint32_t pos = (img_x + img_y * this->width_);
43 const uint8_t gray = progmem_read_byte(this->data_start_ + pos);
44 Color color = Color(gray, gray, gray, 0xFF);
45 switch (this->transparency_) {
47 if (gray == 1) {
48 continue; // skip drawing
49 }
50 break;
52 auto on = (float) gray / 255.0f;
53 auto off = 1.0f - on;
54 // blend color_on and color_off
55 color = Color(color_on.r * on + color_off.r * off, color_on.g * on + color_off.g * off,
56 color_on.b * on + color_off.b * off, 0xFF);
57 break;
58 }
59 default:
60 break;
61 }
62 display->draw_pixel_at(x + img_x, y + img_y, color);
63 }
64 }
65 break;
67 for (int img_x = img_x0; img_x < w; img_x++) {
68 for (int img_y = img_y0; img_y < h; img_y++) {
69 auto color = this->get_rgb565_pixel_(img_x, img_y);
70 if (color.w >= 0x80) {
71 display->draw_pixel_at(x + img_x, y + img_y, color);
72 }
73 }
74 }
75 break;
76 case IMAGE_TYPE_RGB:
77 for (int img_x = img_x0; img_x < w; img_x++) {
78 for (int img_y = img_y0; img_y < h; img_y++) {
79 auto color = this->get_rgb_pixel_(img_x, img_y);
80 if (color.w >= 0x80) {
81 display->draw_pixel_at(x + img_x, y + img_y, color);
82 }
83 }
84 }
85 break;
86 }
87}
88Color Image::get_pixel(int x, int y, const Color color_on, const Color color_off) const {
89 if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
90 return color_off;
91 switch (this->type_) {
93 if (this->get_binary_pixel_(x, y))
94 return color_on;
95 return color_off;
97 return this->get_grayscale_pixel_(x, y);
99 return this->get_rgb565_pixel_(x, y);
100 case IMAGE_TYPE_RGB:
101 return this->get_rgb_pixel_(x, y);
102 default:
103 return color_off;
104 }
105}
106#ifdef USE_LVGL
107lv_image_dsc_t *Image::get_lv_image_dsc() {
108 // lazily construct lvgl image_dsc.
109 if (this->dsc_.data != this->data_start_) {
110 this->dsc_.data = this->data_start_;
111 this->dsc_.header.reserved_2 = 0;
112 this->dsc_.header.stride = this->get_width_stride();
113 this->dsc_.header.w = this->width_;
114 this->dsc_.header.h = this->height_;
115 this->dsc_.data_size = this->get_width_stride() * this->get_height();
116 switch (this->get_type()) {
118 this->dsc_.header.cf = LV_COLOR_FORMAT_A1;
119 break;
120
122 this->dsc_.header.cf = LV_COLOR_FORMAT_A8;
123 break;
124
125 case IMAGE_TYPE_RGB:
126#if LV_COLOR_DEPTH == 32
127 switch (this->transparency_) {
129 this->dsc_.header.cf = LV_IMG_CF_TRUE_COLOR_ALPHA;
130 break;
132 this->dsc_.header.cf = LV_IMG_CF_TRUE_COLOR_CHROMA_KEYED;
133 break;
134 default:
135 this->dsc_.header.cf = LV_IMG_CF_TRUE_COLOR;
136 break;
137 }
138#else
139 this->dsc_.header.cf =
140 this->transparency_ == TRANSPARENCY_ALPHA_CHANNEL ? LV_COLOR_FORMAT_ARGB8888 : LV_COLOR_FORMAT_RGB888;
141#endif
142 break;
143
145#if LV_COLOR_DEPTH == 16
146 switch (this->transparency_) {
148 this->dsc_.header.cf = LV_COLOR_FORMAT_RGB565A8;
149 break;
150 default:
151 this->dsc_.header.cf = LV_COLOR_FORMAT_RGB565;
152 }
153#else
154 this->dsc_.header.cf =
155 this->transparency_ == TRANSPARENCY_ALPHA_CHANNEL ? LV_IMG_CF_RGB565A8 : LV_IMG_CF_RGB565;
156#endif
157 break;
158 }
159 }
160 return &this->dsc_;
161}
162#endif // USE_LVGL
163
164bool Image::get_binary_pixel_(int x, int y) const {
165 const uint32_t width_8 = ((this->width_ + 7u) / 8u) * 8u;
166 const uint32_t pos = x + y * width_8;
167 return progmem_read_byte(this->data_start_ + (pos / 8u)) & (0x80 >> (pos % 8u));
168}
170 const uint32_t pos = (x + y * this->width_) * this->bpp_ / 8;
171 Color color = Color(progmem_read_byte(this->data_start_ + pos + 2), progmem_read_byte(this->data_start_ + pos + 1),
172 progmem_read_byte(this->data_start_ + pos + 0), 0xFF);
173
174 switch (this->transparency_) {
176 if (color.g == 1 && color.r == 0 && color.b == 0) {
177 // (0, 1, 0) has been defined as transparent color for non-alpha images.
178 color.w = 0;
179 }
180 break;
182 color.w = progmem_read_byte(this->data_start_ + (pos + 3));
183 break;
184 default:
185 break;
186 }
187 return color;
188}
190 const uint8_t *pos = this->data_start_ + (x + y * this->width_) * this->bpp_ / 8;
191 uint16_t rgb565 = encode_uint16(progmem_read_byte(pos + 1), progmem_read_byte(pos));
192 auto r = (rgb565 & 0xF800) >> 11;
193 auto g = (rgb565 & 0x07E0) >> 5;
194 auto b = rgb565 & 0x001F;
195 auto a = 0xFF;
196 switch (this->transparency_) {
198 a = progmem_read_byte(this->data_start_ + this->width_ * this->height_ * 2 + (x + y * this->width_));
199 break;
201 if (rgb565 == 0x0020)
202 a = 0;
203 break;
204 default:
205 break;
206 }
207 return Color((r << 3) | (r >> 2), (g << 2) | (g >> 4), (b << 3) | (b >> 2), a);
208}
209
211 const uint32_t pos = (x + y * this->width_);
212 const uint8_t gray = progmem_read_byte(this->data_start_ + pos);
213 switch (this->transparency_) {
215 if (gray == 1)
216 return Color(0, 0, 0, 0);
217 return Color(gray, gray, gray, 0xFF);
219 return Color(0, 0, 0, gray);
220 default:
221 return Color(gray, gray, gray, 0xFF);
222 }
223}
224int Image::get_width() const { return this->width_; }
225int Image::get_height() const { return this->height_; }
226ImageType Image::get_type() const { return this->type_; }
227Image::Image(const uint8_t *data_start, int width, int height, ImageType type, Transparency transparency)
228 : width_(width), height_(height), type_(type), data_start_(data_start), transparency_(transparency) {
229 switch (this->type_) {
231 this->bpp_ = 1;
232 break;
234 this->bpp_ = 8;
235 break;
237 this->bpp_ = 16;
238 break;
239 case IMAGE_TYPE_RGB:
240 this->bpp_ = this->transparency_ == TRANSPARENCY_ALPHA_CHANNEL ? 32 : 24;
241 break;
242 }
243}
244
245} // namespace esphome::image
uint8_t h
Definition bl0906.h:2
void draw_pixel_at(int x, int y)
Set a single pixel at the specified coordinates to default color.
Definition display.h:335
Rect get_clipping() const
Get the current the clipping rectangle.
Definition display.cpp:766
Color get_rgb565_pixel_(int x, int y) const
Definition image.cpp:189
const uint8_t * data_start_
Definition image.h:54
Color get_rgb_pixel_(int x, int y) const
Definition image.cpp:169
size_t get_width_stride() const
Return the stride of the image in bytes, that is, the distance in bytes between two consecutive rows ...
Definition image.h:37
Image(const uint8_t *data_start, int width, int height, ImageType type, Transparency transparency)
Definition image.cpp:227
Color get_pixel(int x, int y, Color color_on=display::COLOR_ON, Color color_off=display::COLOR_OFF) const
Definition image.cpp:88
ImageType type_
Definition image.h:53
int get_height() const override
Definition image.cpp:225
ImageType get_type() const
Definition image.cpp:226
bool get_binary_pixel_(int x, int y) const
Definition image.cpp:164
Transparency transparency_
Definition image.h:55
lv_img_dsc_t dsc_
Definition image.h:59
lv_image_dsc_t * get_lv_image_dsc()
Definition image.cpp:107
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
Definition image.cpp:8
int get_width() const override
Definition image.cpp:224
Color get_grayscale_pixel_(int x, int y) const
Definition image.cpp:210
uint16_t type
@ TRANSPARENCY_ALPHA_CHANNEL
Definition image.h:21
@ TRANSPARENCY_CHROMA_KEY
Definition image.h:20
@ IMAGE_TYPE_GRAYSCALE
Definition image.h:13
@ IMAGE_TYPE_BINARY
Definition image.h:12
@ IMAGE_TYPE_RGB565
Definition image.h:15
@ IMAGE_TYPE_RGB
Definition image.h:14
size_t size_t pos
Definition helpers.h:1038
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:859
uint8_t progmem_read_byte(const uint8_t *addr)
Definition hal.h:43
static void uint32_t
uint8_t w
Definition color.h:42
uint8_t g
Definition color.h:34
uint8_t b
Definition color.h:38
uint8_t r
Definition color.h:30
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6