How to make use of kCIFormatRGBAh to get half floats on iOS with Core Image?

浪尽此生 提交于 2019-12-10 21:35:47

问题


I'm trying to get the per-pixel RGBA values for a CIImage in floating point.

I expect the following to work, using CIContext and rendering as kCIFormatRGBAh, but the output is all zeroes. Otherwise my next step would be converting from half floats to full.

What am I doing wrong? I've also tried this in Objective-C and get the same result.

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)

Here's a related question about getting the output of CIAreaHistogram, which is why I want floating point values rather than integer, but I can't seem to make kCIFormatRGBAh work on any CIImage regardless of its origin, filter output or otherwise.


回答1:


There are two constraints on using RGBAh with [CIContext render:toBitmap:rowBytes:bounds:format:colorSpace:] on iOS

  1. the rowBytes must be a multiple of 8 bytes
  2. calling it under simulator is not supported

These constraints come from the behavior of OpenGLES with RGBAh on iOS.



来源:https://stackoverflow.com/questions/28416330/how-to-make-use-of-kciformatrgbah-to-get-half-floats-on-ios-with-core-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!