Core Graphics Interoperability

Pass image data between Core Graphics and vImage.

Overview

A typical Core Graphics-based vImage workflow consists of:

  1. Selecting a source image; for example, a Core Graphics-backed UIImage instance.

  2. Initializing a vImage buffer from the image's bitmap data.

  3. Performing an operation on the vImage buffer; for example, scaling or adjusting gamma.

  4. Creating a destination image from the operation result in the same format as the source from the vImage buffer.

vImage provides these functions:

vImageBuffer_InitWithCGImage(_:_:_:_:_:)

Initializes a vImage buffer with the contents of a Core Graphics image.

vImageCreateCGImageFromBuffer(_:_:_:_:_:_:)

Creates a Core Graphics image from a vImage buffer.

The following code shows a pass through function that accepts a UIImage, converts its underlying CGImage image data to a vImage buffer, and returns a UIImage created from the buffer. In this simple example, you create a description of the Core Graphics image format from the cgImage property of the supplied UIImage:

func passThrough(sourceImage: UIImage) -> UIImage? {
    guard
        let sourceCGImage = sourceImage.cgImage,
        let colorSpace = sourceCGImage.colorSpace else {
            return nil
    }

    var format = vImage_CGImageFormat(
        bitsPerComponent: UInt32(sourceCGImage.bitsPerComponent),
        bitsPerPixel: UInt32(sourceCGImage.bitsPerPixel),
        colorSpace: Unmanaged.passRetained(colorSpace),
        bitmapInfo: sourceCGImage.bitmapInfo,
        version: 0,
        decode: nil,
        renderingIntent: sourceCGImage.renderingIntent)

The description is used by vImageBuffer_InitWithCGImage(_:_:_:_:_:) to initialize a vImage buffer from the image:

    var buffer = vImage_Buffer()

    vImageBuffer_InitWithCGImage(&buffer,
                                 &format,
                                 nil,
                                 sourceCGImage,
                                 vImage_Flags(kvImageNoFlags))

Finally, vImageCreateCGImageFromBuffer(_:_:_:_:_:_:) uses the same description to create an image from the buffer:

    let destinationCGImage = vImageCreateCGImageFromBuffer(
        &buffer,
        &format,
        nil,
        nil,
        vImage_Flags(kvImageNoFlags),
        nil)
    
    if let destinationCGImage = destinationCGImage {
        return UIImage(cgImage: destinationCGImage.takeRetainedValue())
    }
    
    return nil
}

Topics

Initializing vImage Buffers from Core Graphics Images

Creating Core Graphics Image Formats

struct vImage_CGImageFormat

The description of a Core Graphics image.

struct vImageRGBPrimaries

A structure that represents the chromaticity of primaries defining a color space.

struct vImageWhitePoint

A structure that represents a white point according to the CIE 1931 color space.

struct vImageTransferFunction

A structure that represents a transfer function to convert from linear to nonlinear RGB.

Querying Core Graphics Image Format Attributes

func vImageCGImageFormat_IsEqual(UnsafePointer<vImage_CGImageFormat>!, UnsafePointer<vImage_CGImageFormat>!) -> Bool

Tests two vImage Core Graphics image formats to find out if they're equivalent.

func vImageCGImageFormat_GetComponentCount(UnsafePointer<vImage_CGImageFormat>) -> UInt32

Calculates the number of channels (color and alpha) for a given image format.