/
PNGTest.js
42 lines (37 loc) · 1.73 KB
/
PNGTest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import test from 'ava'
import path from 'path'
import { IntTypes, PixelTypes, getMatrixElement, readImageLocalFile, writeImageLocalFile } from '../../../../dist/index.js'
const testInputFilePath = path.resolve('build-emscripten', 'ExternalData', 'test', 'Input', 'cthead1.png')
const testOutputFilePath = path.resolve('build-emscripten', 'Testing', 'Temporary', 'TestPNG-cthead1.png')
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.RGB, 'pixelType')
t.is(image.imageType.components, 3, 'components')
t.is(image.origin[0], 0.0, 'origin[0]')
t.is(image.origin[1], 0.0, 'origin[1]')
t.is(image.spacing[0], 1.0, 'spacing[0]')
t.is(image.spacing[1], 1.0, '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], 256, 'size[0]')
t.is(image.size[1], 256, 'size[1]')
t.is(image.data.length, 196608, 'data.length')
}
test('Test reading a PNG file', async t => {
const image = await readImageLocalFile(testInputFilePath)
verifyImage(t, image)
})
test('Test writing a PNG file', t => {
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)
})
})
})