Contents

contrastStretch(destination:)

Stretches the histogram of an 8-bit planar pixel buffer.

Declaration

func contrastStretch(destination: vImage.PixelBuffer<vImage.Planar8>)

Parameters

  • destination:

    The destination pixel buffer.

Discussion

Use this function to evenly distributes a histogram’s pixel values across the full range of available pixel values.

For example, the following code stretches the contrast of an image:

let srcImage =  imageLiteral(resourceName: " ... ").cgImage(
    forProposedRect: nil,
    context: nil,
    hints: nil)!

var cgImageFormat = vImage_CGImageFormat(
    bitsPerComponent: 8,
    bitsPerPixel: 8 * 1,
    colorSpace: CGColorSpaceCreateDeviceGray(),
    bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue))!

let buffer = try vImage.PixelBuffer(
    cgImage: srcImage,
    cgImageFormat: &cgImageFormat,
    pixelFormat: vImage.Planar8.self)

buffer.contrastStretch(destination: buffer)

let outputImage = buffer.makeCGImage(cgImageFormat: cgImageFormat)

See Also

Related Documentation

Contrast stretching