Article

Creating a Core Graphics Image Format

Provide descriptions of Core Graphics image formats for conversions to and from vImage.

Overview

Conversions between Core Graphics-backed images and vImage buffers require that you define the Core Graphics image format. The format describes the number of color channels and how they're ordered; the size, in bits, of the color channels; and whether the image data is premultiplied by an alpha value.

You can initialize a Core Graphics image format in either of two ways: by hard-coding values or by deriving the format properties from a CGImage instance.

Initialize a Core Graphics Image Format from Hard-Coded Values

The following example shows how you create a Core Graphics image format that describes an image containing four 8-bit channels in the following order: alpha (transparency), red, green, and blue. This combination of properties is referred to as ARGB8888.

The vImage_CGImageFormat structure created by this code is reusable. You declare it as a variable because it's passed to subsequent vImage functions as an in-out parameter.

var format = vImage_CGImageFormat(
    bitsPerComponent: 8,
    bitsPerPixel: 32,
    colorSpace: CGColorSpaceCreateDeviceRGB(),
    bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.first.rawValue),
    renderingIntent: .defaultIntent)

Initialize a Core Graphics Image Format from an Image

You can derive the properties of a Core Graphics image format from the CGImage instance itself. The following example shows how to create a format description based on the properties of a UIImage instance named sourceImage:

guard
    let cgImage = sourceImage.cgImage,
    let format = vImage_CGImageFormat(cgImage: cgImage) else {
        return nil
}

See Also

Image Processing Essentials

Creating and Populating Buffers from Core Graphics Images

Initialize vImage buffers from Core Graphics images.

Creating a Core Graphics Image from a vImage Buffer

Create displayable representations of vImage buffers.

Applying vImage Operations to Regions of Interest

Limit the effect of vImage operations to rectangular regions of interest.

Optimizing Image Processing Performance

Improve your app's performance by converting image buffer formats from interleaved to planar.

vImage

Manipulate large images using the CPU’s vector processor.