namespace NodePipeline.Application.ImageProcessing;

public static class Convolution
{
    public static void Apply(ImageBuffer src, ImageBuffer dst, float[] kernel)
    {
        var w = src.Width;
        var h = src.Height;
        var radius = kernel.Length / 2;

        for (var y = 0; y < h; y++)
        for (var x = 0; x < w; x++)
        for (var c = 0; c < 3; c++)
        {
            float acc = 0;
            for (var k = -radius; k <= radius; k++)
            {
                var px = Math.Clamp(x + k, 0, w - 1);
                var idx = (y * w + px) * 3 + c;
                acc += src.Pixels[idx] * kernel[k + radius];
            }

            dst.Pixels[(y * w + x) * 3 + c] = (byte)Math.Clamp(acc, 0, 255);
        }
    }
}