• 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: How to determine a single pixels alpha?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to determine a single pixels alpha?


  • Subject: Re: How to determine a single pixels alpha?
  • From: "John C. Randolph" <email@hidden>
  • Date: Sun, 30 Sep 2001 16:11:00 -0700

On Sunday, September 30, 2001, at 03:30 PM, Brian Hook wrote:

At 05:36 PM 9/30/01 -0400, Jesse Grosjean wrote:
unsigned char* pixels = [aBitmapImageRep bitmapData];
int alpha;

// i'm trying to get to the pixel specified by aPoint. I'm sure i should be able to index
// into this with an array somehow "pixels[aPoint.x][aPoint.y]" but can't figure out how
// to do that. In any case I'm pretty sure this loop is where i'm doing things wrong, the pick
// works correctly sometimes, but other returns the wrong answer.
for(i= 0; i <= aPoint.x; i++) {
for (j = 0; j <= aPoint.y; j++) {
pixels++; // red
pixels++; // blue
pixels++; // green
alpha = *pixels++;
}
}

I don't know the ordering of pixels in BSBitmapImageRep off the top of my head, but you seem to be stepping through it in x-major order, so assuming that, you should be able to fetch the pixel you want directly just doing:

unsigned char *pPixel = &pixels[ aPoint.x * 4 + aPoint.y * 4 * imageWidth ];

Where "imageWidth" is the width of the bitmap data in pixels.

Then it's just a matter of:

if ( pPixel[ 3 ] == 0 )
return NO;
return YES;

There's one too many multiplies in the code above.

Here's an example I have of code that fills a 256x256 image with a gradient:

static NSSize imageSize = {256.0, 256.0};

...

NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
initWithBitmapDataPlanes:nil
pixelsWide:imageSize.width
pixelsHigh:imageSize.height
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:32];

imageBytes=[bitmap bitmapData];

y = imageSize.height;

while(y--)
{
x = imageSize.width;
while(x--)
{
int
pixIndex = 4 * (y*(int)imageSize.width+x);
imageBytes[pixIndex] = x; //red
imageBytes[pixIndex +1] = 255 - x; // green
imageBytes[pixIndex +2] = 0; // blue
imageBytes[pixIndex +3] = 255; //Alpha
}
}


-jcr


"I fear all we have done is to awaken a sleeping giant and fill him with a terrible resolve." -Admiral Isoroku Yamamoto, Dec 7, 1941.


  • Follow-Ups:
    • Re: How to determine a single pixels alpha?
      • From: Brian Hook <email@hidden>
References: 
 >Re: How to determine a single pixels alpha? (From: Brian Hook <email@hidden>)

  • Prev by Date: DiscBurning Framework?
  • Next by Date: Re: How to determine a single pixels alpha?
  • Previous by thread: Re: How to determine a single pixels alpha?
  • Next by thread: Re: How to determine a single pixels alpha?
  • Index(es):
    • Date
    • Thread