Mr. A
Mr. A

Reputation: 714

Drawing histogram of CGImage in Swift 3

I have a problem with vImageHistogramCalculation_ARGB8888 method while trying to convert library from Swift 2 to Swift 3 version. The problem is that the method accepts "histogram" argument only as

UnsafeMutablePointer<UnsafeMutablePointer<T>?> 

but Swift 3 construction

let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>>(mutating: rgba)  

return unwrapped value, so I can't cast it to properly type.

The compiler error is:

: Cannot invoke initializer for type 'UnsafeMutablePointer?>' with an argument list of type '(mutating: [UnsafeMutablePointer])'

Have you some ideas? I tried to add "?" to histogram constant but then I receive error:

'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.

there is some compiler suggestion but I have no idea how can use it.

Upvotes: 4

Views: 1782

Answers (2)

Rethunk
Rethunk

Reputation: 4113

Swift 5

The code from shallowThought works, but for Swift 5 generates warnings:

Initialization of 'UnsafeMutablePointer<vImagePixelCount>' 
(aka 'UnsafeMutablePointer<UInt>') results in a dangling pointer

To follow up Valerly Van's comments to shallowThought's accepted answer, here's Swift 5 code that eliminates the warnings. The code below is copied, pasted, and mashed together from two sources.

Apple's example, "Specifying Histograms with vImage" https://developer.apple.com/documentation/accelerate/specifying_histograms_with_vimage

An SO post about generating and using histograms, Swift 2.2 - Count Black Pixels in UIImage

func histogram(image: UIImage) -> (red: [UInt], green: [UInt], blue: [UInt], alpha: [UInt]) {
let img: CGImage = CIImage(image: image)!.cgImage!

let imgProvider: CGDataProvider = img.dataProvider!
let imgBitmapData: CFData = imgProvider.data!
var imgBuffer = vImage_Buffer(
    data: UnsafeMutableRawPointer(mutating: CFDataGetBytePtr(imgBitmapData)),
    height: vImagePixelCount(img.height),
    width: vImagePixelCount(img.width),
    rowBytes: img.bytesPerRow)

// bins: zero = red, green = one, blue = two, alpha = three
var binZero = [vImagePixelCount](repeating: 0, count: 256)
var binOne = [vImagePixelCount](repeating: 0, count: 256)
var binTwo = [vImagePixelCount](repeating: 0, count: 256)
var binThree = [vImagePixelCount](repeating: 0, count: 256)

binZero.withUnsafeMutableBufferPointer { zeroPtr in
    binOne.withUnsafeMutableBufferPointer { onePtr in
        binTwo.withUnsafeMutableBufferPointer { twoPtr in
            binThree.withUnsafeMutableBufferPointer { threePtr in
                
                var histogramBins = [zeroPtr.baseAddress, onePtr.baseAddress,
                                     twoPtr.baseAddress, threePtr.baseAddress]
                
                histogramBins.withUnsafeMutableBufferPointer {
                    histogramBinsPtr in
                    let error = vImageHistogramCalculation_ARGB8888(
                        &imgBuffer,
                        histogramBinsPtr.baseAddress!,
                        vImage_Flags(kvImageNoFlags))
                    
                    guard error == kvImageNoError else {
                        fatalError("Error calculating histogram: \(error)")
                    }
                }
            }
        }
    }
}

return (binZero, binOne, binTwo, binThree)
}

Matching up the bins (zero, one, two, three) with (red, green, blue, alpha) happens to work for my current and rather limited use case.

Upvotes: 3

shallowThought
shallowThought

Reputation: 19602

Cast to UnsafeMutablePointer<vImagePixelCount>?:

let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?

Complete, compiling example:

var inBuffer = vImage_Buffer() // set your buffer here

let alpha = [UInt](repeating: 0, count: 256)
let red = [UInt](repeating: 0, count: 256)
let green = [UInt](repeating: 0, count: 256)
let blue = [UInt](repeating: 0, count: 256)

let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?
let redPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: red) as UnsafeMutablePointer<vImagePixelCount>?
let greenPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: green) as UnsafeMutablePointer<vImagePixelCount>?
let bluePtr = UnsafeMutablePointer<vImagePixelCount>(mutating: blue) as UnsafeMutablePointer<vImagePixelCount>?

let rgba = [redPtr, greenPtr, bluePtr, alphaPtr]

let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>?>(mutating: rgba)
let error = vImageHistogramCalculation_ARGB8888(&inBuffer, histogram, UInt32(kvImageNoFlags))

// alpha, red, green, blue are set now

Upvotes: 7

Related Questions