ssim.js 4.0.1 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLA.md +66 -0
- package/CONTRIBUTING.md +119 -0
- package/LICENSE +1 -1
- package/LICENSES/LIVE Images Database +18 -0
- package/LICENSES/Subjective quality assessment - IVC database +5 -0
- package/LICENSES/Wang et al SSIM +24 -0
- package/LICENSES/bezkrovny +49 -0
- package/LICENSES/cell2csv +2 -0
- package/LICENSES/obartra-ssim +21 -0
- package/LICENSES/rhys-e +21 -0
- package/README.md +216 -34
- package/dist/bezkrovnySsim.d.ts +5 -19
- package/dist/bezkrovnySsim.js +113 -72
- package/dist/bezkrovnySsim.js.map +1 -1
- package/dist/browser.d.mts +9 -0
- package/dist/browser.d.ts +9 -0
- package/dist/browser.js +37 -0
- package/dist/browser.js.map +1 -0
- package/dist/browser.mjs +742 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/defaults.d.ts +3 -1
- package/dist/defaults.js +14 -4
- package/dist/defaults.js.map +1 -1
- package/dist/gaussian.d.ts +2 -0
- package/dist/gaussian.js +168 -0
- package/dist/gaussian.js.map +1 -0
- package/dist/gaussianSymmetric.d.ts +3 -0
- package/dist/gaussianSymmetric.js +62 -0
- package/dist/gaussianSymmetric.js.map +1 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +8 -19
- package/dist/index.js +19 -88
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +718 -0
- package/dist/index.mjs.map +7 -0
- package/dist/options.d.ts +2 -0
- package/dist/options.js +60 -0
- package/dist/options.js.map +1 -0
- package/dist/originalSsim.d.ts +3 -26
- package/dist/originalSsim.js +5 -57
- package/dist/originalSsim.js.map +1 -1
- package/dist/pixels.d.ts +5 -0
- package/dist/pixels.js +157 -0
- package/dist/pixels.js.map +1 -0
- package/dist/prepare.d.ts +7 -0
- package/dist/prepare.js +48 -0
- package/dist/prepare.js.map +1 -0
- package/dist/referenceSsim.d.ts +4 -0
- package/dist/referenceSsim.js +59 -0
- package/dist/referenceSsim.js.map +1 -0
- package/dist/result.d.ts +2 -0
- package/dist/result.js +20 -0
- package/dist/result.js.map +1 -0
- package/dist/ssim.d.ts +3 -21
- package/dist/ssim.js +4 -126
- package/dist/ssim.js.map +1 -1
- package/dist/ssim.web.js +2 -2
- package/dist/ssim.web.js.map +7 -1
- package/dist/types.d.ts +27 -7
- package/dist/weberSsim.d.ts +19 -0
- package/dist/weberSsim.js +119 -0
- package/dist/weberSsim.js.map +1 -0
- package/package.json +116 -1
- package/src/bezkrovnySsim.ts +133 -67
- package/src/browser.ts +49 -0
- package/src/defaults.ts +16 -6
- package/src/gaussian.ts +228 -0
- package/src/gaussianSymmetric.ts +95 -0
- package/src/index.ts +47 -96
- package/src/options.ts +74 -0
- package/src/originalSsim.ts +9 -79
- package/src/pixels.ts +198 -0
- package/src/prepare.ts +73 -0
- package/src/referenceSsim.ts +71 -0
- package/src/result.ts +23 -0
- package/src/ssim.ts +9 -168
- package/src/types.ts +44 -15
- package/src/weberSsim.ts +132 -0
- package/dist/downsample.d.ts +0 -12
- package/dist/downsample.js +0 -70
- package/dist/downsample.js.map +0 -1
- package/dist/math.d.ts +0 -155
- package/dist/math.js +0 -394
- package/dist/math.js.map +0 -1
- package/dist/matlab/conv2.d.ts +0 -137
- package/dist/matlab/conv2.js +0 -250
- package/dist/matlab/conv2.js.map +0 -1
- package/dist/matlab/filter2.d.ts +0 -18
- package/dist/matlab/filter2.js +0 -53
- package/dist/matlab/filter2.js.map +0 -1
- package/dist/matlab/fspecial.d.ts +0 -39
- package/dist/matlab/fspecial.js +0 -102
- package/dist/matlab/fspecial.js.map +0 -1
- package/dist/matlab/imfilter.d.ts +0 -24
- package/dist/matlab/imfilter.js +0 -84
- package/dist/matlab/imfilter.js.map +0 -1
- package/dist/matlab/index.d.ts +0 -23
- package/dist/matlab/index.js +0 -29
- package/dist/matlab/index.js.map +0 -1
- package/dist/matlab/internal/numbers.d.ts +0 -14
- package/dist/matlab/internal/numbers.js +0 -28
- package/dist/matlab/internal/numbers.js.map +0 -1
- package/dist/matlab/mod.d.ts +0 -21
- package/dist/matlab/mod.js +0 -27
- package/dist/matlab/mod.js.map +0 -1
- package/dist/matlab/normpdf.d.ts +0 -36
- package/dist/matlab/normpdf.js +0 -55
- package/dist/matlab/normpdf.js.map +0 -1
- package/dist/matlab/ones.d.ts +0 -15
- package/dist/matlab/ones.js +0 -22
- package/dist/matlab/ones.js.map +0 -1
- package/dist/matlab/padarray.d.ts +0 -37
- package/dist/matlab/padarray.js +0 -349
- package/dist/matlab/padarray.js.map +0 -1
- package/dist/matlab/rgb2gray.d.ts +0 -15
- package/dist/matlab/rgb2gray.js +0 -58
- package/dist/matlab/rgb2gray.js.map +0 -1
- package/dist/matlab/skip2d.d.ts +0 -34
- package/dist/matlab/skip2d.js +0 -55
- package/dist/matlab/skip2d.js.map +0 -1
- package/dist/matlab/sub.d.ts +0 -17
- package/dist/matlab/sub.js +0 -33
- package/dist/matlab/sub.js.map +0 -1
- package/dist/matlab/transpose.d.ts +0 -18
- package/dist/matlab/transpose.js +0 -34
- package/dist/matlab/transpose.js.map +0 -1
- package/dist/matlab/zeros.d.ts +0 -15
- package/dist/matlab/zeros.js +0 -22
- package/dist/matlab/zeros.js.map +0 -1
- package/dist/version.d.ts +0 -2
- package/dist/version.js +0 -5
- package/dist/version.js.map +0 -1
- package/src/downsample.ts +0 -81
- package/src/math.ts +0 -447
- package/src/matlab/conv2.ts +0 -272
- package/src/matlab/filter2.ts +0 -52
- package/src/matlab/fspecial.ts +0 -107
- package/src/matlab/imfilter.ts +0 -93
- package/src/matlab/index.ts +0 -23
- package/src/matlab/internal/numbers.ts +0 -28
- package/src/matlab/mod.ts +0 -23
- package/src/matlab/normpdf.ts +0 -57
- package/src/matlab/ones.ts +0 -19
- package/src/matlab/padarray.ts +0 -375
- package/src/matlab/rgb2gray.ts +0 -65
- package/src/matlab/skip2d.ts +0 -58
- package/src/matlab/sub.ts +0 -38
- package/src/matlab/transpose.ts +0 -33
- package/src/matlab/zeros.ts +0 -19
- package/src/types/bmp-js.d.ts +0 -1
- package/src/version.ts +0 -2
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { ImageMatrix, Matrix } from './types'
|
|
2
|
+
|
|
3
|
+
/** Exact symmetric Gaussian weights, Float64 moments, no omitted taps or rounding. */
|
|
4
|
+
export function symmetricKernel(
|
|
5
|
+
a: ImageMatrix,
|
|
6
|
+
b: ImageMatrix,
|
|
7
|
+
weights: number[],
|
|
8
|
+
c1: number,
|
|
9
|
+
c2: number,
|
|
10
|
+
map = true
|
|
11
|
+
): Matrix {
|
|
12
|
+
const size = weights.length,
|
|
13
|
+
half = size >> 1,
|
|
14
|
+
width = a.width - size + 1,
|
|
15
|
+
height = a.height - size + 1
|
|
16
|
+
const rows = new Float64Array(size * width * 5),
|
|
17
|
+
slots = new Int32Array(size)
|
|
18
|
+
const data = map ? new Array<number>(width * height) : [],
|
|
19
|
+
av = a.data,
|
|
20
|
+
bv = b.data
|
|
21
|
+
let nextRow = 0,
|
|
22
|
+
sum = 0
|
|
23
|
+
for (let y = 0; y < height; y++) {
|
|
24
|
+
for (; nextRow < y + size; nextRow++) {
|
|
25
|
+
const rowOffset = (nextRow % size) * width * 5,
|
|
26
|
+
inputOffset = nextRow * a.width
|
|
27
|
+
for (let x = 0; x < width; x++) {
|
|
28
|
+
const center = inputOffset + x + half,
|
|
29
|
+
ac = av[center],
|
|
30
|
+
bc = bv[center],
|
|
31
|
+
wc = weights[half]
|
|
32
|
+
let sx = ac * wc,
|
|
33
|
+
sy = bc * wc,
|
|
34
|
+
xx = ac * ac * wc,
|
|
35
|
+
yy = bc * bc * wc,
|
|
36
|
+
xy = ac * bc * wc
|
|
37
|
+
for (let tap = 0; tap < half; tap++) {
|
|
38
|
+
const left = inputOffset + x + tap,
|
|
39
|
+
right = inputOffset + x + size - 1 - tap,
|
|
40
|
+
w = weights[tap]
|
|
41
|
+
const al = av[left],
|
|
42
|
+
ar = av[right],
|
|
43
|
+
bl = bv[left],
|
|
44
|
+
br = bv[right]
|
|
45
|
+
sx += (al + ar) * w
|
|
46
|
+
sy += (bl + br) * w
|
|
47
|
+
xx += (al * al + ar * ar) * w
|
|
48
|
+
yy += (bl * bl + br * br) * w
|
|
49
|
+
xy += (al * bl + ar * br) * w
|
|
50
|
+
}
|
|
51
|
+
const i = rowOffset + x * 5
|
|
52
|
+
rows[i] = sx
|
|
53
|
+
rows[i + 1] = sy
|
|
54
|
+
rows[i + 2] = xx
|
|
55
|
+
rows[i + 3] = yy
|
|
56
|
+
rows[i + 4] = xy
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
for (let tap = 0; tap < size; tap++)
|
|
60
|
+
slots[tap] = ((y + tap) % size) * width * 5
|
|
61
|
+
for (let x = 0; x < width; x++) {
|
|
62
|
+
const center = slots[half] + x * 5,
|
|
63
|
+
wc = weights[half]
|
|
64
|
+
let sx = rows[center] * wc,
|
|
65
|
+
sy = rows[center + 1] * wc,
|
|
66
|
+
xx = rows[center + 2] * wc,
|
|
67
|
+
yy = rows[center + 3] * wc,
|
|
68
|
+
xy = rows[center + 4] * wc
|
|
69
|
+
for (let tap = 0; tap < half; tap++) {
|
|
70
|
+
const left = slots[tap] + x * 5,
|
|
71
|
+
right = slots[size - 1 - tap] + x * 5,
|
|
72
|
+
w = weights[tap]
|
|
73
|
+
sx += (rows[left] + rows[right]) * w
|
|
74
|
+
sy += (rows[left + 1] + rows[right + 1]) * w
|
|
75
|
+
xx += (rows[left + 2] + rows[right + 2]) * w
|
|
76
|
+
yy += (rows[left + 3] + rows[right + 3]) * w
|
|
77
|
+
xy += (rows[left + 4] + rows[right + 4]) * w
|
|
78
|
+
}
|
|
79
|
+
const mx2 = sx * sx,
|
|
80
|
+
my2 = sy * sy,
|
|
81
|
+
mxy = sx * sy
|
|
82
|
+
const value =
|
|
83
|
+
((2 * mxy + c1) * (2 * (xy - mxy) + c2)) /
|
|
84
|
+
((mx2 + my2 + c1) * (xx - mx2 + (yy - my2) + c2))
|
|
85
|
+
if (map) data[y * width + x] = value
|
|
86
|
+
else sum += value
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
width,
|
|
91
|
+
height,
|
|
92
|
+
data,
|
|
93
|
+
...(map ? {} : { mssim: sum / (width * height) }),
|
|
94
|
+
}
|
|
95
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,105 +1,56 @@
|
|
|
1
|
-
/**
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
/** Synchronous JavaScript SSIM API; image decoding stays in the application. */
|
|
2
|
+
import { ssim as fastSsim } from './ssim'
|
|
3
|
+
import { originalSsim } from './originalSsim'
|
|
4
|
+
import { bezkrovnySsim } from './bezkrovnySsim'
|
|
5
|
+
import { weberSsim } from './weberSsim'
|
|
6
|
+
import { referenceSsim } from './referenceSsim'
|
|
7
|
+
import { prepareComparison } from './prepare'
|
|
8
|
+
import { finishComparison } from './result'
|
|
9
|
+
import type { ImageData, Options, SsimResult, SsimMapResult } from './types'
|
|
10
|
+
|
|
11
|
+
export { getOptions } from './options'
|
|
12
|
+
export type {
|
|
13
|
+
Options,
|
|
14
|
+
Matrix,
|
|
15
|
+
ImageData,
|
|
16
|
+
SsimResult,
|
|
17
|
+
SsimMapResult,
|
|
18
|
+
} from './types'
|
|
19
|
+
|
|
20
|
+
const kernels = {
|
|
21
|
+
gaussian: referenceSsim,
|
|
22
22
|
fast: fastSsim,
|
|
23
23
|
original: originalSsim,
|
|
24
|
-
bezkrovny: bezkrovnySsim
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
function validateOptions(options: Options) {
|
|
28
|
-
Object.keys(options).forEach(option => {
|
|
29
|
-
if (!(option in defaults)) {
|
|
30
|
-
throw new Error(`"${option}" is not a valid option`);
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
if ("k1" in options && (typeof options.k1 !== "number" || options.k1 < 0)) {
|
|
34
|
-
throw new Error(`Invalid k1 value. Default is ${defaults.k1}`);
|
|
35
|
-
}
|
|
36
|
-
if ("k2" in options && (typeof options.k2 !== "number" || options.k2 < 0)) {
|
|
37
|
-
throw new Error(`Invalid k2 value. Default is ${defaults.k2}`);
|
|
38
|
-
}
|
|
39
|
-
if (!(options.ssim in ssimTargets)) {
|
|
40
|
-
throw new Error(
|
|
41
|
-
`Invalid ssim option (use: ${Object.keys(ssimTargets).join(", ")})`
|
|
42
|
-
);
|
|
43
|
-
}
|
|
24
|
+
bezkrovny: bezkrovnySsim,
|
|
25
|
+
weber: weberSsim,
|
|
44
26
|
}
|
|
45
27
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
function validateDimensions([pixels1, pixels2, options]: Images): Images {
|
|
55
|
-
if (pixels1.width !== pixels2.width || pixels1.height !== pixels2.height) {
|
|
56
|
-
throw new Error("Image dimensions do not match");
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return [pixels1, pixels2, options];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function toGrayScale([pixels1, pixels2, options]: Images): Matrices {
|
|
63
|
-
return [rgb2gray(pixels1), rgb2gray(pixels2), options];
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
function toResize([pixels1, pixels2, options]: Matrices): Matrices {
|
|
67
|
-
const pixels = downsample([pixels1, pixels2], options);
|
|
68
|
-
|
|
69
|
-
return [pixels[0], pixels[1], options];
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
function comparison([pixels1, pixels2, options]: Matrices): Matrix {
|
|
73
|
-
return ssimTargets[options.ssim](pixels1, pixels2, options);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* @method ssim - The ssim method. You can call the package directly or through the `ssim` property.
|
|
78
|
-
* @public
|
|
79
|
-
* @example import mod = from 'ssim.js';
|
|
80
|
-
* mod(imgBuffer1, imgBuffer2);
|
|
81
|
-
* mod.ssim(imgBuffer1, imgBuffer2);
|
|
82
|
-
*/
|
|
28
|
+
/** Compare decoded RGBA images. The default implementation never initializes WASM. */
|
|
29
|
+
export function ssim(
|
|
30
|
+
image1: ImageData,
|
|
31
|
+
image2: ImageData,
|
|
32
|
+
userOptions: Partial<Options> & { map: true }
|
|
33
|
+
): SsimMapResult
|
|
83
34
|
export function ssim(
|
|
84
35
|
image1: ImageData,
|
|
85
36
|
image2: ImageData,
|
|
86
37
|
userOptions?: Partial<Options>
|
|
87
|
-
):
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
const
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
38
|
+
): SsimResult
|
|
39
|
+
export function ssim(
|
|
40
|
+
image1: ImageData,
|
|
41
|
+
image2: ImageData,
|
|
42
|
+
userOptions?: Partial<Options>
|
|
43
|
+
): SsimResult {
|
|
44
|
+
const start = Date.now()
|
|
45
|
+
const { pixels1, pixels2, options } = prepareComparison(
|
|
46
|
+
image1,
|
|
47
|
+
image2,
|
|
48
|
+
userOptions
|
|
49
|
+
)
|
|
50
|
+
return finishComparison(
|
|
51
|
+
kernels[options.ssim](pixels1, pixels2, options, options.map),
|
|
52
|
+
start,
|
|
53
|
+
options.map
|
|
54
|
+
)
|
|
103
55
|
}
|
|
104
|
-
|
|
105
|
-
export default ssim;
|
|
56
|
+
export default ssim
|
package/src/options.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { defaults, legacyDefaults } from './defaults'
|
|
2
|
+
import type { Options } from './types'
|
|
3
|
+
|
|
4
|
+
function validateOptions(options: Options) {
|
|
5
|
+
if (typeof options.map !== 'boolean')
|
|
6
|
+
throw new TypeError('map must be a boolean')
|
|
7
|
+
for (const option of Object.keys(options)) {
|
|
8
|
+
if (!Object.hasOwn(defaults, option)) {
|
|
9
|
+
throw new TypeError(`"${option}" is not a valid option`)
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
for (const option of ['k1', 'k2'] as const) {
|
|
13
|
+
const value = options[option]
|
|
14
|
+
if (!Number.isFinite(value) || value < 0) {
|
|
15
|
+
throw new RangeError(`${option} must be a finite nonnegative number`)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
for (const option of ['windowSize', 'bitDepth'] as const) {
|
|
19
|
+
if (!Number.isSafeInteger(options[option]) || options[option] <= 0) {
|
|
20
|
+
throw new RangeError(`${option} must be a positive safe integer`)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
if (options.bitDepth > 32) {
|
|
24
|
+
throw new RangeError('bitDepth must be between 1 and 32')
|
|
25
|
+
}
|
|
26
|
+
if (
|
|
27
|
+
options.maxSize !== undefined &&
|
|
28
|
+
(!Number.isSafeInteger(options.maxSize) || options.maxSize <= 0)
|
|
29
|
+
) {
|
|
30
|
+
throw new RangeError('maxSize must be a positive safe integer')
|
|
31
|
+
}
|
|
32
|
+
for (const option of ['k1', 'k2'] as const) {
|
|
33
|
+
if (
|
|
34
|
+
!Number.isFinite((options[option] * (2 ** options.bitDepth - 1)) ** 2)
|
|
35
|
+
) {
|
|
36
|
+
throw new RangeError(
|
|
37
|
+
`${option} produces an overflowing stability constant`
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (
|
|
42
|
+
!['gaussian', 'fast', 'original', 'bezkrovny', 'weber'].includes(
|
|
43
|
+
options.ssim
|
|
44
|
+
)
|
|
45
|
+
) {
|
|
46
|
+
throw new TypeError(
|
|
47
|
+
'ssim must be gaussian, weber, original, fast, or bezkrovny'
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
if (![false, 'wang', 'original', 'fast'].includes(options.downsample)) {
|
|
51
|
+
throw new TypeError('downsample must be false, wang, original, or fast')
|
|
52
|
+
}
|
|
53
|
+
if (!['original', 'integer', 'matlab'].includes(options.rgb2grayVersion)) {
|
|
54
|
+
throw new TypeError('rgb2grayVersion must be original, integer, or matlab')
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getOptions(userOptions?: Partial<Options>): Options {
|
|
59
|
+
if (
|
|
60
|
+
userOptions !== undefined &&
|
|
61
|
+
(userOptions === null ||
|
|
62
|
+
typeof userOptions !== 'object' ||
|
|
63
|
+
Array.isArray(userOptions))
|
|
64
|
+
) {
|
|
65
|
+
throw new TypeError('options must be an object')
|
|
66
|
+
}
|
|
67
|
+
const profile =
|
|
68
|
+
userOptions?.ssim && userOptions.ssim !== 'gaussian'
|
|
69
|
+
? legacyDefaults
|
|
70
|
+
: defaults
|
|
71
|
+
const options = { ...profile, ...userOptions }
|
|
72
|
+
validateOptions(options)
|
|
73
|
+
return options
|
|
74
|
+
}
|
package/src/originalSsim.ts
CHANGED
|
@@ -1,82 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
add2d,
|
|
5
|
-
divide2d,
|
|
6
|
-
multiply2d,
|
|
7
|
-
square2d,
|
|
8
|
-
subtract2d,
|
|
9
|
-
sum2d
|
|
10
|
-
} from "./math";
|
|
11
|
-
import { filter2, fspecial } from "./matlab";
|
|
12
|
-
import { Options, Matrix } from "./types";
|
|
1
|
+
import { gaussianSsim } from './gaussian'
|
|
2
|
+
import type { ImageMatrix, Options } from './types'
|
|
13
3
|
|
|
14
|
-
/**
|
|
15
|
-
* Generates a SSIM map based on two input image matrices. For images greater than 512 pixels, it
|
|
16
|
-
* will downsample them.
|
|
17
|
-
*
|
|
18
|
-
* Images must be a 2-Dimensional grayscale image
|
|
19
|
-
*
|
|
20
|
-
* This method is a line-by-line port of `assets/ssim.m`. Some operations are more verbose here
|
|
21
|
-
* since more logic is needed in JS to manipulate matrices than in Matlab
|
|
22
|
-
*
|
|
23
|
-
* Note that setting `options1.k1` and `options.k2` to 0 will generate the UQI (Universal Quality
|
|
24
|
-
* Index), since it's a special case of SSIM. In general that's undesierable since `k1` and `k2`
|
|
25
|
-
* contribute to the stabilization coeficients `c1` and `c2`.
|
|
26
|
-
*
|
|
27
|
-
* For a mathematically equivalent and more efficient implementation check `./ssim.js`.
|
|
28
|
-
*
|
|
29
|
-
* @method originalSsim
|
|
30
|
-
* @param {Matrix} pixels1 - The reference matrix
|
|
31
|
-
* @param {Matrix} pixels2 - The second matrix to compare against
|
|
32
|
-
* @param {Object} options - The input options parameter
|
|
33
|
-
* @returns {Matrix} ssim_map - A matrix containing the map of computed SSIMs
|
|
34
|
-
* @public
|
|
35
|
-
* @memberOf ssim
|
|
36
|
-
* @since 0.0.2
|
|
37
|
-
*/
|
|
4
|
+
/** Reference two-dimensional Gaussian SSIM, including even-sized windows. */
|
|
38
5
|
export function originalSsim(
|
|
39
|
-
pixels1:
|
|
40
|
-
pixels2:
|
|
41
|
-
options: Options
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const c1 = (options.k1 * L) ** 2;
|
|
46
|
-
const c2 = (options.k2 * L) ** 2;
|
|
47
|
-
|
|
48
|
-
w = divide2d(w, sum2d(w));
|
|
49
|
-
|
|
50
|
-
const μ1 = filter2(w, pixels1, "valid");
|
|
51
|
-
const μ2 = filter2(w, pixels2, "valid");
|
|
52
|
-
const μ1Sq = square2d(μ1);
|
|
53
|
-
const μ2Sq = square2d(μ2);
|
|
54
|
-
const μ12 = multiply2d(μ1, μ2);
|
|
55
|
-
const pixels1Sq = square2d(pixels1);
|
|
56
|
-
const pixels2Sq = square2d(pixels2);
|
|
57
|
-
const σ1Sq = subtract2d(filter2(w, pixels1Sq, "valid"), μ1Sq);
|
|
58
|
-
const σ2Sq = subtract2d(filter2(w, pixels2Sq, "valid"), μ2Sq);
|
|
59
|
-
const σ12 = subtract2d(
|
|
60
|
-
filter2(w, multiply2d(pixels1, pixels2), "valid"),
|
|
61
|
-
μ12
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
if (c1 > 0 && c2 > 0) {
|
|
65
|
-
const num1 = add2d(multiply2d(μ12, 2), c1);
|
|
66
|
-
const num2 = add2d(multiply2d(σ12, 2), c2);
|
|
67
|
-
const denom1 = add2d(add2d(μ1Sq, μ2Sq), c1);
|
|
68
|
-
const denom2 = add2d(add2d(σ1Sq, σ2Sq), c2);
|
|
69
|
-
|
|
70
|
-
return divide2d(multiply2d(num1, num2), multiply2d(denom1, denom2));
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const numerator1 = multiply2d(μ12, 2);
|
|
74
|
-
const numerator2 = multiply2d(σ12, 2);
|
|
75
|
-
const denominator1 = add2d(μ1Sq, μ2Sq);
|
|
76
|
-
const denominator2 = add2d(σ1Sq, σ2Sq);
|
|
77
|
-
|
|
78
|
-
return divide2d(
|
|
79
|
-
multiply2d(numerator1, numerator2),
|
|
80
|
-
multiply2d(denominator1, denominator2)
|
|
81
|
-
);
|
|
6
|
+
pixels1: ImageMatrix,
|
|
7
|
+
pixels2: ImageMatrix,
|
|
8
|
+
options: Options,
|
|
9
|
+
map = true
|
|
10
|
+
) {
|
|
11
|
+
return gaussianSsim(pixels1, pixels2, options, false, false, map)
|
|
82
12
|
}
|
package/src/pixels.ts
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import type { ImageData, ImageMatrix, Options } from './types'
|
|
2
|
+
|
|
3
|
+
const littleEndian = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1
|
|
4
|
+
|
|
5
|
+
// First row of the inverse NTSC YIQ transform used by MATLAB rgb2gray.
|
|
6
|
+
function grayMatlab(
|
|
7
|
+
rgba: ImageData['data'],
|
|
8
|
+
start: number,
|
|
9
|
+
output: Uint8Array
|
|
10
|
+
) {
|
|
11
|
+
for (let p = 0, i = start * 4; p < output.length; p++, i += 4)
|
|
12
|
+
output[p] = Math.round(
|
|
13
|
+
0.298936021293775 * rgba[i] +
|
|
14
|
+
0.587043074451121 * rgba[i + 1] +
|
|
15
|
+
0.114020904255103 * rgba[i + 2]
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function grayPacked(words: Uint32Array, start: number, output: Uint8Array) {
|
|
20
|
+
for (let p = 0; p < output.length; p++) {
|
|
21
|
+
const pixel = words[start + p]
|
|
22
|
+
// R/B occupy separate 16-bit lanes. Their weighted sum fits in a lane.
|
|
23
|
+
output[p] =
|
|
24
|
+
((Math.imul(pixel & 0xff00ff, 0x4d001d) >>> 16) +
|
|
25
|
+
150 * ((pixel >>> 8) & 255) +
|
|
26
|
+
128) >>
|
|
27
|
+
8
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function grayInteger(
|
|
31
|
+
rgba: ImageData['data'],
|
|
32
|
+
start: number,
|
|
33
|
+
output: Uint8Array
|
|
34
|
+
) {
|
|
35
|
+
for (let p = 0, i = start * 4; p < output.length; p++, i += 4) {
|
|
36
|
+
output[p] = (77 * rgba[i] + 150 * rgba[i + 1] + 29 * rgba[i + 2] + 128) >> 8
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function grayOriginal(
|
|
40
|
+
rgba: ImageData['data'],
|
|
41
|
+
start: number,
|
|
42
|
+
output: Uint8Array
|
|
43
|
+
) {
|
|
44
|
+
for (let p = 0, i = start * 4; p < output.length; p++, i += 4) {
|
|
45
|
+
output[p] =
|
|
46
|
+
(0.29894 * rgba[i] +
|
|
47
|
+
0.58704 * rgba[i + 1] +
|
|
48
|
+
0.11402 * rgba[i + 2] +
|
|
49
|
+
0.5) |
|
|
50
|
+
0
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function grayDoubles(
|
|
54
|
+
rgba: ImageData['data'],
|
|
55
|
+
words: Uint32Array | undefined,
|
|
56
|
+
integer: boolean
|
|
57
|
+
) {
|
|
58
|
+
const output = new Float64Array(rgba.length / 4)
|
|
59
|
+
if (words) {
|
|
60
|
+
for (let p = 0; p < output.length; p++) {
|
|
61
|
+
const pixel = words[p]
|
|
62
|
+
output[p] =
|
|
63
|
+
((Math.imul(pixel & 0xff00ff, 0x4d001d) >>> 16) +
|
|
64
|
+
150 * ((pixel >>> 8) & 255) +
|
|
65
|
+
128) >>
|
|
66
|
+
8
|
|
67
|
+
}
|
|
68
|
+
} else if (integer) {
|
|
69
|
+
for (let p = 0, i = 0; p < output.length; p++, i += 4)
|
|
70
|
+
output[p] =
|
|
71
|
+
(77 * rgba[i] + 150 * rgba[i + 1] + 29 * rgba[i + 2] + 128) >> 8
|
|
72
|
+
} else {
|
|
73
|
+
for (let p = 0, i = 0; p < output.length; p++, i += 4)
|
|
74
|
+
output[p] =
|
|
75
|
+
(0.29894 * rgba[i] +
|
|
76
|
+
0.58704 * rgba[i + 1] +
|
|
77
|
+
0.11402 * rgba[i + 2] +
|
|
78
|
+
0.5) |
|
|
79
|
+
0
|
|
80
|
+
}
|
|
81
|
+
return output
|
|
82
|
+
}
|
|
83
|
+
function storeRounded(
|
|
84
|
+
sums: Float64Array,
|
|
85
|
+
output: Uint8Array,
|
|
86
|
+
offset: number,
|
|
87
|
+
coefficient: number
|
|
88
|
+
) {
|
|
89
|
+
for (let x = 0; x < sums.length; x++)
|
|
90
|
+
output[offset + x] = (sums[x] * coefficient + 0.5) | 0
|
|
91
|
+
}
|
|
92
|
+
function storeFractional(
|
|
93
|
+
sums: Float64Array,
|
|
94
|
+
output: Float64Array,
|
|
95
|
+
offset: number,
|
|
96
|
+
coefficient: number
|
|
97
|
+
) {
|
|
98
|
+
for (let x = 0; x < sums.length; x++)
|
|
99
|
+
output[offset + x] = sums[x] * coefficient
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Convert bytes before averaging, preserving the reference luminance rounding.
|
|
103
|
+
* Stream contiguous grayscale rows through the box sums; no full gray image is
|
|
104
|
+
* needed for downsampling, and the hot conversion loops have one representation. */
|
|
105
|
+
export function preparePixels(image: ImageData, options: Options): ImageMatrix {
|
|
106
|
+
const { width: sourceWidth, height: sourceHeight, data: rgba } = image
|
|
107
|
+
const factor =
|
|
108
|
+
options.downsample === 'original' || options.downsample === 'wang'
|
|
109
|
+
? Math.round(
|
|
110
|
+
Math.min(sourceWidth, sourceHeight) / (options.maxSize ?? 256)
|
|
111
|
+
)
|
|
112
|
+
: 1
|
|
113
|
+
const integer = options.rgb2grayVersion === 'integer'
|
|
114
|
+
const packed =
|
|
115
|
+
integer && littleEndian && rgba.byteOffset % 4 === 0
|
|
116
|
+
? new Uint32Array(
|
|
117
|
+
rgba.buffer,
|
|
118
|
+
rgba.byteOffset,
|
|
119
|
+
sourceWidth * sourceHeight
|
|
120
|
+
)
|
|
121
|
+
: undefined
|
|
122
|
+
if (factor <= 1) {
|
|
123
|
+
if (options.rgb2grayVersion === 'matlab') {
|
|
124
|
+
const data = new Uint8Array(sourceWidth * sourceHeight)
|
|
125
|
+
grayMatlab(rgba, 0, data)
|
|
126
|
+
return { data, width: sourceWidth, height: sourceHeight }
|
|
127
|
+
}
|
|
128
|
+
if (options.ssim === 'original') {
|
|
129
|
+
return {
|
|
130
|
+
data: grayDoubles(rgba, packed, integer),
|
|
131
|
+
width: sourceWidth,
|
|
132
|
+
height: sourceHeight,
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
const data = new Uint8Array(sourceWidth * sourceHeight)
|
|
136
|
+
if (packed) grayPacked(packed, 0, data)
|
|
137
|
+
else if (integer) grayInteger(rgba, 0, data)
|
|
138
|
+
else grayOriginal(rgba, 0, data)
|
|
139
|
+
return { data, width: sourceWidth, height: sourceHeight }
|
|
140
|
+
}
|
|
141
|
+
const convert =
|
|
142
|
+
options.rgb2grayVersion === 'matlab'
|
|
143
|
+
? (start: number, output: Uint8Array) => grayMatlab(rgba, start, output)
|
|
144
|
+
: packed
|
|
145
|
+
? (start: number, output: Uint8Array) =>
|
|
146
|
+
grayPacked(packed, start, output)
|
|
147
|
+
: integer
|
|
148
|
+
? (start: number, output: Uint8Array) =>
|
|
149
|
+
grayInteger(rgba, start, output)
|
|
150
|
+
: (start: number, output: Uint8Array) =>
|
|
151
|
+
grayOriginal(rgba, start, output)
|
|
152
|
+
const width = Math.ceil(sourceWidth / factor),
|
|
153
|
+
height = Math.ceil(sourceHeight / factor)
|
|
154
|
+
const data =
|
|
155
|
+
options.ssim === 'weber'
|
|
156
|
+
? new Uint8Array(width * height)
|
|
157
|
+
: new Float64Array(width * height)
|
|
158
|
+
const padding =
|
|
159
|
+
options.downsample === 'wang'
|
|
160
|
+
? Math.floor((factor - 1) / 2)
|
|
161
|
+
: Math.floor(factor / 2),
|
|
162
|
+
coefficient = 1 / (factor * factor)
|
|
163
|
+
const columns = new Int32Array(width * factor)
|
|
164
|
+
for (let x = 0; x < columns.length; x++) {
|
|
165
|
+
const source = x - padding
|
|
166
|
+
columns[x] =
|
|
167
|
+
source < 0
|
|
168
|
+
? -source - 1
|
|
169
|
+
: source >= sourceWidth
|
|
170
|
+
? 2 * sourceWidth - source - 1
|
|
171
|
+
: source
|
|
172
|
+
}
|
|
173
|
+
const grayRow = new Uint8Array(sourceWidth),
|
|
174
|
+
sums = new Float64Array(width)
|
|
175
|
+
for (let y = 0; y < height; y++) {
|
|
176
|
+
sums.fill(0)
|
|
177
|
+
for (let row = 0; row < factor; row++) {
|
|
178
|
+
const source = y * factor - padding + row
|
|
179
|
+
const reflected =
|
|
180
|
+
source < 0
|
|
181
|
+
? -source - 1
|
|
182
|
+
: source >= sourceHeight
|
|
183
|
+
? 2 * sourceHeight - source - 1
|
|
184
|
+
: source
|
|
185
|
+
convert(reflected * sourceWidth, grayRow)
|
|
186
|
+
for (let x = 0, p = 0; x < width; x++) {
|
|
187
|
+
let sum = 0
|
|
188
|
+
for (let column = 0; column < factor; column++)
|
|
189
|
+
sum += grayRow[columns[p++]]
|
|
190
|
+
sums[x] += sum
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (data instanceof Uint8Array)
|
|
194
|
+
storeRounded(sums, data, y * width, coefficient)
|
|
195
|
+
else storeFractional(sums, data, y * width, coefficient)
|
|
196
|
+
}
|
|
197
|
+
return { data, width, height }
|
|
198
|
+
}
|
package/src/prepare.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { preparePixels } from './pixels'
|
|
2
|
+
import { getOptions } from './options'
|
|
3
|
+
import type { ImageData, ImageMatrix, Options } from './types'
|
|
4
|
+
|
|
5
|
+
export type PreparedComparison = {
|
|
6
|
+
pixels1: ImageMatrix
|
|
7
|
+
pixels2: ImageMatrix
|
|
8
|
+
options: Options
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function validateImage(image: ImageData, name: string) {
|
|
12
|
+
if (image === null || typeof image !== 'object') {
|
|
13
|
+
throw new TypeError(`${name} must be a decoded RGBA image`)
|
|
14
|
+
}
|
|
15
|
+
if (
|
|
16
|
+
!Number.isSafeInteger(image.width) ||
|
|
17
|
+
image.width <= 0 ||
|
|
18
|
+
!Number.isSafeInteger(image.height) ||
|
|
19
|
+
image.height <= 0 ||
|
|
20
|
+
!Number.isSafeInteger(image.width * image.height * 4)
|
|
21
|
+
) {
|
|
22
|
+
throw new RangeError(
|
|
23
|
+
`${name} dimensions must be positive safe integers with a safe RGBA length`
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
const tag = Object.prototype.toString.call(image.data)
|
|
27
|
+
if (
|
|
28
|
+
!ArrayBuffer.isView(image.data) ||
|
|
29
|
+
!['[object Uint8Array]', '[object Uint8ClampedArray]'].includes(tag)
|
|
30
|
+
) {
|
|
31
|
+
throw new TypeError(
|
|
32
|
+
`${name}.data must be a Uint8Array or Uint8ClampedArray`
|
|
33
|
+
)
|
|
34
|
+
}
|
|
35
|
+
if (image.data.length !== image.width * image.height * 4) {
|
|
36
|
+
throw new RangeError(
|
|
37
|
+
`${name}.data must contain exactly width * height * 4 RGBA bytes`
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function validateDimensions(image1: ImageData, image2: ImageData) {
|
|
43
|
+
validateImage(image1, 'image1')
|
|
44
|
+
validateImage(image2, 'image2')
|
|
45
|
+
if (image1.width !== image2.width || image1.height !== image2.height) {
|
|
46
|
+
throw new RangeError('Image dimensions do not match')
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function prepareComparison(
|
|
51
|
+
image1: ImageData,
|
|
52
|
+
image2: ImageData,
|
|
53
|
+
userOptions?: Partial<Options>
|
|
54
|
+
): PreparedComparison {
|
|
55
|
+
const options = getOptions(userOptions)
|
|
56
|
+
validateDimensions(image1, image2)
|
|
57
|
+
const pixels1 = preparePixels(image1, options)
|
|
58
|
+
const pixels2 = preparePixels(image2, options)
|
|
59
|
+
// The legacy fast kernel rounds even window sizes up to the next odd size.
|
|
60
|
+
const effectiveWindow =
|
|
61
|
+
options.ssim === 'fast'
|
|
62
|
+
? 2 * Math.floor(options.windowSize / 2) + 1
|
|
63
|
+
: options.windowSize
|
|
64
|
+
if (
|
|
65
|
+
options.ssim !== 'bezkrovny' &&
|
|
66
|
+
(pixels1.width < effectiveWindow || pixels1.height < effectiveWindow)
|
|
67
|
+
) {
|
|
68
|
+
throw new RangeError(
|
|
69
|
+
'windowSize exceeds image dimensions after downsampling'
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
return { pixels1, pixels2, options }
|
|
73
|
+
}
|