Re: re Finding transparent pixels in an image
Re: re Finding transparent pixels in an image
- Subject: Re: re Finding transparent pixels in an image
- From: Ron Fleckner <email@hidden>
- Date: Sat, 27 Aug 2005 11:01:39 +1000
On 27/08/2005, at 7:38 AM, Fjölnir Ásgeirsson wrote:
This is extremely easy :)
[usedImage lockFocus];
NSColor *pixelColor = NSReadPixel(localPoint);
[usedImage unlockFocus];
if([pixelColor alphaComponent] > MINIMUM_ALPHA && status !=
DISABLED_STATUS)
{
_doingHitTest = NO;
mouseOffFor = 0;
return self;
}
Now that's meant to be used with NSImage (not sure if it works with
nsbitmapimagerep)
but if you just get the pixel value, what you want to do is to find
the alpha component
of the pixel *not* the rgb's (you have a rgba image)
(char)data[(4 * ((y - 1) * (int)width + x)) + 4] < 1
explanation:
each pixel is 4 bytes (r g b and a) that's why we multiply with 4
then (y - 1) is to get to the end of the last row before our row we
multiply that with the
width because that's the number of pixels in each row, add 'x' to
it to get to our
'x' in the row and last but not least we add 4 to that value to get
the alpha.
p.s. if you didn't figure it out already the < 1 is to see if the
alpha is less than 1
aka. transparent
This is for 24bpp images the nsimage example works with all bpp's
but it's slower and I'm not sure if you can make nsbitmapimagreps
lock focus
On 27/08/2005, at 5:02:46 AM, Andy Matuschak wrote:
NSColor has four values: red, green, blue, and alpha. Simply check if
the last one's 1. If you're reading directly from the unsigned char *
bitmap data, then the alpha value would be the last value in each
group of four samples (unless the bitmap format dictates ARGB
instead). Now, if your bitmap rep doesn't have four samples per pixel
(check with [imageRep samplesPerPixel]), then you already know that
every value in the image is opaque. Hope this helps.
- Andy Matuschak
Yay! Thanks a lot, guys. All I had to do was add one more item (p1
[3]) in my list of values in the 'if' statement below:
unsigned char *srcData = [srcImageRep bitmapData];
unsigned char *destData = [destImageRep bitmapData];
unsigned char *p1, *p2;
for ( y = 0; y < h; y++ ) {
for ( x = 0; x < w; x++ ) {
p1 = srcData + n * (y * w + x);
p2 = destData + 3 * (y * w + x);
if (p1[0] + p1[1] + p1[2] + p1[3] == 0) { // added p1[3]
here
p2[0] = 255;
p2[1] = 255;
p2[2] = 255;
} else {
p2[0] = p1[0];
p2[1] = p1[1];
p2[2] = p1[2];
}
}
}
The new NSImage, made with the destImageRep, is used to make a
patternColor for my app's background. If the original image had
transparency, any previous colors or patternColors would show
through, and any shadows drawing over that background would get
blacker and blacker. All fixed, now.
Thanks again.
Ron Fleckner _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden