import { assert } from "../../../../../core/assert.js";

/**
 *
 * @param {Sampler2D} input
 * @param {Sampler2D} output
 */
export function sampler2d_scale_up_linear(input, output) {
    assert.defined(input, 'input');
    assert.defined(output, 'output');

    const targetItemSize = output.itemSize;

    assert.equal(input.itemSize, targetItemSize, 'both source and target must have the same number of channels');

    const targetWidth = output.width;
    const targetHeight = output.height;
    const sourceWidth = input.width;
    const sourceHeight = input.height;


    const target_data = output.data;

    if (sourceWidth === targetWidth && sourceHeight === targetHeight) {
        // exact size match, nothing to do
        target_data.set(input.data);
        return;
    }

    const targetRowSize = targetItemSize * targetWidth;

    const source_scale_x = sourceWidth / targetWidth;
    const source_scale_y = sourceHeight / targetHeight;

    for (let y = 0; y < targetHeight; y++) {


        const sample_y = y * source_scale_y;
        const target_row_address = y * targetRowSize;

        for (let x = 0; x < targetWidth; x++) {


            const target_offset = target_row_address + x * targetItemSize;

            input.sampleBilinear(x * source_scale_x, sample_y, target_data, target_offset);

        }
    }
}
