Skip to content

Commit

Permalink
feat(readImageLocalDICOMFileSeries): Support componentType, pixelType
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 7, 2022
1 parent 9ac40b9 commit 0a61e48
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/io/readImageDICOMFileSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const readImageDICOMFileSeries = async (

let optionsToPass: ReadImageDICOMArrayBufferSeriesOptions = {}
if (typeof options === 'object') {
optionsToPass = options
optionsToPass = { ...options }
}
if (typeof options === 'boolean') {
// Backwards compatibility
Expand All @@ -25,7 +25,7 @@ const readImageDICOMFileSeries = async (

const fileNames = Array.from(fileList, (file) => file.name)
optionsToPass.fileNames = fileNames
return readImageDICOMArrayBufferSeries(fileContents, optionsToPass)
return await readImageDICOMArrayBufferSeries(fileContents, optionsToPass)
}

export default readImageDICOMFileSeries
27 changes: 18 additions & 9 deletions src/io/readImageLocalDICOMFileSeries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ import Image from '../core/Image.js'
import InterfaceTypes from '../core/InterfaceTypes.js'
import runPipelineEmscripten from '../pipeline/internal/runPipelineEmscripten.js'
import PipelineInput from '../pipeline/PipelineInput.js'
import ReadImageDICOMFileSeriesOptions from './ReadImageDICOMFileSeriesOptions.js'
import castImage from '../core/castImage.js'

async function readImageLocalDICOMFileSeries (
fileNames: string[],
options?: ReadImageDICOMFileSeriesOptions | boolean
): Promise<Image> {
let singleSortedSeries = false
if (typeof options === 'boolean') {
// Backwards compatibility
singleSortedSeries = options
}

/**
* Read an image from a series of DICOM files on the local filesystem in Node.js.
*
* @param: filenames Array of filepaths containing a DICOM study / series on the local filesystem.
* @param: singleSortedSeries: it is known that the files are from a single
* sorted series.
*/
async function readImageLocalDICOMFileSeries (fileNames: string[], singleSortedSeries: boolean = false): Promise<Image> {
if (fileNames.length < 1) {
throw new Error('No fileNames provided')
}
Expand Down Expand Up @@ -44,7 +48,12 @@ async function readImageLocalDICOMFileSeries (fileNames: string[], singleSortedS
] as PipelineInput[]
const { outputs } = runPipelineEmscripten(seriesReaderModule, args, desiredOutputs, inputs)

return outputs[0].data as Image
let image = outputs[0].data as Image
if (typeof options === 'object' && (typeof options.componentType !== 'undefined' || typeof options.pixelType !== 'undefined')) {
image = castImage(image, options)
}

return image
}

export default readImageLocalDICOMFileSeries
72 changes: 36 additions & 36 deletions test/node/io/image/DICOMSeriesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,18 @@ import { IntTypes, PixelTypes, getMatrixElement, readImageLocalDICOMFileSeries }
const testSeriesDirectory = path.resolve('build-emscripten', 'ExternalData', 'test', 'Input', 'DicomImageOrientationTest')
const testFiles = glob.sync(`${testSeriesDirectory}/*.dcm`)

test('Test reading a DICOM file', async t => {
try {
const image = await readImageLocalDICOMFileSeries(testFiles)
t.is(image.imageType.dimension, 3, 'dimension')
t.is(image.imageType.componentType, IntTypes.Int16, 'componentType')
t.is(image.imageType.pixelType, PixelTypes.Scalar, 'pixelType')
t.is(image.imageType.components, 1, 'components')
t.is(image.origin[0], -17.3551, 'origin[0]')
t.is(image.origin[1], -133.9286, 'origin[1]')
t.is(image.origin[2], 116.7857, 'origin[2]')
t.is(image.spacing[0], 1.0, 'spacing[0]')
t.is(image.spacing[1], 1.0, 'spacing[1]')
t.is(image.spacing[2], 1.3000000000000007, 'spacing[2]')
t.is(getMatrixElement(image.direction, 3, 0, 0), 0.0, 'direction (0, 0)')
t.is(getMatrixElement(image.direction, 3, 0, 1), 0.0, 'direction (0, 1)')
t.is(getMatrixElement(image.direction, 3, 0, 2), -1.0, 'direction (0, 2)')
t.is(getMatrixElement(image.direction, 3, 1, 0), 1.0, 'direction (1, 0)')
t.is(getMatrixElement(image.direction, 3, 1, 1), 0.0, 'direction (1, 1)')
t.is(getMatrixElement(image.direction, 3, 1, 2), 0.0, 'direction (1, 2)')
t.is(getMatrixElement(image.direction, 3, 2, 0), 0.0, 'direction (2, 0)')
t.is(getMatrixElement(image.direction, 3, 2, 1), -1.0, 'direction (2, 1)')
t.is(getMatrixElement(image.direction, 3, 2, 2), 0.0, 'direction (2, 2)')
t.is(image.size[0], 256, 'size[0]')
t.is(image.size[1], 256, 'size[1]')
t.is(image.size[2], 3, 'size[2]')
t.is(image.data.length, 3 * 65536, 'data.length')
t.is(image.data[1000], 5, 'data[1000]')
} catch (err) {
console.error(err)
console.trace()
function verifyImage (t, image, expectedComponentType, expectedPixelType) {
let componentType = IntTypes.Int16
if (expectedComponentType) {
componentType = expectedComponentType
}
let pixelType = PixelTypes.Scalar
if (expectedPixelType) {
pixelType = expectedPixelType
}
})

test('Test reading a DICOM file assume sorted', async t => {
const image = await readImageLocalDICOMFileSeries(testFiles.sort(), true)
t.is(image.imageType.dimension, 3, 'dimension')
t.is(image.imageType.componentType, IntTypes.Int16, 'componentType')
t.is(image.imageType.pixelType, PixelTypes.Scalar, 'pixelType')
t.is(image.imageType.componentType, componentType, 'componentType')
t.is(image.imageType.pixelType, pixelType, 'pixelType')
t.is(image.imageType.components, 1, 'components')
t.is(image.origin[0], -17.3551, 'origin[0]')
t.is(image.origin[1], -133.9286, 'origin[1]')
Expand All @@ -66,4 +40,30 @@ test('Test reading a DICOM file assume sorted', async t => {
t.is(image.size[2], 3, 'size[2]')
t.is(image.data.length, 3 * 65536, 'data.length')
t.is(image.data[1000], 5, 'data[1000]')
}

test('Test reading a DICOM file', async t => {
const image = await readImageLocalDICOMFileSeries(testFiles)
verifyImage(t, image)
})

test('Test reading a DICOM file, given componentType, pixelType', async t => {
const componentType = IntTypes.Int32
const pixelType = PixelTypes.Vector
const image = await readImageLocalDICOMFileSeries(testFiles, { componentType, pixelType })
verifyImage(t, image, componentType, pixelType)
})

test('Test reading a DICOM file assume sorted', async t => {
const singleSortedSeries = true
const image = await readImageLocalDICOMFileSeries(testFiles.sort(), { singleSortedSeries })
verifyImage(t, image)
})

test('Test reading a DICOM file assume sorted, given componentType, pixelType', async t => {
const singleSortedSeries = true
const componentType = IntTypes.Int32
const pixelType = PixelTypes.Vector
const image = await readImageLocalDICOMFileSeries(testFiles.sort(), { singleSortedSeries, componentType, pixelType })
verifyImage(t, image, componentType, pixelType)
})

0 comments on commit 0a61e48

Please sign in to comment.