Search code examples
c#iosxamarinurhosharp

How to convert BGRA to RGBA in Xamarin.iOS?


I am using AVFoundation to get real-time frames from camera and display to the UrhoSharp.

However, seems the Texture2D in UrhoSharp is not support the BGRA from AVVideoOutput. So I want to convert the "CVPixelBuffer" to RGB format.

I found there is an API in vImage "vImagePermuteChannels_ARGB8888", which can convert the BGRA to ARGB but this is not provided by Xamarin.

So how to solve this problem?


Solution

  • The Accelerate.framework consists of C functions, so:

    vImage_Error vImagePermuteChannels_ARGB8888(    const vImage_Buffer *src,
                                                    const vImage_Buffer *dest,
                                                    const uint8_t       permuteMap[4],
                                                    vImage_Flags        flags )     VIMAGE_NON_NULL(1,2,3)  __OSX_AVAILABLE_STARTING( __MAC_10_4, __IPHONE_5_0 );
    

    Can be used via:

    [DllImport("/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/vImage")]
    public unsafe static extern vImageError vImagePermuteChannels_ARGB8888(ref vImageBuffer src, ref vImageBuffer dest, [MarshalAs(UnmanagedType.LPArray)]uint[] permuteMap, vImageFlags flags);
    

    Ref: https://developer.apple.com/documentation/accelerate/1533241-vimagepermutechannels_argb8888

    Ref: Conversion.h

    • /Applications/Xcode9.1.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/System/Library/Frameworks/Accelerate.framework/Frameworks/vImage.framework/Headers