import framebuf
import pbm
import math

if __name__ == "__main__":
    FW = 18
    FH = 32
    buffer_frame = bytearray(FW * 10 * math.ceil(FH / 8))
    buffer_preview = bytearray(math.ceil(FW * 10 / 8) * FH)
    frame = framebuf.FrameBuffer(buffer_frame, FW * 10, FH, framebuf.MONO_VLSB)
    preview = framebuf.FrameBuffer(buffer_preview, FW * 10, FH, framebuf.MONO_HLSB)
    for i, char in enumerate("0123456789"):
        offset_x = FW * i
        with open(f"digi{char}_{FW}x{FH}.pbm", "rb") as f:
            iw, ih, _fmt, data, _ = pbm.read_image(f)
            img = framebuf.FrameBuffer(data, iw, ih, framebuf.MONO_HLSB)
        frame.blit(img, offset_x, 0, 0)
        preview.blit(img, offset_x, 0, 0)
    # with open("preview.pbm", "wb") as f:
    #     pbm.make_image(f, FW * 10, FH, buffer_preview)
    data = bytearray()
    data.append(FW * 10 - 1) # image width - 1
    data.append(FH - 1) # image height - 1
    data.extend(buffer_frame) # image data in MONO_VLSB format
    f = open(f"digi{FW}x{FH}.h", "wt")
    f.write("#pragma once\n")
    f.write("#include <stdint.h>\n")
    f.write("\n")
    f.write(f"const uint8_t IMG_DIGI_{FW}_{FH}[] = {{")
    for i, byte in enumerate(data):
        if i % 16 == 0:
            f.write("\n   ")
        f.write(f" 0x{byte:02X},")
    f.write("\n};\n")
    f.write(f"const uint32_t DATA_SIZE_DIGI_{FW}_{FH} = {len(data)};\n")
    f.write("\n")
    f.close()