Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Horizontal/Vertical shift threshold #106

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
29 changes: 28 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module.exports = pixelmatch;

const defaultOptions = {
threshold: 0.1, // matching threshold (0 to 1); smaller is more sensitive
horizontalShiftPixels: 0, // Check matches within X many pixels of current pixel
verticalShiftPixels: 0, // Check matches within Y many pixels of current pixel
includeAA: false, // whether to skip anti-aliasing detection
alpha: 0.1, // opacity of original image in diff output
aaColor: [255, 255, 0], // color of anti-aliased pixels in diff output
Expand Down Expand Up @@ -52,7 +54,32 @@ function pixelmatch(img1, img2, output, width, height, options) {
const pos = (y * width + x) * 4;

// squared YUV distance between colors at this pixel position, negative if the img2 pixel is darker
const delta = colorDelta(img1, img2, pos, pos);
let delta = colorDelta(img1, img2, pos, pos);
if ((delta > maxDelta || delta < -1 * maxDelta) && (options.horizontalShiftPixels > 0 || options.verticalShiftPixels > 0)) {
let minAbsDelta = 9999;
let minOtherDelta = 9999;
for (let hShift = -1 * options.horizontalShiftPixels; hShift <= options.horizontalShiftPixels; ++hShift) {
for (let vShift = -1 * options.verticalShiftPixels; vShift <= options.verticalShiftPixels; ++vShift) {
if (x + hShift < 0 || x + hShift > width || y + vShift < 0 || y + vShift > height) {
//Ignore shifts of pixels outside the image
continue;
}
const currDelta = colorDelta(img1, img2, pos, pos + ((width * vShift) + hShift) * 4);
if (Math.abs(currDelta) < Math.abs(minAbsDelta)) {
minAbsDelta = currDelta;
}
const otherDelta = colorDelta(img1, img2, pos + ((width * vShift) + hShift) * 4, pos);
if (Math.abs(otherDelta) < Math.abs(minOtherDelta)) {
minOtherDelta = otherDelta;
}
}
}
if (Math.abs(minAbsDelta) > Math.abs(minOtherDelta)) {
delta = minAbsDelta;
} else {
delta = minOtherDelta;
}
}

// the color difference is above the threshold
if (Math.abs(delta) > maxDelta) {
Expand Down
Binary file added test/fixtures/8a.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/8b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/8diff.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/fixtures/8empty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ diffTest('5a', '5b', '5diff', options, 0);
diffTest('6a', '6b', '6diff', options, 51);
diffTest('6a', '6a', '6empty', {threshold: 0}, 0);
diffTest('7a', '7b', '7diff', {diffColorAlt: [0, 255, 0]}, 2448);
diffTest('8a', '8b', '8diff', {threshold: 0.1}, 2944);
diffTest('8a', '8b', '8empty', {horizontalShiftPixels: 7, verticalShiftPixels: 6, threshold: 0.1}, 0);

test('throws error if image sizes do not match', (t) => {
t.throws(() => match(new Uint8Array(8), new Uint8Array(9), null, 2, 1), 'Image sizes do not match');
Expand Down