Skip to content
This repository has been archived by the owner on Feb 18, 2021. It is now read-only.

add option 'threshold' representing % of different pixels (number between 0 and 100) #15

Closed
wants to merge 8 commits into from

Conversation

nikchosh
Copy link

Threshold should be a value between 0 and 100 (% of pixels that are
different). Should take into account that compare.exe sometimes returns normalized numbers of kind 9.9999e-999)

Threshold should be a value between 0 and 1 (% of pixels that are
different). DiffPath is the path and name of the diff image. Useful, if
user wants to remove identical diff images.
@@ -73,7 +73,7 @@ ImageDiff.createDiff = function (options, cb) {
// DEV: According to http://www.imagemagick.org/discourse-server/viewtopic.php?f=1&t=17284
// DEV: These values are the total square root mean square (RMSE) pixel difference across all pixels and its percentage
// TODO: This is not very multi-lengual =(
var resultInfo = stderr.match(/all: (\d+\.?\d*) \((\d+\.?\d*)\)/);
var resultInfo = stderr.match(/all: (\d+\.?\d*) \((.*?)\)/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We were intentionally keeping the regexp sniffing digits to keep it as efficient as possible. Please move back to the previous version.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember why I changed this - in case of only a few pixel difference, we get a result like this from compare.exe:
Channel distortion: RMSE
red: 0.00129297 (5.07046e-006)
green: 0 (0)
blue: 0.00158356 (6.21002e-006)
all: 0.00118031 (4.62868e-006)
So, we can either use (. * ) or /\d+.?\d * (e-\d+)?/ to allow for something small

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting. We should probably isolate the parser to a separate function so we can test these individually. I imagine it will become very tedious to create a set of separate images for each of these cases.

As for the regexp, being explicit is always more useful than being implicit. Additionally, we should probably include examples of the different values in comments. The regexp should look like:

/all: (\d+\.?\d*) \((\d+\.?\d*e?\-?\d*)\)/)

This allows for 0, 0.1, 0.1e-3, and even unexpected cases like 1e3, 1.0e3.

@twolfson
Copy link
Contributor

The gist is here but we need tests. Can you add some?

@nikchosh
Copy link
Author

I have included all your suggestions (ultra new to git -- sorry to make you my mentor :) ) I need to look at the tests. I am tester, and getting feet wet in Node development

@twolfson
Copy link
Contributor

I can't see your changes. Please push them to update the PR (pull request).

The values of threshold must be between 0 and 100 and represents
percentage of different pixels.
@nikchosh nikchosh changed the title add option 'threshold', add return value 'diffPath' add option 'threshold' representing % of different pixels (number between 0 and 100) Mar 30, 2015
var totalDifference = resultInfo[1];
return cb(null, totalDifference === '0');
var percentDifference = parseFloat(resultInfo[2], 10);
var threshold = (typeof(options.threshold) === 'undefined' || isNaN(parseFloat(options.threshold, 10))) ? 0.0 : parseFloat(options.threshold);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a lot going on in this line.

  • Using typeof to detect undefined is only necessary when looking at a global (e.g. `typeof window === 'undefined')
    • Otherwise, we can use a simpler direct comparison with undefined (e.g. options.foo === undefined)
  • We don't need to detect undefined, a fallback will work fine (e.g. var foo = options.bar || baz;)
  • We don't need to be defensive and coerce options.threshold. We can assume the developer wants the library to work and will pass in a Number (i.e. no isNaN/parseFloat necessary)
  • In JS, all Numbers are 64 bit floats. The decimal isn't necessary; 0 will work fine.

The simplified result should be:

var threshold = options.threshold || 0;

@twolfson
Copy link
Contributor

Getting closer, still needs tests though =/

@nikchosh
Copy link
Author

Tests added - probably documentation needs to be updated too

@@ -31,7 +31,7 @@ describe('image-diff', function () {
runImageDiff({
actualImage: __dirname + '/test-files/checkerboard.png',
expectedImage: __dirname + '/test-files/white.png',
diffImage: __dirname + '/actual-files/different.png'
diffImage: __dirname + '/actual-files/different.png',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move back this line? It is noise in the PR =/

@twolfson
Copy link
Contributor

Everything looks fine. Mostly fine tuning things now.

@nikchosh
Copy link
Author

Taken all your comments into account - please review

@twolfson
Copy link
Contributor

twolfson commented Apr 1, 2015

There seem to still be a few lingering inline comments that I would like to get sorted out. Can we take care of those?

@nikchosh
Copy link
Author

nikchosh commented Apr 6, 2015

Would you like me to remove all // comments DEV: etc.

@nikchosh
Copy link
Author

Hi Todd, any way to merge the changes? :)

@twolfson
Copy link
Contributor

There is still this trailing comma comment left =/

https://github.com/uber/image-diff/pull/15/files#r27454478

Additionally, we seem to be blocked by #16. I will bump the issue so it can be addressed.

@nikchosh
Copy link
Author

Commas fixed

@twolfson
Copy link
Contributor

Sweet, everything looks great now. Still waiting on a reply in #16

@mlmorg
Copy link
Contributor

mlmorg commented Apr 25, 2015

Can we rebase and squash into a single commit? Thanks!

@twolfson
Copy link
Contributor

This PR hasn't been active in over 1 year. We can probably close it due to inactivity

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

Successfully merging this pull request may close these issues.

3 participants