import { idct_2d } from "../../../../../core/math/idct_2d.js";

const p = new Float64Array(64);

/**
 * An efficient, floating-point quantize and inverse DCT function based on the AAN fast IDCT algorithm.
 * @param {Int32Array} qt quantization table
 * @param {Int32Array} zz zigzag-ordered DCT coefficients
 * @param {Uint8Array|Uint8ClampedArray} output The output array for the 8x8 block.
 * @see idct8x8_fixed
 */
export function idct8x8_float(
    qt,
    zz,
    output
) {

    // 1. Dequantize the coefficients
    for (let i = 0; i < 64; i++) {
        p[i] = (zz[i] * qt[i]);
    }

    idct_2d(p);

    // 4. Level-shift, scale, and clip to 8-bit range
    for (let i = 0; i < 64; ++i) {
        const sample = 128 + Math.round(p[i]);

        // Clip to the valid 8-bit range [0, 255]
        output[i] = sample < 0 ? 0 : sample > 255 ? 255 : sample;
    }
}