From 25e4eece171cd056cb2d8d47f07403551975c41a Mon Sep 17 00:00:00 2001 From: Kamil Bielawski Date: Wed, 30 Aug 2017 00:17:24 +0200 Subject: [PATCH] Allow specifying bounding box for comparison --- resemble.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/resemble.js b/resemble.js index 78d4e12..6952ecc 100644 --- a/resemble.js +++ b/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; };