Skip to content

Commit bbb7b23

Browse files
committed
lv_img_conv: Add CF_TRUE_COLOR output
Only CF_INDEXED_1_BIT and CF_TRUE_COLOR_ALPHA were implemented, so a plain true-colour image -- the obvious choice for a full-screen watch face background -- could not be converted at all. Add CF_TRUE_COLOR: 2 bytes per pixel, RGB565 high byte first, matching the existing alpha path and LV_COLOR_16_SWAP=1, with cf = 4 in the header. Alpha is dropped on input, since the format does not carry it. Also fix a typo on an adjacent line: pritn -> print in the refuse-to-overwrite path, which raised NameError instead of printing the intended message.
1 parent 71d1f5b commit bbb7b23

1 file changed

Lines changed: 23 additions & 2 deletions

File tree

‎src/resources/lv_img_conv.py‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,12 @@ def main():
7979
if args.force:
8080
print(f"overwriting {args.output_file}")
8181
else:
82-
pritn(f"Error: refusing to overwrite {args.output_file} without -f specified.")
82+
print(f"Error: refusing to overwrite {args.output_file} without -f specified.")
8383
return 1
8484
out.touch()
8585

8686
# only implemented the bare minimum, everything else is not implemented
87-
if args.color_format not in ["CF_INDEXED_1_BIT", "CF_TRUE_COLOR_ALPHA"]:
87+
if args.color_format not in ["CF_INDEXED_1_BIT", "CF_TRUE_COLOR", "CF_TRUE_COLOR_ALPHA"]:
8888
raise NotImplementedError(f"argument --color-format '{args.color_format}' not implemented")
8989
if args.output_format != "bin":
9090
raise NotImplementedError(f"argument --output-format '{args.output_format}' not implemented")
@@ -105,6 +105,9 @@ def main():
105105
# support pictures stored in other formats like with a color palette 'P'
106106
# see: https://pillow.readthedocs.io/en/stable/handbook/concepts.html#modes
107107
img = img.convert(mode="RGBA")
108+
elif args.color_format == "CF_TRUE_COLOR" and img.mode != "RGB":
109+
# CF_TRUE_COLOR carries no alpha, so drop any it may have
110+
img = img.convert(mode="RGB")
108111
elif args.color_format == "CF_INDEXED_1_BIT" and img.mode != "L":
109112
# for CF_INDEXED_1_BIT we need just a grayscale value per pixel
110113
img = img.convert(mode="L")
@@ -138,6 +141,22 @@ def main():
138141
buf[i + 1] = c16 & 0xFF
139142
buf[i + 2] = a
140143

144+
elif args.color_format == "CF_TRUE_COLOR" and args.binary_format == "ARGB8565_RBSWAP":
145+
buf = bytearray(img_height*img_width*2) # 2 bytes (16 bit) per pixel
146+
for y in range(img_height):
147+
for x in range(img_width):
148+
i = (y*img_width + x)*2 # buffer-index
149+
pixel = img.getpixel((x,y))
150+
r_act = classify_pixel(pixel[0], 5)
151+
g_act = classify_pixel(pixel[1], 6)
152+
b_act = classify_pixel(pixel[2], 5)
153+
r_act = min(r_act, 0xF8)
154+
g_act = min(g_act, 0xFC)
155+
b_act = min(b_act, 0xF8)
156+
c16 = ((r_act) << 8) | ((g_act) << 3) | ((b_act) >> 3) # RGB565
157+
buf[i + 0] = (c16 >> 8) & 0xFF
158+
buf[i + 1] = c16 & 0xFF
159+
141160
elif args.color_format == "CF_INDEXED_1_BIT": # ignore binary format, use color format as binary format
142161
w = img_width >> 3
143162
if img_width & 0x07:
@@ -168,6 +187,8 @@ def main():
168187

169188
# write header
170189
match args.color_format:
190+
case "CF_TRUE_COLOR":
191+
lv_cf = 4
171192
case "CF_TRUE_COLOR_ALPHA":
172193
lv_cf = 5
173194
case "CF_INDEXED_1_BIT":

0 commit comments

Comments
 (0)