
// vec4 texture2D_bilinear( sampler2D sam, vec2 uv, vec2 res ) {

//     vec2 st = uv*res - 0.5;

//     vec2 iuv = floor( st );
//     vec2 fuv = fract( st );

//     vec4 a = texture2D( sam, (iuv+vec2(0.5,0.5))/res );
//     vec4 b = texture2D( sam, (iuv+vec2(1.5,0.5))/res );
//     vec4 c = texture2D( sam, (iuv+vec2(0.5,1.5))/res );
//     vec4 d = texture2D( sam, (iuv+vec2(1.5,1.5))/res );

//     return mix( mix( a, b, fuv.x),
//                 mix( c, d, fuv.x), fuv.y );
// }

// vec3 FastCatmulRom(sampler2D colorTex, vec2 texcoord, vec4 rtMetrics, float sharpenAmount) {
//     vec2 position = rtMetrics.zw * texcoord;
//     vec2 centerPosition = floor(position - 0.5) + 0.5;
//     vec2 f = position - centerPosition;
//     vec2 f2 = f * f;
//     vec2 f3 = f * f2;

//     float c = sharpenAmount;
//     vec2 w0 =        -c  * f3 +  2.0 * c         * f2 - c * f;
//     vec2 w1 =  (2.0 - c) * f3 - (3.0 - c)        * f2         + 1.0;
//     vec2 w2 = -(2.0 - c) * f3 + (3.0 -  2.0 * c) * f2 + c * f;
//     vec2 w3 =         c  * f3 -                c * f2;

//     vec2 w12 = w1 + w2;
//     vec2 tc12 = rtMetrics.xy * (centerPosition + w2 / w12);
//     vec3 centerColor = texture2D(colorTex, vec2(tc12.x, tc12.y)).rgb;

//     vec2 tc0 = rtMetrics.xy * (centerPosition - 1.0);
//     vec2 tc3 = rtMetrics.xy * (centerPosition + 2.0);
    
//     vec4 color = vec4(texture2D(colorTex, vec2(tc12.x, tc0.y )).rgb, 1.0) * (w12.x * w0.y ) +
//                    vec4(texture2D(colorTex, vec2(tc0.x,  tc12.y)).rgb, 1.0) * (w0.x  * w12.y) +
//                    vec4(centerColor,                                      1.0) * (w12.x * w12.y) +
//                    vec4(texture2D(colorTex, vec2(tc3.x,  tc12.y)).rgb, 1.0) * (w3.x  * w12.y) +
//                    vec4(texture2D(colorTex, vec2(tc12.x, tc3.y )).rgb, 1.0) * (w12.x * w3.y );
// 	return color.rgb/color.a;

// }

// Catmullrom filter with 5 taps.
vec4 FastCatmulRom(sampler2D colorTex, vec2 texcoord, vec4 rtMetrics, float sharpenAmount) {
    vec2 position = rtMetrics.zw * texcoord;
    vec2 centerPosition = floor(position - 0.5) + 0.5;
    vec2 f = position - centerPosition;
    vec2 f2 = f * f;
    vec2 f3 = f * f2;

    float c = sharpenAmount;
    vec2 w0 =        -c  * f3 +  2.0 * c         * f2 - c * f;
    vec2 w1 =  (2.0 - c) * f3 - (3.0 - c)        * f2         + 1.0;
    vec2 w2 = -(2.0 - c) * f3 + (3.0 -  2.0 * c) * f2 + c * f;
    vec2 w3 =         c  * f3 -                c * f2;

    vec2 w12 = max(w1 + w2, 0.0001);
    vec2 tc12 = rtMetrics.xy * (centerPosition + w2 / w12);
    vec4 centerColor = texture2D(colorTex, vec2(tc12.x, tc12.y));

    vec2 tc0 = rtMetrics.xy * (centerPosition - 1.0);
    vec2 tc3 = rtMetrics.xy * (centerPosition + 2.0);
    float weightSum = (w12.x * w0.y) + (w0.x  * w12.y) + (w12.x * w12.y) + (w3.x  * w12.y) + (w12.x * w3.y);
    vec4 color = texture2D(colorTex, vec2(tc12.x, tc0.y )) * (w12.x * w0.y) +
                   texture2D(colorTex, vec2(tc0.x,  tc12.y)) * (w0.x  * w12.y) +
                   centerColor                                * (w12.x * w12.y) +
                   texture2D(colorTex, vec2(tc3.x,  tc12.y)) * (w3.x  * w12.y) +
                   texture2D(colorTex, vec2(tc12.x, tc3.y )) * (w12.x * w3.y );
	return color/max(weightSum, 0.0001);

}

vec4 smoothfilter(in sampler2D tex, in vec2 uv) {
	uv = uv*resolution + 0.5;
	vec2 iuv = floor(uv);
	vec2 fuv = fract(uv);
	uv = iuv + fuv*fuv*fuv*(fuv*(fuv*6.0-15.0)+10.0);
	uv = (uv - 0.5)/resolution;
	return textureLod(tex, uv, 0.0);
}

vec4 textureSmooth(sampler2D t, vec2 x, vec2 textureSize) {
    x *= vec2(textureSize);

    vec2 p = floor(x);
    vec2 f = fract(x);

    vec4 a = texture2D(t, (p                 ) / vec2(textureSize));
    vec4 b = texture2D(t, (p + vec2(1.0, 0.0)) / vec2(textureSize));
    vec4 c = texture2D(t, (p + vec2(0.0, 1.0)) / vec2(textureSize));
    vec4 d = texture2D(t, (p + vec2(1.0, 1.0)) / vec2(textureSize));

    return mix(mix(a, b, f.x), mix(c, d, f.x), f.y);
}

vec4 textureSmooth2(sampler2D t, vec2 x, vec2 textureSize) {
    x *= vec2(textureSize);

    vec2 p = floor(x);
    vec2 f = fract(x);

    vec4 a = texture2D(t, (p                 ) / vec2(textureSize));
    vec4 b = texture2D(t, (p + vec2(1.0, 0.0)) / vec2(textureSize));
    vec4 c = texture2D(t, (p + vec2(0.0, 1.0)) / vec2(textureSize));
    vec4 d = texture2D(t, (p + vec2(1.0, 1.0)) / vec2(textureSize));

    return (a+b+c+d)*0.25;
}

vec4 cubic(float x) {
  float x2 = x * x;
  float x3 = x2 * x;
  vec4 w;
  w.x =   -x3 + 3*x2 - 3*x + 1;
  w.y =  3*x3 - 6*x2       + 4;
  w.z = -3*x3 + 3*x2 + 3*x + 1;
  w.w =  x3;
  return w / 6.f;
}

vec4 bicubicTexture(sampler2D tex, vec2 coord, vec2 resolution) {
  coord *= resolution;

  float fx = fract(coord.x);
  float fy = fract(coord.y);
  coord.x -= fx;
  coord.y -= fy;

  vec4 xcubic = cubic(fx);
  vec4 ycubic = cubic(fy);

  vec4 c = vec4(coord.x - 0.5, coord.x + 1.5, coord.y - 0.5, coord.y + 1.5);
  vec4 s = vec4(xcubic.x + xcubic.y, xcubic.z + xcubic.w, ycubic.x + ycubic.y, ycubic.z + ycubic.w);
  vec4 offset = c + vec4(xcubic.y, xcubic.w, ycubic.y, ycubic.w) / s;

  vec4 sample0 = texture2D(tex, vec2(offset.x, offset.z) / resolution);
  vec4 sample1 = texture2D(tex, vec2(offset.y, offset.z) / resolution);
  vec4 sample2 = texture2D(tex, vec2(offset.x, offset.w) / resolution);
  vec4 sample3 = texture2D(tex, vec2(offset.y, offset.w) / resolution);

  float sx = s.x / (s.x + s.y);
  float sy = s.z / (s.z + s.w);

  return mix( mix(sample3, sample2, sx), mix(sample1, sample0, sx), sy);
}

/*
3x3 Median
Morgan McGuire and Kyle Whitson
http://graphics.cs.williams.edu


Copyright (c) Morgan McGuire and Williams College, 2006
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

// Input texture
uniform sampler2D T;

#ifndef G3D
// vec2(1/width, 1/height) of the texture
    uniform vec2 Tinvsize;
#else
#    define Tinvsize g3d_sampler2DInvSize(T))
#endif 


// Change these 2 defines to change precision,
#define vec vec3
#define toVec(x) x.rgb

//#define vec vec4
//#define toVec(x) x.rgba

#define s2(a, b)				temp = a; a = min(a, b); b = max(temp, b);
#define mn3(a, b, c)			s2(a, b); s2(a, c);
#define mx3(a, b, c)			s2(b, c); s2(a, c);

#define mnmx3(a, b, c)			mx3(a, b, c); s2(a, b);                                   // 3 exchanges
#define mnmx4(a, b, c, d)		s2(a, b); s2(c, d); s2(a, c); s2(b, d);                   // 4 exchanges
#define mnmx5(a, b, c, d, e)	s2(a, b); s2(c, d); mn3(a, c, e); mx3(b, d, e);           // 6 exchanges
#define mnmx6(a, b, c, d, e, f) s2(a, d); s2(b, e); s2(c, f); mn3(a, b, c); mx3(d, e, f); // 7 exchanges

vec3 MedianFilter(sampler2D T, vec2 coord, vec2 texelSize) {

  vec v[9];

  // Add the pixels which make up our window to the pixel array.
  for(int dX = -1; dX <= 1; ++dX) {
    for(int dY = -1; dY <= 1; ++dY) {		
      vec2 offset = vec2(float(dX), float(dY));
		    
      // If a pixel in the window is located at (x+dX, y+dY), put it at index (dX + R)(2R + 1) + (dY + R) of the
      // pixel array. This will fill the pixel array, with the top left pixel of the window at pixel[0] and the
      // bottom right pixel of the window at pixel[N-1].
      v[(dX + 1) * 3 + (dY + 1)] = toVec(texture2DLod(T, coord.xy + offset * texelSize, 0));
    }
  }

  vec temp;

  // Starting with a subset of size 6, remove the min and max each time
  mnmx6(v[0], v[1], v[2], v[3], v[4], v[5]);
  mnmx5(v[1], v[2], v[3], v[4], v[6]);
  mnmx4(v[2], v[3], v[4], v[7]);
  mnmx3(v[3], v[4], v[8]);
//   toVec(gl_FragColor) = v[4];

	return v[4];
}