How do I create a CVPixelBuffer with 32RGBA format for iPhone?

Toad picture Toad · Nov 6, 2011 · Viewed 9.8k times · Source

When trying to create a 32 bits RGBA CVPixelBuffer, I constantly get errors.

Most notably error -6680 which means: "The buffer does not support the specified pixel format."

This is the code fragment: (Width and height are specified as 256*256)

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
//                         [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
//                         [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
//                         [NSNumber numberWithBool:YES], kCVPixelBufferOpenGLCompatibilityKey,
                         nil];
CVPixelBufferRef pxbuffer = NULL;
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, WIDTH,
                                      HEIGHT, kCVPixelFormatType_32RGBA, (CFDictionaryRef) options, 
                                      &pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

Can anyone give a hint as to what I'm doing wrong?

Answer

rob mayoff picture rob mayoff · Nov 6, 2011

You'll need to use a different pixel format. Just because there's a constant defined for 32RGBA doesn't mean it's supported. This tech note lists the supported formats (as of when it was written) and the functions you can use to find out what formats are currently supported:

Technical Q&A QA1501 Core Video - Available Pixel Formats

The most similar formats that are supported are 32ARGB and 32BGRA.