[#64 state:fixed] fixed a bug in PictureBlocks where transparency could make two equal pictures return two different sets of blocks.

--HG--
extra : convert_revision : svn%3Ac306627e-7827-47d3-bdf0-9a457c9553a1/trunk%40150
This commit is contained in:
hsoft 2009-09-28 15:33:25 +00:00
parent 9a44956d8f
commit 7e45f422e1
2 changed files with 10 additions and 11 deletions

View File

@ -11,9 +11,9 @@ http://www.hardcoded.net/licenses/hs_license
@interface PictureBlocks : NSObject { @interface PictureBlocks : NSObject {
} }
+ (NSString *)getBlocksFromImagePath:(NSString *)imagePath blockCount:(NSNumber *)blockCount scanArea:(NSNumber *)scanArea; + (NSString *)getBlocksFromImagePath:(NSString *)imagePath blockCount:(NSNumber *)blockCount;
+ (NSSize)getImageSize:(NSString *)imagePath; + (NSSize)getImageSize:(NSString *)imagePath;
@end @end
NSString* GetBlocks(NSString *filePath, int blockCount, int scanSize); NSString* GetBlocks(NSString *filePath, int blockCount);

View File

@ -9,11 +9,10 @@ http://www.hardcoded.net/licenses/hs_license
#import "PictureBlocks.h" #import "PictureBlocks.h"
#import "Utils.h" #import "Utils.h"
@implementation PictureBlocks @implementation PictureBlocks
+ (NSString *)getBlocksFromImagePath:(NSString *)imagePath blockCount:(NSNumber *)blockCount scanArea:(NSNumber *)scanArea + (NSString *)getBlocksFromImagePath:(NSString *)imagePath blockCount:(NSNumber *)blockCount
{ {
return GetBlocks(imagePath, n2i(blockCount), n2i(scanArea)); return GetBlocks(imagePath, n2i(blockCount));
} }
+ (NSSize)getImageSize:(NSString *)imagePath + (NSSize)getImageSize:(NSString *)imagePath
@ -47,7 +46,11 @@ http://www.hardcoded.net/licenses/hs_license
colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
bitmapData = malloc( bitmapByteCount ); // calloc() must be used to allocate bitmapData here because the buffer has to be zeroed.
// If it's not zeroes, when images with transparency are drawn in the context, this buffer
// will stay with undefined pixels, which means that two pictures with the same pixels will
// most likely have different blocks (which is not supposed to happen).
bitmapData = calloc(bitmapByteCount, 1);
if (bitmapData == NULL) if (bitmapData == NULL)
{ {
fprintf (stderr, "Memory not allocated!"); fprintf (stderr, "Memory not allocated!");
@ -90,7 +93,7 @@ http://www.hardcoded.net/licenses/hs_license
return result; return result;
} }
NSString* GetBlocks (NSString* filePath, int blockCount, int scanSize) NSString* GetBlocks (NSString* filePath, int blockCount)
{ {
CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)filePath, kCFURLPOSIXPathStyle, FALSE); CFURLRef fileURL = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)filePath, kCFURLPOSIXPathStyle, FALSE);
CGImageSourceRef source = CGImageSourceCreateWithURL(fileURL, NULL); CGImageSourceRef source = CGImageSourceCreateWithURL(fileURL, NULL);
@ -101,10 +104,6 @@ http://www.hardcoded.net/licenses/hs_license
return NULL; return NULL;
size_t width = CGImageGetWidth(image); size_t width = CGImageGetWidth(image);
size_t height = CGImageGetHeight(image); size_t height = CGImageGetHeight(image);
if ((scanSize > 0) && (width > scanSize))
width = scanSize;
if ((scanSize > 0) && (height > scanSize))
height = scanSize;
CGContextRef myContext = MyCreateBitmapContext(width, height); CGContextRef myContext = MyCreateBitmapContext(width, height);
CGRect myBoundingBox = CGRectMake (0, 0, width, height); CGRect myBoundingBox = CGRectMake (0, 0, width, height);
CGContextDrawImage(myContext, myBoundingBox, image); CGContextDrawImage(myContext, myBoundingBox, image);