import cv2, numpy as np

W = "/Users/danicosta/Desktop/AI-Instagram/_inpaint"
H, Wd = 1280, 720
cx, cy = 603, 1170
SIGMA = 4.0
DY = 75          # origen de grano: 75 px hacia ABAJO (jersey limpio)

mask = np.zeros((H, Wd), np.uint8)
cv2.ellipse(mask, (cx, cy), (50, 60), 0, 0, 360, 255, -1)
alpha = cv2.GaussianBlur(mask.astype(np.float32), (0, 0), sigmaX=6.0) / 255.0
alpha = np.clip(alpha, 0, 1)[..., None]

for idx, tag in [(0, "0"), (48, "2"), (96, "4")]:
    base = cv2.imread(f"{W}/frames_out/{idx+1:05d}.png").astype(np.float32)   # ProPainter v2 (sin estrella, suave)
    orig = cv2.imread(f"{W}/frames_full/{idx+1:05d}.png").astype(np.float32)  # original (jersey real)
    # desplazar el original para muestrear grano desde 75px abajo
    M = np.float32([[1, 0, 0], [0, 1, -DY]])
    shifted = cv2.warpAffine(orig, M, (Wd, H), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_REFLECT)
    base_lp = cv2.GaussianBlur(base, (0, 0), sigmaX=SIGMA)
    det_hp  = shifted - cv2.GaussianBlur(shifted, (0, 0), sigmaX=SIGMA)
    inj = base_lp + det_hp
    out = base * (1 - alpha) + inj * alpha
    cv2.imwrite(f"{W}/diag2/inj_{tag}.png", np.clip(out, 0, 255).astype(np.uint8))
    print("saved inj", tag)
print("done")
