diff --git a/src/io/writeImageLocalFile.ts b/src/io/writeImageLocalFile.ts index 997f87d02..c78f14bb7 100644 --- a/src/io/writeImageLocalFile.ts +++ b/src/io/writeImageLocalFile.ts @@ -13,23 +13,25 @@ import findLocalImageIOPath from './internal/findLocalImageIOPath.js' import InterfaceTypes from '../core/InterfaceTypes.js' import PipelineInput from '../pipeline/PipelineInput.js' import PipelineOutput from '../pipeline/PipelineOutput.js' +import WriteImageOptions from './WriteImageOptions.js' +import castImage from '../core/castImage.js' import Image from '../core/Image.js' -/** - * Write an image to a file on the local filesystem in Node.js. - * - * @param: image itk.Image instance to write - * @param: filePath path to the file on the local filesystem. - * @param: useCompression compression the pixel data when possible - * - * @return Promise - */ -async function writeImageLocalFile (image: Image, filePath: string, useCompression: boolean = false): Promise { +async function writeImageLocalFile (image: Image, filePath: string, options?: WriteImageOptions | boolean +): Promise { if (typeof image === 'boolean') { throw new Error('useCompression is now the last argument in itk-wasm') } + let useCompression = false + if (typeof options === 'boolean') { + useCompression = options + } + if (typeof options === 'object' && typeof options.useCompression !== 'undefined') { + useCompression = options.useCompression + } + const imageIOsPath = findLocalImageIOPath() const absoluteFilePath = path.resolve(filePath) const mimeType = mime.lookup(absoluteFilePath) @@ -41,8 +43,12 @@ async function writeImageLocalFile (image: Image, filePath: string, useCompressi } const desiredOutputs = [ ] as PipelineOutput[] + let inputImage = image + if (typeof options === 'object' && (typeof options.componentType !== 'undefined' || typeof options.pixelType !== 'undefined')) { + inputImage = castImage(image, options) + } const inputs = [ - { type: InterfaceTypes.Image, data: image } + { type: InterfaceTypes.Image, data: inputImage } ] as PipelineInput[] let io = null diff --git a/test/node/io/image/writeImageLocalFileTest.js b/test/node/io/image/writeImageLocalFileTest.js index 328f311fa..810a26c53 100644 --- a/test/node/io/image/writeImageLocalFileTest.js +++ b/test/node/io/image/writeImageLocalFileTest.js @@ -4,12 +4,20 @@ import path from 'path' import { IntTypes, PixelTypes, getMatrixElement, readImageLocalFile, writeImageLocalFile } from '../../../../dist/index.js' const testInputFilePath = path.resolve('build-emscripten', 'ExternalData', 'test', 'Input', 'cthead1.png') -const testOutputFilePath = path.resolve('build-emscripten', 'Testing', 'Temporary', 'writeImageLocalFileTest-cthead1.png') +const testOutputFilePath = path.resolve('build-emscripten', 'Testing', 'Temporary', 'writeImageLocalFileTest-cthead1.iwi.cbor') -const verifyImage = (t, image) => { +const verifyImage = (t, image, expectedComponentType, expectedPixelType) => { t.is(image.imageType.dimension, 2, 'dimension') - t.is(image.imageType.componentType, IntTypes.UInt8, 'componentType') - t.is(image.imageType.pixelType, PixelTypes.RGB, 'pixelType') + let componentType = IntTypes.UInt8 + if (expectedComponentType) { + componentType = expectedComponentType + } + let pixelType = PixelTypes.RGB + if (expectedPixelType) { + pixelType = expectedPixelType + } + t.is(image.imageType.componentType, componentType, 'componentType') + t.is(image.imageType.pixelType, pixelType, 'pixelType') t.is(image.imageType.components, 3, 'components') t.is(image.origin[0], 0.0, 'origin[0]') t.is(image.origin[1], 0.0, 'origin[1]') @@ -24,14 +32,19 @@ const verifyImage = (t, image) => { t.is(image.data.length, 196608, 'data.length') } -test('writeImageLocalFile writes a file path on the local filesystem', t => { - return readImageLocalFile(testInputFilePath) - .then(function (image) { - return writeImageLocalFile(image, testOutputFilePath) - }) - .then(function () { - return readImageLocalFile(testOutputFilePath).then(function (image) { - verifyImage(t, image) - }) - }) +test('writeImageLocalFile writes a file path on the local filesystem', async t => { + const image = await readImageLocalFile(testInputFilePath) + await writeImageLocalFile(image, testOutputFilePath) + const imageSecondPass = await readImageLocalFile(testOutputFilePath) + verifyImage(t, imageSecondPass) +}) + +test('writeImageLocalFile writes a file path on the local filesystem given componentType, pixelType', async t => { + const image = await readImageLocalFile(testInputFilePath) + const componentType = IntTypes.UInt16 + const pixelType = PixelTypes.Vector + const outputFilePath = testOutputFilePath + 'componentTypePixelType.iwi.cbor' + await writeImageLocalFile(image, outputFilePath, { componentType, pixelType }) + const imageSecondPass = await readImageLocalFile(outputFilePath) + verifyImage(t, imageSecondPass, componentType, pixelType) })