Browse Source

Allow specifying bounding box for comparison

pull/103/head
Kamil Bielawski 7 years ago
parent
commit
25e4eece17
  1. 23
      resemble.js

23
resemble.js

@ -29,6 +29,17 @@ URL: https://github.com/Huddle/Resemble.js
return (Math.abs(c1.r - c2.r) + Math.abs(c1.g - c2.g) + Math.abs(c1.b - c2.b))/3;
}
function withinBoundingBox(x, y) {
if (boundingBox === null) {
return true;
}
return x > boundingBox.x1 &&
x < boundingBox.x2 &&
y > boundingBox.y1 &&
y < boundingBox.y2;
}
var errorPixelTransform = {
flat: function (px, offset, d1, d2) {
px[offset] = errorPixelColor.red;
@ -59,6 +70,7 @@ URL: https://github.com/Huddle/Resemble.js
};
var errorPixel = errorPixelTransform.flat;
var boundingBox = null;
var largeImageThreshold = 1200;
var useCrossOrigin = true;
var document = typeof window != "undefined" ? window.document : {};
@ -421,6 +433,7 @@ URL: https://github.com/Huddle/Resemble.js
}
var offset = (verticalPos*width + horizontalPos) * 4;
var isWithinBoundingBox = withinBoundingBox(horizontalPos, verticalPos);
if (!getPixelInfo(pixel1, data1, offset, 1) || !getPixelInfo(pixel2, data2, offset, 2)) {
return;
@ -431,7 +444,7 @@ URL: https://github.com/Huddle/Resemble.js
addBrightnessInfo(pixel1);
addBrightnessInfo(pixel2);
if( isPixelBrightnessSimilar(pixel1, pixel2) ){
if( isPixelBrightnessSimilar(pixel1, pixel2) || !isWithinBoundingBox ){
copyGrayScalePixel(targetPix, offset, pixel2);
} else {
errorPixel(targetPix, offset, pixel1, pixel2);
@ -441,7 +454,7 @@ URL: https://github.com/Huddle/Resemble.js
return;
}
if( isRGBSimilar(pixel1, pixel2) ){
if( isRGBSimilar(pixel1, pixel2) || !isWithinBoundingBox ){
copyPixel(targetPix, offset, pixel1, pixel2);
} else if( ignoreAntialiasing && (
@ -451,7 +464,7 @@ URL: https://github.com/Huddle/Resemble.js
isAntialiased(pixel2, data2, 2, verticalPos, horizontalPos, width)
)){
if( isPixelBrightnessSimilar(pixel1, pixel2) ){
if( isPixelBrightnessSimilar(pixel1, pixel2) || !isWithinBoundingBox ){
copyGrayScalePixel(targetPix, offset, pixel2);
} else {
errorPixel(targetPix, offset, pixel1, pixel2);
@ -704,6 +717,10 @@ URL: https://github.com/Huddle/Resemble.js
useCrossOrigin = options.useCrossOrigin;
}
if (options.boundingBox !== undefined) {
boundingBox = options.boundingBox;
}
return this;
};

Loading…
Cancel
Save