From 12c0b1d28eae17e0f73a4b4dcc9d8ac5fed5b17a Mon Sep 17 00:00:00 2001 From: elpekenin Date: Sun, 12 Mar 2023 13:36:07 +0100 Subject: [PATCH 1/2] Initial code --- lib/python/qmk/painter.py | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/lib/python/qmk/painter.py b/lib/python/qmk/painter.py index 7ecdc55404a6..e388e37dadbb 100644 --- a/lib/python/qmk/painter.py +++ b/lib/python/qmk/painter.py @@ -189,6 +189,12 @@ def convert_requested_format(im, format): return im +def rgb_to565(r, g, b): + msb = ((r >> 3 & 0x1F) << 3) + (g >> 5 & 0x07) + lsb = ((g >> 2 & 0x07) << 5) + (b >> 3 & 0x1F) + return (msb << 8) + lsb + + def convert_image_bytes(im, format): """Convert the supplied image to the equivalent bytes required by the QMK firmware. """ @@ -246,41 +252,25 @@ def convert_image_bytes(im, format): if image_format == 'IMAGE_FORMAT_RGB565': # Take the red, green, and blue channels - image_bytes_red = im.tobytes("raw", "R") - image_bytes_green = im.tobytes("raw", "G") - image_bytes_blue = im.tobytes("raw", "B") - image_pixels_len = len(image_bytes_red) + red = im.tobytes("raw", "R") + green = im.tobytes("raw", "G") + blue = im.tobytes("raw", "B") # No palette palette = None - bytearray = [] - for x in range(image_pixels_len): - # 5 bits of red, 3 MSb of green - byte = ((image_bytes_red[x] >> 3 & 0x1F) << 3) + (image_bytes_green[x] >> 5 & 0x07) - bytearray.append(byte) - # 3 LSb of green, 5 bits of blue - byte = ((image_bytes_green[x] >> 2 & 0x07) << 5) + (image_bytes_blue[x] >> 3 & 0x1F) - bytearray.append(byte) + bytearray = [byte for r, g, b in zip(red, green, blue) for byte in rgb_to565(r, g, b).to_bytes(2, byteorder="big")] if image_format == 'IMAGE_FORMAT_RGB888': # Take the red, green, and blue channels - image_bytes_red = im.tobytes("raw", "R") - image_bytes_green = im.tobytes("raw", "G") - image_bytes_blue = im.tobytes("raw", "B") - image_pixels_len = len(image_bytes_red) + red = im.tobytes("raw", "R") + green = im.tobytes("raw", "G") + blue = im.tobytes("raw", "B") # No palette palette = None - bytearray = [] - for x in range(image_pixels_len): - byte = image_bytes_red[x] - bytearray.append(byte) - byte = image_bytes_green[x] - bytearray.append(byte) - byte = image_bytes_blue[x] - bytearray.append(byte) + bytearray = [byte for r, g, b in zip(red, green, blue) for byte in (r, g, b)] if len(bytearray) != expected_byte_count: raise Exception(f"Wrong byte count, was {len(bytearray)}, expected {expected_byte_count}") From 13e2746163e980c7989c03859a3cf906574c03fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADnez?= <58857054+elpekenin@users.noreply.github.com> Date: Sun, 12 Mar 2023 13:45:22 +0100 Subject: [PATCH 2/2] Little refactor --- lib/python/qmk/painter.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/python/qmk/painter.py b/lib/python/qmk/painter.py index e388e37dadbb..1df3fbf6bdf3 100644 --- a/lib/python/qmk/painter.py +++ b/lib/python/qmk/painter.py @@ -192,7 +192,7 @@ def convert_requested_format(im, format): def rgb_to565(r, g, b): msb = ((r >> 3 & 0x1F) << 3) + (g >> 5 & 0x07) lsb = ((g >> 2 & 0x07) << 5) + (b >> 3 & 0x1F) - return (msb << 8) + lsb + return [msb, lsb] def convert_image_bytes(im, format): @@ -259,7 +259,7 @@ def convert_image_bytes(im, format): # No palette palette = None - bytearray = [byte for r, g, b in zip(red, green, blue) for byte in rgb_to565(r, g, b).to_bytes(2, byteorder="big")] + bytearray = [byte for r, g, b in zip(red, green, blue) for byte in rgb_to565(r, g, b)] if image_format == 'IMAGE_FORMAT_RGB888': # Take the red, green, and blue channels