AlphaMatteComposite
From Adobe Labs
This Pixel Bender kernel accepts a source image and alpha matte and composites the two together, resulting in a masked version of the source image.
Note: This filter has been written for compatibility with Flash.
// *****************************************************************************************
// AlphaMatteComposite.pbk
//
// Copyright (c) 2008 Ryan Taylor | http://www.boostworthy.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// *****************************************************************************************
//
// + + + + + + + + +
//
// *****************************************************************************************
<languageVersion : 1.0;>
kernel AlphaMatteComposite
<
namespace : "Boostworthy::Filters";
vendor : "Ryan Taylor";
version : 1;
description : "Given a source image and grayscale matte which represents alpha, a composite of the two will result in a masked source image.";
>
{
input image4 source;
input image4 alphaMatte;
output pixel4 result;
void evaluatePixel()
{
// Note: The 'alphaMatte' input is of type image4 rather
// than image1 for maximum compatibilty with Flash.
float2 coord = outCoord();
pixel4 sampSource = sampleNearest(source, coord);
pixel4 sampAlphaMatte = sampleNearest(alphaMatte, coord);
pixel1 alphaMatte = (sampAlphaMatte.r + sampAlphaMatte.g + sampAlphaMatte.b) / 3.0;
result = sampSource * alphaMatte;
}
}
