Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add ignoreAreasColoredWith option support
  • Loading branch information
kamilbielawski committed Jun 30, 2019
1 parent db6f0b8 commit 850a2e4
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 10 deletions.
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -123,6 +123,19 @@ resemble.outputSettings({ ignoredBox: box });
resemble.outputSettings({ ignoredBoxes: [box1, box2] });
```

Another way to exclude parts of the image from comparison, is using the `ignoreAreasColoredWith` option.
Any pixels that match the specified color on a reference image will be excluded from comparison:

```javascript
const color = {
r: 255,
g: 0,
b: 0,
a: 255
};
resemble.outputSettings({ ignoreAreasColoredWith: color });
```

By default, the comparison algorithm skips pixels when the image width or height is larger than 1200 pixels. This is there to mitigate performance issues.

You can modify this behaviour by setting the `largeImageThreshold` option to a different value. Set it to **0** to switch it off completely.
Expand Down
15 changes: 13 additions & 2 deletions demoassets/main.js
Expand Up @@ -217,8 +217,7 @@ $(function() {
})
.repaint();
$this.removeClass("active");
}
if ($this.is("#ignoredBox")) {
} else if ($this.is("#ignoredBox")) {
resembleControl
.outputSettings({
ignoredBox: {
Expand All @@ -230,6 +229,18 @@ $(function() {
})
.repaint();
$this.removeClass("active");
} else if ($this.is("#ignoredColor")) {
resembleControl
.outputSettings({
ignoreAreasColoredWith: {
r: parseInt($("#ignored-color-r").val()),
g: parseInt($("#ignored-color-g").val()),
b: parseInt($("#ignored-color-b").val()),
a: parseInt($("#ignored-color-a").val())
}
})
.repaint();
$this.removeClass("active");
}
});

Expand Down
28 changes: 28 additions & 0 deletions index.html
Expand Up @@ -191,6 +191,34 @@ <h2>Compare two images?</h2>
</div>
</div>

<br/>
<br/>

<div class="btn-group buttons" style="display:none">
<div class="row">
<div class="span1">
<label>Red</label>
<input type="number" class="input-mini" id="ignored-color-r" min="0" max="255" value="255" />
</div>
<div class="span1">
<label>Green</label>
<input type="number" class="input-mini" id="ignored-color-g" min="0" max="255" value="0" />
</div>
<div class="span1">
<label>Blue</label>
<input type="number" class="input-mini" id="ignored-color-b" min="0" max="255" value="0" />
</div>
<div class="span1">
<label>Alpha</label>
<input type="number" class="input-mini" id="ignored-color-a" min="0" max="255" value="255" />
</div>
<div class="span2">
<label>&nbsp;</label>
<button class="btn" id="ignoredColor">Set ignored color</button>
</div>
</div>
</div>

<br/>
<br/>
<div id="diff-results" style="display:none;">
Expand Down
Binary file added nodejs-tests/assets/PeopleWithIgnoreMask.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added nodejs-tests/assets/ignoredColorTestResult.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions nodejs-tests/resemble.test.js
Expand Up @@ -264,4 +264,37 @@ describe("resemble", () => {
});
});
});

test("partial diff with ignored color", () => {
const peopleSrc = `data:image/jpeg;base64,${fs.readFileSync(
"./nodejs-tests/assets/PeopleWithIgnoreMask.png",
"base64"
)}`;
const people2Src = `data:image/jpeg;base64,${fs.readFileSync(
"./demoassets/People2.jpg",
"base64"
)}`;

return new Promise(resolve => {
resemble.outputSettings({
ignoreAreasColoredWith: {
r: 255,
g: 0,
b: 0,
a: 255
}
});

resemble(people2Src)
.compareTo(peopleSrc)
.onComplete(data => {
const buffer = data.getBuffer();
const comparison = fs.readFileSync(
"./nodejs-tests/assets/ignoredColorTestResult.png"
);
expect(buffer.equals(comparison)).toBe(true);
resolve();
});
});
});
});
26 changes: 18 additions & 8 deletions resemble.js
Expand Up @@ -111,6 +111,7 @@ var isNode = new Function(
var errorType;
var boundingBoxes;
var ignoredBoxes;
var ignoreAreasColoredWith;
var largeImageThreshold = 1200;
var useCrossOrigin = true;
var data = {};
Expand Down Expand Up @@ -151,7 +152,7 @@ var isNode = new Function(
);
}

function withinComparedArea(x, y, width, height) {
function withinComparedArea(x, y, width, height, pixel2) {
var isIncluded = true,
i,
boundingBox,
Expand Down Expand Up @@ -180,6 +181,10 @@ var isNode = new Function(
}
}

if (ignoreAreasColoredWith) {
return colorsDistance(pixel2, ignoreAreasColoredWith) !== 0;
}

if (selected === undefined && ignored === undefined) {
return true;
}
Expand Down Expand Up @@ -616,20 +621,21 @@ var isNode = new Function(
}

var offset = (verticalPos * width + horizontalPos) * 4;
var isWithinComparedArea = withinComparedArea(
horizontalPos,
verticalPos,
width,
height
);

if (
!getPixelInfo(pixel1, data1, offset, 1) ||
!getPixelInfo(pixel2, data2, offset, 2)
) {
return;
}

var isWithinComparedArea = withinComparedArea(
horizontalPos,
verticalPos,
width,
height,
pixel2
);

if (ignoreColors) {
addBrightnessInfo(pixel1);
addBrightnessInfo(pixel2);
Expand Down Expand Up @@ -845,6 +851,10 @@ var isNode = new Function(
if (options.ignoredBoxes !== undefined) {
ignoredBoxes = options.ignoredBoxes;
}

if (options.ignoreAreasColoredWith !== undefined) {
ignoreAreasColoredWith = options.ignoreAreasColoredWith;
}
}

function compare(one, two) {
Expand Down

0 comments on commit 850a2e4

Please sign in to comment.