/*
   Simple Edge Directed Interpolation (SEDI) v1.0

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

   de Blur - 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.
    
*/

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

varying vec2 v_texCoord;
uniform sampler2D Frame;

const   vec4 Size = vec4( 2048.0, 1024.0, 0.00048828125, 0.0009765625 );

float CLength(vec3 c1){

	float rmean = c1.r*0.5;

	c1*= c1;

	return sqrt((2.0+rmean)*c1.r+4.0*c1.g+(3.0-rmean)*c1.b);
}

float Cdistance(vec3 c1, vec3 c2){

	float rmean = (c1.r+c2.r)*0.5;

	c1 = pow(c1-c2,vec3(2.0));

	return sqrt((2.0+rmean)*c1.r+4.0*c1.g+(3.0-rmean)*c1.b);
}

vec3 ColMin(vec3 a, vec3 b){

	float dist = step(0.01,sign(CLength(a) - CLength(b)));

	return mix(a,b,dist);

}

vec3 ColMax(vec3 a, vec3 b){

	float dist = step(0.01,sign(CLength(a) - CLength(b)));

	return mix(b,a,dist);

}

vec3 Blur( vec2 TexCoord ) {

	vec2 shift  = Size.zw * 0.5;

	vec3 C06 = texture2D(Frame, TexCoord - shift.xy).rgb;
	vec3 C07 = texture2D(Frame, TexCoord + vec2( shift.x,-shift.y)).rgb;
	vec3 C11 = texture2D(Frame, TexCoord + vec2(-shift.x, shift.y)).rgb;
	vec3 C12 = texture2D(Frame, TexCoord + shift.xy).rgb;

	float dif1 = Cdistance(C06,C12) + 0.00001;
	float dif2 = Cdistance(C07,C11) + 0.00001;

	C06 = (C06+C12)*0.5;
 	C07 = (C07+C11)*0.5;

	dif1 += CLength(C06);
 	dif2 += CLength(C07);

	float filterparam = 7.0; // de Blur control

	dif1=pow(dif1,filterparam);
	dif2=pow(dif2,filterparam);

	float dif3=dif1+dif2; 
 
	return (dif1*C07 + dif2*C06)/dif3;

}

//      de Blur code

vec3 deBlur(vec2 texCoord) {

	vec2 dx = vec2( Size.z,    0.0);
	vec2 dy = vec2(    0.0, Size.w);
	vec2 g1 = vec2( Size.z, Size.w);
	vec2 g2 = vec2(-Size.z, Size.w);

	vec3 C0 = Blur( texCoord-g1).rgb; 
	vec3 C1 = Blur( texCoord-dy).rgb;
	vec3 C2 = Blur( texCoord-g2).rgb;
	vec3 C3 = Blur( texCoord-dx).rgb;
	vec3 C4 = Blur( texCoord   ).rgb;
	vec3 C5 = Blur( texCoord+dx).rgb;
	vec3 C6 = Blur( texCoord+g2).rgb;
	vec3 C7 = Blur( texCoord+dy).rgb;
	vec3 C8 = Blur( texCoord+g1).rgb;

	vec3 mn1 = ColMin(ColMin(C0,C1),C2);
	vec3 mn2 = ColMin(ColMin(C3,C4),C5);
	vec3 mn3 = ColMin(ColMin(C6,C7),C8);
	     mn1 = ColMin(ColMin(mn1,mn2),mn3);

	vec3 mx1 = ColMax(ColMax(C0,C1),C2);
	vec3 mx2 = ColMax(ColMax(C3,C4),C5);
	vec3 mx3 = ColMax(ColMax(C6,C7),C8);
 	     mx1 = ColMax(ColMax(mx1,mx2),mx3);

	float dif1 = Cdistance(C4,mn1) + 0.00001;
	float dif2 = Cdistance(C4,mx1) + 0.00001;
 
	float filterparam = 7.0; // de Blur control

	dif1=pow(dif1,filterparam);
	dif2=pow(dif2,filterparam);

	float dif3=dif1+dif2; 
 
	return (dif1*mx1 + dif2*mn1)/dif3;

}

void main()
{

	gl_FragColor.rgb = deBlur( v_texCoord );

}
