From 045952939edd6d04b338b95bc8ab897f4ffa02c8 Mon Sep 17 00:00:00 2001 From: Jesse Hills <3060199+jesserockz@users.noreply.github.com> Date: Fri, 21 Jan 2022 11:16:18 +1300 Subject: [PATCH] Support simple transparent pngs for display (#3035) --- esphome/components/display/display_buffer.cpp | 8 ++++++++ esphome/components/display/display_buffer.h | 7 ++++++- esphome/components/image/__init__.py | 12 ++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/esphome/components/display/display_buffer.cpp b/esphome/components/display/display_buffer.cpp index b97fb4ae23..bdf299e8f1 100644 --- a/esphome/components/display/display_buffer.cpp +++ b/esphome/components/display/display_buffer.cpp @@ -233,6 +233,14 @@ void DisplayBuffer::image(int x, int y, Image *image, Color color_on, Color colo } } break; + case IMAGE_TYPE_TRANSPARENT_BINARY: + for (int img_x = 0; img_x < image->get_width(); img_x++) { + for (int img_y = 0; img_y < image->get_height(); img_y++) { + if (image->get_pixel(img_x, img_y)) + this->draw_pixel_at(x + img_x, y + img_y, color_on); + } + } + break; } } diff --git a/esphome/components/display/display_buffer.h b/esphome/components/display/display_buffer.h index c803180a2d..b275b43b0e 100644 --- a/esphome/components/display/display_buffer.h +++ b/esphome/components/display/display_buffer.h @@ -73,7 +73,12 @@ extern const Color COLOR_OFF; /// Turn the pixel ON. extern const Color COLOR_ON; -enum ImageType { IMAGE_TYPE_BINARY = 0, IMAGE_TYPE_GRAYSCALE = 1, IMAGE_TYPE_RGB24 = 2 }; +enum ImageType { + IMAGE_TYPE_BINARY = 0, + IMAGE_TYPE_GRAYSCALE = 1, + IMAGE_TYPE_RGB24 = 2, + IMAGE_TYPE_TRANSPARENT_BINARY = 3, +}; enum DisplayRotation { DISPLAY_ROTATION_0_DEGREES = 0, diff --git a/esphome/components/image/__init__.py b/esphome/components/image/__init__.py index a721263dff..70d77dfd14 100644 --- a/esphome/components/image/__init__.py +++ b/esphome/components/image/__init__.py @@ -24,6 +24,7 @@ IMAGE_TYPE = { "BINARY": ImageType.IMAGE_TYPE_BINARY, "GRAYSCALE": ImageType.IMAGE_TYPE_GRAYSCALE, "RGB24": ImageType.IMAGE_TYPE_RGB24, + "TRANSPARENT_BINARY": ImageType.IMAGE_TYPE_TRANSPARENT_BINARY, } Image_ = display.display_ns.class_("Image") @@ -99,6 +100,17 @@ async def to_code(config): pos = x + y * width8 data[pos // 8] |= 0x80 >> (pos % 8) + elif config[CONF_TYPE] == "TRANSPARENT_BINARY": + image = image.convert("RGBA") + width8 = ((width + 7) // 8) * 8 + data = [0 for _ in range(height * width8 // 8)] + for y in range(height): + for x in range(width): + if not image.getpixel((x, y))[3]: + continue + pos = x + y * width8 + data[pos // 8] |= 0x80 >> (pos % 8) + rhs = [HexInt(x) for x in data] prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs) cg.new_Pvariable(