-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(image-io): add writeImageNode to the package
- Loading branch information
Showing
5 changed files
with
124 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import path from 'path' | ||
import mime from 'mime-types' | ||
|
||
import { | ||
Image, | ||
getFileExtension, | ||
castImage, | ||
} from 'itk-wasm' | ||
|
||
import mimeToImageIo from './mime-to-image-io.js' | ||
import extensionToImageIo from './extension-to-image-io.js' | ||
import imageIoIndexNode from './image-io-index-node.js' | ||
|
||
import WriteImageOptions from './write-image-options.js' | ||
|
||
interface WriterOptions { | ||
useCompression?: boolean | ||
mimeType?: boolean | ||
} | ||
interface WriterResult { | ||
couldWrite: boolean | ||
} | ||
type Writer = (image: Image, serializedImage: string, options: WriterOptions) => Promise<WriterResult> | ||
|
||
|
||
/** | ||
* Write an image to a serialized file format and from an the itk-wasm Image | ||
* | ||
* @param {Image} image - Input image | ||
* @param {string} serializedImage - Output image serialized in the file format. | ||
* @param {WriteImageOptions} options - options object | ||
* | ||
* @returns {void} - result object | ||
*/ | ||
async function writeImageNode( | ||
image: Image, | ||
serializedImage: string, | ||
options: WriteImageOptions = {} | ||
) : Promise<void> { | ||
const absoluteFilePath = path.resolve(serializedImage) | ||
const mimeType = mime.lookup(absoluteFilePath) | ||
const extension = getFileExtension(absoluteFilePath) | ||
|
||
let inputImage = image | ||
if (typeof options.componentType !== 'undefined' || typeof options.pixelType !== 'undefined') { | ||
inputImage = castImage(image, { componentType: options.componentType, pixelType: options.pixelType }) | ||
} | ||
|
||
let io = null | ||
if (mimeType !== false && mimeToImageIo.has(mimeType)) { | ||
io = mimeToImageIo.get(mimeType) | ||
} else if (extensionToImageIo.has(extension)) { | ||
io = extensionToImageIo.get(extension) | ||
} else { | ||
for (const readerWriter of imageIoIndexNode.values()) { | ||
if (readerWriter[1] !== null) { | ||
let { couldWrite } = await (readerWriter[1] as Writer)(inputImage, absoluteFilePath, { useCompression: options.useCompression }) | ||
if (couldWrite) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
if (io === null ) { | ||
throw Error('Could not find IO for: ' + absoluteFilePath) | ||
} | ||
const readerWriter = imageIoIndexNode.get(io as string) | ||
|
||
const writer = (readerWriter as Array<Writer>)[1] | ||
let { couldWrite } = await writer(inputImage, absoluteFilePath, { useCompression: options.useCompression }) | ||
if (!couldWrite) { | ||
throw Error('Could not write: ' + absoluteFilePath) | ||
} | ||
} | ||
|
||
export default writeImageNode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { IntTypes, FloatTypes, PixelTypes } from 'itk-wasm' | ||
|
||
interface WriteImageOptions { | ||
/** Component type, from itk-wasm IntTypes, FloatTypes, for the output pixel components. Defaults to the input component type. */ | ||
componentType?: typeof IntTypes[keyof typeof IntTypes] | typeof FloatTypes[keyof typeof FloatTypes] | ||
|
||
/** Pixel type, from itk-wasm PixelTypes, for the output pixels. Defaults to the input pixel type. */ | ||
pixelType?: typeof PixelTypes[keyof typeof PixelTypes] | ||
|
||
/** Use compression when writing the image if the IO formt supports it. */ | ||
useCompression?: boolean | ||
|
||
/** Mime type of the output image file. */ | ||
mimeType?: boolean | ||
} | ||
|
||
export default WriteImageOptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters