#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Compare the board's 32X-only frames with a detector's frames (milestone 3). The detectors (PicoDrive, BlastEm through ``scripts/libretro_frames.py``) give the composite picture, the board the 32X's alone (``scripts/run_32x.py``, ``fNNNNN-32x.png``), so the comparison is of the 320 x 224 picture area, pixel by pixel, with a tolerance per channel for the colour conversion (each emulator expands 5-bit levels its own way; the RAMDAC's levels are unmeasured, ``Quirks.rgb_levels``). python scripts/compare_frames.py BOARD.png DETECTOR_DIR [--first 300 --last 900] \\ [--offset X,Y] [--tolerance 12] [--diff out.png] Each detector frame in ``DETECTOR_DIR`` (``fNNNNN.png``) from ``--first`` to ``--last`` is compared and the closest one reported: for an animated program the two emulators need not agree on which frame shows which picture, only that one of them does. ``--offset`` is where the detector's picture area starts (0,0 for PicoDrive's 320 x 224; BlastEm adds a border). ``--diff`` writes the board's frame with every differing pixel marked magenta. """ from __future__ import annotations import argparse import sys from pathlib import Path from megadrive_python.png import read_png, write_png def differs(a: int, b: int, tolerance: int) -> bool: return ( abs((a >> 16) - (b >> 16)) > tolerance or abs(((a >> 8) & 0xFF) - ((b >> 8) & 0xFF)) > tolerance or abs((a & 0xFF) - (b & 0xFF)) > tolerance ) def compare( board: list[list[int]], other: list[list[int]], dx: int, dy: int, tolerance: int ) -> list[tuple[int, int]]: out = [] for y, row in enumerate(board): if y + dy >= len(other): break theirs = other[y + dy] for x, pixel in enumerate(row[: len(theirs) - dx]): if differs(pixel, theirs[x + dx], tolerance): out.append((x, y)) return out def main() -> int: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("board", type=Path) parser.add_argument("detector", type=Path) parser.add_argument("--first", type=int, default=1) parser.add_argument("--last", type=int, default=100000) parser.add_argument("--offset", default="0,0") parser.add_argument("--tolerance", type=int, default=12) parser.add_argument("--diff", type=Path) args = parser.parse_args() dx, dy = (int(n) for n in args.offset.split(",")) board = read_png(args.board) best: tuple[int, int, list[tuple[int, int]]] | None = 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: continue diff = compare(board, read_png(path), dx, dy, args.tolerance) if best is None or len(diff) < best[1]: best = (number, len(diff), diff) if not diff: break if best is None: print("no detector frame in range", file=sys.stderr) return 1 number, count, diff = best total = len(board) * len(board[0]) print(f"{args.board.name}: closest {args.detector.name} frame {number}: {count} of {total} " f"pixels differ (tolerance {args.tolerance})") # fmt: skip if diff: xs = [x for x, _ in diff] ys = [y for _, y in diff] print(f" differing area x {min(xs)}-{max(xs)}, y {min(ys)}-{max(ys)}") if args.diff is not None: marked = [list(row) for row in board] for x, y in diff: marked[y][x] = 0xFF00FF write_png(args.diff, marked) return 0 if __name__ == "__main__": sys.exit(main())