I'm trying to compare two images (actually locate a smaller "sub-image" in bigger image) and I'm loading the images using the method provided below.
The code below now contains a testing for-loop which sums up all the individual byte values. What I discovered is that this sum and therefor bytes, differ, depending on which device it is being run. My question is why is that happening ?
// Black and white configuration:
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
NSUInteger bytesPerPixel = 1;
NSUInteger bitsPerComponent = 8;
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
// Image
CGImageRef imageRef = [[UIImage imageNamed:@"image.jpg"] CGImage];
NSUInteger imageWidth = CGImageGetWidth(imageRef);
NSUInteger imageHeight = CGImageGetHeight(imageRef);
NSUInteger imageSize = imageHeight * imageWidth * bytesPerPixel;
NSUInteger imageBytesPerRow = bytesPerPixel * imageWidth;
unsigned char *imageRawData = calloc(imageSize, sizeof(unsigned char));
CGContextRef imageContext = CGBitmapContextCreate(imageRawData, imageWidth, imageHeight, bitsPerComponent,
imageBytesPerRow, colorSpace, bitmapInfo);
// Draw the actual image to the bitmap context
CGContextDrawImage(imageContext, CGRectMake(0, 0, imageWidth, imageHeight), imageRef);
CGContextRelease(imageContext);
NSUInteger sum = 0;
for (int byteIndex = 0; byteIndex < imageSize; byteIndex++)
{
sum += imageRawData[byteIndex];
}
NSLog(@"Sum: %i", sum); // Output on simulator: Sum: 18492272
// Output on iPhone 3GS: Sum: 18494036
// Output on another 3GS: Sum: 18494015
// Output on iPhone 4: Sum: 18494015
free(imageRawData);
CGColorSpaceRelease(colorSpace);