// watermarkUtils.ts (Revised function)

/**
 * Identify "quasi-static" pixels within an ROI across multiple frames using statistical analysis (standard deviation).
 * This approach is more robust to transparent watermarks, video noise, and changing backgrounds,
 * as it looks for consistency over time rather than exact matches or comparisons to a single reference frame.
 *
 * @param framesData - An array of ImageData objects collected from different video frames.
 *                     Requires at least 2 frames for meaningful standard deviation calculation.
 * @param roi - The Region of Interest {x, y, width, height} within the video frame to analyze.
 * @param rgbaStdDevThreshold - The maximum allowed standard deviation for R, G, B, and A channels
 *                              for a pixel to be considered "static". Lower value means less tolerance for color/alpha changes.
 * @param luminanceStdDevThreshold - The maximum allowed standard deviation for luminance (brightness)
 *                                   for a pixel to be considered "static". This is crucial for transparent watermarks
 *                                   where absolute RGB values change with background, but relative brightness might be consistent.
 * @returns boolean[] - A flattened boolean array representing the ROI. `true` if the pixel at that position
 *                      is determined to be static, `false` otherwise.
 */
export function identifyStaticPixels(
    framesData: ImageData[],
    roi: { x: number; y: number; width: number; height: number },
    rgbaStdDevThreshold: number,
    luminanceStdDevThreshold: number
): boolean[] {
    if (framesData.length < 2) {
        console.warn("identifyStaticPixels requires at least 2 frames for meaningful standard deviation analysis. Returning all false.");
        return new Array(roi.width * roi.height).fill(false);
    }

    const staticPixelsMap = new Array(roi.width * roi.height).fill(false); // Initialize all to false

    // Iterate over each pixel within the defined ROI
    for (let py = 0; py < roi.height; py++) {
        for (let px = 0; px < roi.width; px++) {
            const currentPixelIdxInRoi = py * roi.width + px;

            // Collect all R, G, B, A, and Luminance values for this specific pixel (roi.x+px, roi.y+py)
            // across ALL sampled frames.
            const rValues: number[] = [];
            const gValues: number[] = [];
            const bValues: number[] = [];
            const aValues: number[] = [];
            const lumValues: number[] = [];

            for (let i = 0; i < framesData.length; i++) {
                const globalX = roi.x + px;
                const globalY = roi.y + py;
                const pixelColor = getPixelRGBA(framesData[i], globalX, globalY);

                rValues.push(pixelColor.r);
                gValues.push(pixelColor.g);
                bValues.push(pixelColor.b);
                aValues.push(pixelColor.a);
                lumValues.push(getLuminance(pixelColor.r, pixelColor.g, pixelColor.b));
            }

            // Calculate the standard deviation for each collected channel and for luminance.
            const rStdDev = calculateStdDev(rValues);
            const gStdDev = calculateStdDev(gValues);
            const bStdDev = calculateStdDev(bValues);
            const aStdDev = calculateStdDev(aValues);
            const lumStdDev = calculateStdDev(lumValues);

            // A pixel is considered "static" if the variation (standard deviation) in all its
            // channels and its luminance is below the respective thresholds.
            // This allows for 'quasi-static' detection, tolerating minor changes inherent in video.
            const isPixelQuasiStatic =
                rStdDev <= rgbaStdDevThreshold &&
                gStdDev <= rgbaStdDevThreshold &&
                bStdDev <= rgbaStdDevThreshold &&
                aStdDev <= rgbaStdDevThreshold &&
                lumStdDev <= luminanceStdDevThreshold;

            staticPixelsMap[currentPixelIdxInRoi] = isPixelQuasiStatic;
        }
    }
    return staticPixelsMap;
}

think harder, this identifyStaticPixels function is still too unforgiving in the sense if just one of the frames is out of stdDeviation it is kulled, i want you to rethink it for maximum forgivability. after a 10 rounds or so nothing is returning, come up with something brilliant for this function that will make tuning it easier and more robust.. with stdDeviation i think you have to remember the transparent logo can get very bright and very dim so perhaps not all that is needed.