import struct, os
from io import BytesIO
from PIL import Image

SRC = os.path.join(os.path.dirname(__file__), "holos.png")
DST = os.path.join(os.path.dirname(__file__), "icon.ico")
SIZES = [16, 24, 32, 48, 64, 128, 256]

src = Image.open(SRC).convert("RGBA")
entries = []

for size in SIZES:
    img = src.resize((size, size), Image.LANCZOS)
    buf = BytesIO()
    img.save(buf, format='PNG')
    data = buf.getvalue()
    w = size if size < 256 else 0
    h = size if size < 256 else 0
    entries.append((w, h, 32, len(data), data))

with open(DST, 'wb') as f:
    f.write(struct.pack('<HHH', 0, 1, len(SIZES)))
    dir_offset = 6 + len(SIZES) * 16
    for entry in entries:
        f.write(struct.pack('<BBBBHHII',
            entry[0], entry[1], 0, 0, 1, entry[2], entry[3], dir_offset))
        dir_offset += entry[3]
    for entry in entries:
        f.write(entry[4])

print(f"Created {DST} ({os.path.getsize(DST)} bytes, {len(SIZES)} sizes)")
