Should I set a color space for a CIImage or a CIContext?

2.3k Views Asked by At

Core Image lets us specify a color space for a CIContext, as in:

let context = CIContext(options: [kCIContextOutputColorSpace: NSNull(),
                        kCIContextWorkingColorSpace: NSNull()])

Or for a CIImage, as in:

let image = CIImage(cvImageBuffer: inputPixelBuffer,
                    options: [kCIImageColorSpace: NSNull()])

How are these three related:

  • kCIContextOutputColorSpace
  • kCIContextWorkingColorSpace
  • kCIImageColorSpace

What are the pros and cons of setting each of them?

1

There are 1 best solutions below

0
Jeshua Lacock On

Apple's documentation explains the differences:

workingColorSpace

A key for the color space to use for image operations.

The working color space determines the color space used when executing filter kernels; Core Image automatically converts to and from the source and destination color spaces of input images and output contexts. You specify a working color space using the workingColorSpace key in the options dictionary when creating a Core Image context.

outputColorSpace

A key for the color space to use for images before they are rendered to the context. By default, Core Image uses the GenericRGB color space, which leaves color matching to the system.

To request that Core Image perform no color management, specify the NSNull object as the value for this key. Use this option for images that don’t contain color data (such as elevation maps, normal vector maps, and sampled function tables).

If images are tagged with a color space, they are converted to linear working space before filtering. If you tag a CIImage with DeviceRGB, it is gamma corrected to linear before filtering.

Setting the keys to NSNull instructs CoreImage to leave the color values as they are which is referred to as unmanaged color space.