All files / extensions/cornerstone/src/components/ViewportWindowLevel getViewportVolumeHistogram.ts

16.66% Statements 4/24
0% Branches 0/11
0% Functions 0/2
16.66% Lines 4/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73    34x   34x                 34x           34x                                                                                                          
import { getWebWorkerManager } from '@cornerstonejs/core';
 
const workerManager = getWebWorkerManager();
 
const WorkerOptions = {
  maxWorkerInstances: 1,
  autoTerminateOnIdle: {
    enabled: true,
    idleTimeThreshold: 1000,
  },
};
 
// Register the task
const workerFn = () => {
  return new Worker(new URL('./histogramWorker.js', import.meta.url), {
    name: 'histogram-worker', // name used by the browser to name the worker
  });
};
 
const getViewportVolumeHistogram = async (viewport, volume, options?) => {
  workerManager.registerWorker('histogram-worker', workerFn, WorkerOptions);
 
  const volumeImageData = viewport.getImageData(volume.volumeId);
 
  Iif (!volumeImageData) {
    return undefined;
  }
 
  let scalarData = volume.scalarData;
 
  if (volume.numTimePoints > 1) {
    const targetTimePoint = volume.numTimePoints - 1; // or any other time point you need
    scalarData = volume.voxelManager.getTimePointScalarData(targetTimePoint);
  } else {
    scalarData = volume.voxelManager.getCompleteScalarDataArray();
  }
 
  Iif (!scalarData?.length) {
    return undefined;
  }
 
  const { dimensions, origin, direction, spacing } = volume;
 
  const range = await workerManager.executeTask('histogram-worker', 'getRange', {
    dimensions,
    origin,
    direction,
    spacing,
    scalarData,
  });
 
  const { minimum: min, maximum: max } = range;
 
  Iif (min === Infinity || max === -Infinity) {
    return undefined;
  }
 
  const calcHistOptions = {
    numBins: 256,
    min: Math.max(min, options?.min ?? min),
    max: Math.min(max, options?.max ?? max),
  };
 
  const histogram = await workerManager.executeTask('histogram-worker', 'calcHistogram', {
    data: scalarData,
    options: calcHistOptions,
  });
 
  return histogram;
};
 
export { getViewportVolumeHistogram };