如何使用kciFormatRGBAh获得与Core Image iOS的一半浮动?
我正在尝试获取浮点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
上工作,无论其来源,filter输出或其他。
在iOS上使用[CIContext render:toBitmap:rowBytes:bounds:format:colorSpace:]
时有两个约束[CIContext render:toBitmap:rowBytes:bounds:format:colorSpace:]
- rowBytes必须是8个字节的倍数
- 在模拟器下调用它不被支持
这些约束来自于iOS上使用RGBAh的OpenGLES行为。