"""Measure the projection bay in GLM-5.3's own captures (SSE-2026-001).

For each capture, over one fixed rectangle (the projection bay, the same box for all three):
  mean luma (Rec. 709), peak luma, and the share of pixels brighter than 128 and than 90.
A rendered figure shows up as bright pixels; an empty bay has none.

Run:  python measure_bay.py ../evidence
Needs Python 3.9+ and Pillow. The box is the only free parameter; change it and re-run to test the result.
"""
import json
import sys
from pathlib import Path

from PIL import Image

BAY = (515, 150, 765, 450)  # left, top, right, bottom in the 1280x720 capture


def luma(p):
    return 0.2126 * p[0] + 0.7152 * p[1] + 0.0722 * p[2]


def measure(path: Path):
    im = Image.open(path).convert("RGB").crop(BAY)
    pixels = getattr(im, "get_flattened_data", im.getdata)()  # Pillow 12.1+ name, older fallback
    values = [luma(p) for p in pixels]
    n = len(values)
    return {
        "file": path.name,
        "bay_box": BAY,
        "mean_luma": round(sum(values) / n, 1),
        "peak_luma": round(max(values)),
        "pct_above_128": round(100 * sum(v > 128 for v in values) / n, 2),
        "pct_above_90": round(100 * sum(v > 90 for v in values) / n, 2),
    }


if __name__ == "__main__":
    folder = Path(sys.argv[1] if len(sys.argv) > 1 else "../evidence")
    print(json.dumps([measure(p) for p in sorted(folder.glob("Z*.png"))], indent=2))
