// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
import * as _ from "lodash-es";
import { decodeBGR8p, decodeBGRA8, decodeBayerBGGR8, decodeBayerGBRG8, decodeBayerGRBG8, decodeI420, decodeBayerRGGB8, decodeFloat1c, decodeMono16, decodeMono8, decodeRGB8, decodeRGB8p, decodeRGBA8, decodeUYVY, decodeYUYV, decodeNV12, decodeBGR8i, decodeYUV420p, } from "@foxglove/den/image";
import { getColorConverter } from "../colorMode";
export async function decodeCompressedImageToBitmap(image, resizeWidth) {
    const bitmapData = new Blob([image.data], { type: `image/${image.format}` });
    return await createImageBitmap(bitmapData, { resizeWidth });
}
export const IMAGE_DEFAULT_COLOR_MODE_SETTINGS = {
    colorMode: "gradient",
    flatColor: "#ffffff",
    gradient: ["#000000", "#ffffff"],
    colorMap: "turbo",
    explicitAlpha: 0
};
const MIN_MAX_16_BIT = { minValue: 0, maxValue: 65535 };
/**
 * See also:
 * https://github.com/ros2/common_interfaces/blob/366eea24ffce6c87f8860cbcd27f4863f46ad822/sensor_msgs/include/sensor_msgs/image_encodings.hpp
 */
export async function decodeRawImage(image, options, output) {
    const { encoding, width, height, step } = image;
    const { stride, planeStride } = image;
    const is_bigendian = "is_bigendian" in image ? image.is_bigendian : false;
    switch (encoding) {
        case "yuv422":
        case "uyvy":
            decodeUYVY(image.data, width, height, step, output);
            break;
        case "nv12":
            decodeNV12(image.data, width, height, output, stride, planeStride);
            break;
        case "yuv422_yuy2":
        case "yuyv":
            decodeYUYV(image.data, width, height, step, output);
            break;
        case "yuv420p":
            decodeYUV420p(image.data, width, height, step, output);
            break;
        case "rgb8":
            decodeRGB8(image.data, width, height, step, output);
            break;
        case "rgb8p":
            decodeRGB8p(image.data, width, height, step, output);
            break;
        case "rgba8":
            decodeRGBA8(image.data, width, height, step, output);
            break;
        case "bgra8":
            decodeBGRA8(image.data, width, height, step, output);
            break;
        case "bgr8":
        case "bgr8p":
        case "8UC3":
            decodeBGR8p(image.data, width, height, step, output);
            break;
        case "bgr8i":
            decodeBGR8i(image.data, width, height, step, output);
            break;
        case "32FC1":
            decodeFloat1c(image.data, width, height, step, is_bigendian, output);
            break;
        case "I420":
            decodeI420(image.data, width, height, step, output);
            break;
        //if (!navigator.gpu) {
        //  console.warn('WebGPU not supported, falling back to CPU');
        //   decodeI420(image.data as Uint8Array, width, height, step, output);
        //   return;
        //}
        //
        //const res = await decodeI420ToRGBA(image.data as Uint8Array, width, height);
        //output.set(res);
        //break;
        case "bayer_rggb8":
            decodeBayerRGGB8(image.data, width, height, step, output);
            break;
        case "bayer_bggr8":
            decodeBayerBGGR8(image.data, width, height, step, output);
            break;
        case "bayer_gbrg8":
            decodeBayerGBRG8(image.data, width, height, step, output);
            break;
        case "bayer_grbg8":
            decodeBayerGRBG8(image.data, width, height, step, output);
            break;
        case "mono8":
        case "8UC1":
            decodeMono8(image.data, width, height, step, output);
            break;
        case "mono16":
        case "16UC1": {
            // combine options with defaults. lodash merge makes sure undefined values in options are replaced with defaults
            // whereas a normal spread would allow undefined values to overwrite defaults
            const settings = _.merge({}, IMAGE_DEFAULT_COLOR_MODE_SETTINGS, MIN_MAX_16_BIT, options);
            if (settings.colorMode === "rgba-fields" || settings.colorMode === "flat") {
                throw Error(`${settings.colorMode} color mode is not supported for mono16 images`);
            }
            const min = settings.minValue;
            const max = settings.maxValue;
            const tempColor = { r: 0, g: 0, b: 0, a: 0 };
            const converter = getColorConverter(settings, min, max);
            decodeMono16(image.data, width, height, step, is_bigendian, output, {
                minValue: options.minValue,
                maxValue: options.maxValue,
                colorConverter: (value) => {
                    converter(tempColor, value);
                    return tempColor;
                },
            });
            break;
        }
        default:
            throw new Error(`Unsupported encoding ${encoding}`);
    }
}
//# sourceMappingURL=decodeImage.js.map