import os
from PIL import Image
import numpy as np
import cv2

diff_png = 'client/cocos/assets/resources/vfx/savage_primal_skills_vfx_atlas.png'
img = Image.open(diff_png)
arr = np.array(img)

alpha = arr[:, :, 3]
rgb = arr[:, :, :3]

mask_opaque = (alpha > 0).astype(np.uint8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
dilated_mask = cv2.dilate(mask_opaque, kernel)

# Fringe is the 1-pixel margin right outside opaque regions (where alpha is 0 in original image)
fringe = (dilated_mask == 1) & (mask_opaque == 0)
fringe_pixel_count = np.sum(fringe)
print(f"Total boundary fringe pixels (alpha=0, adjacent to alpha>0): {fringe_pixel_count}")

fringe_rgb = rgb[fringe]
fringe_mean = np.mean(fringe_rgb)
non_zero_fringe_pixels = np.sum(np.any(fringe_rgb > 0, axis=1))

print(f"Fringe RGB mean value: {fringe_mean:.2f}")
print(f"Fringe pixels with non-zero RGB (dilated color): {non_zero_fringe_pixels} / {fringe_pixel_count} ({non_zero_fringe_pixels/fringe_pixel_count*100:.1f}%)")

# If dilation was NOT applied, fringe pixels in an image created from transparent canvas would have RGB = 0.
# With dilation, non_zero_fringe_pixels should be ~100% and mean should be > 0.
assert fringe_pixel_count > 0, "No boundary fringe found!"
assert non_zero_fringe_pixels / fringe_pixel_count > 0.95, "Dilation padding was not applied to fringe pixels!"
print("DILATION PADDING FORENSIC CHECK: PASS (No dark halos/alpha bleed!)")
