Quartz Composer Patch : Mask Extraction from Any Image
/* Published by b-l-a-c-k-o-p.com
Copyright (c) 2007-2012
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
*/
SAMPLE: QameraSofterIris.qtzAs I was dissatisfied with the mask extraction/merging available in QC, we wrote this patch to be able to extract a matte, or alpha channel, from just about any source image. The kernel works by taking the dot product of the mask pixel, which ultimately is a normalized distance from black, and migrates that value into the alpha channel of the source image. This kernel works great, we use it everywhere.
kernel vec4 msBlackMergeAlphaFromGrey(sampler image, sampler mask)
{
vec4 pixel = sample(image, samplerCoord(image));
vec4 maxel = sample(mask, samplerCoord(mask));
float maskpel = clamp(0.333 * (maxel.r + maxel.g + maxel.b), 0.0, 1.0);
pixel.a = maskpel;
return pixel;
}
If you need the inverse of the matte, either use this kernel, or run the image through Color Invert before extracting:
kernel vec4 msBlackMergeAlphaFromGrey(sampler image, sampler mask)
{
vec4 pixel = sample(image, samplerCoord(image));
vec4 maxel = sample(mask, samplerCoord(mask));
float maskpel = clamp(0.333 * (maxel.r + maxel.g + maxel.b), 0.0, 1.0);
pixel.a = 1.0 - maskpel;
return pixel;
}