"""Phase 1 data: one source frame as an 8bpp 32X picture, plus the exact expected render. Writes frame_pal.bin (256 big-endian words, r | g<<5 | b<<10, through bit 0), frame_pix.bin (320 x 224 indices, row-major) and expected.png: each pixel the palette entry expanded as v<<3 | v>>2 (sega32x-python's `rgb_levels`). py -3.12 study/hello_frame.py --frame 100 --out out32/hello """ from __future__ import annotations import argparse import sys from pathlib import Path import numpy as np from PIL import Image sys.path.insert(0, str(Path(__file__).resolve().parent)) sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from codec_study import FIELDS, expand555, fit_palette, palette_lut, rgb555, weighted_sse # noqa: E402 from rrmd.encode import load_picture # noqa: E402 def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--frame", type=int, default=100, help="video frame (0-based)") ap.add_argument("--out", type=Path, required=True) a = ap.parse_args() src = load_picture(sorted(FIELDS.glob("f*.png"))[a.frame]) pal = fit_palette(src.reshape(-1, 3), rounds=16) d = rgb555(src.astype(np.float32)) idx = palette_lut(pal)[(d[..., 0].astype(np.int32) << 10) | (d[..., 1].astype(np.int32) << 5) | d[..., 2]] words = (pal[:, 0].astype(np.uint16) | (pal[:, 1].astype(np.uint16) << 5) | (pal[:, 2].astype(np.uint16) << 10)) a.out.mkdir(parents=True, exist_ok=True) (a.out / "frame_pal.bin").write_bytes(words.astype(">u2").tobytes()) (a.out / "frame_pix.bin").write_bytes(idx.astype(np.uint8).tobytes()) expected = expand555(pal)[idx] Image.fromarray(expected).save(a.out / "expected.png") err = float(weighted_sse(expected, src).sum() / src[..., 0].size) print(f"frame {a.frame}: {len(np.unique(idx))} colours used, error {err:.1f}") if __name__ == "__main__": main()