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;
if (readImageArrayBuffer.length === 4) { readImageArrayBuffer = (...args) => fn(null, ...args);
resultPreprocessor = ({ webWorker, image }) => { webWorker.terminate(); return image; }; } }
function vtkITKImageReader(publicAPI, model) { model.classHierarchy.push('vtkITKImageReader');
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); }; }
const DEFAULT_VALUES = { fileName: '', arrayName: null, };
export function extend(publicAPI, model, initialValues = {}) { Object.assign(model, DEFAULT_VALUES, initialValues);
macro.obj(publicAPI, model); macro.algo(publicAPI, model, 0, 1); macro.setGet(publicAPI, model, ['fileName', 'arrayName']);
vtkITKImageReader(publicAPI, model);
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 };
|