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

1

3

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.

Bensky answered 9/2, 2015 at 18:4 Comment(0)
F
2

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.

Fer answered 10/2, 2015 at 0:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.