Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to determine a single pixels alpha?



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




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.