This repository was archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 249
Report precent difference when using a tolerance #149
Open
ultramiraculous
wants to merge
1
commit into
facebookarchive:master
Choose a base branch
from
ultramiraculous:percents
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,89 +46,64 @@ @implementation UIImage (Compare) | |
|
|
||
| - (BOOL)fb_compareWithImage:(UIImage *)image tolerance:(CGFloat)tolerance | ||
| { | ||
| return [self fb_isEqualToImage:image] || (tolerance > 0.0 && [self fb_differenceFromImage:image] < tolerance); | ||
| } | ||
|
|
||
|
|
||
| - (BOOL)fb_isEqualToImage:(UIImage *)image { | ||
| NSAssert(CGSizeEqualToSize(self.size, image.size), @"Images must be same size."); | ||
|
|
||
| CGSize referenceImageSize = CGSizeMake(CGImageGetWidth(self.CGImage), CGImageGetHeight(self.CGImage)); | ||
| CGSize imageSize = CGSizeMake(CGImageGetWidth(image.CGImage), CGImageGetHeight(image.CGImage)); | ||
|
|
||
| // The images have the equal size, so we could use the smallest amount of bytes because of byte padding | ||
| size_t minBytesPerRow = MIN(CGImageGetBytesPerRow(self.CGImage), CGImageGetBytesPerRow(image.CGImage)); | ||
| size_t referenceImageSizeBytes = referenceImageSize.height * minBytesPerRow; | ||
| void *referenceImagePixels = calloc(1, referenceImageSizeBytes); | ||
| void *imagePixels = calloc(1, referenceImageSizeBytes); | ||
|
|
||
| if (!referenceImagePixels || !imagePixels) { | ||
| free(referenceImagePixels); | ||
| free(imagePixels); | ||
| return NO; | ||
| } | ||
| CGContextRef referenceContext = [self fb_bitmapContext]; | ||
| CGContextRef imageContext = [image fb_bitmapContext]; | ||
|
|
||
| CGContextRef referenceImageContext = CGBitmapContextCreate(referenceImagePixels, | ||
| referenceImageSize.width, | ||
| referenceImageSize.height, | ||
| CGImageGetBitsPerComponent(self.CGImage), | ||
| minBytesPerRow, | ||
| CGImageGetColorSpace(self.CGImage), | ||
| (CGBitmapInfo)kCGImageAlphaPremultipliedLast | ||
| ); | ||
| CGContextRef imageContext = CGBitmapContextCreate(imagePixels, | ||
| imageSize.width, | ||
| imageSize.height, | ||
| CGImageGetBitsPerComponent(image.CGImage), | ||
| minBytesPerRow, | ||
| CGImageGetColorSpace(image.CGImage), | ||
| (CGBitmapInfo)kCGImageAlphaPremultipliedLast | ||
| ); | ||
|
|
||
| if (!referenceImageContext || !imageContext) { | ||
| CGContextRelease(referenceImageContext); | ||
| CGContextRelease(imageContext); | ||
| free(referenceImagePixels); | ||
| free(imagePixels); | ||
| return NO; | ||
| } | ||
|
|
||
| CGContextDrawImage(referenceImageContext, CGRectMake(0, 0, referenceImageSize.width, referenceImageSize.height), self.CGImage); | ||
| CGContextDrawImage(imageContext, CGRectMake(0, 0, imageSize.width, imageSize.height), image.CGImage); | ||
|
|
||
| CGContextRelease(referenceImageContext); | ||
| size_t pixelCount = CGBitmapContextGetHeight(referenceContext) * CGBitmapContextGetBytesPerRow(referenceContext); | ||
| BOOL matches = (memcmp(CGBitmapContextGetData(referenceContext), CGBitmapContextGetData(imageContext), pixelCount) == 0); | ||
|
|
||
| CGContextRelease(referenceContext); | ||
| CGContextRelease(imageContext); | ||
|
|
||
| return matches; | ||
| } | ||
|
|
||
| BOOL imageEqual = YES; | ||
|
|
||
| // Do a fast compare if we can | ||
| if (tolerance == 0) { | ||
| imageEqual = (memcmp(referenceImagePixels, imagePixels, referenceImageSizeBytes) == 0); | ||
| } else { | ||
| // Go through each pixel in turn and see if it is different | ||
| const NSInteger pixelCount = referenceImageSize.width * referenceImageSize.height; | ||
|
|
||
| FBComparePixel *p1 = referenceImagePixels; | ||
| FBComparePixel *p2 = imagePixels; | ||
|
|
||
| NSInteger numDiffPixels = 0; | ||
| for (int n = 0; n < pixelCount; ++n) { | ||
| // If this pixel is different, increment the pixel diff count and see | ||
| // if we have hit our limit. | ||
| if (p1->raw != p2->raw) { | ||
| numDiffPixels ++; | ||
|
|
||
| CGFloat percent = (CGFloat)numDiffPixels / pixelCount; | ||
| if (percent > tolerance) { | ||
| imageEqual = NO; | ||
| break; | ||
| } | ||
| } | ||
| - (CGFloat)fb_differenceFromImage:(UIImage *)image { | ||
| NSAssert(CGSizeEqualToSize(self.size, image.size), @"Images must be same size."); | ||
|
|
||
| p1++; | ||
| p2++; | ||
| // Go through each pixel in turn and see if it is different | ||
| CGContextRef referenceContext = [self fb_bitmapContext]; | ||
| CGContextRef imageContext = [image fb_bitmapContext]; | ||
|
|
||
| FBComparePixel *p1 = CGBitmapContextGetData(referenceContext); | ||
| FBComparePixel *p2 = CGBitmapContextGetData(imageContext); | ||
|
|
||
| NSInteger pixelCount = CGBitmapContextGetWidth(referenceContext) * CGBitmapContextGetHeight(referenceContext); | ||
| NSInteger numDiffPixels = 0; | ||
| for (int n = 0; n < pixelCount; ++n) { | ||
| // If this pixel is different, increment the pixel diff count and see | ||
| // if we have hit our limit. | ||
| if (p1->raw != p2->raw) { | ||
| numDiffPixels++; | ||
| } | ||
|
|
||
| p1++; | ||
| p2++; | ||
| } | ||
|
|
||
| return (CGFloat)numDiffPixels / pixelCount; | ||
| } | ||
|
|
||
| free(referenceImagePixels); | ||
| free(imagePixels); | ||
|
|
||
| return imageEqual; | ||
| - (CGContextRef)fb_bitmapContext { | ||
| CGContextRef context = CGBitmapContextCreate(NULL, | ||
| self.size.width, | ||
| self.size.height, | ||
| CGImageGetBitsPerComponent(self.CGImage), | ||
| CGImageGetBytesPerRow(self.CGImage), | ||
| CGImageGetColorSpace(self.CGImage), | ||
| (CGBitmapInfo)kCGImageAlphaPremultipliedLast); | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice, remove extra line |
||
| CGContextDrawImage(context, (CGRect){.size = self.size}, self.CGImage); | ||
|
|
||
| NSAssert(context != nil, @"Unable to create context for comparision."); | ||
| return context; | ||
| } | ||
|
|
||
| @end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
same her, docs if possible.