Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colors Inverted when Creating UIImage from CIImage (iOS 6.0)

I've been trying a ton of different options and tricks for getting a UIImage to build properly from a CIImage. But, every time I create the CIImage its colors seem to be inverted.

The most recent code I have is:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"C4Sky" withExtension:@"png"];
CIImage *c = [CIImage imageWithContentsOfURL:url];
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

Which produces the following image:

The inverted color image.

However, building an image like this:

UIImage *i = [UIImage imageNamed:@"C4Sky"];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

... produces the following image:

enter image description here

I've tried a lot of different ways of constructing the CIImage.

  • https://stackoverflow.com/a/7788510/1218605
  • http://www.raywenderlich.com/5689/beginning-core-image-in-ios-5
  • [CIImage imageWithData:UIImagePNGRepresentation(img)];
  • almost every other CIImage constructor...

Is this happening because the pixel format in iOS for a CIImage is ARGB? (It seems this is the only format possible on iOS6 right now)

  • CIImage Pixel Formats (iOS)

If so, how can I swap pixel formats get this CIImage to look normal?



I've created a project repo on git showing all the major different ways I've tried (not including variations of each). There are 6 methods, the first 5 failing:

https://github.com/C4Code/CIImageTest

The last method works, but it's really dirty:

-(void)test6 {
    UIImage *img = [UIImage imageNamed:@"C4Sky"];

    CGContextRef    bitmapContext = NULL;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;
    CGSize          size = img.size;
    bitmapBytesPerRow   = (size.width * 4);
    bitmapByteCount     = (bitmapBytesPerRow * size.height);
    bitmapData = malloc( bitmapByteCount );
    bitmapContext = CGBitmapContextCreate(bitmapData, size.width, size.height,8,bitmapBytesPerRow,
                                          CGColorSpaceCreateDeviceRGB(),kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(bitmapContext, (CGRect){CGPointZero,size}, img.CGImage);
    CGImageRef imgRef = CGBitmapContextCreateImage(bitmapContext);

    CIImage *cii = [CIImage imageWithCGImage:imgRef];
    UIImage *finalImage = [UIImage imageWithCIImage:cii];

    UIImageView *uiiv = [[UIImageView alloc] initWithImage:finalImage];
    [self.canvas addSubview:uiiv];
    //works but it's dirrrrrrrty
}

I am drawing from a UIImage into my own graphics context, and because this works it has me believe that there might be a bug with CIImage's native implementation. That is, creating a CIImage from a UIImage doesn't work... Again, the only thing I can think of is that the CIImage is in ARGB space, and the UIImage isn't... But, I don't know how to get around this?

Is this a bug?

like image 249
C4 - Travis Avatar asked Jan 31 '13 05:01

C4 - Travis


1 Answers

A few additional variants to try:

//Grab the CIImage directly from the UIImage
UIImage *img = [UIImage imageNamed:@"C4Sky.png"];
CIImage *c = img.CIImage;
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

Or

//Maybe you need to set your own color space?
UIImage *img = [UIImage imageNamed:@"C4Sky.png"];
CIImage *c = [CIImage imageWithCGImage:img.CGImage options:@{kCIImageColorSpace: kCGColorSpaceModelRGB}];
UIImage *i = [UIImage imageWithCIImage:c];
UIImageView *uiiv = [[UIImageView alloc] initWithImage:i];
[self.canvas addSubview:uiiv];

-jt

like image 68
jimt Avatar answered Oct 14 '22 15:10

jimt