Skip to content

Commit

Permalink
WIP: test(image-io): add bio-rad node test
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Aug 24, 2023
1 parent 10f53eb commit fc0be05
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 2 deletions.
5 changes: 4 additions & 1 deletion packages/image-io/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
ImageIOIndex.ts.in
dist
emscripten-build
emscripten-build/
wasi-build/
package-lock.json
test/data/
18 changes: 18 additions & 0 deletions packages/image-io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,21 @@ if(EMSCRIPTEN)
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/image-io-index.ts.in
${CMAKE_CURRENT_SOURCE_DIR}/typescript/src/image-io-index.ts @ONLY)
endif()

enable_testing()

set(input_dir ${CMAKE_CURRENT_SOURCE_DIR}/test/data/input)
set(baseline_dir ${CMAKE_CURRENT_SOURCE_DIR}/test/data/baseline)
set(output_dir ${CMAKE_CURRENT_BINARY_DIR})

add_test(NAME bio-rad-read-image-test
COMMAND bio-rad-read-image
${input_dir}/biorad.pic
${output_dir}/bio-rad-read-image-test.could-read.json
${output_dir}/bio-rad-read-image-test.iwi.cbor)

add_test(NAME bio-rad-write-image-test
COMMAND bio-rad-write-image
${baseline_dir}/bio-rad-read-image-test.iwi.cbor
${output_dir}/bio-rad-write-image-test.could-write.json
${output_dir}/bio-rad-write-image-test.pic)
4 changes: 3 additions & 1 deletion packages/image-io/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
},
"scripts": {
"start": "npm run copyShoelaceAssets && vite -c build/vite.config.js",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "npm run test:node",
"test:node": "ava test/node/*.js",
"build": "npm run build:tsc && npm run build:node && npm run build:browser && npm run build:demo",
"build:node": "rollup -c ./build/rollup.node.config.js",
"build:browser": "rollup -c ./build/rollup.browser.config.js",
Expand All @@ -41,6 +42,7 @@
"@rollup/plugin-typescript": "^11.1.1",
"@shoelace-style/shoelace": "^2.5.2",
"@types/node": "^20.2.5",
"ava": "^5.3.1",
"debug": "^4.3.4",
"rollup": "^3.9.0",
"rollup-plugin-copy": "^3.4.0",
Expand Down
62 changes: 62 additions & 0 deletions packages/image-io/typescript/test/node/bio-rad-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import test from 'ava'
import path from 'path'
import fs from 'fs'

function mkdirP(dir) {
try {
fs.mkdirSync(dir, { recursive: true })
} catch (err) {
if (err.code !== 'EEXIST') throw err
}
}

import { bioRadReadImageNode, bioRadWriteImageNode } from '../../dist/bundles/image-io-node.js'
import { IntTypes, PixelTypes, getMatrixElement } from 'itk-wasm'

const testInputFilePath = path.resolve('..', 'test', 'data', 'input', 'biorad.pic')
const testOutputPath = path.resolve('..', 'test', 'output', 'typescript')
const testOutputFilePath = path.join(testOutputPath, 'biorad.pic')

const verifyImage = (t, image) => {
t.is(image.imageType.dimension, 2, 'dimension')
t.is(image.imageType.componentType, IntTypes.UInt8, 'componentType')
t.is(image.imageType.pixelType, PixelTypes.Scalar, 'pixelType')
t.is(image.imageType.components, 1, 'components')
t.is(image.origin[0], 0.0, 'origin[0]')
t.is(image.origin[1], 0.0, 'origin[1]')
t.is(image.spacing[0], 0.06000000238418579, 'spacing[0]')
t.is(image.spacing[1], 0.06000000238418579, 'spacing[1]')
t.is(getMatrixElement(image.direction, 2, 0, 0), 1.0, 'direction (0, 0)')
t.is(getMatrixElement(image.direction, 2, 0, 1), 0.0, 'direction (0, 1)')
t.is(getMatrixElement(image.direction, 2, 1, 0), 0.0, 'direction (1, 0)')
t.is(getMatrixElement(image.direction, 2, 1, 1), 1.0, 'direction (1, 1)')
t.is(image.size[0], 768, 'size[0]')
t.is(image.size[1], 512, 'size[1]')
t.is(image.data.length, 393216, 'data.length')
t.is(image.data[1000], 27, 'data[1000]')
}

test('Test reading a BioRad file', async t => {
const { couldRead, image } = await bioRadReadImageNode(testInputFilePath)
t.true(couldRead)
verifyImage(t, image)
})

test('Test writing a BioRad file', async t => {
const { couldRead, image } = await bioRadReadImageNode(testInputFilePath)
t.true(couldRead)
const useCompression = false
const { couldWrite, serializedImage } = await bioRadWriteImageNode(image, { useCompression })
console.log(couldWrite, serializedImage)

// verifyImage(t, image)
// return readImageLocalFile(testInputFilePath).then(function (image) {
// const useCompression = false
// return writeImageLocalFile(image, testOutputFilePath, useCompression)
// })
// .then(function () {
// return readImageLocalFile(testOutputFilePath).then(function (image) {
// verifyImage(t, image)
// })
// })
})

0 comments on commit fc0be05

Please sign in to comment.