Skip to content

Commit ec60087

Browse files
authored
Merge pull request #2 from happo/add-max-diff
Add maxDiff to return value
2 parents b65cec3 + c4dde7d commit ec60087

File tree

6 files changed

+57
-11
lines changed

6 files changed

+57
-11
lines changed

src/__tests__/createDiffImage-test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const crypto = require('crypto');
2+
const path = require('path');
3+
4+
const Jimp = require('jimp');
5+
6+
const computeAndInjectDiffs = require('../computeAndInjectDiffs');
7+
const createDiffImage = require('../createDiffImage');
8+
9+
let image1;
10+
let image2;
11+
let subject;
12+
13+
beforeEach(async () => {
14+
image1 = (await Jimp.read(
15+
'https://dummyimage.com/200/000/ffffff.png&text=aa',
16+
)).bitmap;
17+
image2 = (await Jimp.read(
18+
'https://dummyimage.com/200/000/f7f7f7.png&text=aa',
19+
)).bitmap;
20+
subject = () =>
21+
createDiffImage(
22+
computeAndInjectDiffs({
23+
image1,
24+
image2,
25+
}),
26+
);
27+
});
28+
29+
it('has a total diff value and a max diff', async () => {
30+
const { maxDiff, diff } = await subject();
31+
expect(maxDiff).toEqual(0.027169424432452977);
32+
expect(diff).toEqual(0.00043751263781383705);
33+
});

src/__tests__/index-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ it('produces a trace svg', () => {
3434
const img = subject();
3535
expect(img.trace.toSVG()).toMatch(/<svg.*viewBox="0 0 100 100".*<\/svg>/);
3636
});
37+
38+
it('has meta-data', () => {
39+
const img = subject();
40+
expect(img.diff).toEqual(0.1991598705880487);
41+
expect(img.maxDiff).toEqual(1);
42+
});

src/createDiffImage.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module.exports = function createDiffImage({ image1Data, image2Data }) {
1515
const data = new Uint8ClampedArray(width * height);
1616
const trace = new DiffTrace({ width, height });
1717
let totalDiff = 0;
18+
let maxDiff = 0;
1819

1920
for (let row = 0; row < height; row += 1) {
2021
// Render image
@@ -35,6 +36,9 @@ module.exports = function createDiffImage({ image1Data, image2Data }) {
3536
);
3637

3738
totalDiff += diff;
39+
if (diff > maxDiff) {
40+
maxDiff = diff;
41+
}
3842

3943
/* eslint-disable prefer-destructuring */
4044
if (diff > 0) {
@@ -58,7 +62,8 @@ module.exports = function createDiffImage({ image1Data, image2Data }) {
5862
}
5963

6064
return {
61-
diff: totalDiff / (width + height),
65+
diff: totalDiff / (width * height),
66+
maxDiff,
6267
trace,
6368
data,
6469
width: width / 4,

src/euclideanDistance.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
const MAX_EUCLIDEAN_DISTANCE = Math.sqrt(255 ** 2 * 4);
12

23
function euclideanDistance(rgba1, rgba2) {
3-
return Math.sqrt(((rgba1[0] - rgba2[0]) ** 2)
4-
+ ((rgba1[1] - rgba2[1]) ** 2)
5-
+ ((rgba1[2] - rgba2[2]) ** 2)
6-
+ ((rgba1[3] - rgba2[3]) ** 2));
4+
return (
5+
Math.sqrt(
6+
(rgba1[0] - rgba2[0]) ** 2 +
7+
(rgba1[1] - rgba2[1]) ** 2 +
8+
(rgba1[2] - rgba2[2]) ** 2 +
9+
(rgba1[3] - rgba2[3]) ** 2,
10+
) / MAX_EUCLIDEAN_DISTANCE
11+
);
712
}
813

9-
euclideanDistance.MAX_EUCLIDEAN_DISTANCE = Math.sqrt((255 ** 2) * 4);
10-
1114
module.exports = euclideanDistance;

src/getDiffPixel.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const compose = require('./compose');
22
const euclideanDistance = require('./euclideanDistance');
3-
const { MAX_EUCLIDEAN_DISTANCE } = require('./euclideanDistance');
43

54
const TRANSPARENT = [0, 0, 0, 0];
65

@@ -15,7 +14,7 @@ module.exports = function getDiffPixel(previousPixel, currentPixel) {
1514
// Delta E in Lab colorspace, we probably don't need perceptual accuracy for
1615
// this application, and it is nice to avoid the overhead of converting RGBA
1716
// to Lab.
18-
const diff = euclideanDistance(previousPixel, currentPixel) / MAX_EUCLIDEAN_DISTANCE;
17+
const diff = euclideanDistance(previousPixel, currentPixel);
1918
if (diff === 0) {
2019
if (currentPixel[3] === 0) {
2120
return {

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ function imageDiff(image1, image2, { hashFunction } = {}) {
99
hashFunction,
1010
});
1111

12-
const { data, width, height, diff, trace } = createDiffImage({
12+
const { data, width, height, diff, trace, maxDiff } = createDiffImage({
1313
image1Data,
1414
image2Data,
1515
});
16-
return { data, width, height, diff, trace };
16+
return { data, width, height, diff, trace, maxDiff };
1717
}
1818

1919
imageDiff.DIFF_TRACE_PADDING = DIFF_TRACE_PADDING;

0 commit comments

Comments
 (0)