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

Loading…
Cancel
Save