#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Compare the board's composite frames with a detector's (milestone 4), separating what a rule disagreement looks like from animation phase. ``scripts/compare_frames.py`` counts differing pixels against the closest detector frame. In a game the two layers animate on their own, so a composite never matches whole: the question here is which layer each pixel came from. Using the board's composite (``C``, ``fNNNNN.png``) and 32X-only frame (``X``, ``fNNNNN-32x.png``), for each detector frame ``D``: - ``32x``: pixels where the board shows the 32X (C = X) and D differs from C; - ``md``: pixels where the board shows the Mega Drive (C != X) and D differs from C; - ``took-32x``: of those, pixels where D equals X: the detector took the 32X's pixel where the board took the Mega Drive's -- a rule difference, not a phase; - ``took-md``: pixels where the board shows the 32X and D differs from C but equals the Mega Drive's own frame (``fNNNNN-md.png``, when the run saved one). The detector frame with the fewest differing pixels is reported, and the smallest ``took-32x`` over all frames (a real rule difference survives every phase). python scripts/compare_composite.py BOARD_DIR FRAME DETECTOR_DIR \\ [--first N --last N] [--offset X,Y] [--tolerance 12] [--step 1] """ from __future__ import annotations import argparse import sys from pathlib import Path from compare_frames import differs from megadrive_python.png import read_png def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("board", type=Path) parser.add_argument("frame", type=int) parser.add_argument("detector", type=Path) parser.add_argument("--first", type=int, default=1) parser.add_argument("--last", type=int, default=1000000) parser.add_argument("--offset", default="0,0") parser.add_argument("--tolerance", type=int, default=12) parser.add_argument("--step", type=int, default=1) args = parser.parse_args() dx, dy = (int(n) for n in args.offset.split(",")) tol = args.tolerance composite = read_png(args.board / f"f{args.frame:05d}.png") x32 = read_png(args.board / f"f{args.frame:05d}-32x.png") md_path = args.board / f"f{args.frame:05d}-md.png" md = read_png(md_path) if md_path.exists() else None shows_32x = [ [not differs(c, x, 0) for c, x in zip(rc, rx, strict=False)] for rc, rx in zip(composite, x32, strict=False) ] total_md = sum(row.count(False) for row in shows_32x) best = None fewest_took = None for path in sorted(args.detector.glob("f*.png")): try: number = int(path.stem[1:]) except ValueError: continue if not args.first <= number <= args.last or (number - args.first) % args.step: continue other = read_png(path) d32 = dmd = took = took_md = 0 for y, row in enumerate(composite): if y + dy >= len(other): break theirs = other[y + dy] mask = shows_32x[y] xrow = x32[y] for x, pixel in enumerate(row[: min(len(mask), len(theirs) - dx)]): d = theirs[x + dx] if differs(pixel, d, tol): if mask[x]: d32 += 1 if md is not None and not differs(d, md[y][x], tol): took_md += 1 else: dmd += 1 if not differs(d, xrow[x], tol): took += 1 if best is None or d32 + dmd < best[1] + best[2]: best = (number, d32, dmd, took, took_md) if fewest_took is None or took < fewest_took[1]: fewest_took = (number, took) if best is None or fewest_took is None: print("no detector frame in range", file=sys.stderr) return 1 print(f"{args.board.name} f{args.frame}: board shows 32X on " f"{sum(row.count(True) for row in shows_32x)}, Mega Drive on {total_md}; " f"closest {args.detector.name} frame {best[0]}: 32x {best[1]}, md {best[2]}, " f"took-32x {best[3]}, took-md {best[4]}; fewest took-32x {fewest_took[1]} " f"at frame {fewest_took[0]}") # fmt: skip return 0 if __name__ == "__main__": sys.exit(main())