views:

1583

answers:

4

hi,everybody:

i have a gdi engine that result in the standard bmp bits stream ,and now i wanna to display it by the CG in iphone , i use it like this:

// size  -> image size 
// rect  -> current view rect
// pBits -> the BITMAPINFO image bits stream

long imgSizePerRow = ((long)(24 * size.width + 31) / 32) * 4;
CGDataProviderRef providerRef = CGDataProviderCreateWithData(NULL, pBits,
                                          imgSizePerRow * size.height, NULL);

CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

CGImageRef imageRef = CGImageCreate(size.width, size.height, 8, 24,
                                    imgSizePerRow, colorSpaceRef,
                                    kCGBitmapByteOrderDefault, providerRef,
                                    NULL, YES, kCGRenderingIntentDefault);

CGContextDrawImage(context, rect, imageRef);

CGImageRelease(imageRef);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(providerRef);

attention: i check the row pixel fit to the times of 4

My gdi generate the bits stream , in this case , R8G8B8 format , no alpha channel ,i use this for CGDataProviderRef and draw the CGImage to my view. it is strange that the image's color seems change, yet the whole shape seems ok , i save the bits stream by BITMAP FORMAT to a file before, and make a contrast , everything seems ok just the whole color space.

CAN anyone tell me where is my fault in the code, or i miss some para. setting?

+2  A: 

CGImageCreate takes raw pixels, which a .bmp file is not (it has that “BITMAPINFO” header). Instead, create a UIImage with the .bmp data, then get the CGImage from that object.

Peter Hosey
hi, Peter . Actually i use the bitmap' raw pixels and just use the "BTIMAPINFO" header for the setting para(i do not construct a CGImage from the header info)?
Eric
Assuming there's no spare data *after* the bitmap data, the next thing I'd look to is the arithmetic. In this case, I'm suspicious of your imgSizePerRow calculation. Why the (+31)/32 dance? Why not just multiply by four?
Peter Hosey
A: 

hi, Peter . Actually i use the bitmap' raw pixels and just use the "BTIMAPINFO" header for the setting para(i do not construct a CGImage from the header info)?

Eric
A: 

And probably something seems strange ,when i use the follow methods to draw image ,everything just perfert

  UIImage *image = [UIImage imageNamed: @"S.bmp"];
  CGImageRef r = [image CGImage];
  CGDataProviderRef provider = CGImageGetDataProvider(r);
  CFDataRef bitmapData = CGDataProviderCopyData(provider);
  const UInt8* data = CFDataGetBytePtr(bitmapData);

  long imgSizePerRow = ((long)(24 * size.width + 31) / 32) * 4;
  CGDataProviderRef providerRef = CGDataProviderCreateWithData(NULL, (UInt8*)data, imgSizePerRow * size.height, NULL);

the bitstream is gotten through the UIImage ....

Eric
A: 

OK , I MISS the important point, MY GDI BITMAP stream is sequenced by B8->G8->R8

sorry about this!!!

Eric