Re: How to determine a single pixels alpha?
Re: How to determine a single pixels alpha?
- Subject: Re: How to determine a single pixels alpha?
- From: Peter Ammon <email@hidden>
- Date: Sun, 30 Sep 2001 19:12:55 -0400
On Sunday, September 30, 2001, at 05:36 PM, Jesse Grosjean wrote:
I'm trying to figure out the best way to determine if a NSImage is
transparent at a given point, and wondering what the best way to go
about it would be. I've gotten to the point of grabbing a
NSBitmapImageRep from the NSImage but can't seem to figure out how to
go through the bitmapData correctly. Bellow is what i have now (doesn't
work very well). Would love to hear how someone else would go about
doing this.
- (BOOL)pick:(NSPoint)aPoint using:(NSBitmapImageRep *)aBitmapImageRep {
if (![aBitmapImageRep hasAlpha]) {
return YES;
} else {
int i,j;
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++;
}
}
if (alpha == 0) {
return NO;
}
}
return YES;
}
Try alpha=*(pixels + [bitmap pixelsWide]*[bitmap
samplesPerPixel]*(int)aPoint.y + (int)aPoint.x * [bitmap
samplesPerPixel] + 3);
Don't you just love pointer arithmetic?
Hope this helps.
-Peter