diff --git a/packages/image-io/.gitignore b/packages/image-io/.gitignore index 625d51c2e..54d9f904c 100644 --- a/packages/image-io/.gitignore +++ b/packages/image-io/.gitignore @@ -1,3 +1,6 @@ ImageIOIndex.ts.in dist -emscripten-build +emscripten-build/ +wasi-build/ +package-lock.json +test/data/ diff --git a/packages/image-io/CMakeLists.txt b/packages/image-io/CMakeLists.txt index ee43ac0b1..34be93caf 100644 --- a/packages/image-io/CMakeLists.txt +++ b/packages/image-io/CMakeLists.txt @@ -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) \ No newline at end of file diff --git a/packages/image-io/typescript/package.json b/packages/image-io/typescript/package.json index 7ecdc2cc6..a19d4e3c3 100644 --- a/packages/image-io/typescript/package.json +++ b/packages/image-io/typescript/package.json @@ -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", @@ -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", diff --git a/packages/image-io/typescript/test/node/bio-rad-test.js b/packages/image-io/typescript/test/node/bio-rad-test.js new file mode 100644 index 000000000..00a65aae4 --- /dev/null +++ b/packages/image-io/typescript/test/node/bio-rad-test.js @@ -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) + // }) + // }) +})