How to determine a single pixels alpha?
How to determine a single pixels alpha?
- Subject: How to determine a single pixels alpha?
- From: Jesse Grosjean <email@hidden>
- Date: Sun, 30 Sep 2001 17:36:41 -0400
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;
}
Thanks,
Jesse