All files / Sources/IO/Misc/ITKImageReader index.js

14.7% Statements 5/34
0% Branches 0/13
0% Functions 0/10
15.62% Lines 5/32

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      1x 1x 1x                                                                                                                             1x                                                             1x          
import macro from 'vtk.js/Sources/macros';
import ITKHelper from 'vtk.js/Sources/Common/DataModel/ITKHelper';
 
const { convertItkToVtkImage } = ITKHelper;
let readImageArrayBuffer = null;
let resultPreprocessor = (result) => result;
 
function getArrayName(filename) {
  const idx = filename.lastIndexOf('.');
  const name = idx > -1 ? filename.substring(0, idx) : filename;
  return `Scalars ${name}`;
}
 
function setReadImageArrayBufferFromITK(fn) {
  readImageArrayBuffer = fn;
 
  // itk.js 9.0.0 introduced breaking changes, which can be detected
  // by an updated function signature.
  if (readImageArrayBuffer.length === 4) {
    // first arg is a webworker if reuse is desired.
    readImageArrayBuffer = (...args) => fn(null, ...args);
 
    // an object is now passed out which includes a webworker which we
    // should terminate
    resultPreprocessor = ({ webWorker, image }) => {
      webWorker.terminate();
      return image;
    };
  }
}
 
// ----------------------------------------------------------------------------
// vtkITKImageReader methods
// ----------------------------------------------------------------------------
 
function vtkITKImageReader(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkITKImageReader');
 
  // Returns a promise to signal when image is ready
  publicAPI.parseAsArrayBuffer = (arrayBuffer) => {
    if (!arrayBuffer || arrayBuffer === model.rawDataBuffer) {
      return Promise.resolve();
    }
 
    model.rawDataBuffer = arrayBuffer;
 
    return readImageArrayBuffer(arrayBuffer, model.fileName)
      .then(resultPreprocessor)
      .then((itkImage) => {
        const imageData = convertItkToVtkImage(itkImage, {
          scalarArrayName: model.arrayName || getArrayName(model.fileName),
        });
        model.output[0] = imageData;
 
        publicAPI.modified();
      });
  };
 
  publicAPI.requestData = (inData, outData) => {
    publicAPI.parseAsArrayBuffer(model.rawDataBuffer, model.fileName);
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  fileName: '',
  // If null/undefined a unique array will be generated
  arrayName: null,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Build VTK API
  macro.obj(publicAPI, model);
  macro.algo(publicAPI, model, 0, 1);
  macro.setGet(publicAPI, model, ['fileName', 'arrayName']);
 
  // vtkITKImageReader methods
  vtkITKImageReader(publicAPI, model);
 
  // Check that ITK function has been injected
  if (!readImageArrayBuffer) {
    console.error(`
      // Dependency needs to be added inside your project
      import readImageArrayBuffer from 'itk/readImageArrayBuffer';
      vtkITKImageReader.setReadImageArrayBufferFromITK(readImageArrayBuffer);
      `);
  }
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkITKImageReader');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend, setReadImageArrayBufferFromITK };