alpha value from NSBitmapImageRep
I want to compute the alpha value of each pixel of an image. What i am doing right now is i create a NSBitmapImageRep from the image and use colorAtX: y: to get the alpha value form the color at that pixel. So i need to do this for each and every pixel in the image. I was just going through the WWDC 2008's 916-Getting started with Instruments session where they worked on a sample image enhancement app. They had a similar case where they had to compute the color at every pixel. The author says the process can be optimized if we first gather the imageRep's data and then access the color values directly from that data So heres how i get the data: unsigned char *data = [mImageRep bitmapData] , *pixel; Now how do i access the pixel information from over here? any idea? I'd be glad if someone can help me optimize this. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: alpha value from NSBitmapImageRep
Hi chaitanya, Actually, much better than accessing image data is getting the image drawing machinery to do the work for you. It will get the various image data formats right, and you're providing high level information (do the same thing to the entire image) that may enable optimizations. Accessing image data directly is quite error prone, as it turns out. It's easy to accidentally hardwire assumptions about bitmap format that do not hold when the OS changes. In this case, you want would want to make a CGBitmapContextRef with kCGImageAlphaOnly. This will let you produce a buffer full of your alpha values. Creating and drawing in the context looks something like this (typed in Mail): CGContextRef alphaBitmapCtx = CGBitmapContextCreate(myDestinationBuffer, width, height, 8/*bitsPerComponent*/, bytesPerRow, NULL/*colorSpace*/, kCGImageAlphaOnly); [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:alphaBitmapCtx flipped:NO]]; [image drawInRect:NSMakeRect(0,0,width,height) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; [NSGraphicsContext restoreGraphicsState]; Following this code, myDestinationBuffer has all the alpha data from the image. -Ken Cocoa Frameworks On Tue, Oct 28, 2008 at 2:59 AM, chaitanya pandit [EMAIL PROTECTED] wrote: I want to compute the alpha value of each pixel of an image. What i am doing right now is i create a NSBitmapImageRep from the image and use colorAtX: y: to get the alpha value form the color at that pixel. So i need to do this for each and every pixel in the image. I was just going through the WWDC 2008's 916-Getting started with Instruments session where they worked on a sample image enhancement app. They had a similar case where they had to compute the color at every pixel. The author says the process can be optimized if we first gather the imageRep's data and then access the color values directly from that data So heres how i get the data: unsigned char *data = [mImageRep bitmapData] , *pixel; Now how do i access the pixel information from over here? any idea? I'd be glad if someone can help me optimize this. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/kenferry%40gmail.com This email sent to [EMAIL PROTECTED] ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: alpha value from NSBitmapImageRep
On 28 Oct 2008, at 8:59 pm, chaitanya pandit wrote: unsigned char *data = [mImageRep bitmapData] , *pixel; Now how do i access the pixel information from over here? any idea? If your bitmap format is ARGB with 8 bits per component, say, then to get the value of A do: unsigned char alpha = *data;// get first alpha data += 4; // increment pointer to next pixel's alpha You have to carefully watch the buffer format though. If the bits are packed you'll have to mask off the relevant parts and shift them down to the right position. The raw pixel values have a value range depending on the number of bits per component and may or may not be premultiplied by the alpha value. Also watch the ends of each scanline - the lines are rowBytes long, not necessarily the width of the image. Basically if you understand how raw pixel buffers are laid out, then you can extract the data you want - you simply get a big chunk of memory laid out accordingly. Note that the format is completely predictable, because you specified it when you created the bitmap in the first place, or, you can ask it for all the various formatting parameters. hth, Graham ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]
Re: alpha value from NSBitmapImageRep
Thanks ken, This sounds more legal than manually accessing the image data with some hard coded values. I created a CGContextRef as you mentioned, now how can i access the alpha value at a given point in the ref? I am totally new to CG and imaging so pl. bare with me. On 28-Oct-08, at 4:08 PM, Ken Ferry wrote: Hi chaitanya, Actually, much better than accessing image data is getting the image drawing machinery to do the work for you. It will get the various image data formats right, and you're providing high level information (do the same thing to the entire image) that may enable optimizations. Accessing image data directly is quite error prone, as it turns out. It's easy to accidentally hardwire assumptions about bitmap format that do not hold when the OS changes. In this case, you want would want to make a CGBitmapContextRef with kCGImageAlphaOnly. This will let you produce a buffer full of your alpha values. Creating and drawing in the context looks something like this (typed in Mail): CGContextRef alphaBitmapCtx = CGBitmapContextCreate(myDestinationBuffer, width, height, 8/*bitsPerComponent*/, bytesPerRow, NULL/*colorSpace*/, kCGImageAlphaOnly); [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:alphaBitmapCtx flipped:NO]]; [image drawInRect:NSMakeRect(0,0,width,height) fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0]; [NSGraphicsContext restoreGraphicsState]; Following this code, myDestinationBuffer has all the alpha data from the image. -Ken Cocoa Frameworks On Tue, Oct 28, 2008 at 2:59 AM, chaitanya pandit [EMAIL PROTECTED] wrote: I want to compute the alpha value of each pixel of an image. What i am doing right now is i create a NSBitmapImageRep from the image and use colorAtX: y: to get the alpha value form the color at that pixel. So i need to do this for each and every pixel in the image. I was just going through the WWDC 2008's 916-Getting started with Instruments session where they worked on a sample image enhancement app. They had a similar case where they had to compute the color at every pixel. The author says the process can be optimized if we first gather the imageRep's data and then access the color values directly from that data So heres how i get the data: unsigned char *data = [mImageRep bitmapData] , *pixel; Now how do i access the pixel information from over here? any idea? I'd be glad if someone can help me optimize this. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/kenferry%40gmail.com This email sent to [EMAIL PROTECTED] ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]