|
| 1 | +#version 150 |
| 2 | + |
| 3 | +// AntiAliased Nearest Neighbor |
| 4 | +// by jimbo1qaz and wareya |
| 5 | +// Licensed MIT |
| 6 | + |
| 7 | +precision highp float; |
| 8 | + |
| 9 | +uniform sampler2D source[]; |
| 10 | +uniform vec4 sourceSize[]; |
| 11 | +uniform vec4 targetSize[]; |
| 12 | + |
| 13 | +in Vertex { |
| 14 | + vec2 texCoord; |
| 15 | +}; |
| 16 | + |
| 17 | +out vec4 fragColor; |
| 18 | + |
| 19 | +#define NOT(fl) (1-fl) |
| 20 | +#define YES(fl) fl |
| 21 | + |
| 22 | +vec4 vpow(vec4 n, float e) |
| 23 | +{ |
| 24 | + return vec4(pow(n.x, e), pow(n.y, e), pow(n.z, e), pow(n.w, e)); |
| 25 | +} |
| 26 | + |
| 27 | +vec4 getLQV(vec3 mine) { |
| 28 | + return vec4 |
| 29 | + ( mine.r |
| 30 | + , mine.g |
| 31 | + , mine.b |
| 32 | + , mine.r*0.2989 + mine.g*0.5870 + mine.b*0.1140); |
| 33 | +} |
| 34 | +vec3 fromLQV(vec4 mine) { |
| 35 | + float f = mine.w/(mine.r*0.2989 + mine.g*0.5870 + mine.b*0.1140); |
| 36 | + return vec3(mine.rgb)*f; |
| 37 | +} |
| 38 | + |
| 39 | +vec3 percent(float ssize, float tsize, float coord) { |
| 40 | + float minfull = (coord*tsize - 0.5) /tsize*ssize; |
| 41 | + float maxfull = (coord*tsize + 0.5) /tsize*ssize; |
| 42 | + |
| 43 | + float realfull = floor(maxfull); |
| 44 | + |
| 45 | + if (minfull > realfull) { |
| 46 | + return vec3(1, (realfull+0.5)/ssize, (realfull+0.5)/ssize); |
| 47 | + } |
| 48 | + |
| 49 | + return vec3( |
| 50 | + (maxfull - realfull) / (maxfull - minfull), |
| 51 | + (realfull-0.5) / ssize, |
| 52 | + (realfull+0.5) / ssize |
| 53 | + ); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +void main() { |
| 58 | + float cheapsrgb = 2.1; |
| 59 | + float gamma = 3.0; |
| 60 | + vec3 xstuff = percent(sourceSize[0].x, targetSize[0].x, texCoord.x); |
| 61 | + vec3 ystuff = percent(sourceSize[0].y, targetSize[0].y, texCoord.y); |
| 62 | + |
| 63 | + float xkeep = xstuff[0]; |
| 64 | + float ykeep = ystuff[0]; |
| 65 | + |
| 66 | + // get points to interpolate across, in linear rgb |
| 67 | + vec4 a = getLQV(vpow(texture(source[0],vec2(xstuff[1],ystuff[1])), cheapsrgb).rgb); |
| 68 | + vec4 b = getLQV(vpow(texture(source[0],vec2(xstuff[2],ystuff[1])), cheapsrgb).rgb); |
| 69 | + vec4 c = getLQV(vpow(texture(source[0],vec2(xstuff[1],ystuff[2])), cheapsrgb).rgb); |
| 70 | + vec4 d = getLQV(vpow(texture(source[0],vec2(xstuff[2],ystuff[2])), cheapsrgb).rgb); |
| 71 | + |
| 72 | + // use perceptual gamma for luminance component |
| 73 | + a.w = pow(a.w, 1/gamma); |
| 74 | + b.w = pow(b.w, 1/gamma); |
| 75 | + c.w = pow(c.w, 1/gamma); |
| 76 | + d.w = pow(d.w, 1/gamma); |
| 77 | + |
| 78 | + // interpolate |
| 79 | + vec4 gammaLQVresult = |
| 80 | + NOT(xkeep)*NOT(ykeep)*a + |
| 81 | + YES(xkeep)*NOT(ykeep)*b + |
| 82 | + NOT(xkeep)*YES(ykeep)*c + |
| 83 | + YES(xkeep)*YES(ykeep)*d; |
| 84 | + |
| 85 | + // change luminance gamma back to linear |
| 86 | + vec4 LQVresult = gammaLQVresult; |
| 87 | + LQVresult.w = pow(gammaLQVresult.w, gamma); |
| 88 | + |
| 89 | + // convert back to srgb; lqv -> lrgb -> srgb |
| 90 | + vec4 c1 = vpow(vec4(fromLQV(LQVresult), 1), 1/cheapsrgb); |
| 91 | + |
| 92 | + fragColor = c1; |
| 93 | +} |
0 commit comments