Skip to content

Commit

Permalink
feat(readImageLocalFile): Support casting options
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 4, 2022
1 parent 65a5d10 commit 149721d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/core/castImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ import FloatTypes from './FloatTypes.js'
* @param {Image} image - The input image
* @param {CastImageOptions} options - specify the componentType and/or pixelType of the output
*/
function castImage (inputImage: Image, options: CastImageOptions): Image {
function castImage (inputImage: Image, options?: CastImageOptions): Image {
const outputImageType = { ...inputImage.imageType }

if (typeof options.pixelType !== 'undefined') {
if (typeof options !== 'undefined' && typeof options.pixelType !== 'undefined') {
outputImageType.pixelType = options.pixelType
if (options.pixelType === PixelTypes.Scalar && outputImageType.components !== 1) {
throw new Error('Cannot cast multi-component image to a scalar image')
}
}
if (typeof options.componentType !== 'undefined' && options.componentType !== inputImage.imageType.componentType) {
if (typeof options !== 'undefined' && typeof options.componentType !== 'undefined' && options.componentType !== inputImage.imageType.componentType) {
outputImageType.componentType = options.componentType
}

Expand All @@ -33,7 +33,7 @@ function castImage (inputImage: Image, options: CastImageOptions): Image {
outputImage.metadata = { ...inputImage.metadata }

if (inputImage.data !== null) {
if (typeof options.componentType !== 'undefined' && options.componentType !== inputImage.imageType.componentType) {
if (typeof options !== 'undefined' && typeof options.componentType !== 'undefined' && options.componentType !== inputImage.imageType.componentType) {
switch (inputImage.imageType.componentType) {
case IntTypes.UInt8:
case IntTypes.Int8:
Expand Down
13 changes: 11 additions & 2 deletions src/io/readImageLocalFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ import PipelineEmscriptenModule from '../pipeline/PipelineEmscriptenModule.js'
import findLocalImageIOPath from './internal/findLocalImageIOPath.js'
import InterfaceTypes from '../core/InterfaceTypes.js'
import PipelineInput from '../pipeline/PipelineInput.js'
import CastImageOptions from '../core/CastImageOptions.js'
import castImage from '../core/castImage.js'

/**
* Read an image from a file on the local filesystem in Node.js.
*
* @param: filePath path to the file on the local filesystem.
* @param {CastImageOptions} options - specify the componentType and/or pixelType of the output
*/
async function readImageLocalFile (filePath: string): Promise<Image> {
async function readImageLocalFile (filePath: string, options?: CastImageOptions): Promise<Image> {
const imageIOsPath = findLocalImageIOPath()
const absoluteFilePath = path.resolve(filePath)
const mimeType = mime.lookup(absoluteFilePath)
Expand Down Expand Up @@ -58,7 +61,13 @@ async function readImageLocalFile (filePath: string): Promise<Image> {
const mountedFilePath = readImageModule.mountContainingDir(absoluteFilePath)
const { outputs } = runPipelineEmscripten(readImageModule, args, desiredOutputs, inputs)
readImageModule.unmountContainingDir(mountedFilePath)
return outputs[0].data as Image
let result = outputs[0].data as Image

if (typeof options !== 'undefined') {
result = castImage(result, options)
}

return result
}

export default readImageLocalFile
55 changes: 37 additions & 18 deletions test/node/io/image/readImageLocalFileTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,41 @@ import { IntTypes, PixelTypes, getMatrixElement, readImageLocalFile } from '../.

const testFilePath = path.resolve('build-emscripten', 'ExternalData', 'test', 'Input', 'cthead1.png')

test('readImageLocalFile reads a file path given on the local filesystem', t => {
return readImageLocalFile(testFilePath).then(function (image) {
t.is(image.imageType.dimension, 2)
t.is(image.imageType.componentType, IntTypes.UInt8)
t.is(image.imageType.pixelType, PixelTypes.RGB)
t.is(image.imageType.components, 3)
t.is(image.origin[0], 0.0)
t.is(image.origin[1], 0.0)
t.is(image.spacing[0], 1.0)
t.is(image.spacing[1], 1.0)
t.is(getMatrixElement(image.direction, 2, 0, 0), 1.0)
t.is(getMatrixElement(image.direction, 2, 0, 1), 0.0)
t.is(getMatrixElement(image.direction, 2, 1, 0), 0.0)
t.is(getMatrixElement(image.direction, 2, 1, 1), 1.0)
t.is(image.size[0], 256)
t.is(image.size[1], 256)
t.is(image.data.length, 196608)
})
function verifyImage(t, image, componentType, pixelType) {
t.is(image.imageType.dimension, 2)
t.is(image.imageType.componentType, componentType)
t.is(image.imageType.pixelType, pixelType)
t.is(image.imageType.components, 3)
t.is(image.origin[0], 0.0)
t.is(image.origin[1], 0.0)
t.is(image.spacing[0], 1.0)
t.is(image.spacing[1], 1.0)
t.is(getMatrixElement(image.direction, 2, 0, 0), 1.0)
t.is(getMatrixElement(image.direction, 2, 0, 1), 0.0)
t.is(getMatrixElement(image.direction, 2, 1, 0), 0.0)
t.is(getMatrixElement(image.direction, 2, 1, 1), 1.0)
t.is(image.size[0], 256)
t.is(image.size[1], 256)
t.is(image.data.length, 196608)
}

test('readImageLocalFile reads a file path given on the local filesystem', async t => {
const image = await readImageLocalFile(testFilePath)
const componentType = IntTypes.UInt8
const pixelType = PixelTypes.RGB
verifyImage(t, image, componentType, pixelType)
})

test('readImageLocalFile casts to the specified componentType', async t => {
const componentType = IntTypes.UInt16
const image = await readImageLocalFile(testFilePath, { componentType })
const pixelType = PixelTypes.RGB
verifyImage(t, image, componentType, pixelType)
})

test('readImageLocalFile casts to the specified pixelType', async t => {
const pixelType = PixelTypes.Vector
const image = await readImageLocalFile(testFilePath, { pixelType })
const componentType = IntTypes.UInt8
verifyImage(t, image, componentType, pixelType)
})

0 comments on commit 149721d

Please sign in to comment.