Search code examples
macoscore-graphicsrgbyuvcore-video

Drawing CGImageRef in YUV


I am using the code here to convert a CGImageRef into a CVPixelBufferRef on OS X .

Convert UIImage to CVImageBufferRef

However, I need the image to be drawn in YUV (kCVPixelFormatType_420YpCbCr8Planar) instead of RBG as it is now.

Is there anyway to directly draw a CGImage in YUV colorspace? And if not, does anyone have an example for the best way to go about converting the CVPixedBufferRef from RBG into YUV?

I understand the formulas for the conversion but doing it on the CPU is painfully slow.


Solution

  • Figured it out using:

    CVPixelBufferRef converted_frame;
    
    CVPixelBufferCreate(kCFAllocatorDefault, width, height, kCVPixelFormatType_420YpCbCr8Planar, 0, &converted_frame);
    
    VTPixelTransferSessionTransferImage(_vtpt_ref, imageBuffer, converted_frame);
    

    Where imageBuffer is the source CVPixedBufferRef