vImageMatrixMultiply_ARGB8888ToPlanar8 在构建发布模式 iOS 应用程序时不起作用
vImageMatrixMultiply_ARGB8888ToPlanar8 is not work while build release mode iOS application
我正在开发图像处理应用程序并尝试使用 vImage。它工作得很好,并在调试模式下得到正确的结果。但是,它总是 returns 黑色图像或在发布模式下引发错误。
我已经尝试过官方 example,当我在发布模式下构建它时,它也会得到一个黑色图像。
func uiImageToBuffer(image: UIImage) -> vImage_Buffer? {
guard let cgImage = image.cgImage,
var format = cgImageFormat(of: image)
else { return nil }
var buffer = vImage_Buffer()
let error = vImageBuffer_InitWithCGImage(&buffer, &format, nil, cgImage, vImage_Flags(kvImageNoFlags))
if error == kvImageNoError {
return buffer
} else {
return nil
}
}
func grayBuffer(_ buffer: vImage_Buffer) -> vImage_Buffer {
var _buffer = buffer
var destinationBuffer = vImage_Buffer()
vImageBuffer_Init(&destinationBuffer, buffer.height, buffer.width, 8, vImage_Flags(kvImageNoFlags))
let redCoefficient: Float = 0.2126
let greenCoefficient: Float = 0.7152
let blueCoefficient: Float = 0.0722
let divisor = Float(0x1000)
var coefficientsMatrix = [
Int16(redCoefficient * divisor),
Int16(greenCoefficient * divisor),
Int16(blueCoefficient * divisor)
]
var preBias: Int16 = 0
let postBias: Int32 = 0
vImageMatrixMultiply_ARGB8888ToPlanar8(&_buffer, &destinationBuffer,
&coefficientsMatrix, 0x1000, &preBias, postBias, vImage_Flags(kvImageNoFlags))
return destinationBuffer
}
func main() {
guard let imageBuffer = uiImageToBuffer(image: image),
var grayBuffer = grayBuffer(buffer)
else { return }
var monoFormat = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 8,
colorSpace: Unmanaged.passRetained(CGColorSpaceCreateDeviceGray()),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue),
version: 0, decode: nil, renderingIntent: .defaultIntent)
let cgImage = vImageCreateCGImageFromBuffer(&grayBuffer, &monoFormat, nil, nil, vImage_Flags(kvImageNoFlags), nil)
if let cgImage = cgImage {
imageView.image = UIImage(cgImage: cgImage.takeRetainedValue())
}
free(imageBuffer.data)
free(grayBuffer.data)
}
预偏置应该是一个包含四个元素的向量。试试这个:
let preBias: [Int16] = [0, 0, 0, 0]
let postBias: Int32 = 0
vImageMatrixMultiply_ARGB8888ToPlanar8(&_buffer,
destinationBuffer,
&coefficientsMatrix,
0x1000,
preBias,
postBias,
vImage_Flags(kvImageNoFlags))
我正在开发图像处理应用程序并尝试使用 vImage。它工作得很好,并在调试模式下得到正确的结果。但是,它总是 returns 黑色图像或在发布模式下引发错误。
我已经尝试过官方 example,当我在发布模式下构建它时,它也会得到一个黑色图像。
func uiImageToBuffer(image: UIImage) -> vImage_Buffer? {
guard let cgImage = image.cgImage,
var format = cgImageFormat(of: image)
else { return nil }
var buffer = vImage_Buffer()
let error = vImageBuffer_InitWithCGImage(&buffer, &format, nil, cgImage, vImage_Flags(kvImageNoFlags))
if error == kvImageNoError {
return buffer
} else {
return nil
}
}
func grayBuffer(_ buffer: vImage_Buffer) -> vImage_Buffer {
var _buffer = buffer
var destinationBuffer = vImage_Buffer()
vImageBuffer_Init(&destinationBuffer, buffer.height, buffer.width, 8, vImage_Flags(kvImageNoFlags))
let redCoefficient: Float = 0.2126
let greenCoefficient: Float = 0.7152
let blueCoefficient: Float = 0.0722
let divisor = Float(0x1000)
var coefficientsMatrix = [
Int16(redCoefficient * divisor),
Int16(greenCoefficient * divisor),
Int16(blueCoefficient * divisor)
]
var preBias: Int16 = 0
let postBias: Int32 = 0
vImageMatrixMultiply_ARGB8888ToPlanar8(&_buffer, &destinationBuffer,
&coefficientsMatrix, 0x1000, &preBias, postBias, vImage_Flags(kvImageNoFlags))
return destinationBuffer
}
func main() {
guard let imageBuffer = uiImageToBuffer(image: image),
var grayBuffer = grayBuffer(buffer)
else { return }
var monoFormat = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 8,
colorSpace: Unmanaged.passRetained(CGColorSpaceCreateDeviceGray()),
bitmapInfo: CGBitmapInfo(rawValue: CGImageAlphaInfo.none.rawValue),
version: 0, decode: nil, renderingIntent: .defaultIntent)
let cgImage = vImageCreateCGImageFromBuffer(&grayBuffer, &monoFormat, nil, nil, vImage_Flags(kvImageNoFlags), nil)
if let cgImage = cgImage {
imageView.image = UIImage(cgImage: cgImage.takeRetainedValue())
}
free(imageBuffer.data)
free(grayBuffer.data)
}
预偏置应该是一个包含四个元素的向量。试试这个:
let preBias: [Int16] = [0, 0, 0, 0]
let postBias: Int32 = 0
vImageMatrixMultiply_ARGB8888ToPlanar8(&_buffer,
destinationBuffer,
&coefficientsMatrix,
0x1000,
preBias,
postBias,
vImage_Flags(kvImageNoFlags))