From 78edb9bd23ed3d2d7e632988240aea1d8fe2b682 Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Mon, 18 Sep 2023 23:12:33 +0200 Subject: [PATCH] lv_img_conv_py: first working version --- img/lv_img_conv.py | 57 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 55 insertions(+), 2 deletions(-) diff --git a/img/lv_img_conv.py b/img/lv_img_conv.py index ebd02ae..6a22648 100755 --- a/img/lv_img_conv.py +++ b/img/lv_img_conv.py @@ -2,6 +2,7 @@ import argparse import pathlib import sys +from PIL import Image parser = argparse.ArgumentParser() @@ -36,9 +37,9 @@ # --dither, -d enable dither [boolean] args = parser.parse_args() -img = pathlib.Path(args.img) +img_path = pathlib.Path(args.img) out = pathlib.Path(args.output_file) -if not img.is_file(): +if not img_path.is_file(): print(f"Input file is missing: '{args.img}'") sys.exit(1) if out.exists(): @@ -48,3 +49,55 @@ print(f"output-file exists, set --force to allow overwriting of file") sys.exit(0) out.touch() + +# only implemented the bare minimum, everything else is not implemented +if args.color_format != "CF_TRUE_COLOR_ALPHA": + raise NotImplementedError(f"args.color_format '{args.color_format}' not implemented") +if args.output_format != "bin": + raise NotImplementedError(f"args.output_format '{args.output_format}' not implemented") +if args.binary_format != "ARGB8565_RBSWAP": + raise NotImplementedError(f"args.binary_format '{args.binary_format}' not implemented") + +img = Image.open(img_path) +img.convert() + +def classify_pixel(value, bits): + tmp = 1 << (8 - bits) + val = round(value / tmp) * tmp + if val < 0: + val = 0 + return val +img_height = img.height +img_width = img.width +buf = bytearray(4 + img_height*img_width*3) # 4bytes header + 3 bytes (21 bit) per pixel +print(f"loaded image with width x heigth: {img_width} x {img_height}") +for y in range(img_height): + for x in range(img_width): + i = 4 + (y*img_width + x)*3 # buffer-index + pixel = img.getpixel((x,y)) + r_act = classify_pixel(pixel[0], 5) + g_act = classify_pixel(pixel[1], 6) + b_act = classify_pixel(pixel[2], 5) + a = pixel[3] + if r_act > 0xF8: + r_act = 0xF8; + if g_act > 0xFC: + g_act = 0xFC; + if b_act > 0xF8: + b_act = 0xF8; + c16 = ((r_act) << 8) | ((g_act) << 3) | ((b_act) >> 3) # RGR565 + buf[i + 0] = (c16 >> 8) & 0xFF + buf[i + 1] = c16 & 0xFF + buf[i + 2] = a + +# write header +lv_cf = 5 # CF_TRUE_COLOR_ALPHA +header_32bit = lv_cf | (img_width << 10) | (img_height << 21) +buf[0] = header_32bit & 0xFF +buf[1] = (header_32bit & 0xFF00) >> 8 +buf[2] = (header_32bit & 0xFF0000) >> 16 +buf[3] = (header_32bit & 0xFF000000) >> 24 + +# write byte buffer to file +with open(out, "wb") as f: + f.write(buf)