/*
   Bilinear Sharp Shader v 1.0

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

   ColMin and ColMax functions - Copyright (C) 2018 SimoneT

   de Blur - Copyright (C) 2016 guest(r) - guest.r@gmail.com

   colour length and colour distance functions from "Colour metric"

   https://www.compuphase.com/cmetric.htm

   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 3
   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.
    
*/

#version 130

uniform sampler2D OGL2Texture;
uniform vec4 OGL2Size;
uniform vec4 OGL2InvSize;
uniform vec4 OGL2Param;

const   vec2 InputSize = vec2( 1024.0, 512.0 ); //Texture input size

float clength(vec3 a){

	float rmean = a.r*0.5;

	a*= a;

	return sqrt(dot(vec3(2.0+rmean,4.0,3.0-rmean),a));

}

float cdistance(vec3 a, vec3 b){

	float rmean = (a.r+b.r)*0.5;

	a = pow(a-b,vec3(2.0));

	return sqrt(dot(vec3(2.0+rmean,4.0,3.0-rmean),a));

}

vec3 ColMin(vec3 a, vec3 b){

	float diff = step(0.0,sign(clength(a)-clength(b)));

	return mix(a,b,diff);

}

vec3 ColMax(vec3 a, vec3 b){

	float diff = step(0.0,sign(clength(a)-clength(b)));

	return mix(b,a,diff);

}

//      de Blur code

vec3 deBlur(vec2 texCoord) {

	vec2 Size2X = (0.5/InputSize);

	vec2 dx = vec2( Size2X.x,      0.0);
	vec2 dy = vec2(      0.0, Size2X.y);
	vec2 g1 = vec2( Size2X.x, Size2X.y);
	vec2 g2 = vec2(-Size2X.x, Size2X.y);

	vec3 C0 = texture(OGL2Texture, texCoord-g1 ).rgb; 
	vec3 C1 = texture(OGL2Texture, texCoord-dy ).rgb;
	vec3 C2 = texture(OGL2Texture, texCoord-g2 ).rgb;
	vec3 C3 = texture(OGL2Texture, texCoord-dx ).rgb;
	vec3 C4 = texture(OGL2Texture, texCoord    ).rgb;
	vec3 C5 = texture(OGL2Texture, texCoord+dx ).rgb;
	vec3 C6 = texture(OGL2Texture, texCoord+g2 ).rgb;
	vec3 C7 = texture(OGL2Texture, texCoord+dy ).rgb;
	vec3 C8 = texture(OGL2Texture, 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);
	float dif2 = cdistance(C4,mx1);
 
	float filterparam = 9.0; // de Blur control

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

	float dif3=dif1+dif2; 

	vec3 C9 = (dif1*mx1 + dif2*mn1)/dif3;

	if (dif3 == 0.0 ) { C9 = C4; }
 
	return C9;

}

void main()
{

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

}
