在Swift 3中绘制CGImage的直方图

时间:2016-11-12 12:33:54

标签: ios swift histogram cgimage accelerate-framework

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

UnsafeMutablePointer<UnsafeMutablePointer<T>?> 

但是Swift 3构建

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

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

编译错误是:

  

:无法为类型调用初始值设定项   &#39;&UnsafeMutablePointer GT;&#39;同   类型&#39;的参数列表(变异:   [UnsafeMutablePointer])&#39;

你有什么想法吗?我试着添加&#34;?&#34;到直方图常数然后我收到错误:

  

&#39;初始化&#39;不可用:使用&#39; withMemoryRebound(to:capacity:_)&#39;至   暂时将内存视为另一种与布局兼容的类型。

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

2 个答案:

答案 0 :(得分:5)

转换为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

答案 1 :(得分:0)

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 帖子,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)
}

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