#!/usr/bin/env python3 """Run test ROMs that ship a reference screenshot on MAME 0.285 and compare the final frames. For krom's suite (``tests/test_roms/krom``: each ``X.sfc`` has ``X.png`` beside it, 256x224) and anything laid out the same way. MAME's snapshot is 512 wide (its screen runs at twice the dot clock), so every second column is taken. Colours are compared after rounding to the SNES's 5 bits per channel, so the two programs' different 5-to-8-bit expansions do not count (MAME draws full white-blue as 247, krom's pictures as 248). python scripts/mame_screens.py tests/test_roms/krom/CPUTest --seconds 5 --jobs 8 Prints one line per ROM: differing pixels, then the path. MAME is a detector (docs/validation.md); krom's screenshots are emulator output, so a difference names a suspect, never a verdict. Needs Pillow; run with ``python -I``. """ from __future__ import annotations import argparse import concurrent.futures import subprocess import sys from pathlib import Path from PIL import Image ROOT = Path(__file__).resolve().parents[1] def run(rom: Path, seconds: int, driver: str) -> tuple[Path, int | str]: expected = rom.with_suffix(".png") out = ROOT / "tests" / "mame_traces" / "screens" / rom.parent.name / rom.stem result = subprocess.run( [sys.executable, "-I", str(ROOT / "scripts" / "mame_trace.py"), driver, str(rom), "--no-trace", "--seconds", str(seconds), "--out", str(out)], capture_output=True, text=True, check=False, ) # fmt: skip snaps = sorted((out / "snap").rglob("*.png")) if result.returncode or not snaps: return rom, "no snapshot" got = Image.open(snaps[-1]).convert("RGB") want = Image.open(expected).convert("RGB") if got.width == 2 * want.width: got = got.resize((want.width, got.height), Image.Resampling.NEAREST) # MAME's snapshot is one line taller (225, or 450 interlaced) than the 224 # active lines; measured on CPUADC (2026-10-21) the extra line is at the # bottom: cropping it there makes the frames identical, cropping the top # leaves 4,314 pixels different. extra = got.height - (want.height if want.height in (224, 239, 448, 478) else 2 * want.height) if extra in (1, 2): got = got.crop((0, 0, got.width, got.height - extra)) if want.height == 2 * got.height: # krom doubles pseudo-hires pictures vertically want = want.resize((want.width, got.height), Image.Resampling.NEAREST) if got.size != want.size: return rom, f"size {got.size} vs {want.size}" a, b = got.load(), want.load() diff = sum( 1 for y in range(want.height) for x in range(want.width) if tuple(round(c * 31 / 255) for c in a[x, y]) != tuple(round(c * 31 / 255) for c in b[x, y]) ) return rom, diff def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument( "tree", type=Path, help="directory searched for *.sfc with a .png beside it" ) parser.add_argument("--seconds", type=int, default=5) parser.add_argument("--driver", default="snes") parser.add_argument("--jobs", type=int, default=4) args = parser.parse_args() roms = sorted(p for p in args.tree.rglob("*.sfc") if p.with_suffix(".png").exists()) exact = 0 with concurrent.futures.ThreadPoolExecutor(args.jobs) as pool: for rom, diff in pool.map(lambda r: run(r, args.seconds, args.driver), roms): exact += diff == 0 print(f"{diff!s:>12} {rom.relative_to(args.tree)}") print(f"{exact} of {len(roms)} identical") return 0 if __name__ == "__main__": sys.exit(main())