Last active
August 29, 2015 14:27
-
-
Save kmuehlbauer/b438ce327d8bdbdbee78 to your computer and use it in GitHub Desktop.
bicubic filter straight forward approach
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// coefficients for the cubic polynomial | |
vec4 c0 = vec4(-1.0, 3.0, -3.0, 1.0 ) / 6.0; | |
vec4 c1 = vec4( 3.0, -6.0, 0.0, 4.0 ) / 6.0; | |
vec4 c2 = vec4(-3.0, 3.0, 3.0, 1.0 ) / 6.0; | |
vec4 c3 = vec4( 1.0, 0.0, 0.0, 0.0 ) / 6.0; | |
vec4 cubic(vec4 var, vec4 p0, vec4 p1, vec4 p2, vec4 p3 ) { | |
// return cubic polynomial | |
return p0 * dot( c0, var) + | |
p1 * dot( c1, var) + | |
p2 * dot( c2, var) + | |
p3 * dot( c3, var); | |
} | |
vec4 flt(sampler2D texture, vec2 uv, vec2 pixel) { | |
vec2 sh = pixel; | |
vec2 texel = uv/pixel - vec2(0.5,0.5); | |
vec2 f = fract(texel); | |
texel = (texel-f+vec2(0.001,0.001))*pixel; | |
vec2 xy = texel; | |
float x = f.x; | |
float x2 = x * x; | |
float x3 = x * x2; | |
vec4 varx = vec4(x3, x2, x, 1.0); | |
vec4 r0 = cubic(varx, | |
texture2D(texture, xy + vec2(-1, -1)*pixel), | |
texture2D(texture, xy + vec2(0, -1)*pixel), | |
texture2D(texture, xy + vec2(1, -1)*pixel), | |
texture2D(texture, xy + vec2(2, -1)*pixel)); | |
vec4 r1 = cubic(varx, | |
texture2D(texture, xy + vec2(-1, 0)*pixel), | |
texture2D(texture, xy + vec2(0, 0)*pixel), | |
texture2D(texture, xy + vec2(1, 0)*pixel), | |
texture2D(texture, xy + vec2(2, 0)*pixel)); | |
vec4 r2 = cubic(varx, | |
texture2D(texture, xy + vec2(-1, 1)*pixel), | |
texture2D(texture, xy + vec2(0, 1)*pixel), | |
texture2D(texture, xy + vec2(1, 1)*pixel), | |
texture2D(texture, xy + vec2(2, 1)*pixel)); | |
vec4 r3 = cubic(varx, | |
texture2D(texture, xy + vec2(-1, 2)*pixel), | |
texture2D(texture, xy + vec2(0, 2)*pixel), | |
texture2D(texture, xy + vec2(1, 2)*pixel), | |
texture2D(texture, xy + vec2(2, 2)*pixel)); | |
x = f.y; | |
x2 = x * x; | |
x3 = x * x2; | |
vec4 vary = vec4(x3, x2, x, 1.0); | |
return cubic( vary, r0, r1, r2, r3); | |
} | |
// bicubic interpolation | |
vec4 bicubic(sampler2D texture, vec2 shape, vec2 uv) { | |
return flt(texture, uv, 1.0/shape); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment