Re: Brightness -> Alpha mod?
Re: Brightness -> Alpha mod?
- Subject: Re: Brightness -> Alpha mod?
- From: Stephen Sample <email@hidden>
- Date: Fri, 14 Oct 2005 09:23:37 -0400
On Oct 13, 2005, at 12:01 PM, email@hidden wrote:
Thanks for the response! I also received one like this from a Mr.
Randolph leading me to an example on apple's site which accomplishes
a similar goal. I'll look into implementing one of these two
suggestions, so thank you both VERY much! ^_^ Incedentally, is there
a reasonably quick way to transform the darkness of each pixel to its
alpha value as well? Thus, full black becomes completely opaque, and
full white becomes completely transparent?
If you don't mind requiring Tiger, you can do this very easily with
Core Image. Core Image will even auto-optimize the image processing
to run efficiently on your CPU and graphics card.
Assuming that you have a color image to start with, you'd chain
several filters:
CIColorMonochrome (to make the image greyscale),
CIColorInvert (to invert the greyscale image so that dark pixels
in the original will be opaque), and
CIMaskToAlpha (to build the mask image).
If your initial image is already greyscale, you could skip the first
filter.
/*--- untested pseudocode ---*/
NSURL *url;
CIImage * sourceImage;
CIImage * monochromeImage;
CIImage * invertedMonochromeImage;
CIImage * alphaMaskImage;
CIFilter * monochromeFilter;
CIFilter * invertFilter;
CIFilter * alphaMaskFilter;
url = [NSURL fileURLWithPath:path];
sourceImage = [CIImage imageWithContentsOfURL:url];
// set up the initial monochrome filter to turn a color image into
greyscale
//-- if your initial image is already greyscale, you can skip this
filter block
monochromeFilter = [CIFilter filterWithName:@"CIColorMonochrome"];
[monochromeFilter setValue:sourceImage forKey:@"inputImage"];
// use black as the monochrome color (i.e. make the image greyscale)
[monochromeFilter setValue:[CIColor colorWithString:@"0.0 0.0
0.0 0.0"] forKey:@"inputColor"];
// make the dynamic range go all the way from black to white
[monochromeFilter setValue:[NSNumber numberWithFloat:1.0]
forKey:@"inputIntensity"];
// grab the output image to pass to the next filter
monochromeImage = [monochromeFilter valueForKey:@"outputImage"];
// set up the inversion filter to make dark pixels from the source
opaque
invertFilter = [CIFilter filterWithName:@"CIColorInvert"];
[invertFilter setValue: monochromeImage forKey:@"inputImage"];
// grab the output image to pass to the next filter
invertedMonochromeImage = [invertFilter
valueForKey:@"outputImage"];
// build an alpha mask from the greyscale map we created above
alphaMaskFilter = [CIFilter filterWithName:@"CIMaskToAlpha"];
[alphaMaskFilter setValue:invertedMonochromeImage
forKey:@"inputImage"];
// grab the output image to pass to the bitmap export
alphaMaskImage = [alphaMaskFilter valueForKey:@"outputImage"];
/*--- enough with the untested pseudocode, already! ---*/
Otherwise, if you need pre-Tiger support, you might need to mess with
the pixel values yourself...
Ta,
-Stephen
_______________________________________________
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