Search code examples
iosaccelerate-framework

Modify Alpha based on RGB values with Accelerate Framework


Is it possible to adjust the alpha using the Accelerate framework based on the pixels RGB value?

Specifically I want to set the Alpha to 0 if the color is black (RGB 0/0/0)


Solution

  • Not with Accelerate. In CoreGraphics, you can set a masking color, if you like.

    https://developer.apple.com/library/mac/documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_images/dq_images.html#//apple_ref/doc/uid/TP30001066-CH212-CJBHCADE

    Strictly speaking, if you set a masking color on a CGImageRef and then decode it to pixels with vImageBufer_InitWithCGImage, it should do that which /might/ qualify. However, CG is doing the masking work in that case.

    You can file a Radar asking for it. It hasn't been a priority so far because the alpha you get is not anti-aliased.

    typedef __attribute__ ((ext_vector_type( 4),__aligned__( 16))) uint32_t uint4;
    
    // ARGB example (on little endian processor):
    //   uint4 result = maskByColor( pixels, (uint4)0, (uint4) 0x000000ff );
    uint4 maskByColor( uint4 fourPixels, uint4 maskColor, uint4 alphaMask ){
        return fourPixels & ~(maskColor == (fourPixels & ~alphaMask));
    }