"""Replace the laptop screen in lid-open.mp4 frames (and the lap_open still) with the SG render.

The film's screen is an AI-painted copy of the KL UI, so it is not differenced against our HTML.
Instead: the screen quad on the final frame is known; each frame i is registered to the final frame
by SIFT on the film's own (identical) screen content -> H_i; the fade-in amount a_i is the high-pass
correlation of the frame with the warped final-frame content; output = F + a*(S_i - K_i) where K_i is
the final frame's screen warped into frame i and S_i is the SG render warped into the same quad.
"""
import sys, numpy as np, cv2
S = sys.argv[1]; LID = S + '/lid'; N = 193
Q193 = np.float32([[503, 110], [1776, 107], [1793, 974], [495, 978]])
dom_s = cv2.imread(S + '/dom-sg.png'); dom_k = cv2.imread(S + '/dom-my.png'); h, w = dom_s.shape[:2]
DOMQ = np.float32([[0, 0], [w, 0], [w, h], [0, h]])
sift = cv2.SIFT_create(6000); bf = cv2.BFMatcher()

def mask_of(quad, shape, shrink=0):
    m = np.zeros(shape[:2], np.uint8); cv2.fillConvexPoly(m, quad.astype(np.int32).reshape(-1, 2), 255)
    return cv2.erode(m, np.ones((3 + shrink, 3 + shrink), np.uint8)) if shrink >= 0 else m

def reg(src_gray, src_mask, dst_gray, dst_mask):
    k1, d1 = sift.detectAndCompute(src_gray, src_mask); k2, d2 = sift.detectAndCompute(dst_gray, dst_mask)
    if d1 is None or d2 is None or len(k2) < 30: return None, 0
    m = bf.knnMatch(d1, d2, k=2); good = [a for a, b in (x for x in m if len(x) == 2) if a.distance < 0.7 * b.distance]
    if len(good) < 30: return None, len(good)
    H, inl = cv2.findHomography(np.float32([k1[g.queryIdx].pt for g in good]), np.float32([k2[g.trainIdx].pt for g in good]), cv2.RANSAC, 2.0)
    return H, (int(inl.sum()) if inl is not None else 0)

def hp(x): return x - cv2.GaussianBlur(x, (0, 0), 5)

def tone_from(dom_ref, target_img, quad):
    """per-channel mean/std map from the KL render to the KL content as painted in the target"""
    Hd = cv2.getPerspectiveTransform(DOMQ, quad)
    Kd = cv2.warpPerspective(dom_ref, Hd, (target_img.shape[1], target_img.shape[0]), flags=cv2.INTER_AREA).astype(np.float32)
    sel = mask_of(quad, target_img.shape, 6) > 0; t = target_img.astype(np.float32)
    return [(t[..., c][sel].std() / max(Kd[..., c][sel].std(), 1e-3), t[..., c][sel].mean(), Kd[..., c][sel].mean()) for c in range(3)]

def apply_tone(img, tone):
    o = img.astype(np.float32).copy()
    for c, (g, mt, md) in enumerate(tone): o[..., c] = (o[..., c] - md) * g + mt
    return o

if __name__ == '__main__' and sys.argv[2] == 'film':
    # Pass 1: register every frame to the final one (backwards, each search seeded by the last good
    # homography), measure the fade-in a_i; extrapolate the motion for the faint frames that do not
    # register. Pass 2: inside the tracked screen, out = (1-a)*wallpaper + a*SG, the wallpaper being
    # the last frame before any content (G0) carried along by the same tracking.
    F = {i: cv2.imread(f'{LID}/f{i:03d}.png') for i in range(1, N + 1)}
    fN = F[N]; gN = cv2.cvtColor(fN, cv2.COLOR_BGR2GRAY); mN = mask_of(Q193, fN.shape, 6)
    tone = tone_from(dom_k, fN, Q193)
    Sd = apply_tone(dom_s, tone).clip(0, 255).astype(np.uint8)
    Hdq = cv2.getPerspectiveTransform(DOMQ, Q193)
    Hs, A = {N: np.eye(3)}, {N: 1.0}; prev = np.eye(3); first = None
    for i in range(N - 1, 0, -1):
        f = F[i]; g = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
        guess = cv2.perspectiveTransform(Q193.reshape(-1, 1, 2), prev).reshape(-1, 2)
        dm = cv2.dilate(mask_of(guess, f.shape, -1), np.ones((81, 81), np.uint8))
        H, inl = reg(gN, mN, g, dm)
        if H is None or inl < 60: break
        K = cv2.warpPerspective(fN, H, (f.shape[1], f.shape[0])).astype(np.float32)
        sel = mask_of(cv2.perspectiveTransform(Q193.reshape(-1, 1, 2), H).reshape(-1, 2), f.shape, 6) > 0
        ff = f.astype(np.float32)
        A[i] = float(np.clip((hp(ff)[sel] * hp(K)[sel]).sum() / max((hp(K)[sel] ** 2).sum(), 1e-6), 0, 1))
        Hs[i] = H; prev = H; first = i
    M = Hs[first] @ np.linalg.inv(Hs[first + 1]); slope = A[first + 1] - A[first]
    i = first - 1
    while True:
        Hs[i] = np.linalg.matrix_power(M, first - i) @ Hs[first]; A[i] = max(0.0, A[first] - slope * (first - i))
        if A[i] == 0.0: break
        i -= 1
    g0 = i  # last frame with no content: the clean wallpaper
    for k in range(N, g0 - 1, -1): A[k] = 1.0 if k >= N - 8 else A[k]
    for k in sorted(A): 
        if A[k] < 0.999: print(k, round(A[k], 3))
    print('tracked to', first, 'wallpaper frame', g0)
    for i in range(1, N + 1):
        f = F[i]
        if i <= g0: cv2.imwrite(f'{LID}/o{i:03d}.png', f); continue
        H = Hs[i]; q = cv2.perspectiveTransform(Q193.reshape(-1, 1, 2), H).reshape(-1, 2)
        Sw = cv2.GaussianBlur(cv2.warpPerspective(Sd, H @ Hdq, (f.shape[1], f.shape[0]), flags=cv2.INTER_AREA).astype(np.float32), (0, 0), 0.6)
        G = cv2.warpPerspective(F[g0], H @ np.linalg.inv(Hs[g0]), (f.shape[1], f.shape[0])).astype(np.float32)
        soft = cv2.GaussianBlur(mask_of(q, f.shape, 0).astype(np.float32) / 255, (0, 0), 1.0)[..., None]
        inner = (1 - A[i]) * G + A[i] * Sw
        out = f.astype(np.float32) * (1 - soft) + inner * soft
        cv2.imwrite(f'{LID}/o{i:03d}.png', np.clip(out, 0, 255).astype(np.uint8))

if __name__ == '__main__' and sys.argv[2] == 'still':
    im = cv2.imread(sys.argv[3], cv2.IMREAD_UNCHANGED); rgb = im[..., :3]; al = im[..., 3]
    fN = cv2.imread(f'{LID}/f{N:03d}.png')
    q = np.float32([[346, 74], [3057, 72], [3098, 1925], [331, 1924]])  # measured on the bezel's inner edge
    tone = tone_from(dom_k, rgb, q)
    Sd = apply_tone(dom_s, tone).clip(0, 255).astype(np.uint8)
    Sw = cv2.warpPerspective(Sd, cv2.getPerspectiveTransform(DOMQ, q), (rgb.shape[1], rgb.shape[0]), flags=cv2.INTER_CUBIC).astype(np.float32)
    soft = cv2.GaussianBlur(mask_of(q, rgb.shape, 0).astype(np.float32) / 255, (0, 0), 1.5)[..., None]
    out = rgb.astype(np.float32) * (1 - soft) + Sw * soft
    cv2.imwrite(sys.argv[4], np.dstack([np.clip(out, 0, 255).astype(np.uint8), al]))
