Skip to content

Commit 5192b82

Browse files
tmeasdaymourner
authored andcommitted
Add diffMask option (#73)
To draw diff over a transparent background.
1 parent 577f336 commit 5192b82

5 files changed

Lines changed: 9 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Implements ideas from the following papers:
4949
- `alpha` — Blending factor of unchanged pixels in the diff output. Ranges from `0` for pure white to `1` for original brightness. `0.1` by default.
5050
- `aaColor` — The color of anti-aliased pixels in the diff output in `[R, G, B]` format. `[255, 255, 0]` by default.
5151
- `diffColor` — The color of differing pixels in the diff output in `[R, G, B]` format. `[255, 0, 0]` by default.
52+
- `diffMask` — Draw the diff over a transparent background (a mask), rather than over the original image. Will not draw anti-aliased pixels (if detected).
5253

5354
Compares two images, writes the output diff and returns the number of mismatched pixels.
5455

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ const defaultOptions = {
77
includeAA: false, // whether to skip anti-aliasing detection
88
alpha: 0.1, // opacity of original image in diff ouput
99
aaColor: [255, 255, 0], // color of anti-aliased pixels in diff output
10-
diffColor: [255, 0, 0] // color of different pixels in diff output
10+
diffColor: [255, 0, 0], // color of different pixels in diff output
11+
diffMask: false // draw the diff over a transparent background (a mask)
1112
};
1213

1314
function pixelmatch(img1, img2, output, width, height, options) {
@@ -32,7 +33,7 @@ function pixelmatch(img1, img2, output, width, height, options) {
3233
if (a32[i] !== b32[i]) { identical = false; break; }
3334
}
3435
if (identical) { // fast path if identical
35-
if (output) {
36+
if (output && !options.diffMask) {
3637
for (let i = 0; i < len; i++) drawGrayPixel(img1, 4 * i, options.alpha, output);
3738
}
3839
return 0;
@@ -61,7 +62,8 @@ function pixelmatch(img1, img2, output, width, height, options) {
6162
if (!options.includeAA && (antialiased(img1, x, y, width, height, img2) ||
6263
antialiased(img2, x, y, width, height, img1))) {
6364
// one of the pixels is anti-aliasing; draw as yellow and do not count as difference
64-
if (output) drawPixel(output, pos, aaR, aaG, aaB);
65+
// note that we do not include such pixels in a mask
66+
if (output && !options.diffMask) drawPixel(output, pos, aaR, aaG, aaB);
6567

6668
} else {
6769
// found substantial difference not caused by anti-aliasing; draw it as red
@@ -71,7 +73,7 @@ function pixelmatch(img1, img2, output, width, height, options) {
7173

7274
} else if (output) {
7375
// pixels are similar; draw background as grayscale image blended with white
74-
drawGrayPixel(img1, pos, options.alpha, output);
76+
if (!options.diffMask) drawGrayPixel(img1, pos, options.alpha, output);
7577
}
7678
}
7779
}

test/fixtures/1diffmask.png

805 Bytes
Loading

test/fixtures/1emptydiffmask.png

588 Bytes
Loading

test/test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const match = require('../.');
99
const options = {threshold: 0.05};
1010

1111
diffTest('1a', '1b', '1diff', options, 143);
12+
diffTest('1a', '1b', '1diffmask', {threshold: 0.05, includeAA: false, diffMask: true}, 143);
13+
diffTest('1a', '1a', '1emptydiffmask', {threshold: 0, diffMask: true}, 0);
1214
diffTest('2a', '2b', '2diff', {
1315
threshold: 0.05,
1416
alpha: 0.5,

0 commit comments

Comments
 (0)