TruerWords Logo
Google
 
Web www.truerwords.net

Search TruerWords

Welcome
Sign Up  Log On

“My First Core Image Filter (for Acorn)”

From: Seth Dillingham In Response To: Top of Thread.  
Date Posted: Thursday, September 27, 2007 10:20:30 PM Replies: 3
   
Enclosures: None.

While playing with Acorn, I was trying to figure out how to make a scan of some black and white line art appear as black-and-transparent, so it would look as though it was "drawn" directly onto whatever background over which it was placed.

Not *quite* as easy as it sounds, for a couple of reasons. The biggest problem is that you can't just give all white pixels an opacity of 0% (or 100% transparency). Black and white line art "scans" are not just black and white, even after you clean them up: the edges of the black have a sort of anti-aliasing: light gray pixels that help to smooth out the image. The second problem is that Acorn doesn't support channel-based editing. You can change opacity and color all you want to, but you can't just view and edit the image's alpha channel.

I asked Gus if he thought I should do an Acorn plugin (and described what I was trying to accomplish). See, years ago I wrote a little plugin for a little company called Adobe, for their little app called Photoshop (v.4, I think). The money was good, and we still have the B&W G3 and 17" display that they sent me along with the check. Anyway, I figured if I could write a Photoshop plugin for Adobe, I could write an Acorn plugin for my own use, right? Right!?

Gus said I would be better served writing a Core Image Filter. It's like a filter/plugin, but it's for the OS instead of a specific app.

I didn't make time to work on it until today. It took a couple of hours to find the right documentation and actually write the filter. Gus had sent me the heart of the filter in the form of a single, very short function that just manipulated the alpha value of a pixel based on the channel's combined r/g/b values.

Apple's docs for this stuff are surprisingly bad. I found one tutorial that helped a bit, and then in the end I just had to try a few things to make it work.

Three Images, Three Backgrounds

Plain black and white image, no transparency.
White converted to 100% transparency. The 'halo' comes from the light gray pixels that smooth out the black lines when the image is over a white background.
Image after applying the 'White to Alpha' filter in Acorn.
Plain black and white image, no transparency.
White converted to 100% transparency. The 'halo' comes from the light gray pixels that smooth out the black lines when the image is over a white background.
Image after applying the 'White to Alpha' filter in Acorn.
Plain black and white image, no transparency.
White converted to 100% transparency. The 'halo' comes from the light gray pixels that smooth out the black lines when the image is over a white background.
Image after applying the 'White to Alpha' filter in Acorn.

So what does it do, exactly? Well, the idea was to display black-and-white line art so that it would look "good" on any background (not just white). In Photoshop, the easiest way to do this is to copy the artwork to the alpha channel, invert the channel (100% -> 0%, 0% -> 100%, 25% -> 75%, etc.), go back to the main image (either the RGB channels or the Gray channel) and bring the brightness way down and the contrast way up so that any pixels which were anything other than pure white are now pure black.

(Why? Because their "gray" levels will now be opacity levels, so they will blend against any background. What was light gray is now solid black but mostly transparent.)

That's how I'd do it in Photoshop. Now I can do it in Acorn with a single click. :-D

If you want it, feel free to download a copy of the filter. Copy it to /Libryar/Graphics/Image Units/ or ~/Library/Graphics/Image Units/. I've used it in Acorn, Core Image Fun House, and Quartz Composer… so I can assure you that it works on my machine. ;-)

Update: If you install it, you'll find it in Acorn under the Filters->Stylize menu. Maybe not the best place for it, but I'm not sure where it should be.


Discussion Thread:
Trackbacks:

There are no trackbacks.



TruerWords
is Seth Dillingham's
personal web site.
Truer words were never spoken.