ESPHome  2024.10.1
image.h
Go to the documentation of this file.
1 #pragma once
2 #include "esphome/core/color.h"
4 
5 #ifdef USE_LVGL
6 // required for clang-tidy
7 #ifndef LV_CONF_H
8 #define LV_CONF_SKIP 1 // NOLINT
9 #endif // LV_CONF_H
10 
11 #include <lvgl.h>
12 #endif // USE_LVGL
13 
14 namespace esphome {
15 namespace image {
16 
17 enum ImageType {
23 };
24 
26  switch (type) {
27  case IMAGE_TYPE_BINARY:
28  return 1;
30  return 8;
31  case IMAGE_TYPE_RGB565:
32  return 16;
33  case IMAGE_TYPE_RGB24:
34  return 24;
35  case IMAGE_TYPE_RGBA:
36  return 32;
37  }
38  return 0;
39 }
40 
41 inline int image_type_to_width_stride(int width, ImageType type) { return (width * image_type_to_bpp(type) + 7u) / 8u; }
42 
43 class Image : public display::BaseImage {
44  public:
45  Image(const uint8_t *data_start, int width, int height, ImageType type);
46  Color get_pixel(int x, int y, Color color_on = display::COLOR_ON, Color color_off = display::COLOR_OFF) const;
47  int get_width() const override;
48  int get_height() const override;
49  const uint8_t *get_data_start() const { return this->data_start_; }
50  ImageType get_type() const;
51 
52  void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override;
53 
54  void set_transparency(bool transparent) { transparent_ = transparent; }
55  bool has_transparency() const { return transparent_; }
56 
57 #ifdef USE_LVGL
58  lv_img_dsc_t *get_lv_img_dsc();
59 #endif
60  protected:
61  bool get_binary_pixel_(int x, int y) const;
62  Color get_rgb24_pixel_(int x, int y) const;
63  Color get_rgba_pixel_(int x, int y) const;
64  Color get_rgb565_pixel_(int x, int y) const;
65  Color get_grayscale_pixel_(int x, int y) const;
66 
67  int width_;
68  int height_;
70  const uint8_t *data_start_;
72 #ifdef USE_LVGL
73  lv_img_dsc_t dsc_{};
74 #endif
75 };
76 
77 } // namespace image
78 } // namespace esphome
bool has_transparency() const
Definition: image.h:55
int image_type_to_width_stride(int width, ImageType type)
Definition: image.h:41
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
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:49
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
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:69
int get_width() const override
Definition: image.cpp:171
void set_transparency(bool transparent)
Definition: image.h:54
Color get_rgb565_pixel_(int x, int y) const
Definition: image.cpp:149
uint8_t type
int image_type_to_bpp(ImageType type)
Definition: image.h:25
const uint8_t * data_start_
Definition: image.h:70
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:73
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