Re: Transparency Help 2
Re: Transparency Help 2
- Subject: Re: Transparency Help 2
- From: BravoBug Software <email@hidden>
- Date: Mon, 23 Nov 2009 18:32:42 -0800
Here is a method that applies 'mask' based on a given NSColor. ie, you
pass it an NSImage, a NSColor (in your case, white), and a threshold,
and it will make transparent all the pixels of that color within a
threshold. So you can use this to knock out the white pixels of an
NSImage.
Then, of course, just draw that new NSImage into your view using
NSCompositeSourceOver.
(PS: This was thrown together pretty quickly so you may need to tweak
it if it doesn't suit your purposes, but you can at the least perhaps
use it as a jumping off point)
-(NSImage*)calcMaskForColor:(NSColor*)c :(int)threshold :(NSImage*)img
{
NSBitmapImageRep *imageData = [NSBitmapImageRep imageRepWithData:
[img TIFFRepresentation]];
int i=0;
int w = [imageData pixelsWide];
int h = [imageData pixelsHigh];
int numPixels = w * h;
unsigned char *data = [imageData bitmapData];
unsigned char vals[4];
unsigned char col[3];
//assumes nscolor passed has r/g/b components
//ie, dont just pass [NSColor whiteColor]
col[0] = [c redComponent]*255;
col[1] = [c greenComponent]*255;
col[2] = [c blueComponent]*255;
int dif;
//assumes RGBA
for(i=0;i<(numPixels*4);i+=4)
{
memcpy(vals, data+i, 4);
//custom alpha check, you can remove or modify:
if(vals[3] > 240)
{
//visible pixel, check color
dif = 0;
dif += abs(vals[0] - col[0]);
dif += abs(vals[1] - col[1]);
dif += abs(vals[2] - col[2]);
if(dif < threshold)
{
memset(data + i, 0, 4);
}
}
}
NSImage *newImg = [[[NSImage alloc] initWithSize: NSMakeSize(w,h)]
autorelease];
[newImg addRepresentation: imageData];
return newImg;
}
-matt
On Mon, Nov 23, 2009 at 10:01 AM, R T <email@hidden> wrote:
> Given: A litho Image, Black & White pixels only.
> I want to show just the Black Pixels in a subclassed NSView.
>
> I've posted previously with some code and had 1 simple response that has not panned out for me. Any ideas out there, anybody been down this road?
>
> Thanks
> Rick
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden