How to render a CIImage with indexed colorspace in UIImageView?

29 views Asked by At

I'm trying to convert an image coming from a video stream (CVImageBuffer) to a given set of predefined colors. Below I'm first converting the stream to a CIImage with a indexed colorspace, then converting the CIImage to a UIImage, but this final conversion gives the following error: [render] Could not support source colorspace: Indexed Colorspace 0x281608ba0.

Any idea of how to get this effect (like a Posterize effect, but with defined colors)?

let palette: [UInt8] = [
    0, 0, 0,
    0, 0, 255
    0, 255, 0,
    255, 0, 0,
    255, 255, 255]

let indexedColorSpace = CGColorSpace(indexedBaseSpace: CGColorSpaceCreateDeviceRGB(),
                                     last: palette.count - 1,
                                     colorTable: palette)!

let image = CIImage(cvImageBuffer: videoPixelBuffer,
                    options: [CIImageOption.colorSpace: indexedColorSpace])

let context = CIContext()
let cgImage = context.createCGImage(image, from: image.extent)!

indexedImageView.image = UIImage(cgImage: cgImage)

[EDIT]

After matt comment, I've added the rendering through a CIContext, but still get the same colorspace error…

0

There are 0 answers