8

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?

Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55
  • I'm not able to give you any "definitive" answer, but I'll guess it may well depend on *your* needs. Using a `CIContext` is costly in terms of performance, so if you can stick with one - required if you are using a `GLKView` - then do that (stick with one that is). A `CIImage` is *not* an image, instead it's a "recipe" to *draw* or *manipulate* an image. None of this explains which is best for your needs though, so I guess the question for *you* is **why** are you setting a color space? It's **that** answer that will probably dictate which place to set things. –  Nov 03 '17 at 02:37
  • 2
    I'm trying to follow Apple's recommendation to optimise my app by not doing color space conversions: https://developer.apple.com/library/content/documentation/GraphicsImaging/Conceptual/CoreImaging/ci_performance/ci_performance.html But when I set kCIImageColorSpace to nil, the gamma is messed up, and I have to set one or more of the other two keys listed in the question. At that point, I no longer knew what I was doing :) So I wanted to understand. Thanks for your help. – Kartick Vaddadi Nov 03 '17 at 08:09

1 Answers1

0

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.

Jeshua Lacock
  • 5,730
  • 1
  • 28
  • 58