Contents

contrastStretch(destination:)

Stretches the histogram of a multiple-plane 8-bit pixel buffer.

Declaration

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

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: "Landscape_28_lowContrast.jpg").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 interleavedBuffer = try vImage.PixelBuffer(
    cgImage: srcImage,
    cgImageFormat: &cgImageFormat,
    pixelFormat: vImage.Interleaved8x3.self)

let multiplaneBuffer = vImage.PixelBuffer<vImage.Planar8x3>(
    interleavedBuffer: interleavedBuffer)

multiplaneBuffer.contrastStretch(destination: multiplaneBuffer)

multiplaneBuffer.convert(to: interleavedBuffer)

let outputImage = interleavedBuffer.makeCGImage(cgImageFormat: cgImageFormat)

See Also

Related Documentation

Contrast stretching