我想获取浮点数格式下CIImage
每个像素的RGBA值。
我尝试使用CIContext
并以kCIFormatRGBAh
渲染,但输出全为零。否则我的下一步将是从半精度转换为全精度。
我做错了什么?我也在Objective-C中尝试过,结果相同。
let image = UIImage(named: "test")!
let sourceImage = CIImage(CGImage: image.CGImage)
let context = CIContext(options: [kCIContextWorkingColorSpace: NSNull()])
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bounds = sourceImage.extent()
let bytesPerPixel: UInt = 8
let format = kCIFormatRGBAh
let rowBytes = Int(bytesPerPixel * UInt(bounds.size.width))
let totalBytes = UInt(rowBytes * Int(bounds.size.height))
var bitmap = calloc(totalBytes, UInt(sizeof(UInt8)))
context.render(sourceImage, toBitmap: bitmap, rowBytes: rowBytes, bounds: bounds, format: format, colorSpace: colorSpace)
let bytes = UnsafeBufferPointer<UInt8>(start: UnsafePointer<UInt8>(bitmap), count: Int(totalBytes))
for (var i = 0; i < Int(totalBytes); i += 2) {
println("half float :: left: \(bytes[i]) / right: \(bytes[i + 1])")
// prints all zeroes!
}
free(bitmap)
这里有一个关于获取 CIAreaHistogram
输出的相关问题的链接,这也是为什么我想得到浮点数值而不是整数的原因,但是我似乎无法使 kCIFormatRGBAh
在任何来源、过滤器输出或其他情况下对任何 CIImage
都起作用。