'use client';
import { e as expose, t as transfer } from './comlink-jf8bdrAf.js';

// 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/


// Reusable buffers to avoid allocations
let depthBuffer = null;
let colorBuffer = null;
function colorDepht(data, encoding, width, height, maxSteroDepth) {
  const pixelCount = width * height;
  if (!depthBuffer || depthBuffer.length < pixelCount) {
    depthBuffer = new Float32Array(pixelCount);
  }
  if (!colorBuffer || colorBuffer.length < pixelCount * 3) {
    colorBuffer = new Uint8Array(pixelCount * 3);
  }
  parseDepthData(data, encoding, width, height, depthBuffer);
  colorizeDepthFrame(depthBuffer, width, height, maxSteroDepth, colorBuffer);
  const result = new Uint8Array(pixelCount * 3);
  result.set(colorBuffer.subarray(0, pixelCount * 3));
  return transfer(result, [result.buffer]);
}
function parseDepthData(data, encoding, width, height, depthData) {
  const pixelCount = width * height;
  if (encoding === "mono16" || encoding === "16UC1") {
    const byteOffset = data.byteOffset;
    const byteLength = data.byteLength;
    if (byteOffset % 2 === 0 && byteLength >= pixelCount * 2) {
      const uint16View = new Uint16Array(data.buffer, byteOffset, pixelCount);
      for (let i = 0; i < pixelCount; i++) {
        depthData[i] = uint16View[i] * 0.001;
      }
    } else {
      // Fallback: use DataView for unaligned data
      const view = new DataView(data.buffer, byteOffset, byteLength);
      for (let i = 0; i < pixelCount; i++) {
        depthData[i] = view.getUint16(i * 2, true) * 0.001;
      }
    }
  } else if (encoding === "32FC1") {
    const byteOffset = data.byteOffset;
    const byteLength = data.byteLength;
    if (byteOffset % 4 === 0 && byteLength >= pixelCount * 4) {
      const float32View = new Float32Array(data.buffer, byteOffset, pixelCount);
      for (let i = 0; i < pixelCount; i++) {
        depthData[i] = float32View[i];
      }
    } else {
      const view = new DataView(data.buffer, byteOffset, byteLength);
      for (let i = 0; i < pixelCount; i++) {
        depthData[i] = view.getFloat32(i * 4, true);
      }
    }
  } else {
    throw new Error(`Unsupported depth encoding: ${encoding}`);
  }
}
expose({
  colorDepht
});
function colorizeDepthFrame(depthData, width, height, maxDepth, colorizedData) {
  const pixelCount = width * height;
  const colormap = getJetColormap();
  const invMaxDepth = 255 / maxDepth;
  for (let i = 0; i < pixelCount; i++) {
    const depth = depthData[i];
    const clipped = depth < 0 ? 0 : depth > maxDepth ? maxDepth : depth;
    const normalized = Math.floor(clipped * invMaxDepth);
    const colorOffset = normalized * 3;
    const offset = i * 3;
    colorizedData[offset] = colormap[colorOffset];
    colorizedData[offset + 1] = colormap[colorOffset + 1];
    colorizedData[offset + 2] = colormap[colorOffset + 2];
  }
}
let cachedColormapFlat = null;
function getJetColormap() {
  if (cachedColormapFlat) {
    return cachedColormapFlat;
  }
  const colormap = new Uint8Array(256 * 3);
  for (let i = 0; i < 256; i++) {
    const value = i / 255.0;
    let r, g, b;
    if (value < 0.125) {
      r = 0;
      g = 0;
      b = 0.5 + 0.5 * (value / 0.125);
    } else if (value < 0.375) {
      r = 0;
      g = (value - 0.125) / 0.25;
      b = 1;
    } else if (value < 0.625) {
      r = (value - 0.375) / 0.25;
      g = 1;
      b = 1 - (value - 0.375) / 0.25;
    } else if (value < 0.875) {
      r = 1;
      g = 1 - (value - 0.625) / 0.25;
      b = 0;
    } else {
      r = 1 - 0.5 * (value - 0.875) / 0.125;
      g = 0;
      b = 0;
    }
    const offset = i * 3;
    colormap[offset] = Math.floor(r * 255);
    colormap[offset + 1] = Math.floor(g * 255);
    colormap[offset + 2] = Math.floor(b * 255);
  }
  colormap[0] = 0;
  colormap[1] = 0;
  colormap[2] = 0;
  cachedColormapFlat = colormap;
  return colormap;
}

export { colorDepht };
