Skip to content

Commit 428bf89

Browse files
committed
feat(writeArrayBuffer): Initial addition
1 parent 6808783 commit 428bf89

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

src/writeArrayBuffer.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import writeImageArrayBuffer from './writeImageArrayBuffer'
2+
import writeMeshArrayBuffer from './writeMeshArrayBuffer'
3+
4+
import getFileExtension from './getFileExtension'
5+
import extensionToMeshIO from './extensionToMeshIO'
6+
import mimeToMeshIO from './MimeToMeshIO'
7+
8+
const writeArrayBuffer = (webWorker, useCompression, imageOrMesh, fileName, mimeType) => {
9+
const extension = getFileExtension(fileName)
10+
const isMesh = !!extensionToMeshIO.hasOwnProperty(extension) || !!mimeToMeshIO.hasOwnProperty(mimeType)
11+
if (isMesh) {
12+
return writeMeshArrayBuffer(webWorker, useCompression, imageOrMesh, fileName, mimeType)
13+
.catch(function () {
14+
webWorker.terminate()
15+
return writeImageArrayBuffer(null, useCompression, imageOrMesh, fileName, mimeType)
16+
})
17+
} else {
18+
return writeImageArrayBuffer(webWorker, useCompression, imageOrMesh,
19+
fileName, mimeType)
20+
}
21+
}
22+
23+
export default writeArrayBuffer

test/Browser/tests.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ import readMeshTest from './readMeshTest'
66
import readTest from './readTest'
77
import writeImageTest from './writeImageTest'
88
import writeMeshTest from './writeMeshTest'
9+
import writeTest from './writeTest'
910
import runPipelineBrowserTest from './runPipelineBrowserTest'

test/Browser/writeTest.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import test from 'tape'
2+
import axios from 'axios'
3+
import PromiseFileReader from 'promise-file-reader'
4+
5+
import readImageArrayBuffer from 'readImageArrayBuffer'
6+
import readMeshArrayBuffer from 'readMeshArrayBuffer'
7+
import writeArrayBuffer from 'writeArrayBuffer'
8+
9+
import IntTypes from 'IntTypes'
10+
import FloatTypes from 'FloatTypes'
11+
import PixelTypes from 'PixelTypes'
12+
13+
import getMatrixElement from 'getMatrixElement'
14+
15+
const cthead1SmallBase64DataURI = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAAAAABWESUoAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElNRQfhBQYVKw8AZTNIAAADdklEQVQ4y2WTa2wUVRiGp6W7O3POnLmc2VrstokJlrBIUBJigjfSICVCCAo/QKM/FFNRIESJQKAws3M7M2f20t3GthRKQQq0kkoXMIq9oFwCXkg0UpMakGLgR9EmJF4TNOvZhRBb31+TvM955/vO+T6Ou69pAgSwKCCAEPc/lYUhFEUkMgH2ESmbYocEEUmKLIQqBKmEgUlERQhAPhyJiDMXPFZZDmRGoP8Q5TwC4ciMpatfXE9zmT2NVRVIQiLi76cDUVRDT/m72zLUc/Srv+gNCi8jhCrupvMAQIWf1zJx58pRj7g7h/sduunhiIIkUAJ4AUBZ0LZev3TondmeS42TuaYms6kOapJUalYQAAKxt+j4qD3yxvMZ0z47NLi/ydhWA7GMinWyAH6G1Wwe/OdUz6dz33T35dPdIxdIYrPGK0qxTnYrobVtjm+3pNvPxGu9/dTRgw8/e89et0AKF1uFItS2u7ZP7fr4K3H19VbP94me/T6fXRifM6+a/QKC6N5+PWGYZhVeNn9pzvUoTVnt3/QEz81dUTONgwjis4UzvS2Z5JbY9JlPdxmEuFZzX9va0yu5WlXmRAlWd3Tmjg980vXBprJZbYPtza0dXw40ZleeP1ZbrWKOXXpsu7Grb3gnsY/27B46+e3ElVuF3w+sm7Pki2VAUxkAo1t0a7TL8YnVPZxy6KG9fX/+2qu/+9DARoAVBiDYaHjnfc/3nHOdicA1Em6WpnOdG/I6zwCA5PCzrn6uw6VO99gBnRBKGUyIMfz3BgmrHHta8cEdu04dN6wjPwy6FinaTNT8emKNzGrgBEmJLLf7T6Tf/60wpFP2oKToB/bNr+pVTWHjghQxZuTzW51C4aIZENdj8gMv+1f3I7iYwPEqrFu+z1/zzI3vHN/ziEd9P0haV39aXxXFRaBMRrCu9Vjj5o/S5C4QBCnjws+pJ9SoqpZmRlqyeNWlPa922El22PMCl5if38q9FGV+CeAaFuK4OZY5nLRoksnsPX19nL5do2GsREoAlCtr68lo4VoXNROWdXD8j7GUNV96AMPye5MtYgU/ujF/887tHy+PXLt9o9/asUipvDfWpc1QNFWKPfla8PHI5Ysnsua2l2dH1Un7WS6rKlamxx9f/MKKhkX1syoxmLqcUMVRDTNMlZGkilPsUrOsJ6wxRSel/wuAkzbenLRf4gAAACV0RVh0ZGF0ZTpjcmVhdGUAMjAxNy0wNS0wNlQxNzoyNjozNC0wNDowMORO/MMAAAAldEVYdGRhdGU6bW9kaWZ5ADIwMTctMDUtMDZUMTc6MjY6MzQtMDQ6MDCVE0R/AAAAAElFTkSuQmCC'
16+
const byteString = window.atob(cthead1SmallBase64DataURI.split(',')[1])
17+
const mimeString = cthead1SmallBase64DataURI.split(',')[0].split(':')[1].split(';')[0]
18+
let intArray = new Uint8Array(byteString.length)
19+
for (let ii = 0; ii < byteString.length; ++ii) {
20+
intArray[ii] = byteString.charCodeAt(ii)
21+
}
22+
const cthead1SmallBlob = new window.Blob([intArray], { type: mimeString })
23+
const cthead1SmallFileWriteTest = new window.File([cthead1SmallBlob], 'cthead1Small.png')
24+
25+
const verifyImage = (t, image) => {
26+
t.is(image.imageType.dimension, 2, 'dimension')
27+
t.is(image.imageType.componentType, IntTypes.UInt8, 'componentType')
28+
t.is(image.imageType.pixelType, PixelTypes.Scalar, 'pixelType')
29+
t.is(image.imageType.components, 1, 'components')
30+
t.is(image.origin[0], 0.0, 'origin[0]')
31+
t.is(image.origin[1], 0.0, 'origin[1]')
32+
t.is(image.spacing[0], 1.0, 'spacing[0]')
33+
t.is(image.spacing[1], 1.0, 'spacing[1]')
34+
t.is(getMatrixElement(image.direction, 0, 0), 1.0, 'direction (0, 0)')
35+
t.is(getMatrixElement(image.direction, 0, 1), 0.0, 'direction (0, 1)')
36+
t.is(getMatrixElement(image.direction, 1, 0), 0.0, 'direction (1, 0)')
37+
t.is(getMatrixElement(image.direction, 1, 1), 1.0, 'direction (1, 1)')
38+
t.is(image.size[0], 32, 'size[0]')
39+
t.is(image.size[1], 32, 'size[1]')
40+
t.is(image.data.length, 1024, 'data.length')
41+
t.is(image.data[512], 12, 'data[512]')
42+
t.end()
43+
}
44+
45+
const meshFileName = 'cow.vtk'
46+
const testMeshFilePath = 'base/build/ExternalData/test/Input/' + meshFileName
47+
48+
const verifyMesh = (t, mesh) => {
49+
t.is(mesh.meshType.dimension, 3)
50+
t.is(mesh.meshType.pointComponentType, FloatTypes.Float32)
51+
t.is(mesh.meshType.cellComponentType, IntTypes.UInt32)
52+
t.is(mesh.meshType.pointPixelType, 1)
53+
t.is(mesh.meshType.cellPixelType, 1)
54+
t.is(mesh.numberOfPoints, 2903)
55+
t.is(mesh.numberOfCells, 3263)
56+
t.end()
57+
}
58+
59+
test('writeArrayBuffer writes an image to an ArrayBuffer', (t) => {
60+
return PromiseFileReader.readAsArrayBuffer(cthead1SmallFileWriteTest)
61+
.then((arrayBuffer) => {
62+
return readImageArrayBuffer(null, arrayBuffer, 'cthead1Small.png').then(function ({ image, webWorker }) {
63+
webWorker.terminate()
64+
const useCompression = false
65+
return writeArrayBuffer(null, useCompression, image, 'cthead1Small.png')
66+
})
67+
})
68+
.then(function ({ arrayBuffer: writtenArrayBuffer, webWorker }) {
69+
webWorker.terminate()
70+
return readImageArrayBuffer(null, writtenArrayBuffer, 'cthead1Small.png').then(function ({ image }) {
71+
verifyImage(t, image)
72+
})
73+
})
74+
})
75+
76+
test('writeArrayBuffer writes a mesh to an ArrayBuffer', t => {
77+
return axios.get(testMeshFilePath, { responseType: 'arraybuffer' })
78+
.then(function (response) {
79+
return readMeshArrayBuffer(null, response.data, 'cow.vtk').then(function ({ mesh, webWorker }) {
80+
webWorker.terminate()
81+
const useCompression = false
82+
return writeArrayBuffer(null, useCompression, mesh, 'cow.vtk')
83+
})
84+
})
85+
.then(function ({ arrayBuffer: writtenArrayBuffer, webWorker }) {
86+
webWorker.terminate()
87+
return readMeshArrayBuffer(null, writtenArrayBuffer, 'cow.vtk').then(function ({ mesh, webWorker }) {
88+
webWorker.terminate()
89+
verifyMesh(t, mesh)
90+
})
91+
})
92+
})

0 commit comments

Comments
 (0)