Contents

decode

The decode array for the image.

Declaration

var decode: UnsafePointer<CGFloat>!

Discussion

The decode array contains the upper and lower bounds for each color channel. When you define a decode array, the conversion functions that accept a vImage_CGImageFormat parameter apply a linear transform to map colors to the specified bounds.

A decode array for an RGB color space contains six entries: one pair for each red, green, and blue channel.

The following code shows how the vImage library remaps a buffer that contains five planar pixels in the range 0 ... 0.5 to the range 0 ... 1. The code passes a decode array with the values [0, 0.5] to the makeCGImage(cgImageFormat:) function that remaps the source pixel values from [0.0, 0.125, 0.25, 0.375, 0.5] to [0.0, 0.25, 0.5, 0.75, 1.0].

// Create a grayscale vImage pixel buffer.
let pixels: [Pixel_F] = [0.0, 0.125, 0.25, 0.375, 0.5]
let pixelBuffer = vImage.PixelBuffer(
    pixelValues: pixels,
    size: .init(width: 5, height: 1),
    pixelFormat: vImage.PlanarF.self)

// Define the decode array to `0 ... 0.5`.
let decodeArray: [CGFloat] = [0, 0.5]

decodeArray.withUnsafeBufferPointer { decodeArrayPtr in

    let cgImageFormat = vImage_CGImageFormat(
        bitsPerComponent: 32,
        bitsPerPixel: 32,
        colorSpace: Unmanaged.passRetained(CGColorSpaceCreateDeviceGray()),
        bitmapInfo: CGBitmapInfo(
            rawValue: CGBitmapInfo.byteOrder32Little.rawValue |
                CGBitmapInfo.floatComponents.rawValue |
                CGImageAlphaInfo.none.rawValue),
        version: 0,
        decode: decodeArrayPtr.baseAddress,
        renderingIntent: .defaultIntent)
    
    let cgImage = pixelBuffer.makeCGImage(cgImageFormat: cgImageFormat)
    
    // Display the raw pixel values of the `CGImage`.
    if let pixelData = cgImage?.dataProvider?.data {
        let pixelPointer = (pixelData as NSData)
            .bytes
            .assumingMemoryBound(to: Pixel_F.self)
        
        let recreatedPixels = UnsafeBufferPointer<Pixel_F>(
            start: pixelPointer,
            count: 5)
        
        // Prints "[0.0, 0.25, 0.5, 0.75, 1.0]".
        print(Array(recreatedPixels))
    }
}

Set the decodeArray to nil to prevent the vImage-Core Graphics conversion functions from remapping values.

See Also

Instance properties