/* vim: syntax=glsl
 * StereoCrosstalkReductionShader.frag.txt
 *
 * A fragment shader that receives a stereo input image pair and computes
 * a corrected stereo output image for one stereo channel in which stereo
 * crosstalk is suppressed to some degree.
 *
 * 'Image1' is the primary stereo input image, which should be written back
 * as output image. 'Image2' is the unrelated input image for the other eye,
 * whose influence onto the output image should be reduced, ie., Image2 is
 * the reason for ghosting in the eye related to 'Image1'.
 *
 * This shader combines both input images into a new output image with
 * the aim of reducing the ghosting. The shader will get called by Screens
 * image processing pipeline twice, once for the left eye, once for the right
 * eye, with reversed roles for Image1 and Image2, to achieve suppression for
 * both eyes.
 *
 * Ghost suppression is achieved by subtraction of the contrast of
 * the suppressed image (scaled by a contrast gain) from the target
 * image.
 *
 * background luminance / color level (set by backGroundClr uniform)
 * should not be zero as then there is no way to invert contrast w.r.t.
 * the background.
 *
 * (c) 2014 by Diederick Niehorster, licensed under MIT license.
 */

#extension GL_ARB_texture_rectangle : enable

uniform sampler2DRect Image1;
uniform sampler2DRect Image2;
uniform vec3  crosstalkGain;
uniform vec3  backGroundClr;

void main()
{
    vec2 inpos = gl_TexCoord[0].st;

    /* Get current input image pixel color values for both input images
     * at current location inpos: */
    vec4 targetimage = texture2DRect(Image1, inpos);
    vec4 suppressedimage = texture2DRect(Image2, inpos);

    /* Get contrast in suppressed image and scale by gain: */
    suppressedimage.rgb -= backGroundClr;
    suppressedimage.rgb *= -crosstalkGain;

    /* Write output pixel color corresponding to a de-ghosted Image1,
     * sum of target image and suppressed image */
    gl_FragColor.rgb = targetimage.rgb + suppressedimage.rgb;
    gl_FragColor.a = targetimage.a;
}
