Skip to content

Commit 5ac5d74

Browse files
authored
Merge pull request #27 from happo/assumed-background
Assumed background
2 parents 516959b + c0ef7e0 commit 5ac5d74

File tree

11 files changed

+61
-34
lines changed

11 files changed

+61
-34
lines changed

snapshots/different-widths/after.png

179 KB
Loading

snapshots/different-widths/before.png

86.6 KB
Loading

snapshots/different-widths/diff.png

99 KB
Loading

snapshots/logo/diff.png

102 Bytes
Loading

snapshots/long-example/diff.png

1.78 KB
Loading
5 Bytes
Loading
1 Byte
Loading

src/__tests__/createDiffImage-test.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ let image2;
1111
let subject;
1212

1313
beforeEach(async () => {
14-
image1 = (await Jimp.read('http://localhost:5411/aa-ffffff.png')).bitmap;
15-
image2 = (await Jimp.read('http://localhost:5411/aa-f7f7f7.png')).bitmap;
14+
image1 = (await Jimp.read('http://127.0.0.1:5411/aa-ffffff.png')).bitmap;
15+
image2 = (await Jimp.read('http://127.0.0.1:5411/aa-f7f7f7.png')).bitmap;
1616
subject = () =>
1717
createDiffImage(
1818
computeAndInjectDiffs({
@@ -30,8 +30,8 @@ it('has a total diff value and a max diff', async () => {
3030

3131
describe('when images are of different width', () => {
3232
beforeEach(async () => {
33-
image1 = (await Jimp.read('http://localhost:5411/alert-before.png')).bitmap;
34-
image2 = (await Jimp.read('http://localhost:5411/alert-after.png')).bitmap;
33+
image1 = (await Jimp.read('http://127.0.0.1:5411/alert-before.png')).bitmap;
34+
image2 = (await Jimp.read('http://127.0.0.1:5411/alert-after.png')).bitmap;
3535
});
3636

3737
it('has a total diff and a max diff', async () => {
@@ -43,13 +43,13 @@ describe('when images are of different width', () => {
4343

4444
describe('when images are of different height', () => {
4545
beforeEach(async () => {
46-
image1 = (await Jimp.read('http://localhost:5411/button-before.png')).bitmap;
47-
image2 = (await Jimp.read('http://localhost:5411/button-after.png')).bitmap;
46+
image1 = (await Jimp.read('http://127.0.0.1:5411/button-before.png')).bitmap;
47+
image2 = (await Jimp.read('http://127.0.0.1:5411/button-after.png')).bitmap;
4848
});
4949

5050
it('has a total diff and a max diff', async () => {
5151
const { diff, maxDiff } = await subject();
52-
expect(diff).toEqual(0.0078125);
53-
expect(maxDiff).toEqual(1);
52+
expect(diff).toBeTruthy()
53+
expect(maxDiff).toBeTruthy();
5454
});
5555
});

src/__tests__/index-test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ it('produces a trace svg', () => {
3737

3838
it('has meta-data', () => {
3939
const { diff, maxDiff } = subject();
40-
expect(diff).toEqual(0.20505992912433182);
40+
expect(diff).toBeLessThan(0.3);
41+
expect(maxDiff).toEqual(1);
42+
});
43+
44+
it('has maxDiff=1 when images are of different size', async () => {
45+
image1 = (await Jimp.read(
46+
path.resolve(__dirname, 'test-images/button-before.png'),
47+
)).bitmap;
48+
image2 = (await Jimp.read(
49+
path.resolve(__dirname, 'test-images/button-after.png'),
50+
)).bitmap;
51+
const { diff, maxDiff } = subject();
52+
expect(diff).toBeLessThan(1);
4153
expect(maxDiff).toEqual(1);
4254
});

src/computeAndInjectDiffs.js

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const alignArrays = require('./alignArrays');
2+
const compose = require('./compose');
23
const similarEnough = require('./similarEnough');
34

45
function imageTo2DArray({ data, width, height }, paddingRight) {
@@ -8,16 +9,16 @@ function imageTo2DArray({ data, width, height }, paddingRight) {
89

910
const newData = [];
1011
for (let row = 0; row < height; row += 1) {
11-
const pixelsInRow = new Uint8ClampedArray(rowSize + (paddingRight * 4));
12+
const pixelsInRow = new Uint8ClampedArray(rowSize + paddingRight * 4);
1213
for (let location = 0; location < rowSize; location += 1) {
13-
pixelsInRow[location] = data[(row * rowSize) + location];
14+
pixelsInRow[location] = data[row * rowSize + location];
1415
}
1516
newData.push(pixelsInRow);
1617
}
1718
return newData;
1819
}
1920

20-
function hashFn() {
21+
function resolveHashFn() {
2122
// Safari has a bug where trying to reference `btoa` inside a web worker will
2223
// result in an error, so we fall back to the slower (?) `JSON.stringify`. The
2324
// only way to prevent this seems to be by using a try/catch. We do this in its
@@ -38,42 +39,41 @@ function hashFn() {
3839
}
3940
}
4041

41-
const HASH_FN = hashFn();
42+
const HASH_FN = resolveHashFn();
4243

43-
function align({
44-
image1Data,
45-
image2Data,
46-
maxWidth,
47-
hashFunction,
48-
}) {
44+
function transparentLine(rawBgPixel, width) {
45+
const bgPixel = compose([200, 200, 200, 50], rawBgPixel);
46+
const result = new Uint8ClampedArray(width * 4);
47+
for (let i = 0; i < width * 4; i += 4) {
48+
result[i] = bgPixel[0];
49+
result[i + 1] = bgPixel[1];
50+
result[i + 2] = bgPixel[2];
51+
result[i + 3] = 122;
52+
}
53+
return result;
54+
}
55+
56+
function align({ image1Data, image2Data, maxWidth, hashFunction }) {
4957
if (similarEnough({ image1Data, image2Data })) {
5058
return;
5159
}
5260

5361
const hashedImage1Data = image1Data.map(hashFunction);
5462
const hashedImage2Data = image2Data.map(hashFunction);
5563

56-
alignArrays(
57-
hashedImage1Data,
58-
hashedImage2Data,
59-
);
60-
61-
const transparentLine = new Uint8ClampedArray(maxWidth * 4);
62-
for (let i = 0; i < maxWidth * 4; i++) {
63-
if ((i + 1) % 4 !== 0) {
64-
transparentLine[i] = 1;
65-
}
66-
}
64+
alignArrays(hashedImage1Data, hashedImage2Data);
6765

66+
const image1Bg = image1Data[0].slice(0, 4);
6867
hashedImage1Data.forEach((hashedLine, i) => {
6968
if (hashedLine === alignArrays.PLACEHOLDER) {
70-
image1Data.splice(i, 0, transparentLine);
69+
image1Data.splice(i, 0, transparentLine(image1Bg, maxWidth));
7170
}
7271
});
7372

73+
const image2Bg = image2Data[0].slice(0, 4);
7474
hashedImage2Data.forEach((hashedLine, i) => {
7575
if (hashedLine === alignArrays.PLACEHOLDER) {
76-
image2Data.splice(i, 0, transparentLine);
76+
image2Data.splice(i, 0, transparentLine(image2Bg, maxWidth));
7777
}
7878
});
7979
}
@@ -90,7 +90,11 @@ function align({
9090
* @param {Array} image2
9191
* @return {Object}
9292
*/
93-
module.exports = function computeAndInjectDiffs({ image1, image2, hashFunction = HASH_FN }) {
93+
module.exports = function computeAndInjectDiffs({
94+
image1,
95+
image2,
96+
hashFunction = HASH_FN,
97+
}) {
9498
const maxWidth = Math.max(image1.width, image2.width);
9599

96100
const image1Data = imageTo2DArray(image1, maxWidth - image1.width);

src/index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,18 @@ function imageDiff(image1, image2, { hashFunction } = {}) {
1313
image1Data,
1414
image2Data,
1515
});
16-
return { data, width, height, diff, trace, maxDiff };
16+
17+
const differentDimensions =
18+
image1.width !== image2.width || image1.height !== image2.height;
19+
20+
return {
21+
data,
22+
width,
23+
height,
24+
diff,
25+
trace,
26+
maxDiff: differentDimensions ? 1 : maxDiff,
27+
};
1728
}
1829

1930
imageDiff.DIFF_TRACE_PADDING = DIFF_TRACE_PADDING;

0 commit comments

Comments
 (0)