@itk-wasm/image-io
Version:
Input and output for scientific and medical image file formats.
91 lines (75 loc) • 2.54 kB
text/typescript
// Generated file. To retain edits, remove this comment.
import {
Image,
JsonCompatible,
BinaryFile,
InterfaceTypes,
PipelineOutput,
PipelineInput,
runPipeline
} from 'itk-wasm'
import WasmZstdWriteImageOptions from './wasm-zstd-write-image-options.js'
import WasmZstdWriteImageResult from './wasm-zstd-write-image-result.js'
import { getPipelinesBaseUrl } from './pipelines-base-url.js'
import { getPipelineWorkerUrl } from './pipeline-worker-url.js'
import { getDefaultWebWorker } from './default-web-worker.js'
/**
* Write an itk-wasm file format converted to an image file format
*
* @param {Image} image - Input image
* @param {string} serializedImage - Output image serialized in the file format.
* @param {WasmZstdWriteImageOptions} options - options object
*
* @returns {Promise<WasmZstdWriteImageResult>} - result object
*/
async function wasmZstdWriteImage(
image: Image,
serializedImage: string,
options: WasmZstdWriteImageOptions = {}
) : Promise<WasmZstdWriteImageResult> {
const desiredOutputs: Array<PipelineOutput> = [
{ type: InterfaceTypes.JsonCompatible },
{ type: InterfaceTypes.BinaryFile, data: { path: serializedImage, data: new Uint8Array() }},
]
const inputs: Array<PipelineInput> = [
{ type: InterfaceTypes.Image, data: image },
]
const args = []
// Inputs
const imageName = '0'
args.push(imageName)
// Outputs
const couldWriteName = '0'
args.push(couldWriteName)
const serializedImageName = serializedImage
args.push(serializedImageName)
// Options
args.push('--memory-io')
if (options.informationOnly) {
options.informationOnly && args.push('--information-only')
}
if (options.useCompression) {
options.useCompression && args.push('--use-compression')
}
const pipelinePath = 'wasm-zstd-write-image'
let workerToUse = options?.webWorker
if (workerToUse === undefined) {
workerToUse = await getDefaultWebWorker()
}
const {
webWorker: usedWebWorker,
returnValue,
stderr,
outputs
} = await runPipeline(pipelinePath, args, desiredOutputs, inputs, { pipelineBaseUrl: getPipelinesBaseUrl(), pipelineWorkerUrl: getPipelineWorkerUrl(), webWorker: workerToUse, noCopy: options?.noCopy })
if (returnValue !== 0 && stderr !== "") {
throw new Error(stderr)
}
const result = {
webWorker: usedWebWorker as Worker,
couldWrite: outputs[0]?.data as JsonCompatible,
serializedImage: outputs[1]?.data as BinaryFile,
}
return result
}
export default wasmZstdWriteImage