Skip to content

Commit

Permalink
feat(writeImageArrayBuffer): Support componentType, pixelType
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 7, 2022
1 parent 0a61e48 commit 2b50d9c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
8 changes: 8 additions & 0 deletions src/io/WriteImageOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import CastImageOptions from '../core/CastImageOptions.js'

interface WriteImageOptions extends CastImageOptions {
useCompression?: boolean
mimeType?: boolean
}

export default WriteImageOptions
2 changes: 2 additions & 0 deletions src/io/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ export { default as readImageDICOMFileSeries } from './../readImageDICOMFileSeri
export { default as ReadImageDICOMArrayBufferSeriesOptions } from './../ReadImageDICOMArrayBufferSeriesOptions.js'
export { default as readImageDICOMArrayBufferSeries } from './../readImageDICOMArrayBufferSeries.js'

export { default as WriteImageOptions } from './../WriteImageOptions.js'
export { default as writeImageArrayBuffer } from './../writeImageArrayBuffer.js'
export { default as WriteMeshOptions } from './../WriteMeshOptions.js'
export { default as writeMeshArrayBuffer } from './../writeMeshArrayBuffer.js'
export { default as writeArrayBuffer } from './../writeArrayBuffer.js'

Expand Down
2 changes: 2 additions & 0 deletions src/io/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export { default as readMeshLocalFile } from './../readMeshLocalFile.js'
export { default as readDICOMTagsLocalFile } from './../readDICOMTagsLocalFile.js'
export { default as readImageLocalDICOMFileSeries } from './../readImageLocalDICOMFileSeries.js'

export { default as WriteImageOptions } from './../WriteImageOptions.js'
export { default as writeImageLocalFile } from './../writeImageLocalFile.js'
export { default as WriteMeshOptions } from './../WriteMeshOptions.js'
export { default as writeMeshLocalFile } from './../writeMeshLocalFile.js'
export { default as writeLocalFile } from './../writeLocalFile.js'

Expand Down
23 changes: 21 additions & 2 deletions src/io/writeImageArrayBuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,29 @@ import PipelineInput from '../pipeline/PipelineInput.js'
import PipelineOutput from '../pipeline/PipelineOutput.js'
import InterfaceTypes from '../core/InterfaceTypes.js'
import getTransferable from '../core/getTransferable.js'
import castImage from '../core/castImage.js'

import WriteImageOptions from './WriteImageOptions.js'
import WriteArrayBufferResult from './WriteArrayBufferResult.js'

async function writeImageArrayBuffer (webWorker: Worker | null, image: Image, fileName: string, mimeType: string = '', useCompression: boolean = false): Promise<WriteArrayBufferResult> {
async function writeImageArrayBuffer (webWorker: Worker | null, image: Image, fileName: string, options?: WriteImageOptions | string, useCompressionBackwardsCompatibility?: boolean
): Promise<WriteArrayBufferResult> {
if (typeof image === 'boolean') {
throw new Error('useCompression is now at the last argument position in itk-wasm')
}

let mimeType = ''
if (typeof options === 'string') {
mimeType = options
}
let useCompression = false
if (typeof useCompressionBackwardsCompatibility === 'boolean') {
useCompression = useCompressionBackwardsCompatibility
}
if (typeof options === 'object' && typeof options.useCompression !== 'undefined') {
useCompression = options.useCompression
}

let worker = webWorker
const { webworkerPromise, worker: usedWorker } = await createWebWorkerPromise(worker)
worker = usedWorker
Expand All @@ -27,8 +42,12 @@ async function writeImageArrayBuffer (webWorker: Worker | null, image: Image, fi
const outputs = [
{ data: { path: filePath }, type: InterfaceTypes.BinaryFile }
] 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[]

const transferables: ArrayBuffer[] = []
Expand Down
49 changes: 31 additions & 18 deletions test/browser/io/writeImageTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,18 @@ for (let ii = 0; ii < byteString.length; ++ii) {
const cthead1SmallBlob = new window.Blob([intArray], { type: mimeString })
const cthead1SmallFile = new window.File([cthead1SmallBlob], 'cthead1Small.png')

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.Scalar, 'pixelType')
let componentType = IntTypes.UInt8
if (expectedComponentType) {
componentType = expectedComponentType
}
let pixelType = PixelTypes.Scalar
if (expectedPixelType) {
pixelType = expectedPixelType
}
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], 0.0, 'origin[0]')
t.is(image.origin[1], 0.0, 'origin[1]')
Expand All @@ -34,20 +42,25 @@ const verifyImage = (t, image) => {
}

export default function () {
test('writeImageArrayBuffer writes to an ArrayBuffer', (t) => {
return PromiseFileReader.readAsArrayBuffer(cthead1SmallFile)
.then((arrayBuffer) => {
return readImageArrayBuffer(null, arrayBuffer, 'cthead1Small.png').then(function ({ image, webWorker }) {
webWorker.terminate()
const useCompression = false
return writeImageArrayBuffer(null, image, 'cthead1Small.png', useCompression)
})
})
.then(function ({ arrayBuffer: writtenArrayBuffer, webWorker }) {
webWorker.terminate()
return readImageArrayBuffer(null, writtenArrayBuffer, 'cthead1Small.png').then(function ({ image }) {
verifyImage(t, image)
})
})
test('writeImageArrayBuffer writes to an ArrayBuffer', async (t) => {
const arrayBuffer = await PromiseFileReader.readAsArrayBuffer(cthead1SmallFile)
const { image, webWorker } = await readImageArrayBuffer(null, arrayBuffer, 'cthead1Small.png')
const useCompression = false
const { arrayBuffer: writtenArrayBuffer } = await writeImageArrayBuffer(null, image, 'cthead1Small.png', { useCompression })
webWorker.terminate()
const { image: imageSecondPass } = await readImageArrayBuffer(null, writtenArrayBuffer, 'cthead1Small.png')
verifyImage(t, imageSecondPass)
})

test('writeImageArrayBuffer writes to an ArrayBuffer, given componentType, pixelType', async (t) => {
const arrayBuffer = await PromiseFileReader.readAsArrayBuffer(cthead1SmallFile)
const { image, webWorker } = await readImageArrayBuffer(null, arrayBuffer, 'cthead1Small.png')
const useCompression = false
const componentType = IntTypes.UInt16
const pixelType = PixelTypes.Vector
const { arrayBuffer: writtenArrayBuffer } = await writeImageArrayBuffer(null, image, 'cthead1Small.iwi.cbor', { useCompression, componentType, pixelType })
webWorker.terminate()
const { image: imageSecondPass } = await readImageArrayBuffer(null, writtenArrayBuffer, 'cthead1Small.iwi.cbor')
verifyImage(t, imageSecondPass, componentType, pixelType)
})
}

0 comments on commit 2b50d9c

Please sign in to comment.