Skip to content

Commit

Permalink
feat(writeImageLocalFile): Support componentType, pixelType
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 7, 2022
1 parent 2b50d9c commit 23eca87
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 25 deletions.
28 changes: 17 additions & 11 deletions src/io/writeImageLocalFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<null>
*/
async function writeImageLocalFile (image: Image, filePath: string, useCompression: boolean = false): Promise<null> {
async function writeImageLocalFile (image: Image, filePath: string, options?: WriteImageOptions | boolean
): Promise<null> {
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)
Expand All @@ -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
Expand Down
41 changes: 27 additions & 14 deletions test/node/io/image/writeImageLocalFileTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]')
Expand All @@ -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)
})

0 comments on commit 23eca87

Please sign in to comment.