Support simple transparent pngs for display (#3035)

This commit is contained in:
Jesse Hills 2022-01-21 11:16:18 +13:00 committed by GitHub
parent 1c51cac5ba
commit 045952939e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View file

@ -233,6 +233,14 @@ void DisplayBuffer::image(int x, int y, Image *image, Color color_on, Color colo
} }
} }
break; 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;
} }
} }

View file

@ -73,7 +73,12 @@ extern const Color COLOR_OFF;
/// Turn the pixel ON. /// Turn the pixel ON.
extern const Color COLOR_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 { enum DisplayRotation {
DISPLAY_ROTATION_0_DEGREES = 0, DISPLAY_ROTATION_0_DEGREES = 0,

View file

@ -24,6 +24,7 @@ IMAGE_TYPE = {
"BINARY": ImageType.IMAGE_TYPE_BINARY, "BINARY": ImageType.IMAGE_TYPE_BINARY,
"GRAYSCALE": ImageType.IMAGE_TYPE_GRAYSCALE, "GRAYSCALE": ImageType.IMAGE_TYPE_GRAYSCALE,
"RGB24": ImageType.IMAGE_TYPE_RGB24, "RGB24": ImageType.IMAGE_TYPE_RGB24,
"TRANSPARENT_BINARY": ImageType.IMAGE_TYPE_TRANSPARENT_BINARY,
} }
Image_ = display.display_ns.class_("Image") Image_ = display.display_ns.class_("Image")
@ -99,6 +100,17 @@ async def to_code(config):
pos = x + y * width8 pos = x + y * width8
data[pos // 8] |= 0x80 >> (pos % 8) 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] rhs = [HexInt(x) for x in data]
prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs) prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs)
cg.new_Pvariable( cg.new_Pvariable(