/*
   Super 4xGLSoft Shader v 1.0

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

   de Blur, 4xGLSoft lite - Copyright (C) 2016 guest(r) - guest.r@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 sampler2D OGL2Texture;
uniform vec4 OGL2Size;
uniform vec4 OGL2InvSize;
uniform vec4 OGL2Param;

const   vec3 ones = vec3(1.0,1.0,1.0);
const	vec4 yx = vec4(1.0/4096,1.0/2048,-1.0/4096,-1.0/2048);
const	vec4 xy = vec4(1.0/2048,1.0/1024,-1.0/2048,-1.0/1024);
const   vec3 decim = vec3(0.001,0.001,0.001);

//	4xGLSoft lite code

vec3 lite4xGLSoft(vec2 texcoord) {

	vec3 c11 = texture2D(OGL2Texture, texcoord        ).rgb; 
	vec3 c00 = texture2D(OGL2Texture, texcoord + xy.zw).rgb; 
	vec3 c20 = texture2D(OGL2Texture, texcoord + xy.xw).rgb; 
	vec3 c22 = texture2D(OGL2Texture, texcoord + xy.xy).rgb; 
	vec3 c02 = texture2D(OGL2Texture, texcoord + xy.zy).rgb; 
	vec3 s00 = texture2D(OGL2Texture, texcoord + yx.zw).rgb; 
	vec3 s20 = texture2D(OGL2Texture, texcoord + yx.xw).rgb; 
	vec3 s22 = texture2D(OGL2Texture, texcoord + yx.xy).rgb; 
	vec3 s02 = texture2D(OGL2Texture, texcoord + yx.zy).rgb; 

	float d1=dot(abs(c00-c22),ones)+0.001;
	float d2=dot(abs(c20-c02),ones)+0.001;
	float m1=dot(abs(s00-s22),ones)+0.001;
	float m2=dot(abs(s02-s20),ones)+0.001;

	vec3 t2=(d1*(c20+c02)+d2*(c00+c22))/(2.0*(d1+d2));
	
	return .25*(c11+t2+(m2*(s00+s22)+m1*(s02+s20))/(m1+m2));

}

//      de Blur code

vec3 deBlur(vec3 c11) {
  
	vec2 dx  = vec2(xy.x, 0.0);
	vec2 dy  = vec2( 0.0,xy.y);
	vec2 g1  = xy.xy;
	vec2 g2  = vec2(xy.z,xy.y);

	vec3 C0 = lite4xGLSoft(gl_TexCoord[0].xy-g1); 
	vec3 C1 = lite4xGLSoft(gl_TexCoord[0].xy-dy);
	vec3 C2 = lite4xGLSoft(gl_TexCoord[0].xy-g2);
	vec3 C3 = lite4xGLSoft(gl_TexCoord[0].xy-dx);
	vec3 C4 = lite4xGLSoft(gl_TexCoord[0].xy   );
	vec3 C5 = lite4xGLSoft(gl_TexCoord[0].xy+dx);
	vec3 C6 = lite4xGLSoft(gl_TexCoord[0].xy+g2);
	vec3 C7 = lite4xGLSoft(gl_TexCoord[0].xy+dy);
	vec3 C8 = lite4xGLSoft(gl_TexCoord[0].xy+g1);

	vec3 mn1 = min(min(C0,C1),C2);
	vec3 mn2 = min(min(C3,C4),C5);
	vec3 mn3 = min(min(C6,C7),C8);
	vec3 mx1 = max(max(C0,C1),C2);
	vec3 mx2 = max(max(C3,C4),C5);
	vec3 mx3 = max(max(C6,C7),C8);
 
	mn1 = min(min(mn1,mn2),mn3);
	mx1 = max(max(mx1,mx2),mx3);
 
	vec3 dif1 = abs(c11-mn1) + decim;
	vec3 dif2 = abs(c11-mx1) + decim;
 
	float filterparam = 2.0 + 1.0 * OGL2Param.z; // de Blur control

	dif1=vec3(pow(dif1.x,filterparam),pow(dif1.y,filterparam),pow(dif1.z,filterparam));
	dif2=vec3(pow(dif2.x,filterparam),pow(dif2.y,filterparam),pow(dif2.z,filterparam));
 
	c11.r = (dif1.x*mx1.x + dif2.x*mn1.x)/(dif1.x + dif2.x);
	c11.g = (dif1.y*mx1.y + dif2.y*mn1.y)/(dif1.y + dif2.y);
	c11.b = (dif1.z*mx1.z + dif2.z*mn1.z)/(dif1.z + dif2.z);

	return c11;

}

void main()
{

	gl_FragColor.rgb = deBlur(lite4xGLSoft(gl_TexCoord[0].xy));

}
