import sys, json
import numpy as np
from PIL import Image

def box(path, reg):
    x0,y0,x1,y1,thr = reg[:5]
    mode = reg[5] if len(reg)>5 else 'dark'
    a = np.asarray(Image.open(path).convert('RGB'), dtype=float).mean(axis=2)
    s = a[y0:y1, x0:x1]
    m = (s>thr) if mode=='bright' else (s<thr)
    ys,xs = np.where(m)
    if len(ys)==0: return None
    return (x0+xs.min(), y0+ys.min(), x0+xs.max(), y0+ys.max())

regs = json.load(open(sys.argv[1]))
r = sys.argv[2]
print(f"{'name':11s} {'mock':28s} {'render':28s}  dL  dT  dW  dH")
for k,v in regs.items():
    a=box('mock.jpg',v); b=box(r,v)
    if a is None or b is None:
        print(k, a, b); continue
    dL,dT = b[0]-a[0], b[1]-a[1]
    dW,dH = (b[2]-b[0])-(a[2]-a[0]), (b[3]-b[1])-(a[3]-a[1])
    flag=' <<<' if max(abs(dL),abs(dT),abs(dW),abs(dH))>=3 else ''
    print(f"{k:11s} {str(a):28s} {str(b):28s} {dL:+4d}{dT:+4d}{dW:+4d}{dH:+4d}{flag}")
