ITKImageReader

Introduction

The vtkITKImageReader aims to read a ITK file format.

Methods

extend

Method used to decorate a given object (publicAPI+model) with vtkITKImageReader characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues IITKImageReaderInitialValues No (default: {})

getArrayName

Get the array name.

getFileName

Get the filename.

newInstance

Method used to create a new instance of vtkITKImageReader

Argument Type Required Description
initialValues IITKImageReaderInitialValues No for pre-setting some of its content

parseAsArrayBuffer

Parse data as array buffer.

Argument Type Required Description
arrayBuffer ArrayBuffer Yes The array buffer to parse.

requestData

Argument Type Required Description
inData Yes
outData Yes

setArrayName

Set the array name.

Argument Type Required Description
arrayName String Yes The name of the array.

setFileName

Set the filename.

Argument Type Required Description
filename String Yes

setReadImageArrayBufferFromITK

Argument Type Required Description
fn any Yes

Source

index.d.ts
import { vtkAlgorithm, vtkObject } from '../../../interfaces';

/**
*
*/
export interface IITKImageReaderInitialValues {
fileName?: string;
arrayName?: string;
}

type vtkITKImageReaderBase = vtkObject &
Omit<
vtkAlgorithm,
| 'getInputData'
| 'setInputData'
| 'setInputConnection'
| 'getInputConnection'
| 'addInputConnection'
| 'addInputData'
>;

export interface vtkITKImageReader extends vtkITKImageReaderBase {
/**
* Get the array name.
*/
getArrayName(): string;

/**
* Get the filename.
*/
getFileName(): string;

/**
* Parse data as array buffer.
* @param {ArrayBuffer} arrayBuffer The array buffer to parse.
*/
parseAsArrayBuffer(arrayBuffer: ArrayBuffer): void;

/**
*
* @param inData
* @param outData
*/
requestData(inData: any, outData: any): void;

/**
* Set the array name.
* @param {String} arrayName The name of the array.
*/
setArrayName(arrayName: string): boolean;

/**
* Set the filename.
* @param {String} filename
*/
setFileName(filename: string): boolean;
}

/**
* Method used to decorate a given object (publicAPI+model) with vtkITKImageReader characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {IITKImageReaderInitialValues} [initialValues] (default: {})
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: IITKImageReaderInitialValues
): void;

/**
* Method used to create a new instance of vtkITKImageReader
* @param {IITKImageReaderInitialValues} [initialValues] for pre-setting some of its content
*/
export function newInstance(
initialValues?: IITKImageReaderInitialValues
): vtkITKImageReader;

/**
*
* @param {*} fn
*/
export function setReadImageArrayBufferFromITK(fn: any): Promise<any>;

/**
* The vtkITKImageReader aims to read a ITK file format.
*/
export declare const vtkITKImageReader: {
newInstance: typeof newInstance;
extend: typeof extend;
setReadImageArrayBufferFromITK: typeof setReadImageArrayBufferFromITK;
};
export default vtkITKImageReader;
index.js
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 };