What is the proper colorspace conversion for this image on iOS?

How do we fix our code below to properly color the image you see below from our incoming sampleBuffer?

We are attempting to convert an incoming sampleBuffer image to a UIImage but the result is the inverted off-color image you see below.

You can see our attempt to use the kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange option in the code – but the results were the same.

The incoming image we are trying to show has all the right colors as evidenced by the fact that if we render the image into a GLKView, all the colors are there.

Could this be a YUV420 conversion issue?

Here is the conversion code we are using:

- (CGImageRef) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer // Create a CGImageRef from sample buffer data
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);        // Lock the image buffer

    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);   // Get information of the image
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    // CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);

    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange | kCGImageAlphaPremultipliedFirst);

    CGImageRef newImage = CGBitmapContextCreateImage(newContext);

    /* CVBufferRelease(imageBuffer); */  // do not call this!

    return newImage;

Here is the setup code we use for the incoming CVPixelBuffer:

// Now create the CVPixelBuffer to which we will render
    CVReturn retVal = CVPixelBufferCreate(kCFAllocatorDefault,

Inverted Image
Any suggestions for what to try to restore and display all the proper colors?

Source: ios

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.