-
Notifications
You must be signed in to change notification settings - Fork 94
add option 'threshold' representing % of different pixels (number between 0 and 100) #15
Conversation
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*) \((.*?)\)/); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
.
The gist is here but we need tests. Can you add some? |
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 |
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.
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); |
There was a problem hiding this comment.
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
)
- Otherwise, we can use a simpler direct comparison with
- 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 aNumber
(i.e. noisNaN/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;
Getting closer, still needs tests though =/ |
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', |
There was a problem hiding this comment.
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 =/
Everything looks fine. Mostly fine tuning things now. |
Taken all your comments into account - please review |
There seem to still be a few lingering inline comments that I would like to get sorted out. Can we take care of those? |
Would you like me to remove all // comments DEV: etc. |
Hi Todd, any way to merge the changes? :) |
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. |
Commas fixed |
Sweet, everything looks great now. Still waiting on a reply in #16 |
Can we rebase and squash into a single commit? Thanks! |
This PR hasn't been active in over 1 year. We can probably close it due to inactivity |
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)