import { min2 } from "../../../../core/math/min2.js";
import { sampler_from_bitmap } from "./computeImageBitmapEquality.js";


/**
 *
 * @param {ImageBitmap} bitmap
 * @returns {number}
 */
export function computeImageBitmapHash(bitmap) {

    const w = min2(bitmap.width, 8);
    const h = min2(bitmap.height, 8);

    const sampler = sampler_from_bitmap(bitmap);

    const sampler_data = sampler.data;
    const item_size = sampler.itemSize;

    let hash = 0;

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

        const row_size = y * sampler.width;

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

            const texel_index = x + row_size;
            const address = texel_index * item_size;

            for (let i = 0; i < item_size; i++) {

                const v = sampler_data[address];

                hash = (hash << 5) - hash + v;
            }
        }
    }

    return hash;
}
