UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to work with indexed colorspace?

Forums > iOS

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)

3      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.