
// kiitti kimmo

uniform sampler2D texture;
uniform float radiusx;
uniform float radiusy;

float med5(float a, float b, float c, float d, float e) {
  float t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;

  if (a<b) { t0=a; t1=b; } else { t0=b; t1=a; }
  if (c<d) { t2=c; t3=d; } else { t2=d; t3=c; }
  if (t0>t2) { t4=t3; t5=t0; t6=t1; } else { t4=t1; t5=t2; t6=t3; }
  if (t4<e) { t7=t4; t8=e; } else { t7=e; t8=t4; }
  if (t5>t6) { t9=t6; t10=t7; } else { t9=t8; t10=t5; }
  return min(t9, t10);
}

void main() {
  vec4 a, b, c, d, e;
  float rr, gg, bb;

  a = texture2D(texture, vec2(gl_TexCoord[0].x-radiusx, gl_TexCoord[0].y-radiusy));
  b = texture2D(texture, vec2(gl_TexCoord[0].x+radiusx, gl_TexCoord[0].y-radiusy));
  c = texture2D(texture, vec2(gl_TexCoord[0].x+radiusx, gl_TexCoord[0].y+radiusy));
  d = texture2D(texture, vec2(gl_TexCoord[0].x-radiusx, gl_TexCoord[0].y+radiusy));
  e = texture2D(texture, gl_TexCoord[0].xy);

  rr = med5(a.r, b.r, c.r, d.r, e.r);
  gg = med5(a.g, b.g, c.g, d.g, e.g);
  bb = med5(a.b, b.b, c.b, d.b, e.b);

	gl_FragColor = vec4(rr, gg, bb, 1.0);
}

/*float4 main(
	float2 texcoord : TEXCOORD0,
	uniform sampler2D texture,
  uniform float radiusx,
  uniform float radiusy) : COLOR0
{
  float4 a, b, c, d, e;
  float rr, gg, bb;

  a = tex2D(texture, float2(texcoord.x-radiusx, texcoord.y-radiusy));
  b = tex2D(texture, float2(texcoord.x+radiusx, texcoord.y-radiusy));
  c = tex2D(texture, float2(texcoord.x+radiusx, texcoord.y+radiusy));
  d = tex2D(texture, float2(texcoord.x-radiusx, texcoord.y+radiusy));
  e = tex2D(texture, texcoord);

  rr = med5(a.r, b.r, c.r, d.r, e.r);
  gg = med5(a.g, b.g, c.g, d.g, e.g);
  bb = med5(a.b, b.b, c.b, d.b, e.b);

  return float4(rr, gg, bb, 1.0);
}
*/