Search code examples
iosiphoneswiftipadciimage

CIColorInvert and CIColorMatrix does not invert image properly


I got stuck on inverting CIImage colors. I am creating CIImage from CGImage and then appling CIColorInvert filter or CIColorMatrix filter but none of them is working properly. When rendering CIImage as CGImage, I obtain results, which seems to be saturated (see attached example).

exmples of color invert using different approaches

I guess that the problem is colorspace but I have no idea how to fix it. I was following Core Image Programming Guide (chapter: Subclassing CIFilter: Recipes for Custom Effects, section: Subclassing CIFilter to Create the CIColorInvert Filter) but without any success.

I would be very thankful for any hints...


Solution

  • The problem was in working color space. I found answer to my question here. The CIContext which I creates looks now as follwing:

    var ciContext = CIContext(options: [
        kCIImageColorSpace: NSNull(), 
        kCIImageProperties: NSNull(), 
        kCIContextWorkingColorSpace: NSNull()
    ])
    

    To be honest, I do not know exactly the background, so I am not sure, why it is not working without setting WorkingColorSpace to NSNull. If someone knows, I would be really thankful for explanation.