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 CGImageRef 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

vImageBuffer_InitWithCGImage

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

Creating Core Graphics Images from vImage Buffers

vImageCreateCGImageFromBuffer

Creates a Core Graphics image from a vImage buffer.

Creating Core Graphics Image Formats

vImage_CGImageFormat

The description of a Core Graphics image.

vImageCreateRGBColorSpaceWithPrimariesAndTransferFunction

Creates an RGB color space based on primitives typically found in Y'CbCr specifications.

vImageRGBPrimaries

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

vImageCreateMonochromeColorSpaceWithWhitePointAndTransferFunction

Creates a monochrome color space based on primitives typically found in Y'CbCr specifications.

vImageWhitePoint

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

vImageTransferFunction

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

Querying Core Graphics Image Format Attributes

vImageCGImageFormat_IsEqual

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

vImageCGImageFormat_GetComponentCount

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