"""App-surface diffs for PR 27671: compare initial stills below the iOS status bar (y >= 60 of 874 at 1x).
Diff0 = |before-after| amplified x255 (any change is white), no threshold. DiffT = magenta where max channel delta > 16/255.
The status bar (system clock, battery, signal) is outside every WebView this PR changes; the full-frame
stills keep it."""
import sys, json
from pathlib import Path
from PIL import Image, ImageChops
src, out = Path(sys.argv[1]), Path(sys.argv[2])
TOP = 60
rows = sys.argv[3:]
report = {}
for r in rows:
    a = Image.open(src / f"{r}-before-initial.png").convert("RGB")
    b = Image.open(src / f"{r}-after-initial.png").convert("RGB")
    assert a.size == b.size == (402, 874), (r, a.size, b.size)
    box = (0, TOP, a.width, a.height)
    a, b = a.crop(box), b.crop(box)
    d = ImageChops.difference(a, b)
    px = list(d.getdata())
    n0 = sum(1 for p in px if max(p) > 0)
    nT = sum(1 for p in px if max(p) > 16)
    d.point(lambda v: min(255, v * 255)).save(out / f"{r}-app-diff0.png")
    t = Image.new("RGB", d.size, (0, 0, 0))
    t.putdata([(255, 0, 255) if max(p) > 16 else (max(p) * 3,) * 3 for p in px])
    t.save(out / f"{r}-app-diffT16.png")
    report[r] = {"region": f"x0-402 y{TOP}-874 (1x pt)", "changed_zero_threshold": n0, "changed_over_16": nT, "max_delta": max(max(p) for p in px)}
(out / "app-diff-report.json").write_text(json.dumps(report, indent=1))
print(json.dumps(report, indent=1))
