Saving planar bitmap data into a file
Saving planar bitmap data into a file
- Subject: Saving planar bitmap data into a file
- From: Bertrand Mansion <email@hidden>
- Date: Wed, 29 Aug 2001 14:54:32 +0200
Hi all,
Finally, I solved my previous problem (but I am not sure I did it the
best way).
Here is the code I used to convert the content of my view to a planar
bitmap representation:
int pixWide = 320; // width
int pixHigh = 240; // height
int y,b; // counters
unsigned char *bitmapPointer; // pointer to char bitmap data
// memory alloc for RGB planes
unsigned char *redplane = (unsigned
char*)malloc((pixWide*pixHigh)*sizeof(char));
unsigned char *greenplane = (unsigned
char*)malloc((pixWide*pixHigh)*sizeof(char));
unsigned char *blueplane = (unsigned
char*)malloc((pixWide*pixHigh)*sizeof(char));
unsigned char *planes[3];
NSBitmapImageRep *originBitmap; // source rep
NSBitmapImageRep *destBitmap; // destination rep
// Take data from the view
[view lockFocus];
originBitmap = [[NSBitmapImageRep alloc]
initWithFocusedViewRect:[view bounds]];
[view unlockFocus];
// Filters the view data to make a planar bitmap
bitmapPointer = [originBitmap bitmapData];
b=0;
for (y=0; y < ([originBitmap bytesPerRow]*[originBitmap
pixelsHigh]); y+=4) {
redplane[b] = bitmapPointer[y];
greenplane[b] = bitmapPointer[y+1];
blueplane[b] = bitmapPointer[y+2];
b++;
}
planes[0] = redplane;
planes[1] = greenplane;
planes[2] = blueplane;
destBitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:planes
pixelsWide:pixWide
pixelsHigh:pixHigh
bitsPerSample:8
samplesPerPixel:3
hasAlpha:NO
isPlanar:YES
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:8];
Now, I don't know how to save this NSBitmapImageRep in a file as such,
ie as a planar bitmap.
If someone could help me, it would be great. I am also wondering if this
bit of code could be optimized.
Thanks for your replies.
Bertrand Mansion
Mamasam