NSBitmapImageRep/CIImage questions

Apprentice
Posts: 5
Joined: 2008.08
Post: #1
Hi I want to begin with some image processing, I want to get an image, apply filters and then accessing pixel data to do some algorithms

1.-I create a CIImage object from a jpg "myCiimage"
2.-I apply some filters

3.-I try to create a NSBitmapImageRep object with
NSBitmapImageRep * myImage = [[NSBitmapImageRep alloc] initWithCIImage:"myCiimage"];

it compiles fine but when I run the application I get the error message "Uncaught exception: <NSInvalidArgumentException> *** -[NSBitmapImageRep initWithCIImage:]: selector not recognized"


here is my code(I'm modifying the CIExposureSample example):



CIContext * context;
CIImage * image;
NSBitmapImageRep * myImage;
CGRect cg = CGRectMake(NSMinX(rect), NSMinY(rect),
NSWidth(rect), NSHeight(rect));
context=[[NSGraphicsContext currentContext] CIContext];

image = [CIImage imageWithContentsOfURL: [NSURL fileURLWithPath:
[[NSBundle mainBundle] pathForResource: @"myJPGimage" ofType: @"jpg"]]];

filter1 = [CIFilter filterWithName: @"CIExposureAdjust"
keysAndValues: @"inputImage", image, nil];
[filter1 setValue: [NSNumber numberWithFloat: exposureValue] forKey: @"inputEV"];

image = [filter1 valueForKey: @"outputImage"];

filter2 = [CIFilter filterWithName:@"CIHueAdjust"];
[filter2 setValue: image forKey: @"inputImage"];
[filter2 setValue: [NSNumber numberWithFloat: hueValue] forKey: @"inputAngle"];

image = [filter2 valueForKey: @"outputImage"];
//until here it works fine
myImage = [[NSBitmapImageRep alloc] initWithCIImage:image];


what do you think it can be wrong?
Sad
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #2
You're not trying to run it on Tiger by any chance are you?

NSBitmapImageRep's initWithCIImage method is 10.5 only. NSCIImageRep's initWithCIImage is available in 10.4, which is why you wouldn't be getting any compile errors.

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2008.08
Post: #3
Oh crap, yes I'm in tiger

any other solution to this? I want to get pixel data from a CIImage object...
Quote this message in a reply
Member
Posts: 320
Joined: 2003.06
Post: #4
I'm not sure if this is the easiest way, but probably via CIImageRep's (NSImageRep's) drawInRect: method. There are quite a few different ways of capturing that output into various objects, the easiest of which is probably NSImage with something like this (untested):
Code:
NSImage* anImage = [[NSImage alloc] initWithSize:rect.size];
[anImage lockFocus];

[[[[CIImageRep alloc] initWithCIImage:image] autorelease] drawInRect:rect];

[anImage unlockFocus];

Otherwise try looking at graphicsContextWithBitmapImageRep: and make that context current and use the same drawInRect function.

Lots of options, none as easy as the one you were trying to use unfortunately.

Chopper, iSight Screensavers, DuckDuckDuck: http://majicjungle.com
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #5
I get pixel data from a CIImage like this. Works on Tiger:

Code:
CGContextRef cgContext;
CIContext * ciContext;
CIImage * outputImage;
unsigned char * outputPixels;

...

outputPixels = calloc(width * height, BYTES_PER_PIXEL);
cgContext = CGBitmapContextCreate(outputPixels,
                                  width,
                                  height,
                                  8,
                                  width * 4,
                                  CGColorSpaceCreateDeviceRGB(),
                                  kCGImageAlphaPremultipliedLast);
ciContext = [CIContext contextWithCGContext: cgContext options: [NSDictionary dictionary]];

...

[ciContext drawImage: outputImage inRect: CGRectMake(0, 0, width, height) fromRect: CGRectMake(0, 0, width, height)];
CGContextRelease(cgContext);

Once you've done that, outputPixels contains the pixel data from the CIImage (outputImage).
Quote this message in a reply
Apprentice
Posts: 5
Joined: 2008.08
Post: #6
how do I get the BYTES_PER_PIXEL from my jpg image?

also how is the information stored in outputPixels? can I create a NSBitmapImageRep object from outputPixels? to have a method to get the color of the pixel (x,y), a NSColor or something I can work on, or can I get somehow that information from outputPixels directly?
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #7
Whoops, I forgot to replace BYTES_PER_PIXEL in my paste with its actual value, which is 4. outputPixels ends up as a linear array of bytes representing pixel components (four per pixel, premultiplied), laid out as R, G, B, A, R, G, B, A, ..., so you can just look up color values directly from the array.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Strange OpenGl or NSBitmapImageRep error magnusrw 4 3,363 Apr 24, 2009 11:13 AM
Last Post: magnusrw
  Copying between NSBitmapImageRep objects Emehr 3 4,219 Oct 31, 2007 01:27 PM
Last Post: Emehr
  NSBitmapImageRep ICC profile stripping? kelvin 0 2,161 Mar 9, 2006 05:42 PM
Last Post: kelvin
  How to get resolution of an NSImage or NSBitmapImageRep? aegidian 4 5,770 Oct 18, 2005 02:39 AM
Last Post: aegidian
  "Inconsistent set of values to create NSBitmapImageRep" Help! Joseph Duchesne 2 6,048 Sep 30, 2005 05:32 PM
Last Post: Joseph Duchesne