/*!
 * Copyright (c) 2026 Vertex Software LLC. All rights reserved.
 */
import { ImageLoadError } from "../errors";
function asBlobPart(bytes) {
    const copy = new Uint8Array(bytes.byteLength);
    copy.set(bytes);
    return copy.buffer;
}
function loadImageBytesAsImageElement(imageData) {
    return new Promise((resolve, reject) => {
        const blob = new Blob([asBlobPart(imageData)]);
        const blobUrl = URL.createObjectURL(blob);
        const image = new Image();
        image.addEventListener('load', () => {
            resolve({ image, dispose: () => undefined });
            URL.revokeObjectURL(blobUrl);
        });
        image.addEventListener('error', () => {
            reject(new ImageLoadError('Failed to load image data'));
            URL.revokeObjectURL(blobUrl);
        });
        image.src = blobUrl;
    });
}
async function loadImageBytesAsImageBitmap(imageData) {
    const blob = new Blob([asBlobPart(imageData)]);
    const bitmap = await window.createImageBitmap(blob);
    return { image: bitmap, dispose: () => bitmap.close() };
}
export function loadImageBytes(imageBytes) {
    if (window.createImageBitmap != null) {
        return loadImageBytesAsImageBitmap(imageBytes);
    }
    else {
        return loadImageBytesAsImageElement(imageBytes);
    }
}
//# sourceMappingURL=imageLoaders.js.map
