在 Swift 3 中绘制 CGImage 的直方图

Drawing histogram of CGImage in Swift 3

我在尝试将库从 Swift 2 版本转换为 Swift 3 版本时遇到了 vImageHistogramCalculation_ARGB8888 方法的问题。问题是该方法只接受 "histogram" 参数作为

UnsafeMutablePointer<UnsafeMutablePointer<T>?> 

但Swift3个建筑

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

return 未包装的值,因此我无法将其强制转换为正确的类型。

编译器错误是:

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

你有什么想法吗?我试着加“?”直方图常量但随后我收到错误:

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

有一些编译器建议,但我不知道如何使用它。

转换为 UnsafeMutablePointer<vImagePixelCount>?

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

完成,编译示例:

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

Swift 5

来自 shallowThought 的代码有效,但对于 Swift 5 生成警告:

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

为了跟进 Valerly Van 对 shallowThought 接受的答案的评论,这里是 Swift 5 消除警告的代码。下面的代码是从两个来源复制、粘贴和混搭而成的。

Apple 的示例“使用 vImage 指定直方图” https://developer.apple.com/documentation/accelerate/specifying_histograms_with_vimage

有关生成和使用直方图的 SO post,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)
}

将 bins(零、一、二、三)与(红、绿、蓝、alpha)匹配恰好适用于我当前且相当有限的用例。