-
-
Notifications
You must be signed in to change notification settings - Fork 383
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Cornerstone): Add module to handle cornerstone interoperability
- Loading branch information
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
Sources/Filters/Cornerstone/ImageDataToCornerstoneImage/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import macro from 'vtk.js/Sources/macro'; | ||
|
||
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; | ||
} else { | ||
const offset = | ||
model.sliceIndex * dims[0] * dims[1] * rawData.BYTES_PER_ELEMENT; | ||
pixelData = new macro.TYPED_ARRAYS[(scalars.getDataType())]( | ||
rawData.buffer, | ||
offset, | ||
dims[0] * dims[1] | ||
); | ||
} | ||
|
||
const cornerstoneImage = { | ||
imageId: model.imageId, | ||
color: scalars.getNumberOfComponents() > 1, | ||
|
||
columnPixelSpacing: spacing[1], | ||
columns: dims[1], | ||
width: dims[1], | ||
|
||
rowPixelSpacing: spacing[0], | ||
rows: dims[0], | ||
height: dims[0], | ||
|
||
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import vtkImageDataToCornerstoneImage from './ImageDataToCornerstoneImage'; | ||
|
||
export default { | ||
vtkImageDataToCornerstoneImage, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters