scalefx-pass1.fs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #version 150
  2. /*
  3. ScaleFX - Pass 1
  4. by Sp00kyFox, 2016-03-30
  5. Filter: Nearest
  6. Scale: 1x
  7. ScaleFX is an edge interpolation algorithm specialized in pixel art. It was
  8. originally intended as an improvement upon Scale3x but became a new filter in
  9. its own right.
  10. ScaleFX interpolates edges up to level 6 and makes smooth transitions between
  11. different slopes. The filtered picture will only consist of colours present
  12. in the original.
  13. Pass 1 resolves ambiguous configurations of corner candidates at pixel junctions.
  14. Copyright (c) 2016 Sp00kyFox - ScaleFX@web.de
  15. Permission is hereby granted, free of charge, to any person obtaining a copy
  16. of this software and associated documentation files (the "Software"), to deal
  17. in the Software without restriction, including without limitation the rights
  18. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  19. copies of the Software, and to permit persons to whom the Software is
  20. furnished to do so, subject to the following conditions:
  21. The above copyright notice and this permission notice shall be included in
  22. all copies or substantial portions of the Software.
  23. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  29. THE SOFTWARE.
  30. */
  31. #define SFX_CLR 0.35
  32. const float THR = 1.0 - SFX_CLR;
  33. uniform sampler2D source[];
  34. uniform vec4 sourceSize[];
  35. uniform vec4 targetSize;
  36. in Vertex {
  37. vec2 vTexCoord;
  38. };
  39. out vec4 FragColor;
  40. #define LE(x, y) (1.0 - step(y, x))
  41. #define GE(x, y) (1.0 - step(x, y))
  42. #define LEQ(x, y) step(x, y)
  43. #define GEQ(x, y) step(y, x)
  44. #define NOT(x) (1.0 - (x))
  45. // corner strength
  46. vec4 str(vec4 crn, vec4 ort){
  47. //return (crn > THR) ? max(2.0*crn - (ort + ort.wxyz), 0.0) : 0.0;
  48. return GE(crn, vec4(THR)) * max(2.0*crn - (ort + ort.wxyz), vec4(0.0));
  49. }
  50. // corner dominance at junctions
  51. vec4 dom(vec3 strx, vec3 stry, vec3 strz, vec3 strw){
  52. vec4 res;
  53. res.x = max(2.0*strx.y - (strx.x + strx.z), 0.0);
  54. res.y = max(2.0*stry.y - (stry.x + stry.z), 0.0);
  55. res.z = max(2.0*strz.y - (strz.x + strz.z), 0.0);
  56. res.w = max(2.0*strw.y - (strw.x + strw.z), 0.0);
  57. return res;
  58. }
  59. // necessary but not sufficient junction condition for orthogonal edges
  60. float clear(vec2 crn, vec4 ort){
  61. //return all(crn.xyxy <= THR || crn.xyxy <= ort || crn.xyxy <= ort.wxyz);
  62. vec4 res = LEQ(crn.xyxy, vec4(THR)) + LEQ(crn.xyxy, ort) + LEQ(crn.xyxy, ort.wxyz);
  63. return min(res.x * res.y * res.z * res.w, 1.0);
  64. }
  65. void main() {
  66. /* grid metric pattern
  67. M A B C P x y z x y
  68. N D E F Q o w w z
  69. O G H I R
  70. J K L
  71. */
  72. #define TEX(x, y) textureOffset(source[0], vTexCoord, ivec2(x, y))
  73. // metric data
  74. vec4 A = TEX(-1,-1), B = TEX( 0,-1), C = TEX( 1,-1);
  75. vec4 D = TEX(-1, 0), E = TEX( 0, 0), F = TEX( 1, 0);
  76. vec4 G = TEX(-1, 1), H = TEX( 0, 1), I = TEX( 1, 1);
  77. vec4 J = TEX(-1, 2), K = TEX( 0, 2), L = TEX( 1, 2);
  78. vec4 M = TEX(-2,-1), N = TEX(-2, 0), O = TEX(-2, 1);
  79. vec4 P = TEX( 2,-1), Q = TEX( 2, 0), R = TEX( 2, 1);
  80. // corner strength
  81. vec4 As = str(vec4(M.z, B.x, D.zx), vec4(A.yw, D.y, M.w));
  82. vec4 Bs = str(vec4(A.z, C.x, E.zx), vec4(B.yw, E.y, A.w));
  83. vec4 Cs = str(vec4(B.z, P.x, F.zx), vec4(C.yw, F.y, B.w));
  84. vec4 Ds = str(vec4(N.z, E.x, G.zx), vec4(D.yw, G.y, N.w));
  85. vec4 Es = str(vec4(D.z, F.x, H.zx), vec4(E.yw, H.y, D.w));
  86. vec4 Fs = str(vec4(E.z, Q.x, I.zx), vec4(F.yw, I.y, E.w));
  87. vec4 Gs = str(vec4(O.z, H.x, J.zx), vec4(G.yw, J.y, O.w));
  88. vec4 Hs = str(vec4(G.z, I.x, K.zx), vec4(H.yw, K.y, G.w));
  89. vec4 Is = str(vec4(H.z, R.x, L.zx), vec4(I.yw, L.y, H.w));
  90. // strength & dominance junctions
  91. vec4 jSx = vec4(As.z, Bs.w, Es.x, Ds.y), jDx = dom(As.yzw, Bs.zwx, Es.wxy, Ds.xyz);
  92. vec4 jSy = vec4(Bs.z, Cs.w, Fs.x, Es.y), jDy = dom(Bs.yzw, Cs.zwx, Fs.wxy, Es.xyz);
  93. vec4 jSz = vec4(Es.z, Fs.w, Is.x, Hs.y), jDz = dom(Es.yzw, Fs.zwx, Is.wxy, Hs.xyz);
  94. vec4 jSw = vec4(Ds.z, Es.w, Hs.x, Gs.y), jDw = dom(Ds.yzw, Es.zwx, Hs.wxy, Gs.xyz);
  95. // majority vote for ambiguous dominance junctions
  96. //bvec4 jx = jDx != 0.0 && jDx + jDx.zwxy > jDx.yzwx + jDx.wxyz;
  97. //bvec4 jy = jDy != 0.0 && jDy + jDy.zwxy > jDy.yzwx + jDy.wxyz;
  98. //bvec4 jz = jDz != 0.0 && jDz + jDz.zwxy > jDz.yzwx + jDz.wxyz;
  99. //bvec4 jw = jDw != 0.0 && jDw + jDw.zwxy > jDw.yzwx + jDw.wxyz;
  100. vec4 jx = GE(jDx, vec4(0.0)) * GE(jDx + jDx.zwxy, jDx.yzwx + jDx.wxyz);
  101. vec4 jy = GE(jDy, vec4(0.0)) * GE(jDy + jDy.zwxy, jDy.yzwx + jDy.wxyz);
  102. vec4 jz = GE(jDz, vec4(0.0)) * GE(jDz + jDz.zwxy, jDz.yzwx + jDz.wxyz);
  103. vec4 jw = GE(jDw, vec4(0.0)) * GE(jDw + jDw.zwxy, jDw.yzwx + jDw.wxyz);
  104. // inject strength without creating new contradictions
  105. //bvec4 res;
  106. //res.x = jx.z || !(jx.y || jx.w) && (jSx.z != 0.0 && (jx.x || jSx.x + jSx.z > jSx.y + jSx.w));
  107. //res.y = jy.w || !(jy.z || jy.x) && (jSy.w != 0.0 && (jy.y || jSy.y + jSy.w > jSy.x + jSy.z));
  108. //res.z = jz.x || !(jz.w || jz.y) && (jSz.x != 0.0 && (jz.z || jSz.x + jSz.z > jSz.y + jSz.w));
  109. //res.w = jw.y || !(jw.x || jw.z) && (jSw.y != 0.0 && (jw.w || jSw.y + jSw.w > jSw.x + jSw.z));
  110. vec4 res;
  111. res.x = min(jx.z + (NOT(jx.y) * NOT(jx.w)) * (GE(jSx.z, 0.0) * (jx.x + GE(jSx.x + jSx.z, jSx.y + jSx.w))), 1.0);
  112. res.y = min(jy.w + (NOT(jy.z) * NOT(jy.x)) * (GE(jSy.w, 0.0) * (jy.y + GE(jSy.y + jSy.w, jSy.x + jSy.z))), 1.0);
  113. res.z = min(jz.x + (NOT(jz.w) * NOT(jz.y)) * (GE(jSz.x, 0.0) * (jz.z + GE(jSz.x + jSz.z, jSz.y + jSz.w))), 1.0);
  114. res.w = min(jw.y + (NOT(jw.x) * NOT(jw.z)) * (GE(jSw.y, 0.0) * (jw.w + GE(jSw.y + jSw.w, jSw.x + jSw.z))), 1.0);
  115. // single pixel & end of line detection
  116. //res = res && (bvec4(jx.z, jy.w, jz.x, jw.y) || !(res.wxyz && res.yzwx));
  117. res = min(res * (vec4(jx.z, jy.w, jz.x, jw.y) + NOT(res.wxyz * res.yzwx)), vec4(1.0));
  118. // output
  119. vec4 clr;
  120. clr.x = clear(vec2(D.z, E.x), vec4(A.w, E.y, D.wy));
  121. clr.y = clear(vec2(E.z, F.x), vec4(B.w, F.y, E.wy));
  122. clr.z = clear(vec2(H.z, I.x), vec4(E.w, I.y, H.wy));
  123. clr.w = clear(vec2(G.z, H.x), vec4(D.w, H.y, G.wy));
  124. vec4 low = max(vec4(E.yw, H.y, D.w), vec4(THR));
  125. vec4 hori = vec4(low.x < max(D.w, A.w), low.x < max(E.w, B.w), low.z < max(E.w, H.w), low.z < max(D.w, G.w)) * clr; // horizontal edges
  126. vec4 vert = vec4(low.w < max(E.y, D.y), low.y < max(E.y, F.y), low.y < max(H.y, I.y), low.w < max(H.y, G.y)) * clr; // vertical edges
  127. vec4 or = vec4(A.w < D.y, B.w <= F.y, H.w < I.y, G.w <= G.y); // orientation
  128. FragColor = (res + 2.0 * hori + 4.0 * vert + 8.0 * or) / 15.0;
  129. }