All files / Sources/Filters/Cornerstone/ImageDataToCornerstoneImage index.js

39.28% Statements 11/28
0% Branches 0/7
50% Functions 2/4
39.28% Lines 11/28

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122    1x                             1x   1x                                                                                                                                         1x               1x     1x     1x   1x     1x 1x         1x                
import macro from 'vtk.js/Sources/macros';
 
const { vtkErrorMacro } = macro;
 
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// TODO:
// - Support image stack
// - Support slice orientation (see stack?)
// - may need some data conversion
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
// ----------------------------------------------------------------------------
// vtkImageDataToCornerstoneImage methods
// ----------------------------------------------------------------------------
 
function vtkImageDataToCornerstoneImage(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageDataToCornerstoneImage');
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];
 
    if (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
 
    // Retrieve output and volume data
    // const origin = input.getOrigin();
    const spacing = input.getSpacing();
    const dims = input.getDimensions();
    const scalars = input.getPointData().getScalars();
    const dataRange = scalars.getRange(0);
    const rawData = scalars.getData();
 
    // FIXME probably need to expand to RGBA
    let pixelData = null;
    if (dims[2] === 1) {
      pixelData = !scalars.data ? rawData : scalars.data;
    } else {
      const offset =
        model.sliceIndex * dims[0] * dims[1] * rawData.BYTES_PER_ELEMENT;
      pixelData = macro.newTypedArray(
        scalars.getDataType(),
        rawData.buffer,
        offset,
        dims[0] * dims[1]
      );
    }
 
    const cornerstoneImage = {
      imageId: model.imageId,
      color: scalars.getNumberOfComponents() > 1,
 
      columnPixelSpacing: spacing[0],
      columns: dims[0],
      width: dims[0],
 
      rowPixelSpacing: spacing[1],
      rows: dims[1],
      height: dims[1],
 
      intercept: 0,
      invert: false,
      minPixelValue: dataRange[0],
      maxPixelValue: dataRange[1],
 
      sizeInBytes: pixelData.length * pixelData.BYTES_PER_ELEMENT,
      slope: 1,
 
      windowCenter: Math.round((dataRange[0] + dataRange[1]) / 2),
      windowWidth: dataRange[1] - dataRange[0],
      decodeTimeInMS: 0,
 
      getPixelData() {
        return pixelData;
      },
    };
 
    outData[0] = cornerstoneImage;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  imageId: 'default-image-id',
  sliceIndex: 0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  // Also make it an algorithm with one input and one output
  macro.algo(publicAPI, model, 1, 1);
 
  macro.setGet(publicAPI, model, ['imageId', 'sliceIndex']);
 
  // Object specific methods
  macro.algo(publicAPI, model, 1, 1);
  vtkImageDataToCornerstoneImage(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkImageDataToCornerstoneImage'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };