ESPHome  2024.11.2
image.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/color.h"
4 
5 #ifdef USE_LVGL
7 #endif // USE_LVGL
8 
9 namespace esphome {
10 namespace image {
11 
12 enum ImageType {
18 };
19 
20 class Image : public display::BaseImage {
21  public:
22  Image(const uint8_t *data_start, int width, int height, ImageType type);
23  Color get_pixel(int x, int y, Color color_on = display::COLOR_ON, Color color_off = display::COLOR_OFF) const;
24  int get_width() const override;
25  int get_height() const override;
26  const uint8_t *get_data_start() const { return this->data_start_; }
27  ImageType get_type() const;
28 
29  int get_bpp() const {
30  switch (this->type_) {
31  case IMAGE_TYPE_BINARY:
32  return 1;
34  return 8;
35  case IMAGE_TYPE_RGB565:
36  return this->transparent_ ? 24 : 16;
37  case IMAGE_TYPE_RGB24:
38  return 24;
39  case IMAGE_TYPE_RGBA:
40  return 32;
41  }
42  return 0;
43  }
44 
47  uint32_t get_width_stride() const { return (this->width_ * this->get_bpp() + 7u) / 8u; }
48  void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override;
49 
50  void set_transparency(bool transparent) { transparent_ = transparent; }
51  bool has_transparency() const { return transparent_; }
52 
53 #ifdef USE_LVGL
54  lv_img_dsc_t *get_lv_img_dsc();
55 #endif
56  protected:
57  bool get_binary_pixel_(int x, int y) const;
58  Color get_rgb24_pixel_(int x, int y) const;
59  Color get_rgba_pixel_(int x, int y) const;
60  Color get_rgb565_pixel_(int x, int y) const;
61  Color get_grayscale_pixel_(int x, int y) const;
62 
63  int width_;
64  int height_;
66  const uint8_t *data_start_;
68 #ifdef USE_LVGL
69  lv_img_dsc_t dsc_{};
70 #endif
71 };
72 
73 } // namespace image
74 } // namespace esphome
bool has_transparency() const
Definition: image.h:51
ImageType get_type() const
Definition: image.cpp:173
Color get_rgb24_pixel_(int x, int y) const
Definition: image.cpp:136
uint16_t x
Definition: tt21100.cpp:17
uint32_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:47
const Color COLOR_OFF(0, 0, 0, 0)
Turn the pixel OFF.
Definition: display.h:191
const uint8_t * get_data_start() const
Definition: image.h:26
bool get_binary_pixel_(int x, int y) const
Definition: image.cpp:126
Color get_pixel(int x, int y, Color color_on=display::COLOR_ON, Color color_off=display::COLOR_OFF) const
Definition: image.cpp:64
int get_bpp() const
Definition: image.h:29
Color get_grayscale_pixel_(int x, int y) const
Definition: image.cpp:165
uint16_t y
Definition: tt21100.cpp:18
ImageType type_
Definition: image.h:65
int get_width() const override
Definition: image.cpp:171
void set_transparency(bool transparent)
Definition: image.h:50
Color get_rgb565_pixel_(int x, int y) const
Definition: image.cpp:149
uint8_t type
const uint8_t * data_start_
Definition: image.h:66
lv_img_dsc_t * get_lv_img_dsc()
Definition: image.cpp:83
const Color COLOR_ON(255, 255, 255, 255)
Turn the pixel ON.
Definition: display.h:193
int get_height() const override
Definition: image.cpp:172
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
Definition: image.cpp:8
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
lv_img_dsc_t dsc_
Definition: image.h:69
Image(const uint8_t *data_start, int width, int height, ImageType type)
Definition: image.cpp:174
Color get_rgba_pixel_(int x, int y) const
Definition: image.cpp:131