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

Add new option for ignored regions #122

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 60 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const defaultOptions = {
aaColor: [255, 255, 0], // color of anti-aliased pixels in diff output
diffColor: [255, 0, 0], // color of different pixels in diff output
diffColorAlt: null, // whether to detect dark on light differences between img1 and img2 and set an alternative color to differentiate between the two
diffMask: false // draw the diff over a transparent background (a mask)
diffMask: false, // draw the diff over a transparent background (a mask)
ignoredRegions: null, // array of {x1,y1,x2,y2} objects representing image regions to ignore from checking
ignoredColor: null, // color of ignored region
};

function pixelmatch(img1, img2, output, width, height, options) {
Expand Down Expand Up @@ -50,9 +52,45 @@ function pixelmatch(img1, img2, output, width, height, options) {
for (let x = 0; x < width; x++) {

const pos = (y * width + x) * 4;
if (isPixelIgnored(x, y, options.ignoredRegions)) {
if (output && !options.diffMask) {

if (options.ignoredColor) {
drawPixel(output, pos, ...options.ignoredColor);
} else {
drawGrayPixel(img1, pos, options.alpha, output);
}
}
continue;
}

// 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 Expand Up @@ -234,3 +272,23 @@ function drawGrayPixel(img, i, alpha, output) {
const val = blend(rgb2y(r, g, b), alpha * img[i + 3] / 255);
drawPixel(output, i, val, val, val);
}

function isPixelIgnored(x, y, ignoredRegions) {
if (ignoredRegions == null) {
return false;
}
for (const region of ignoredRegions) {
if (isPixelInRegion(x, y, region)) {
return true;
}
}
return false;
}

function isPixelInRegion(x, y, region) {
const {x1, y1, x2, y2} = region;
if (isNaN(x1) || isNaN(y1) || isNaN(x2) || isNaN(y2)) {
return false;
}
return x1 <= x && x <= x2 && y1 <= y && y <= y2;
}
Binary file added test/fixtures/2diff_ignored_regions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,24 @@ diffTest('2a', '2b', '2diff', {
aaColor: [0, 192, 0],
diffColor: [255, 0, 255]
}, 12437);
diffTest('2a', '2b', '2diff_ignored_regions', {
threshold: 0.05,
ignoredRegions: [
{
x1: 128,
y1: 0,
x2: 256,
y2: 128,
},
{
x1: 0,
y1: 128,
x2: 128,
y2: 256
}
],
ignoredColor: [0, 255, 255]
}, 2229);
diffTest('3a', '3b', '3diff', options, 212);
diffTest('4a', '4b', '4diff', options, 36049);
diffTest('5a', '5b', '5diff', options, 0);
Expand Down