ESPHome  2024.4.0
image.cpp
Go to the documentation of this file.
1 #include "image.h"
2 
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace image {
7 
8 void Image::draw(int x, int y, display::Display *display, Color color_on, Color color_off) {
9  switch (type_) {
10  case IMAGE_TYPE_BINARY: {
11  for (int img_x = 0; img_x < width_; img_x++) {
12  for (int img_y = 0; img_y < height_; img_y++) {
13  if (this->get_binary_pixel_(img_x, img_y)) {
14  display->draw_pixel_at(x + img_x, y + img_y, color_on);
15  } else if (!this->transparent_) {
16  display->draw_pixel_at(x + img_x, y + img_y, color_off);
17  }
18  }
19  }
20  break;
21  }
23  for (int img_x = 0; img_x < width_; img_x++) {
24  for (int img_y = 0; img_y < height_; img_y++) {
25  auto color = this->get_grayscale_pixel_(img_x, img_y);
26  if (color.w >= 0x80) {
27  display->draw_pixel_at(x + img_x, y + img_y, color);
28  }
29  }
30  }
31  break;
32  case IMAGE_TYPE_RGB565:
33  for (int img_x = 0; img_x < width_; img_x++) {
34  for (int img_y = 0; img_y < height_; img_y++) {
35  auto color = this->get_rgb565_pixel_(img_x, img_y);
36  if (color.w >= 0x80) {
37  display->draw_pixel_at(x + img_x, y + img_y, color);
38  }
39  }
40  }
41  break;
42  case IMAGE_TYPE_RGB24:
43  for (int img_x = 0; img_x < width_; img_x++) {
44  for (int img_y = 0; img_y < height_; img_y++) {
45  auto color = this->get_rgb24_pixel_(img_x, img_y);
46  if (color.w >= 0x80) {
47  display->draw_pixel_at(x + img_x, y + img_y, color);
48  }
49  }
50  }
51  break;
52  case IMAGE_TYPE_RGBA:
53  for (int img_x = 0; img_x < width_; img_x++) {
54  for (int img_y = 0; img_y < height_; img_y++) {
55  auto color = this->get_rgba_pixel_(img_x, img_y);
56  if (color.w >= 0x80) {
57  display->draw_pixel_at(x + img_x, y + img_y, color);
58  }
59  }
60  }
61  break;
62  }
63 }
64 Color Image::get_pixel(int x, int y, Color color_on, Color color_off) const {
65  if (x < 0 || x >= this->width_ || y < 0 || y >= this->height_)
66  return color_off;
67  switch (this->type_) {
68  case IMAGE_TYPE_BINARY:
69  return this->get_binary_pixel_(x, y) ? color_on : color_off;
71  return this->get_grayscale_pixel_(x, y);
72  case IMAGE_TYPE_RGB565:
73  return this->get_rgb565_pixel_(x, y);
74  case IMAGE_TYPE_RGB24:
75  return this->get_rgb24_pixel_(x, y);
76  case IMAGE_TYPE_RGBA:
77  return this->get_rgba_pixel_(x, y);
78  default:
79  return color_off;
80  }
81 }
82 bool Image::get_binary_pixel_(int x, int y) const {
83  const uint32_t width_8 = ((this->width_ + 7u) / 8u) * 8u;
84  const uint32_t pos = x + y * width_8;
85  return progmem_read_byte(this->data_start_ + (pos / 8u)) & (0x80 >> (pos % 8u));
86 }
87 Color Image::get_rgba_pixel_(int x, int y) const {
88  const uint32_t pos = (x + y * this->width_) * 4;
89  return Color(progmem_read_byte(this->data_start_ + pos + 0), progmem_read_byte(this->data_start_ + pos + 1),
90  progmem_read_byte(this->data_start_ + pos + 2), progmem_read_byte(this->data_start_ + pos + 3));
91 }
92 Color Image::get_rgb24_pixel_(int x, int y) const {
93  const uint32_t pos = (x + y * this->width_) * 3;
94  Color color = Color(progmem_read_byte(this->data_start_ + pos + 0), progmem_read_byte(this->data_start_ + pos + 1),
95  progmem_read_byte(this->data_start_ + pos + 2));
96  if (color.b == 1 && color.r == 0 && color.g == 0 && transparent_) {
97  // (0, 0, 1) has been defined as transparent color for non-alpha images.
98  // putting blue == 1 as a first condition for performance reasons (least likely value to short-cut the if)
99  color.w = 0;
100  } else {
101  color.w = 0xFF;
102  }
103  return color;
104 }
106  const uint32_t pos = (x + y * this->width_) * 2;
107  uint16_t rgb565 =
108  progmem_read_byte(this->data_start_ + pos + 0) << 8 | progmem_read_byte(this->data_start_ + pos + 1);
109  auto r = (rgb565 & 0xF800) >> 11;
110  auto g = (rgb565 & 0x07E0) >> 5;
111  auto b = rgb565 & 0x001F;
112  Color color = Color((r << 3) | (r >> 2), (g << 2) | (g >> 4), (b << 3) | (b >> 2));
113  if (rgb565 == 0x0020 && transparent_) {
114  // darkest green has been defined as transparent color for transparent RGB565 images.
115  color.w = 0;
116  } else {
117  color.w = 0xFF;
118  }
119  return color;
120 }
122  const uint32_t pos = (x + y * this->width_);
123  const uint8_t gray = progmem_read_byte(this->data_start_ + pos);
124  uint8_t alpha = (gray == 1 && transparent_) ? 0 : 0xFF;
125  return Color(gray, gray, gray, alpha);
126 }
127 int Image::get_width() const { return this->width_; }
128 int Image::get_height() const { return this->height_; }
129 ImageType Image::get_type() const { return this->type_; }
130 Image::Image(const uint8_t *data_start, int width, int height, ImageType type)
131  : width_(width), height_(height), type_(type), data_start_(data_start) {}
132 
133 } // namespace image
134 } // namespace esphome
ImageType get_type() const
Definition: image.cpp:129
Color get_rgb24_pixel_(int x, int y) const
Definition: image.cpp:92
uint16_t x
Definition: tt21100.cpp:17
bool get_binary_pixel_(int x, int y) const
Definition: image.cpp:82
Color get_pixel(int x, int y, Color color_on=display::COLOR_ON, Color color_off=display::COLOR_OFF) const
Definition: image.cpp:64
Color get_grayscale_pixel_(int x, int y) const
Definition: image.cpp:121
uint8_t g
Definition: color.h:18
uint16_t y
Definition: tt21100.cpp:18
ImageType type_
Definition: image.h:57
int get_width() const override
Definition: image.cpp:127
Color get_rgb565_pixel_(int x, int y) const
Definition: image.cpp:105
uint8_t type
const uint8_t * data_start_
Definition: image.h:58
uint8_t progmem_read_byte(const uint8_t *addr)
Definition: core.cpp:55
uint8_t w
Definition: color.h:26
int get_height() const override
Definition: image.cpp:128
uint8_t b
Definition: color.h:22
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
Definition: image.cpp:8
void draw_pixel_at(int x, int y)
Set a single pixel at the specified coordinates to default color.
Definition: display.h:225
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
uint8_t r
Definition: color.h:14
Image(const uint8_t *data_start, int width, int height, ImageType type)
Definition: image.cpp:130
Color get_rgba_pixel_(int x, int y) const
Definition: image.cpp:87