Skip to content

Commit 6808783

Browse files
authored
Merge pull request #233 from thewtex/writeFileSync
feat(writeLocalFileSync): Initial addition
2 parents f2f800f + 416f736 commit 6808783

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

doc/content/api/node_io.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ Read an image or mesh from a file on the local filesystem.
1515

1616
Similar to `readLocalFile`, but returns the image or mesh directly instead of a promise.
1717

18+
## writeLocalFile(useCompression, imageOrMesh, filePath) -> null
19+
20+
Write an image to a file on the local filesystem with Node.js.
21+
22+
*useCompression*: compress the pixel data when possible
23+
*imageOrMesh*: [itk/Image](./Image.html) or [itk/Mesh](./Mesh.html) instance to write
24+
*filePath*: path to the file on the local filesystem
25+
26+
## writeFileSync(useCompression, image, filePath) -> null
27+
28+
Similar to `writeImageLocalFile`, but synchronous.
29+
1830
---
1931

2032
## readImageLocalFile(filePath) -> [itk/Image](./Image.html)

src/writeLocalFileSync.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
const path = require('path')
2+
3+
const getFileExtension = require('./getFileExtension.js')
4+
const extensionToMeshIO = require('./extensionToMeshIO.js')
5+
6+
const writeImageLocalFileSync = require('./writeImageLocalFileSync.js')
7+
const writeMeshLocalFileSync = require('./writeMeshLocalFileSync.js')
8+
9+
/**
10+
* Write an image or mesh to a file on the local filesystem in Node.js.
11+
*
12+
* @param: useCompression compression the pixel data when possible
13+
* @param: imageOrMesh itk.Image or itk.Mesh instance to write
14+
* @param: filePath path to the file on the local filesystem.
15+
*
16+
* @return empty Promise
17+
*/
18+
const writeLocalFileSync = (useCompression, imageOrMesh, filePath) => {
19+
const absoluteFilePath = path.resolve(filePath)
20+
const extension = getFileExtension(absoluteFilePath)
21+
22+
const isMesh = extensionToMeshIO.hasOwnProperty(extension)
23+
if (isMesh) {
24+
try {
25+
writeMeshLocalFileSync(useCompression, imageOrMesh, filePath)
26+
return
27+
} catch (err) {
28+
// Was a .vtk image file? Continue to write as an image.
29+
writeImageLocalFileSync(useCompression, imageOrMesh, filePath)
30+
}
31+
} else {
32+
writeImageLocalFileSync(useCompression, imageOrMesh, filePath)
33+
}
34+
}
35+
36+
module.exports = writeLocalFileSync

test/writeLocalFileSyncTest.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import test from 'ava'
2+
import path from 'path'
3+
4+
const IntTypes = require(path.resolve(__dirname, '..', 'dist', 'IntTypes.js'))
5+
const FloatTypes = require(path.resolve(__dirname, '..', 'dist', 'FloatTypes.js'))
6+
const PixelTypes = require(path.resolve(__dirname, '..', 'dist', 'PixelTypes.js'))
7+
const readImageLocalFile = require(path.resolve(__dirname, '..', 'dist', 'readImageLocalFile.js'))
8+
const readMeshLocalFile = require(path.resolve(__dirname, '..', 'dist', 'readMeshLocalFile.js'))
9+
const writeLocalFileSync = require(path.resolve(__dirname, '..', 'dist', 'writeLocalFileSync.js'))
10+
11+
const testImageInputFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'cthead1.png')
12+
const testImageOutputFilePath = path.resolve(__dirname, '..', 'build', 'Testing', 'Temporary', 'writeLocalFileSyncTest-cthead1.png')
13+
const testMeshInputFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'cow.vtk')
14+
const testMeshOutputFilePath = path.resolve(__dirname, '..', 'build', 'Testing', 'Temporary', 'writeLocalFileSyncTest-cow.vtk')
15+
16+
const verifyImage = (t, image) => {
17+
t.is(image.imageType.dimension, 2, 'dimension')
18+
t.is(image.imageType.componentType, IntTypes.UInt8, 'componentType')
19+
t.is(image.imageType.pixelType, PixelTypes.RGB, 'pixelType')
20+
t.is(image.imageType.components, 3, 'components')
21+
t.is(image.origin[0], 0.0, 'origin[0]')
22+
t.is(image.origin[1], 0.0, 'origin[1]')
23+
t.is(image.spacing[0], 1.0, 'spacing[0]')
24+
t.is(image.spacing[1], 1.0, 'spacing[1]')
25+
t.is(image.direction.getElement(0, 0), 1.0, 'direction (0, 0)')
26+
t.is(image.direction.getElement(0, 1), 0.0, 'direction (0, 1)')
27+
t.is(image.direction.getElement(1, 0), 0.0, 'direction (1, 0)')
28+
t.is(image.direction.getElement(1, 1), 1.0, 'direction (1, 1)')
29+
t.is(image.size[0], 256, 'size[0]')
30+
t.is(image.size[1], 256, 'size[1]')
31+
t.is(image.data.length, 196608, 'data.length')
32+
}
33+
34+
test('writeLocalFileSync writes an image file path on the local filesystem', t => {
35+
return readImageLocalFile(testImageInputFilePath)
36+
.then(function (image) {
37+
const useCompression = false
38+
writeLocalFileSync(useCompression, image, testImageOutputFilePath)
39+
return readImageLocalFile(testImageOutputFilePath).then(function (image) {
40+
verifyImage(t, image)
41+
})
42+
})
43+
})
44+
45+
const verifyMesh = (t, mesh) => {
46+
t.is(mesh.meshType.dimension, 3)
47+
t.is(mesh.meshType.pointComponentType, FloatTypes.Float32)
48+
t.is(mesh.meshType.cellComponentType, IntTypes.UInt32)
49+
t.is(mesh.meshType.pointPixelType, 1)
50+
t.is(mesh.meshType.cellPixelType, 1)
51+
t.is(mesh.numberOfPoints, 2903)
52+
t.is(mesh.numberOfCells, 3263)
53+
}
54+
55+
test('writeLocalFile writes a mesh file path on the local filesystem', (t) => {
56+
return readMeshLocalFile(testMeshInputFilePath)
57+
.then(function (mesh) {
58+
writeLocalFileSync(false, mesh, testMeshOutputFilePath)
59+
return readMeshLocalFile(testMeshOutputFilePath).then(function (mesh) {
60+
verifyMesh(t, mesh)
61+
})
62+
})
63+
})

0 commit comments

Comments
 (0)