• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Setting a NSPitmapImageRep which NSImages draw to
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Setting a NSPitmapImageRep which NSImages draw to


  • Subject: Re: Setting a NSPitmapImageRep which NSImages draw to
  • From: Henrik Dalgaard <email@hidden>
  • Date: Fri, 18 Jun 2004 14:43:48 +0200

Den Jun 18, 2004, kl. 1:36 AM, skrev Andrew Platzer:

On Jun 17, 2004, at 10:09 AM, Henrik Dalgaard wrote:
I can not figure out how to pull pixels out of a NSImage!

[image lockFocus];
NSBitmapImageRep* rep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, [image size].width, [image size].height)];
[image unlockFocus];

That will return an NSBitmapImageRep that is 8 bits/sample, 3 or 4 samples/pixel depending on whether the image has alpha in RGBA premultiplied format. You can then read the individual pixels by using the following:

unsigned char* pixel = [rep bitmapData] + y * [rep bytesPerRow] + x * [rep samplesPerPixel];
unsigned char r = pixel[0];
unsigned char g = pixel[1];
unsigned char b = pixel[2];
unsigned char a = pixel[3]; // if spp == 4 or hasAlpha == YES

Thanks this is good information, which I could not find in the documentation. It seems that I only have to be concerned with the optional alpha channel. I is nice to avoid making support for all the pixelformats NSBitmapImageRep supports.
I suppose (but check anyways) that i won't get compressed data, planar data or CMYK pixels.

I have tried to force the image to get an alpha channel by sending setAlpha:YES to its cached image rep, but that did not work. I don't really need the alpha channel, but the vImage framework requires alpha when dealing with chunky pixels. I was looking for a way to avoid manually inserting the alpha bytes. Switching to planar pixels in my code will probably be my best option. The alpha is also the first component in vImage.

Thanks,
-Hernik
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.


  • Follow-Ups:
    • Re: Setting a NSPitmapImageRep which NSImages draw to
      • From: Ian Henderson <email@hidden>
  • Prev by Date: Re: Decorating Images
  • Next by Date: Re: low level webview/drawer crash, need help to find a fix
  • Previous by thread: Re: Decorating Images
  • Next by thread: Re: Setting a NSPitmapImageRep which NSImages draw to
  • Index(es):
    • Date
    • Thread