Skip to content

Commit d003ccf

Browse files
committed
Assume background color when adding padding
When two images aren't matching sizes, we add placeholder transparent pixels. This works in many cases, but when the two images are very different, the diff tends to put a giant block of magenta either at the top or the bottom, hiding "real" content underneath. To fix this, I'm making the placeholder transparent pixels grayish, blended with the background color, which is assumed to be the top leftmost pixel. The assumption about the top left pixel could prove too naive, but for now it's better than the previous approach which included making a transparent black pixel.
1 parent 516959b commit d003ccf

File tree

4 files changed

+26
-22
lines changed

4 files changed

+26
-22
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

src/computeAndInjectDiffs.js

Lines changed: 26 additions & 22 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,9 +9,9 @@ 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
}
@@ -40,40 +41,39 @@ function hashFn() {
4041

4142
const HASH_FN = hashFn();
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);

0 commit comments

Comments
 (0)