/*
	Global algorithm - XPeng
*/

#version 130


uniform sampler2D 	inText;		// input sampler
out     vec4 		outText;	// output sampler

uniform float rangeX, rangeY, rangeW, rangeH;

uniform float brightness;		// Brightness ranges from -1.0 to 1.0, with 0.0 as the normal level
uniform float contrast;			// Contrast ranges from 0.0 to 4.0 (max contrast), with 1.0 as the normal level
uniform float saturation;		// Saturation ranges from 0.0 (fully desaturated) to 2.0 (max saturation), with 1.0 as the normal level
uniform float exposure;         // Exposure ranges from -10.0 to 10.0, with 0.0 as the normal level
uniform float temperature;        // choose color temperature, in degrees Kelvin
uniform float tint;               // adjust tint to compensate

// vignette
const vec2 vignetteCenter = vec2(0.5, 0.5);   // the center for the vignette in tex coords (defaults to 0.5, 0.5)
const vec3 vignetteColor = vec3(0, 0, 0); // The color to use for the Vignette (defaults to black)
const float vignetteStart = 0;  // The normalized distance from the center where the vignette effect starts. Default of 0.
const float vignetteRange = 0.75;  // The normalized range from the vignette effect start and ends. Default of 0.5.
uniform float vignetteStrength;
uniform  float aspectRatio;

// rotation
uniform  float sinRINV;
uniform  float cosRINV;

// for satuartion, Values from "Graphics Shaders: Theory and Practice" by Bailey and Cunningham
const  vec3 luminanceWeighting = vec3(0.2125, 0.7154, 0.0721);

const  vec3 warmFilter = vec3(0.93, 0.54, 0.0);
const  mat3 RGBtoYIQ = mat3(0.299, 0.587, 0.114, 0.596, -0.274, -0.322, 0.212, -0.523, 0.311);
const  mat3 YIQtoRGB = mat3(1.0, 0.956, 0.621, 1.0, -0.272, -0.647, 1.0, -1.105, 1.702);
 
void main()
{
	 vec2 v_texCoord = vec2(gl_TexCoord[0].s*rangeW+rangeX, gl_TexCoord[0].t*rangeH+rangeY);
     vec2 v_texCoordRINV;
    v_texCoordRINV.x = (v_texCoord.x - 0.5) * cosRINV + (v_texCoord.y - 0.5) * sinRINV * aspectRatio + 0.5;
    v_texCoordRINV.y = -(v_texCoord.x - 0.5) * sinRINV / aspectRatio + (v_texCoord.y - 0.5) * cosRINV + 0.5;
    v_texCoord = v_texCoordRINV;

    // source
	 vec4 col = textureLod(inText, v_texCoord, 0);

	// exposure     
     vec4 colExposure = vec4(col.rgb * pow(1.25, exposure), col.w);

	// contrast
     vec4 colContrast = vec4(((colExposure.rgb - vec3(0.5)) * contrast + vec3(0.5)), colExposure.w);
    colContrast = clamp(colContrast, 0, 1);

	// brightness
     vec4 colBrightness = vec4(colContrast.rgb + vec3(brightness), colContrast.w);
    colBrightness = clamp(colBrightness, 0, 1);

    // white banlance
	 vec3 yiq = RGBtoYIQ * colBrightness.rgb; //adjusting tint
	yiq.b = clamp(yiq.b + tint*0.5226*0.1, -0.5226, 0.5226);
	 vec3 rgb = YIQtoRGB * yiq;
	 vec3 processed = vec3(
		(rgb.r < 0.5 ? (2.0 * rgb.r * warmFilter.r) : (1.0 - 2.0 * (1.0 - rgb.r) * (1.0 - warmFilter.r))), //adjusting temperature
		(rgb.g < 0.5 ? (2.0 * rgb.g * warmFilter.g) : (1.0 - 2.0 * (1.0 - rgb.g) * (1.0 - warmFilter.g))), 
		(rgb.b < 0.5 ? (2.0 * rgb.b * warmFilter.b) : (1.0 - 2.0 * (1.0 - rgb.b) * (1.0 - warmFilter.b))));
	 vec4 colWB = vec4(mix(rgb, processed, temperature), colBrightness.a);

	// saturation
     float luminance = dot(colWB.rgb, luminanceWeighting);
     vec3 greyScaleColor = vec3(luminance);    
	 vec4 colSaturation = vec4(mix(greyScaleColor, colWB.rgb, saturation), colWB.w);
    colSaturation = clamp(colSaturation, 0, 1);

    // vegitte
    float d = distance(v_texCoord, vec2(vignetteCenter.x, vignetteCenter.y));
    float percent = smoothstep(vignetteStart, vignetteStart+vignetteRange, d);
    //float percent = clamp((d-vignetteStart)/vignetteRange, 0, 1);
    percent = vignetteStrength * percent;
    vec4 colVeg = vec4(mix(colSaturation.x, vignetteColor.x, percent), mix(colSaturation.y, vignetteColor.y, percent), mix(colSaturation.z, vignetteColor.z, percent), 1.0);

    // output
	outText = colVeg;
}

