Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

4.6 KB
Raw Blame
#!/usr/bin/env python3
"""Pre-render Pod's engine logos at the exact sizes it draws them (no runtime scaling, so they stay crisp).

    python3 scripts/pod_gen_logos.py            # needs Pillow; run from anywhere

Reads the repo's official 512 x 512 RGBA logos (.github/assets/engines/<engine>.png), crops each to its alpha
bounding box, pads it square with transparency and resizes it with LANCZOS (alpha kept) to every size in SIZES.
Writes main/pod/engine_logos.{c,h}: per engine and size an RGB565 array in native order (what
ht_pro_image wants) and an alpha8 array. `light` per engine: the alpha-weighted mean luminance of its art is
above LIGHT_LUMA, so the logo would vanish on white glass and sits on the dark chip / cover instead.

SIZES are the only sizes drawn (keep in step with pod_pet.c / home/ph_overview.c / home/ph_toast.c; test_pod_assets.c checks):
  44   Pets: the notice toast and the Home card's notices rows (home/ph_toast.c, ph_overview.c), and the faded fallback of a card's logo
  72   every pet-less cover (omitted for engines with a pet: parsed from ht_pets in main/pod/pets.c), centred and never scaled further
"""
import re
from pathlib import Path
from PIL import Image

ENGINES = "agy amp claude codex commandcode copilot cursor devin grok hermes kilo muse opencode pi".split()
SIZES = [44, 72]
COVER = 72
LIGHT_LUMA = 0.75

here = Path(__file__).resolve().parent
fw = here.parent
repo = fw.parents[2]
src = repo / ".github" / "assets" / "engines"
dest = fw / "main" / "pod"
PETS = re.findall(r'\{"(\w+)",\d+,\d+,', re.search(r"ht_pets\[\] = \{.*", (dest / "pets.c").read_text()).group(0))
assert PETS, "no pets found in pets.c"


def square(im: Image.Image) -> Image.Image:
    box = im.getchannel("A").getbbox()
    im = im.crop(box)
    side = max(im.size)
    out = Image.new("RGBA", (side, side), (0, 0, 0, 0))
    out.paste(im, ((side - im.width) // 2, (side - im.height) // 2))
    return out


def luma(im: Image.Image) -> float:
    num = den = 0.0
    for r, g, b, a in zip(*[iter(im.tobytes())] * 4):
        num += a * (0.2126 * r + 0.7152 * g + 0.0722 * b) / 255
        den += a
    return num / den if den else 0.0


def rgb565(im: Image.Image) -> list:
    return [((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3) for r, g, b, _ in zip(*[iter(im.tobytes())] * 4)]


def main() -> None:
    c = ['// Generated by scripts/pod_gen_logos.py from .github/assets/engines/*.png. Do not edit.',
         '// px is RGB565 in native order (what ht_pro_image wants), a is alpha; one pre-rendered pair per size.',
         '#include <string.h>', '#include "pod_types.h"', '']
    light, table, total = [], [], 0
    for e in ENGINES:
        im = square(Image.open(src / f"{e}.png").convert("RGBA"))
        if luma(im) > LIGHT_LUMA:
            light.append(e)
        for s in SIZES:
            if s == COVER and e in PETS:
                continue   # a pet engine never draws its cover logo; its slot below is {0, NULL, NULL}
            r = im.resize((s, s), Image.LANCZOS)
            c.append(f"static const uint16_t l_{e}_{s}_px[{s * s}] = {{{','.join(map(str, rgb565(r)))}}};")
            c.append(f"static const uint8_t l_{e}_{s}_a[{s * s}] = {{{','.join(map(str, r.getchannel('A').tobytes()))}}};")
            total += s * s * 3
        c.append(f"static const pod_logo_t l_{e}[POD_LOGO_NSIZES] = {{" +
                 ", ".join("{0, NULL, NULL}" if s == COVER and e in PETS else f"{{{s}, l_{e}_{s}_px, l_{e}_{s}_a}}" for s in SIZES) + "};")
        table.append(f'    {{"{e}", {"true" if e in light else "false"}, l_{e}}},')
    c += ['', f'const uint16_t pod_logo_sizes[POD_LOGO_NSIZES] = {{{", ".join(map(str, SIZES))}}};', '',
          'const pod_mark_t pod_marks[] = {'] + table + ['};', f'const unsigned pod_mark_count = {len(ENGINES)};', '',
          'const pod_mark_t *pod_mark(const char *engine) {', '    if (!engine) return NULL;',
          '    for (unsigned i = 0; i < pod_mark_count; i++)',
          '        if (!strcmp(pod_marks[i].engine, engine)) return &pod_marks[i];', '    return NULL;', '}', '']
    (dest / "engine_logos.c").write_text("\n".join(c))
    (dest / "engine_logos.h").write_text("\n".join([
        "#pragma once", "// Generated by scripts/pod_gen_logos.py. Do not edit.",
        "#include <stdint.h>", "",
        f"enum {{ POD_LOGO_NSIZES = {len(SIZES)}, POD_LOGO_COVER = {COVER} }};   // COVER: every pet-less cover",
        "extern const uint16_t pod_logo_sizes[POD_LOGO_NSIZES];   // ascending; every engine has one logo per size", ""]))
    print("sizes:", SIZES, "engines:", len(ENGINES))
    print("flash bytes (px + alpha):", total, "= %.2f MB" % (total / 1048576))
    print("light:", " ".join(light))


if __name__ == "__main__":
    main()