using System;

namespace FullScreenShaders.Structure
{
    static class BicubicKernel
    {
        public static float[] GetWeights(int downscaleFactor)
        {
            return GetWeights(downscaleFactor, -0.5);
        }

        /// <param name="downscaleFactor"></param>
        /// <param name="a">
        /// Defines the sharpness or smoothness of the interpolation.
        /// Values between -0.5 and 0 tend to a smoother, bilinear-like filter.
        /// Values between -0.5 and -1 tend to a sharper, sinc-like filter.
        /// </param>
        public static float[] GetWeights(int downscaleFactor, double a)
        {
            var weights = new float[downscaleFactor];

            for (int i = 0; i < weights.Length; i++)
            {
                double absX = Math.Abs(1.0 / downscaleFactor + (double)i / downscaleFactor * 2);

                double weight;
                if (absX > 0 && absX <= 1)
                    weight = (a + 2) * Math.Pow(absX, 3) - (a + 3) * Math.Pow(absX, 2) + 1;
                else if (absX <= 2)
                    weight = a * Math.Pow(absX, 3) - 5 * a * Math.Pow(absX, 2) + 8 * a * absX - 4 * a;
                else
                    weight = 0;

                weights[i] = (float)(weight / downscaleFactor * 2);
            }

            return weights;
        }
    }
}
