Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CIColorClamp not working correctly in OS X El Capitan

I am using Swift to do some video processing. After upgrading to El Capitan (and Swift 2) my code broke. I traced an error down to the CIFilter function CIColorClamp. This function is supposed to clamp the pixel values, but in fact messes up the image extent.

    let _c:CGFloat = 0.05
    let minComp = CIVector(x:_c, y:_c, z:_c, w: 1)
    let maxComp = CIVector(x:1, y:1, z:1, w: 1)
    let clamp: CIFilter = CIFilter(name: "CIColorClamp")!
    print("clamp-in \(image.extent)")
    clamp.setDefaults()
    clamp.setValue(image, forKey: kCIInputImageKey)
    clamp.setValue(minComp, forKey: "inputMinComponents")
    clamp.setValue(maxComp, forKey: "inputMaxComponents")
    print("clamp-out \(clamp.outputImage!.extent)")

The code above produces the output:

> clamp-in (6.0, 6.0, 1268.0, 708.0)
CoreAnimation: Warning! CAImageQueueSetOwner() is deprecated and does nothing. Please stop calling this method.
> clamp-out (-8.98846567431158e+307, -8.98846567431158e+307, 1.79769313486232e+308, 1.79769313486232e+308)

The fact that this call produces an internal warning does not instill confidence either!

Can anyone confirm this behavior? What am I doing wrong?

like image 940
Harry Avatar asked Sep 27 '22 14:09

Harry


2 Answers

I also ran into this Problem. The extent was always set like this

-8.98846567431158e+307, -8.98846567431158e+307, 1.79769313486232e+308, 1.79769313486232e+308

but then I tried calling filter.debugDescription and recognized that in the extent in the sourceImage is given properly.

Here's my workaround. Because I use different filters, I ask if the filters name is 'CIColorClamp' and then I set the extent used in the CGImageRef to the values from the original image.

var extent = filteredImage.extent
if filter.name == "CIColorClamp" {
    extent = sourceImage.extent
}
let cgImage:CGImageRef = context.createCGImage(filteredImage, fromRect: extent)
UIImageJPEGRepresentation(UIImage(CGImage: cgImage), 1.0).writeToFile(...)

Before that fix I always had an Crash cause the UIImageJPEGRepresentation could not be created, cause of the wrong extent values.

So it looks like that the extent is not transferred to the filtered image.

like image 198
tetanuss Avatar answered Sep 30 '22 06:09

tetanuss


I had exactly the problem. I fixed it simply by cropping the returned image to the original image rect (Objective-C code):

if ([filter.name isEqualToString:@"CIColorClamp"]) {
    image = [image imageByCroppingToRect:sourceImage.extent];
}
like image 27
gpdawson Avatar answered Sep 30 '22 08:09

gpdawson