const b = 1.0 / 3.0;
const c = 1.0 / 3.0;

/**
 * @see Don P. Mitchell, Arun N. Netravali. Reconstruction Filters in Computer Graphics. Computer Graphics, Volume 22, Number 4, AT&T Bell Laboratories, Murray Hill, New Jersey, August 1988
 * @see https://github.com/terifan/ImageResampler/blob/dbc212ce6aaa769bf3c9623cb6ead58ffd51d76c/src/org/terifan/image_resampler/FilterFactory.java#L540
 * @param {number} x
 * @returns {number}
 */
export function mitchell(x) {
    const p0 = (6.0 - 2.0 * b) / 6.0;
    const p2 = (-18.0 + 12.0 * b + 6.0 * c) / 6.0;
    const p3 = (12.0 - 9.0 * b - 6.0 * c) / 6.0;
    const q0 = (8.0 * b + 24.0 * c) / 6.0;
    const q1 = (-12.0 * b - 48.0 * c) / 6.0;
    const q2 = (6.0 * b + 30.0 * c) / 6.0;
    const q3 = (-b - 6.0 * c) / 6.0;

    if (x < -2.0) {
        return 0.0;
    }
    if (x < -1.0) {
        return q0 - x * (q1 - x * (q2 - x * q3));
    }
    if (x < 0.0) {
        return p0 + x * x * (p2 - x * p3);
    }
    if (x < 1.0) {
        return p0 + x * x * (p2 + x * p3);
    }
    if (x < 2.0) {
        return q0 + x * (q1 + x * (q2 + x * q3));
    }
    return 0.0;
}

mitchell.support = 2;

