Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: global tolerance value #18

Open
cburgmer opened this issue Oct 23, 2013 · 3 comments
Open

Feature request: global tolerance value #18

cburgmer opened this issue Oct 23, 2013 · 3 comments

Comments

@cburgmer
Copy link
Contributor

I have a use case where I'd like to compare images using a global tolerance aka "the images are less than 5% different". So far equal only accounts for a threshold on a per color and pixel value.

The implementation is probably fairly trivial, I'd see the difficulty in finding a meaningful way of integrating that.

Would that feature make sense here?

@cesutherland
Copy link
Member

I think it would make sense. To do so, I'd like to see toImageDiffEqual(expected, tolerance) implemented as toImageDiffEqual(expected, options).

With an options configuration like:

{
  tolerance : 0.05,
  globalTolerance : true
}

Or something along those lines.

@cburgmer
Copy link
Contributor Author

Would you then also change the unit of tolerance for the per-pixel basis? Right now it is measured in pixels.

@janouma
Copy link

janouma commented Sep 17, 2014

Here is how I see it:

...

function equal(a, b, tolerance, errorPercentageAllowed) {

    var
        aData = a.data,
        bData = b.data,
        length = aData.length,
        i,
        mismatchesCount = 0,
        errorPercentage;

    tolerance = tolerance || 0;
    errorPercentageAllowed = errorPercentageAllowed || 0;

    if (!equalDimensions(a, b)) return false;

    for (i = length; i--;) {
        if (aData[i] !== bData[i] && Math.abs(aData[i] - bData[i]) > tolerance) {
            mismatchesCount++;
        }
    }

    errorPercentage = (mismatchesCount / length) * 100;

    if (errorPercentage > errorPercentageAllowed) {
        console.log('mismatchesCount: ' + mismatchesCount);
        console.log('length: ' + length);
        console.log(errorPercentage + ' % mismatches encountered');
        return false;
    } else {
        return true;
    }
}

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants