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

/**
 *
 * @param {Sampler2D} sampler
 * @param {HTMLCanvasElement} canvas
 */
export function sampler2d_write_to_canvas_raw(sampler, canvas) {

    assert.defined(sampler, 'sampler');
    assert.defined(canvas, 'canvas');

    const width = sampler.width;
    const height = sampler.height;

    if (canvas.width !== width) {
        canvas.width = width;
    }

    if (canvas.height !== height) {
        canvas.height = height;
    }

    if (height === 0 || width === 0) {
        //there is no data, just return
        return;
    }

    const context = canvas.getContext("2d");

    const imageData = context.createImageData(width, height);
    const array = imageData.data;

    const destination_sampler = new Sampler2D(array, 4, width, height);

    sampler2d_to_uint8_RGBA(destination_sampler, sampler);

    context.putImageData(imageData, 0, 0);

}
