// 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 { estimateObjectSize } from "@foxglove/studio-base/players/messageMemoryEstimation.js";
import { ensureWebCodecsSupported } from "../../../utils/compatibility.js";
import { parseMessage, parsePixelFormat } from "../../utils.js";
export async function deserializeMJPEGFrame({ topic, message, callback }) {
    const { receiveTime } = parseMessage(message);
    const supported = ensureWebCodecsSupported("ImageDecoder");
    if (!supported) {
        return;
    }
    const decoder = new ImageDecoder({
        type: "image/jpeg",
        data: message.data
    });
    const { image: frame } = await decoder.decode();
    const buffer = new Uint8Array(frame.allocationSize());
    await frame.copyTo(buffer);
    const [encoding, step] = parsePixelFormat(frame.format);
    const foxgloveMessage = {
        timestamp: receiveTime,
        frame_id: "camera",
        width: frame.displayWidth,
        height: frame.displayHeight,
        data: buffer,
        encoding,
        step: frame.displayWidth * step
    };
    frame.close();
    callback({
        topic,
        receiveTime,
        message: foxgloveMessage,
        sizeInBytes: estimateObjectSize(foxgloveMessage),
        schemaName: "foxglove.RawImage"
    });
}
//# sourceMappingURL=mjpeg.js.map