I made a test bmp of 320*320px, added a Red, Green and Blue area.
Opened it in an text editor and removed meta data at the beginning.
When I used this code
NSString *imagePath = [[NSBundle mainBundle] pathForResource: @"test3" ofType: @"bmp"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
CGFloat width = 320.0;
CGFloat height = 320.0;
NSUInteger bitPerByte = 8;
NSUInteger bytePerColor = 3;
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
byteData,
width*height*bytePerColor,
NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGImageRef imageRef = CGImageCreate(width,
height,
bitPerByte,
bytePerColor*bitPerByte,
bytePerColor*width,colorSpaceRef,
bitmapInfo,
provider,NULL,NO,0);
Red and Blue where switched
The data looking like this
<… 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000
ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff
0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00
00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000
ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff
0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00
00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000
ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff
0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ff0000ff 0000ff00 00ff0000 ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff
ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff ffffffff …>
BTW: the groping of 8 bytes has nothing to do with the number of color components in the image. NSData doesn't know which data it contains. this is just the way it's description method displays the data.
One solution would be to create a BGR colorspace, but I did't have this option, as I decided to create an iOS app, as I am not very familiar with Mac Cocoa.
Instead I exchanged red and blue values manually
// on ios i cannot create a BGR colorspace, there for I swap red and blue values
for (NSUInteger i =2; i < len; i+=3) {
Byte l = byteData[i-2];
Byte r = byteData[i];
byteData[i] = l;
byteData[i-2] = r;
}
giving me following code
NSString *imagePath = [[NSBundle mainBundle] pathForResource: @"test3" ofType: @"bmp"];
NSData *data = [NSData dataWithContentsOfFile:imagePath];
CGFloat width = 320.0;
CGFloat height = 320.0;
NSUInteger bitPerByte = 8;
NSUInteger bytePerColor = 3;
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
// on ios i cannot create a BGR colorspace, there for I swap red and blue values
for (NSUInteger i =2; i < len; i+=3) {
Byte l = byteData[i-2];
Byte r = byteData[i];
byteData[i] = l;
byteData[i-2] = r;
}
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
byteData,
width*height*bytePerColor,
NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGImageRef imageRef = CGImageCreate(width,
height,
bitPerByte,
bytePerColor*bitPerByte,
bytePerColor*width,colorSpaceRef,
bitmapInfo,
provider,NULL,NO,0);
and this result:
So as you haven't provided a sample data set yet, you may have to adjust certain things, mainly bitPerByte
and bytePerColor
. you also have to check, if the scanner adds some meta data.
I don't know why, but that raw data array has exactly twice as much bytes as told by the metada, 89352 istead of 44676.
with this code
NSData *data = [NSData dataWithContentsOfFile:imagePath];
CGFloat width = 146.0;
CGFloat height = 102.0;
NSUInteger bitPerByte = 8;
NSUInteger bytePerColor = 2*3;
NSUInteger len = [data length];
Byte *byteData = (Byte*)malloc(len);
memcpy(byteData, [data bytes], len);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL,
byteData,
width*height*bytePerColor,
NULL);
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrder16Big;
CGImageRef imageRef = CGImageCreate(width,
height,
bitPerByte * 2,
bytePerColor*bitPerByte ,
bytePerColor*width ,
colorSpaceRef,
bitmapInfo,
provider,NULL,NO,0);
I am able to display