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 <chromaticsp...@yahoo.com> 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 (Cocoa-dev@lists.apple.com)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/bravobug%40gmail.com
>
> This email sent to bravo...@gmail.com
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to