#!/usr/bin/env python3 """B9L: convert a 128x128 PNG to the LK splash: RGB565 little-endian u16 (same format as the stock Aurora LK logo @0x8f64b004), emitted as a C header for lk2nd/aurora. Alpha is composited over black (stop if the image is not 128x128). Also writes a 2x preview PNG rendered back from the RGB565 data. usage: mk-splash.py in.png out.h preview.png""" import struct, sys from PIL import Image im = Image.open(sys.argv[1]) if im.size != (128, 128): sys.exit(f'STOP: image is {im.size}, need 128x128') bg = Image.new('RGBA', im.size, (0, 0, 0, 255)) rgb = Image.alpha_composite(bg, im.convert('RGBA')).convert('RGB') px = list(rgb.getdata()) words = [(r >> 3) << 11 | (g >> 2) << 5 | b >> 3 for r, g, b in px] with open(sys.argv[2], 'w') as f: f.write('/* Generated by b9/b9l/splash/mk-splash.py from %s: 128x128 RGB565 (LE u16 in memory) */\n' % sys.argv[1].split('/')[-1]) f.write('static const uint16_t aurora_splash[128 * 128] = {\n') for i in range(0, len(words), 12): f.write('\t' + ', '.join('0x%04x' % w for w in words[i:i + 12]) + ',\n') f.write('};\n') prev = Image.new('RGB', (128, 128)) prev.putdata([((w >> 11) * 255 // 31, (w >> 5 & 63) * 255 // 63, (w & 31) * 255 // 31) for w in words]) prev.resize((256, 256), Image.NEAREST).save(sys.argv[3]) open(sys.argv[2] + '.bin', 'wb').write(b''.join(struct.pack('