Re: Brightness -> Alpha mod?

  • On Oct 13, 2005, at 12:01 PM, <cocoa-dev-request...> 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
previous month october 2005 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today