Faster way to get pixels from CGImageRef with color space conversion applied

Hi, I'm doing video processing and need to get video frames decoded as fast as possible with the embedded color space applied to the bitmap RGB values (converting it to the display's color space) that I'll be accessing. I get a CGImageRef for each frame using AVAssetReader all fine, but the only way I've found to get the RGB bitmap values AFTER applying the embedded color profile to convert to the display profile is as follows, and it is relatively VERY SLOW:


// get the display profile
CGDirectDisplayID displayID = CGMainDisplayID();
CGColorSpaceRef colorSpace = CGDisplayCopyColorSpace(displayID);
...
// create the bitmap context
CGContextRef bitmapContext = CGBitmapContextCreate(
     bufferCopy,
     width,
     height,
     bitsPerComponent,
     bytesPerRow,
     colorSpace,
     bmInfo);
...
// draw the CGImage into the context
CGContextDrawImage(bitmapContext, rect, myImage);
// values are now in my own buffer, bufferCopy


I've found a few other ways to get access to the bitmap RGB pixel values of a CGImageRef which are faster, but I can find no way to convert the values to another color space. The methods I've tried are:


CFDataRef inPixelData = CGDataProviderCopyData(CGImageGetDataProvider(myImage));
const unsigned char* readBufferStart = CFDataGetBytePtr(inPixelData);


and a similar thing with NSBitmapImage:


NSBitmapImageRep* bitmapOrig = [[NSBitmapImageRep alloc] initWithCGImage:myImage];
unsigned char* readBufferStart = [bitmap bitmapData];


I tried using [NSBitmapImageRep bitmapImageRepByConvertingToColorSpace] and CGImageCreateCopyWithColorSpace() in these respectively, but neither seemed to actually convert the pixel values in the buffer to the screen color space (and they also nullified any speed gains from not using CGContextDrawImage(), presumably from allocating a whole new copy of the buffer). Any ideas for how to do this quickly?

Take a look at the vImage API of the Accelerate framework, particularly the CGImage-related functions and AnyToAny conversion described in the vImage_Utilities.h header. Also, if it would be more (or at least as) convenient to get CVPixelBuffer objects instead of CGImages, see the functions in vImage_CVUtilities.h.

Faster way to get pixels from CGImageRef with color space conversion applied
 
 
Q