{"version":3,"file":"image.cjs","names":[],"sources":["../../../src/vision/preprocess/image.ts"],"sourcesContent":["/** @generated Vendored from @mauriciobenjamin700/ort-vision-sdk-web. Do not hand-edit — regenerate with `npm run vendor:vision`. */\n/**\n * Composable image preprocessing primitives (resize, normalize, letterbox, layout).\n *\n * Mirrors the Python `preprocess.image` module. Operates on the canonical\n * {@link RGBImage} (HWC RGB uint8) and produces uint8 or float32 buffers\n * depending on the operation.\n */\n\nimport * as ortRuntime from \"onnxruntime-web\";\nimport type * as ort from \"onnxruntime-web\";\n\nimport { createCanvas, get2DContext, imageDataToRGB, rgbToImageData } from \"../core/canvas\";\nimport { RGBImage } from \"../types\";\n\n/** Resize an image to `(targetWidth, targetHeight)` using high-quality canvas resampling. */\nexport function resize(image: RGBImage, targetWidth: number, targetHeight: number): RGBImage {\n    if (targetWidth <= 0 || targetHeight <= 0) {\n        throw new Error(`Invalid resize target ${targetWidth}x${targetHeight}.`);\n    }\n    if (targetWidth === image.width && targetHeight === image.height) {\n        return image;\n    }\n\n    const srcCanvas = createCanvas(image.width, image.height);\n    const srcCtx = get2DContext(srcCanvas);\n    srcCtx.putImageData(rgbToImageData(image), 0, 0);\n\n    const dstCanvas = createCanvas(targetWidth, targetHeight);\n    const dstCtx = get2DContext(dstCanvas);\n    dstCtx.imageSmoothingEnabled = true;\n    dstCtx.imageSmoothingQuality = \"high\";\n    dstCtx.drawImage(srcCanvas as CanvasImageSource, 0, 0, targetWidth, targetHeight);\n\n    const imageData = dstCtx.getImageData(0, 0, targetWidth, targetHeight);\n    return imageDataToRGB(imageData);\n}\n\n/**\n * Convert a uint8 image to a normalized float32 array (HWC layout preserved).\n *\n * Applies `(pixel * scale - mean) / std` channel-wise.\n */\nexport function normalize(\n    image: RGBImage,\n    mean: readonly [number, number, number],\n    std: readonly [number, number, number],\n    scale: number = 1 / 255,\n): Float32Array {\n    const out = new Float32Array(image.data.length);\n    const data = image.data;\n    const m0 = mean[0];\n    const m1 = mean[1];\n    const m2 = mean[2];\n    const s0 = std[0];\n    const s1 = std[1];\n    const s2 = std[2];\n    for (let i = 0; i < data.length; i += 3) {\n        out[i] = ((data[i] as number) * scale - m0) / s0;\n        out[i + 1] = ((data[i + 1] as number) * scale - m1) / s1;\n        out[i + 2] = ((data[i + 2] as number) * scale - m2) / s2;\n    }\n    return out;\n}\n\n/** Convert a uint8 image to a `Float32Array` in `[0, 1]` (HWC layout preserved). */\nexport function toFloat32(image: RGBImage, scale: number = 1 / 255): Float32Array {\n    const out = new Float32Array(image.data.length);\n    const data = image.data;\n    for (let i = 0; i < data.length; i++) {\n        out[i] = (data[i] as number) * scale;\n    }\n    return out;\n}\n\n/**\n * Transpose interleaved HWC data to planar CHW layout.\n *\n * @param hwc Source array of length `width * height * channels`.\n */\nexport function toCHW(\n    hwc: Float32Array,\n    width: number,\n    height: number,\n    channels: number = 3,\n): Float32Array {\n    const expected = width * height * channels;\n    if (hwc.length !== expected) {\n        throw new Error(\n            `toCHW: expected length ${expected} for ${width}x${height}x${channels}, got ${hwc.length}.`,\n        );\n    }\n    const chw = new Float32Array(expected);\n    const plane = width * height;\n    for (let y = 0; y < height; y++) {\n        for (let x = 0; x < width; x++) {\n            const hwcBase = (y * width + x) * channels;\n            const planeIdx = y * width + x;\n            for (let c = 0; c < channels; c++) {\n                chw[c * plane + planeIdx] = hwc[hwcBase + c] as number;\n            }\n        }\n    }\n    return chw;\n}\n\n/** Wrap a Float32 buffer into an `ort.Tensor`. */\nexport function toFloat32Tensor(data: Float32Array, dims: readonly number[]): ort.Tensor {\n    return new ortRuntime.Tensor(\"float32\", data, dims as number[]);\n}\n\n/**\n * Convert an HWC uint8 image to a CHW `Float32Array` scaled to `[0, 1]`.\n *\n * Mirrors `torchvision.transforms.ToTensor()` semantics: HWC → CHW,\n * `uint8 → float32 / 255`. Useful as input to YOLO-style detectors that\n * don't require ImageNet normalization.\n *\n * @returns CHW `Float32Array` of length `width * height * 3`.\n */\nexport function toTensor(image: RGBImage): Float32Array {\n    const f32 = toFloat32(image);\n    return toCHW(f32, image.width, image.height, 3);\n}\n\n/**\n * Convert an HWC BGR uint8 buffer (OpenCV layout) to the SDK's HWC RGB.\n *\n * Use when you receive image bytes from `cv2.imencode` over the wire and\n * want to feed them to the SDK without going through a canvas decode.\n *\n * @param bgr Flat BGR Uint8Array of length `width * height * 3`.\n */\nexport function fromCv2(bgr: Uint8Array, width: number, height: number): RGBImage {\n    if (bgr.length !== width * height * 3) {\n        throw new Error(\n            `fromCv2: data length ${bgr.length} does not match width * height * 3 = ${width * height * 3}.`,\n        );\n    }\n    const rgb = new Uint8Array(bgr.length);\n    for (let i = 0; i < bgr.length; i += 3) {\n        rgb[i] = bgr[i + 2] as number;\n        rgb[i + 1] = bgr[i + 1] as number;\n        rgb[i + 2] = bgr[i] as number;\n    }\n    return new RGBImage(rgb, width, height);\n}\n\n/**\n * Convert the SDK's HWC RGB image to an HWC BGR `Uint8Array` (OpenCV layout).\n *\n * Useful for round-tripping data to a Python OpenCV consumer.\n */\nexport function toCv2(image: RGBImage): Uint8Array {\n    const rgb = image.data;\n    const bgr = new Uint8Array(rgb.length);\n    for (let i = 0; i < rgb.length; i += 3) {\n        bgr[i] = rgb[i + 2] as number;\n        bgr[i + 1] = rgb[i + 1] as number;\n        bgr[i + 2] = rgb[i] as number;\n    }\n    return bgr;\n}\n\nexport interface LetterboxResult {\n    /** The padded image at the target size. */\n    readonly image: RGBImage;\n    /** The factor applied to the original image (`< 1` if downscaled). */\n    readonly scale: number;\n    /** Horizontal padding in pixels (left side; right side has the same or +1). */\n    readonly padLeft: number;\n    /** Vertical padding in pixels (top side). */\n    readonly padTop: number;\n}\n\n/**\n * Resize preserving aspect ratio, padding to `(targetWidth, targetHeight)`\n * with a constant fill color.\n *\n * Standard YOLO preprocessing — returning `scale` and `padLeft`/`padTop`\n * lets callers map detections back to the original image coordinates.\n */\nexport function letterbox(\n    image: RGBImage,\n    targetWidth: number,\n    targetHeight: number,\n    fill: readonly [number, number, number] = [114, 114, 114],\n): LetterboxResult {\n    const scale = Math.min(targetWidth / image.width, targetHeight / image.height);\n    const newW = Math.round(image.width * scale);\n    const newH = Math.round(image.height * scale);\n    const resized = resize(image, newW, newH);\n\n    const out = new Uint8Array(targetWidth * targetHeight * 3);\n    const f0 = fill[0];\n    const f1 = fill[1];\n    const f2 = fill[2];\n    for (let i = 0; i < out.length; i += 3) {\n        out[i] = f0;\n        out[i + 1] = f1;\n        out[i + 2] = f2;\n    }\n\n    const padLeft = Math.floor((targetWidth - newW) / 2);\n    const padTop = Math.floor((targetHeight - newH) / 2);\n    const rowBytes = newW * 3;\n    for (let y = 0; y < newH; y++) {\n        const srcOffset = y * rowBytes;\n        const dstOffset = ((padTop + y) * targetWidth + padLeft) * 3;\n        out.set(resized.data.subarray(srcOffset, srcOffset + rowBytes), dstOffset);\n    }\n\n    return {\n        image: new RGBImage(out, targetWidth, targetHeight),\n        scale,\n        padLeft,\n        padTop,\n    };\n}\n"],"mappings":"oKAgBA,SAAgB,EAAO,EAAiB,EAAqB,EAAgC,CACzF,GAAI,GAAe,GAAK,GAAgB,EACpC,MAAU,MAAM,yBAAyB,EAAY,GAAG,EAAa,EAAE,EAE3E,GAAI,IAAgB,EAAM,OAAS,IAAiB,EAAM,OACtD,OAAO,EAGX,IAAM,EAAY,EAAA,aAAa,EAAM,MAAO,EAAM,MAAM,EAExD,EADe,aAAa,CAC5B,CAAA,CAAO,aAAa,EAAA,eAAe,CAAK,EAAG,EAAG,CAAC,EAE/C,IAAM,EAAY,EAAA,aAAa,EAAa,CAAY,EAClD,EAAS,EAAA,aAAa,CAAS,EACrC,EAAO,sBAAwB,GAC/B,EAAO,sBAAwB,OAC/B,EAAO,UAAU,EAAgC,EAAG,EAAG,EAAa,CAAY,EAEhF,IAAM,EAAY,EAAO,aAAa,EAAG,EAAG,EAAa,CAAY,EACrE,OAAO,EAAA,eAAe,CAAS,CACnC,CAOA,SAAgB,EACZ,EACA,EACA,EACA,EAAgB,EAAI,IACR,CACZ,IAAM,E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