Hi, I am having exactly the same issue. Did you find a solution?
I have the raw data from a camera sensor with 14bit/pixel with an RGGB color-filter array (CFA).
I tried so many things to convert this to a color image, while letting iOS/Swift take care of the demosaicing. It obviously can do it. No need for me to program the de-bayering myself and probably doing a poor job at it compared to what iOS could do.
Here is my code, which only gets me black and white (or better grayscale) images:
let dummyImg = UIImage(systemName: "star.fill")?.cgImage
// imgRawData is a [UInt16] array
var pixelBuffer: CVPixelBuffer?
let attrs: [ CFString : Any ] = [ kCVPixelBufferCGImageCompatibilityKey : kCFBooleanTrue as Any,
kCVPixelBufferCGBitmapContextCompatibilityKey : kCFBooleanTrue as Any,
kCVPixelBufferPixelFormatTypeKey : "rgg4" ]
CVPixelBufferCreateWithBytes(kCFAllocatorDefault, width, height, kCVPixelFormatType_14Bayer_RGGB, &imgRawData, 2*width, nil, nil, attrs as CFDictionary, &pixelBuffer)
let ciimg = CIImage(cvImageBuffer: pixelBuffer!)
let context: CIContext = CIContext.init(options: [ CIContextOption(rawValue: "workingFormat") : CIFormat.RGBA16 ] )
guard let cgi = context.createCGImage(ciimg, from: ciimg.extent, format: CIFormat.RGBA16, colorSpace: CGColorSpace(name: CGColorSpace.sRGB), deferred: false)
else { return dummyImg! }
I also tried this more direct approach with VideoTools.... it just returns nil:
var cgI: CGImage?
VTCreateCGImageFromCVPixelBuffer(pixelBuffer!, options: nil, imageOut: &cgI)
How did you solve it?