From 4f37b8f9e511a15172733b9365000cb146d7825e Mon Sep 17 00:00:00 2001 From: Rommel Santor Date: Fri, 27 Jul 2018 07:04:26 -0700 Subject: [PATCH] Add misMatchThreshold option to short-circuit and stop comparing if determined not a match --- resemble.js | 73 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/resemble.js b/resemble.js index a3a71e3..eb41d89 100644 --- a/resemble.js +++ b/resemble.js @@ -170,6 +170,8 @@ URL: https://github.com/Huddle/Resemble.js var ignoreAntialiasing = false; var ignoreColors = false; var scaleToSameSize = false; + var compareOnly = false; + var misMatchThreshold = null; function triggerDataUpdate() { var len = updateCallbackArray.length; @@ -513,17 +515,19 @@ URL: https://github.com/Huddle/Resemble.js } function analyseImages(img1, img2, width, height) { - var hiddenCanvas = document.createElement("canvas"); + var hiddenCanvas = !compareOnly ? document.createElement("canvas") : null; + + if (!compareOnly) { + hiddenCanvas.width = width; + hiddenCanvas.height = height; + } var data1 = img1.data; var data2 = img2.data; - hiddenCanvas.width = width; - hiddenCanvas.height = height; - - var context = hiddenCanvas.getContext("2d"); - var imgd = context.createImageData(width, height); - var pix = imgd.data; + var context = !compareOnly ? hiddenCanvas.getContext("2d") : null; + var imgd = !compareOnly ? context.createImageData(width, height) : null; + var pix = !compareOnly ? imgd.data : null; var mismatchCount = 0; var diffBounds = { @@ -554,7 +558,13 @@ URL: https://github.com/Huddle/Resemble.js var pixel1 = { r: 0, g: 0, b: 0, a: 0 }; var pixel2 = { r: 0, g: 0, b: 0, a: 0 }; + var skipTheRest = false; + loop(width, height, function(horizontalPos, verticalPos) { + if (skipTheRest) { + return + } + if (skip) { // only skip if the image isn't small if ( @@ -588,9 +598,14 @@ URL: https://github.com/Huddle/Resemble.js isPixelBrightnessSimilar(pixel1, pixel2) || !isWithinComparedArea ) { - copyGrayScalePixel(pix, offset, pixel2); + if (!compareOnly) { + copyGrayScalePixel(pix, offset, pixel2); + } } else { - errorPixel(pix, offset, pixel1, pixel2); + if (!compareOnly) { + errorPixel(pix, offset, pixel1, pixel2); + } + mismatchCount++; updateBounds(horizontalPos, verticalPos); } @@ -598,7 +613,9 @@ URL: https://github.com/Huddle/Resemble.js } if (isRGBSimilar(pixel1, pixel2) || !isWithinComparedArea) { - copyPixel(pix, offset, pixel1); + if (!compareOnly) { + copyPixel(pix, offset, pixel1); + } } else if ( ignoreAntialiasing && (addBrightnessInfo(pixel1), // jit pixel info augmentation looks a little weird, sorry. @@ -624,17 +641,33 @@ URL: https://github.com/Huddle/Resemble.js isPixelBrightnessSimilar(pixel1, pixel2) || !isWithinComparedArea ) { - copyGrayScalePixel(pix, offset, pixel2); + if (!compareOnly) { + copyGrayScalePixel(pix, offset, pixel2); + } } else { - errorPixel(pix, offset, pixel1, pixel2); + if (!compareOnly) { + errorPixel(pix, offset, pixel1, pixel2); + } + mismatchCount++; updateBounds(horizontalPos, verticalPos); } } else { - errorPixel(pix, offset, pixel1, pixel2); + if (!compareOnly) { + errorPixel(pix, offset, pixel1, pixel2); + } + mismatchCount++; updateBounds(horizontalPos, verticalPos); } + + if (compareOnly) { + var currentMisMatchPercent = mismatchCount / (height * width) * 100; + + if (currentMisMatchPercent > misMatchThreshold) { + skipTheRest = true; + } + } }); data.rawMisMatchPercentage = mismatchCount / (height * width) * 100; @@ -643,6 +676,10 @@ URL: https://github.com/Huddle/Resemble.js data.analysisTime = Date.now() - time; data.getImageDataUrl = function(text) { + if (compareOnly) { + throw Error('No diff image available - ran in compareOnly mode') + } + var barHeight = 0; if (text) { @@ -654,7 +691,7 @@ URL: https://github.com/Huddle/Resemble.js return hiddenCanvas.toDataURL("image/png"); }; - if (hiddenCanvas.toBuffer) { + if (!compareOnly && hiddenCanvas.toBuffer) { data.getBuffer = function(includeOriginal) { if (includeOriginal) { var imageWidth = hiddenCanvas.width + 2; @@ -826,6 +863,10 @@ URL: https://github.com/Huddle/Resemble.js } var self = { + comparisonLimit: function(percent) { + compareOnly = true; + misMatchThreshold = percent; + }, scaleToSameSize: function() { scaleToSameSize = true; @@ -1006,6 +1047,10 @@ URL: https://github.com/Huddle/Resemble.js compare = res.compareTo(image2); + if (opt.misMatchThreshold) { + compare.comparisonLimit(opt.misMatchThreshold); + } + if (opt.scaleToSameSize) { compare.scaleToSameSize(); }