Files

78 lines
2.8 KiB
Python

#!/usr/bin/env python3
"""Render frames of the scene VM and compare them with the capture's Mesen screenshots.
usage: scr_compare.py ROM CAPDIR --frames "mesenframe,..." [--plan ...] [--pokes ...] [--out DIR]
For each requested Mesen frame (one that has a screenshot), the VM frame aligned to it is rendered (and its
neighbours, since alignment can be off by one) and the best pixel-difference fraction is reported (a pixel
differs if any channel is off by more than 8). Side-by-side PNGs (Mesen | ours) go to DIR.
"""
import argparse
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
import gfx_snes # noqa: E402
import scr_ppu # noqa: E402
import scr_run # noqa: E402
def diff_fraction(ours, ref, w=256, h=224):
bad = 0
for y in range(h):
row = ours[y]
for x in range(w):
i = (y * w + x) * 3
r, g, b = row[x]
if max(abs(r - ref[i]), abs(g - ref[i + 1]), abs(b - ref[i + 2])) > 8:
bad += 1
return bad / (w * h)
def main():
ap = argparse.ArgumentParser()
ap.add_argument('rom')
ap.add_argument('capdir')
ap.add_argument('--frames', required=True)
ap.add_argument('--plan', default='')
ap.add_argument('--pokes', default='')
ap.add_argument('--out', default='re/runs/scr/compare')
ap.add_argument('--window', type=int, default=1)
args = ap.parse_args()
want = sorted(int(x) for x in args.frames.split(','))
os.makedirs(args.out, exist_ok=True)
rom = open(args.rom, 'rb').read()
r = scr_run.Runner(rom, args.capdir)
plan, pokes = scr_run.parse_plan(args.plan), scr_run.parse_pokes(args.pokes)
refs = {}
for f in want:
p = os.path.join(args.capdir, f'f{f:05d}.png')
if os.path.exists(p):
refs[f] = gfx_snes.read_png_rgb(p)[2]
best = {}
def on_frame(rr, lines):
mf = rr.mesen_frame() - 1
for f in want:
if f in refs and abs(mf - f) <= args.window:
img = scr_ppu.render_frame(rr.s, lines)
d = diff_fraction(img, refs[f])
if f not in best or d < best[f][0]:
best[f] = (d, mf, img)
last = max(want)
while r.mesen_frame() <= last + args.window + 1:
r.run(1, plan, pokes, on_frame)
for f in want:
if f not in best:
print(f'f{f}: not reached or no screenshot')
continue
d, mf, img = best[f]
ref = refs[f]
side = [[(ref[(y * 256 + x) * 3], ref[(y * 256 + x) * 3 + 1], ref[(y * 256 + x) * 3 + 2]) for x in range(256)]
+ [(255, 0, 255)] * 4 + img[y] for y in range(224)]
scr_ppu.write_png(os.path.join(args.out, f'cmp_f{f:05d}.png'), side)
print(f'f{f}: {100 * d:.2f}% pixels differ (our frame aligned to mesen f{mf})')
if __name__ == '__main__':
main()