/*
   New MangaToon Shader v 1.2 + PSX OGL2 Noise v1.1

   Copyright (C) 2016-2018 SimoneT - simone1tarditi@gmail.com

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
    
*/

uniform vec4 OGL2Param;
uniform vec4 OGL2Size;
uniform vec4 OGL2InvSize;
uniform sampler2D tex0;

float PHI = 1.61803398874989484820459 * 00000.1; // Golden Ratio   
float PI  = 3.14159265358979323846264 * 00000.1; // PI
float SQ2 = 1.41421356237309504880169 * 10000.0; // Square Root of Two

float gold_noise( vec2 coordinate, float seed){

	return fract(sin(dot(coordinate*(seed+PHI), vec2(PHI, PI)))*SQ2);

}

vec3 ntscrgb_2_xyz( vec3 c ) {

    //Inverse Companding
    vec3 v = pow( c, vec3(2.2) ); 
    return v *
        mat3(  0.6068909,  0.1735011,  0.2003480,
 	       0.2989164,  0.5865990,  0.1144845,
 	       0.0000000,  0.0660957,  1.1162243 );
}

// conversion from XYZ to sRGB Reference White D65 ( color space used by windows ) 

vec3 xyz_2_srgb( vec3 c ) {

    vec3 v =  c *         
	mat3(  3.2404542, -1.5371385, -0.4985314,
	      -0.9692660,  1.8760108,  0.0415560,
 	       0.0556434, -0.2040259,  1.0572252 );

    //Companding
    v.r = ( v.r > 0.0031308 ) ? (( 1.055 * pow( v.r, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.r;
    v.g = ( v.g > 0.0031308 ) ? (( 1.055 * pow( v.g, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.g;
    v.b = ( v.b > 0.0031308 ) ? (( 1.055 * pow( v.b, ( 1.0 / 2.4 ))) - 0.055 ) : 12.92 * v.b;
    return v;
}

// NTSC RGB to sRGB

vec3 ntscrgb_2_srgb( vec3 c ) { 
     return xyz_2_srgb( ntscrgb_2_xyz( c ) ); 
}

void main (void)
{     
	vec3 c0 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy).rgb);//c 
	float c1 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy + OGL2InvSize.xy * vec2( 0.0, 1.0)).rgb).g;//b
	float c2 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy + OGL2InvSize.xy * vec2(-1.0, 1.0)).rgb).g;//bl
	float c3 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy + OGL2InvSize.xy * vec2(-1.0, 0.0)).rgb).g;//l
	float c4 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy + OGL2InvSize.xy * vec2(-1.0,-1.0)).rgb).g;//tl
	float c5 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy + OGL2InvSize.xy * vec2( 0.0,-1.0)).rgb).g;//t
	float c6 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy + OGL2InvSize.xy * vec2( 1.0,-1.0)).rgb).g;//tr
	float c7 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy + OGL2InvSize.xy * vec2( 1.0, 0.0)).rgb).g;//r
	float c8 = ntscrgb_2_srgb(texture2D(tex0, gl_TexCoord[0].xy + OGL2InvSize.xy * vec2( 1.0, 1.0)).rgb).g;//br
            
	float a0 = c8 - c4;
	float a1 = a0 - c2 - 2.0 * c3 + c6 + 2.0 * c7;
	a0 = a0 + c2 + c1 * 2.0 - 2.0 * c5 - c6;

	float ctoon = a0 * a0 + a1 * a1;

	c0 = c0-vec3(ctoon);

	float noise1 = gold_noise(gl_TexCoord[0].xy+0.5,(gl_TexCoord[0].x+0.5)*OGL2Size.x);
	float noise2 = gold_noise(gl_TexCoord[0].xy-0.5,(gl_TexCoord[0].x-0.5)*OGL2Size.x);
	float noise3 = gold_noise(gl_TexCoord[0].xy+0.5,(gl_TexCoord[0].y+0.5)*OGL2Size.y);
	float noise4 = gold_noise(gl_TexCoord[0].xy-0.5,(gl_TexCoord[0].y-0.5)*OGL2Size.y);

	gl_FragColor.rgb = vec3(min(min(noise1,noise2),min(noise3,noise4)))*c0+c0;

}

