uniform sampler2D tex0; 
uniform vec4 blendenabled;
uniform vec4 leftedge;
uniform vec4 rightedge;
uniform float pfactor;
uniform vec4  finalColorScale;
uniform vec4  finalColorBias;
uniform vec3  blendInvGamma;

vec4 colorScaleBias( vec4 inColor )
{
    return( inColor*finalColorScale + finalColorBias );
}

vec4 edgeBlend( vec4 finalcolor, float percent )
{
    if ( percent < 1.0 )
    {
        if ( percent < .5 )
        {
            finalcolor.a = 0.5 * pow( (2.0 * percent ), pfactor );
        }
        else
        {
            finalcolor.a = 1.0 - .5 * pow( ( 2.0 * ( 1.0 - percent ) ), pfactor );
        }
        finalcolor.a = pow( finalcolor.a, blendInvGamma[ 0 ] );
    }
    return finalcolor;
}

float blendPercent( in float blendcoord )
{
    // beginnings of calculations for sep top/bottom blend
    float dl = leftedge.x  - leftedge.y;
    float dr = rightedge.x - rightedge.y;

    float localrightedge = rightedge.y + gl_TexCoord[ 1 ].y * dr ;
    float localleftedge  = leftedge.y  + gl_TexCoord[ 1 ].y * dl;

    // compute the percent based on blend parameters
    float percent = 1.0;
    if ( blendcoord < leftedge.y )
        percent = (blendcoord-leftedge.x)/(leftedge.y-leftedge.x);
    else if ( blendcoord > rightedge.x )
        percent = 1.0-(blendcoord-rightedge.x)/(rightedge.y-rightedge.x);
    return( percent );
}

void main()
{
    // acquire ut screen texture
    vec4 projtex = gl_TexCoord[ 0 ] / gl_TexCoord[ 0 ].w;
    vec4 finalcolor = texture2D( tex0, projtex.xy );

    // compute blend percentage
    float percent = 1.0;
    if ( blendenabled.x != 0.0 )
    {
        percent = blendPercent( gl_Color.x );
    }

    // apply results, scale color, blend
    if ( blendenabled.y != 0.0 )
    {
        finalcolor = vec4( 1.0 );
    }
    gl_FragColor = edgeBlend( colorScaleBias( finalcolor ), percent );
}
