Saving NSBitmapImageRep as NSBMPFileType file. Wrong BMP headers and bitmap content
- by niko34
I save a NSBitmapImageRep to a BMP file (Snow Leopard). It seems ok when i open it on macos. But it makes an error on my multimedia device (which can show any BMP file from internet). I cannot figure out what is wrong, but when i look inside the file (with the cool hexfiend app on macos), 2 things wrong:
the header have a wrong value for the biHeight parameter : 4294966216 (hex=C8FBFFFF)
the header have a correct biWidth parameter : 1920
the first pixel in the bitmap content (after 54 bytes headers in BMP format) correspond to the upper left corner of the original image. In the original BMP file and as specified in the BMP format, it should be the down left corner pixel first.
To explain the full workflow in my app, i have an NSImageView where i can drag a BMP image. This View is bind to an NSImage.
After a drag & drop i have an action to save this image (with some text drawing over it) to a BMP file.
Here's the code for saving the new BMP file :
CGColorSpaceRefcolorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRefcontext = CGBitmapContextCreate(NULL, (int)1920, (int)1080, 8, 4*(int)1920, colorSpace, kCGImageAlphaNoneSkipLast);
[duneScreenViewdrawBackgroundWithDuneFolder:self inContext:context inRect:NSMakeRect(0,0,1920,1080) needScale:NO];
if(folderType==DXFolderTypeMovie) {
[duneScreenViewdrawSynopsisContentWithDuneFolder:self inContext:context inRect:NSMakeRect(0,0,1920,1080) withScale:1.0];
}
CGImageRef backgroundImageRef = CGBitmapContextCreateImage(context);
NSBitmapImageRep*bitmapBackgroundImageRef = [[NSBitmapImageRepalloc] initWithCGImage:backgroundImageRef];
NSData*data = [destinationBitmap representationUsingType:NSBMPFileType properties:nil];
[data writeToFile:[NSStringstringWithFormat:@"%@/%@", folderPath,backgroundPath] atomically: YES];
The duneScreenViewdrawSynopsisContentWithDuneFolder method uses CGContextDrawImage to draw the image.
The duneScreenViewdrawSynopsis method uses CoreText to draw some text in the same context.
Do you know what's wrong?