Core Image


Handling RAW Formats

You can read RAW formatted images by invoking either the init(imageURL:options:) or the init(imageData:options:) CIFilter initializers. You can then read the image by asking for the outputImage.

Note that, at least for iOS 13 beta 1, the simulator can’t read some (all? I only tried with Canon RAW format files) RAW images. However, using macOS allows this to work.

RAW Format Options

With the RAW format CIFilter initializers, you can optionally pass a dictionary of how to read the image. The documentation for those keys is here.


Fun with Filters

Color Control (Brightness, Contrast, Saturation)

The CIColorControls filter allows you to adjust the brightness, contrast, and saturation of an image. These are done with 3 different input parameters (in addition to the inputImage parameter), which are, respectively: inputBrightness (float, between -1 and +1, default is 0), inputContrast (float, default is 1), and inputSaturation (float, between 0 and 1, default is 1)


You can generate a histogram of an image by using the CIAreaHistogram filter. You give it the image (inputImage), the region of interest (inputExtent), the number of buckets to create (inputCount), and a scaling factor (inputScale). That filter then produces an inputCount wide by 1 pixel high image, with each pixel representing the amount of pixels in the inputImage that fit in that particular bucket.

You can then pass the outputImage from that filter into a CIHistogramDisplayFilter filter. This takes an image as produced by CIAreaHistogram and computes a histogram image. The histogram image is the same width as the number of buckets given to your CIAreaHistogram filter, with the height being given as a parameter to the filter. The parameters to the filter are: inputImage, inputHeight (the height of the produced image. Float, between 1 and 200), inputHighLimit (float, between 0 and 1, default 1), and inputLowLimit (float, between 0 and 1, default 0).

You can combine these two filters like so, to produce a histogram image of the given input image:

func histogram(of image: CIImage, width: CGFloat, height: CGFloat) -> CIImage {
    let histogram = image.applyingFilter("CIAreaHistogram", parameters: ["inputExtent": image.extent, "inputCount": width, "inputScale": 1])
    return histogram.applyingFilter("CIHistogramDisplayFilter", parameters: ["inputHeight": height])

Last updated: 2020-06-07 16:24:37 -0700