views:

338

answers:

0

Trying to convert a 32 bit CGImage to 24 bit and would like to avoid packing it byte-by-byte. Surely there must be a more efficient way ...?

Have tried a couple of things like creating a 24 bpp NSBitmapImageRep like this:

NSBitmapImageRep* image24 = [[NSBitmapImageRep alloc]  
    initWithBitmapDataPlanes:nil 
    pixelsWide:width 
    pixelsHigh:height 
    bitsPerSample:8 
    samplesPerPixel:3 
    hasAlpha:NO 
    isPlanar:NO 
    colorSpaceName:NSDeviceRGBColorSpace 
    bytesPerRow:width*3 
    bitsPerPixel:24];

then setting it as the current graphics context, and drawing the 32 bit image to it:

[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:image24]];

NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:cgImage];
NSImage *image = [[NSImage alloc] init];
[image addRepresentation:bitmapRep];
[bitmapRep release];
[image drawAtPoint:NSMakePoint(0.0, 0.0) 
    fromRect:NSMakeRect(0.0, 0.0, width, height) 
    operation:NSCompositeSourceOver 
    fraction:1.0];
...

[NSGraphicsContext restoreGraphicsState];

But it doesn't seem like the 24 bit format is supported...

also tried creating an NSImage, adding the 24 bit rep to it, and attempting to draw the 32 bit image there without much success.

What would be the best way to do this (surely) pretty standard task?

Thanks!