MultiplePlanePixelFormat
A pixel format that contains multiple homogeneous planes represented by multiple underlying vImage buffers.
Declaration
protocol MultiplePlanePixelFormat : PixelFormatOverview
Use multiple-plane pixel buffers to store image data as discrete planar buffers that represent individual channels.
For example, the following code deinterleaves an interleaved buffer and applies different gamma adjustments to different color channels. The code calls PixelBuffer.convert(to:) to reinterleave the image data and generates a CGImage instance of the final output.
let srcImage = imageLiteral(resourceName: "...").cgImage(
forProposedRect: nil,
context: nil,
hints: nil)!
var cgImageFormat = vImage_CGImageFormat(
bitsPerComponent: 8,
bitsPerPixel: 8 * 3,
colorSpace: CGColorSpaceCreateDeviceRGB(),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue))!
let interleaved = try vImage.PixelBuffer(
cgImage: srcImage,
cgImageFormat: &cgImageFormat,
pixelFormat: vImage.Interleaved8x3.self)
let multiplane = vImage.PixelBuffer<vImage.Planar8x3>(interleavedBuffer: interleaved)
multiplane.withUnsafePixelBuffers { pixelBuffers in
// Apply gamma to red channel.
pixelBuffers[0].applyGamma(.nineOverElevenHalfPrecision,
destination: pixelBuffers[0])
// Apply gamma to green channel.
pixelBuffers[1].applyGamma(.fiveOverElevenHalfPrecision,
destination: pixelBuffers[1])
}
multiplane.convert(to: interleaved)
let outputImage = interleaved