All files / extensions/cornerstone/src/components/ViewportWindowLevel histogramWorker.js

0% Statements 0/50
0% Branches 0/18
0% Functions 0/2
0% Lines 0/48

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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                                   
import { expose } from 'comlink';
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
 
/**
 * This object simulates a heavy task by implementing a sleep function and a recursive Fibonacci function.
 * It's used for testing or demonstrating purposes where a heavy or time-consuming task is needed.
 */
const obj = {
  getRange: ({ dimensions, origin, direction, spacing, scalarData }) => {
    const imageData = vtkImageData.newInstance();
    imageData.setDimensions(dimensions);
    imageData.setOrigin(origin);
    imageData.setDirection(direction);
    imageData.setSpacing(spacing);
 
    const scalarArray = vtkDataArray.newInstance({
      name: 'Pixels',
      numberOfComponents: 1,
      values: scalarData,
    });
 
    imageData.getPointData().setScalars(scalarArray);
 
    imageData.modified();
 
    const range = imageData.computeHistogram(imageData.getBounds());
 
    return range;
  },
  calcHistogram: ({ data, options }) => {
    if (options === undefined) {
      options = {};
    }
    const histogram = {
      numBins: options.numBins || 256,
      range: { min: 0, max: 0 },
      bins: new Int32Array(1),
      maxBin: 0,
      maxBinValue: 0,
    };
 
    let minToUse = options.min;
    let maxToUse = options.max;
 
    if (minToUse === undefined || maxToUse === undefined) {
      let min = Infinity;
      let max = -Infinity;
      let index = data.length;
 
      while (index--) {
        const value = data[index];
        if (value < min) {
          min = value;
        }
        if (value > max) {
          max = value;
        }
      }
 
      minToUse = min;
      maxToUse = max;
    }
 
    histogram.range = { min: minToUse, max: maxToUse };
 
    const bins = new Int32Array(histogram.numBins);
    const binScale = histogram.numBins / (maxToUse - minToUse);
 
    for (let index = 0; index < data.length; index++) {
      const value = data[index];
      if (value < minToUse) {
        continue;
      }
      if (value > maxToUse) {
        continue;
      }
      const bin = Math.floor((value - minToUse) * binScale);
      bins[bin] += 1;
    }
 
    histogram.bins = bins;
    histogram.maxBin = 0;
    histogram.maxBinValue = 0;
 
    for (let bin = 0; bin < histogram.numBins; bin++) {
      if (histogram.bins[bin] > histogram.maxBinValue) {
        histogram.maxBin = bin;
        histogram.maxBinValue = histogram.bins[bin];
      }
    }
 
    return histogram;
  },
};
 
expose(obj);