/*****************************************************************************
 *
 * Copyright (C) 2010 Adobe Systems Incorporated
 * All Rights Reserved.
 *
 * NOTICE:  All information contained  herein is,  and remains the property of
 * Adobe Systems Incorporated and its suppliers, if any.  The intellectual and
 * technical  concepts  contained  herein  are  proprietary  to  Adobe Systems
 * Incorporated  and  its suppliers  and may  be covered  by U.S.  and Foreign
 * Patents, patents in process, and are protected by trade secret or copyright
 * law.  Dissemination of this information or reproduction of this material is
 * strictly forbidden  unless prior  written permission is obtained from Adobe
 * Systems Incorporated.
 *
 *****************************************************************************/
<languageVersion: 1.0;>

kernel AlphaFromMaxColor
<
	namespace: "AfterEffects";
	vendor : "Adobe Systems Incorporated";
	version : 2;
    description : "Estimate alpha based on color channels.";
	displayname: "Alpha From Max Color";
	category: "Utility";
>
{
	
	input image4 src;
	output pixel4 dst;
	
    void
    evaluatePixel()
    {
		dst = sampleNearest(src, outCoord());
		dst.rgb *= dst.a;							// premultiply first. does nothing on opaque
		
		dst.a = max(max(dst.r, dst.g), dst.b);		// take max of color components
		dst.a *= 254.0/255.0;						// and scale slightly.
		
		// assume incoming colors are premultiplied against black
		// alpha. unmultiply if dst.a != 0 [otherwise div/0]
		if (dst.a != 0.0) {
			dst.rgb /= dst.a;
		}
	}
}