Skip to content

Commit f2f800f

Browse files
authored
Merge pull request #232 from thewtex/writeLocalFile
feat(writeLocalFile): Initial addition
2 parents 574604a + f4fab9f commit f2f800f

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

src/writeLocalFile.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const path = require('path')
2+
3+
const getFileExtension = require('./getFileExtension.js')
4+
const extensionToMeshIO = require('./extensionToMeshIO.js')
5+
6+
const writeImageLocalFile = require('./writeImageLocalFile.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 writeLocalFile = (useCompression, imageOrMesh, filePath) => {
19+
const absoluteFilePath = path.resolve(filePath)
20+
const extension = getFileExtension(absoluteFilePath)
21+
22+
return new Promise(function (resolve, reject) {
23+
try {
24+
const isMesh = extensionToMeshIO.hasOwnProperty(extension)
25+
if (isMesh) {
26+
try {
27+
writeMeshLocalFileSync(useCompression, imageOrMesh, filePath)
28+
resolve(null)
29+
} catch (err) {
30+
// Was a .vtk image file? Continue to write as an image.
31+
writeImageLocalFile(useCompression, imageOrMesh, filePath).then(() => {
32+
resolve(null)
33+
})
34+
}
35+
} else {
36+
writeImageLocalFile(useCompression, imageOrMesh, filePath).then(() => {
37+
resolve(null)
38+
})
39+
}
40+
} catch (err) {
41+
reject(err)
42+
}
43+
})
44+
}
45+
46+
module.exports = writeLocalFile

test/writeLocalFileTest.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 writeLocalFile = require(path.resolve(__dirname, '..', 'dist', 'writeLocalFile.js'))
10+
11+
const testImageInputFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'cthead1.png')
12+
const testImageOutputFilePath = path.resolve(__dirname, '..', 'build', 'Testing', 'Temporary', 'writeLocalFileTest-cthead1.png')
13+
const testMeshInputFilePath = path.resolve(__dirname, '..', 'build', 'ExternalData', 'test', 'Input', 'cow.vtk')
14+
const testMeshOutputFilePath = path.resolve(__dirname, '..', 'build', 'Testing', 'Temporary', 'writeLocalFileTest-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('writeLocalFile writes an image file path on the local filesystem', t => {
35+
return readImageLocalFile(testImageInputFilePath)
36+
.then(function (image) {
37+
const useCompression = false
38+
return writeLocalFile(useCompression, image, testImageOutputFilePath)
39+
})
40+
.then(function () {
41+
return readImageLocalFile(testImageOutputFilePath).then(function (image) {
42+
verifyImage(t, image)
43+
})
44+
})
45+
})
46+
47+
const verifyMesh = (t, mesh) => {
48+
t.is(mesh.meshType.dimension, 3)
49+
t.is(mesh.meshType.pointComponentType, FloatTypes.Float32)
50+
t.is(mesh.meshType.cellComponentType, IntTypes.UInt32)
51+
t.is(mesh.meshType.pointPixelType, 1)
52+
t.is(mesh.meshType.cellPixelType, 1)
53+
t.is(mesh.numberOfPoints, 2903)
54+
t.is(mesh.numberOfCells, 3263)
55+
}
56+
57+
test('writeLocalFile writes a mesh file path on the local filesystem', (t) => {
58+
return readMeshLocalFile(testMeshInputFilePath)
59+
.then(function (mesh) {
60+
return writeLocalFile(false, mesh, testMeshOutputFilePath)
61+
})
62+
.then(function () {
63+
return readMeshLocalFile(testMeshOutputFilePath).then(function (mesh) {
64+
verifyMesh(t, mesh)
65+
})
66+
})
67+
})

0 commit comments

Comments
 (0)