Advertisement
Guest User

Super-xBR ported to C/C++ (fast shader version only))

a guest
Mar 14th, 2016
6,486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 14.87 KB | None | 0 0
  1. //// *** Super-xBR code begins here - MIT LICENSE *** ///
  2.  
  3. /*
  4.  
  5. *******  Super XBR Scaler  *******
  6.  
  7. Copyright (c) 2016 Hyllian - sergiogdb@gmail.com
  8.  
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15.  
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18.  
  19. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. THE SOFTWARE.
  26.  
  27. */
  28.  
  29. #define R(_col) ((_col>> 0)&0xFF)
  30. #define G(_col) ((_col>> 8)&0xFF)
  31. #define B(_col) ((_col>>16)&0xFF)
  32. #define A(_col) ((_col>>24)&0xFF)
  33.  
  34.  
  35. #define wgt1 0.129633f
  36. #define wgt2 0.175068f
  37. #define w1  (-wgt1)
  38. #define w2  (wgt1+0.5f)
  39. #define w3  (-wgt2)
  40. #define w4  (wgt2+0.5f)
  41.  
  42.  
  43.  
  44. float df(float A, float B)
  45. {
  46.     return abs(A - B);
  47. }
  48.  
  49. float min4(float a, float b, float c, float d)
  50. {
  51.     return std::min(std::min(a,b),std::min(c, d));
  52. }
  53.  
  54. float max4(float a, float b, float c, float d)
  55. {
  56.     return std::max(std::max(a, b), std::max(c, d));
  57. }
  58.  
  59. template<class T>
  60. T clamp(T x, T floor, T ceil)
  61. {
  62.     return std::max(std::min(x, ceil), floor);
  63. }
  64.  
  65. /*
  66.                          P1
  67. |P0|B |C |P1|         C     F4          |a0|b1|c2|d3|
  68. |D |E |F |F4|      B     F     I4       |b0|c1|d2|e3|   |e1|i1|i2|e2|
  69. |G |H |I |I4|   P0    E  A  I     P3    |c0|d1|e2|f3|   |e3|i3|i4|e4|
  70. |P2|H5|I5|P3|      D     H     I5       |d0|e1|f2|g3|
  71.                       G     H5
  72.                          P2
  73.  
  74. sx, sy  
  75. -1  -1 | -2  0   (x+y) (x-y)    -3  1  (x+y-1)  (x-y+1)
  76. -1   0 | -1 -1                  -2  0
  77. -1   1 |  0 -2                  -1 -1
  78. -1   2 |  1 -3                   0 -2
  79.  
  80.  0  -1 | -1  1   (x+y) (x-y)      ...     ...     ...
  81.  0   0 |  0  0
  82.  0   1 |  1 -1
  83.  0   2 |  2 -2
  84.  
  85.  1  -1 |  0  2   ...
  86.  1   0 |  1  1
  87.  1   1 |  2  0
  88.  1   2 |  3 -1
  89.  
  90.  2  -1 |  1  3   ...
  91.  2   0 |  2  2
  92.  2   1 |  3  1
  93.  2   2 |  4  0
  94.  
  95.                          
  96. */
  97.  
  98. float diagonal_edge(float mat[][4], float *wp) {
  99.     float dw1 = wp[0]*(df(mat[0][2], mat[1][1]) + df(mat[1][1], mat[2][0]) + df(mat[1][3], mat[2][2]) + df(mat[2][2], mat[3][1])) +\
  100.                 wp[1]*(df(mat[0][3], mat[1][2]) + df(mat[2][1], mat[3][0])) + \
  101.                 wp[2]*(df(mat[0][3], mat[2][1]) + df(mat[1][2], mat[3][0])) +\
  102.                 wp[3]*df(mat[1][2], mat[2][1]) +\
  103.                 wp[4]*(df(mat[0][2], mat[2][0]) + df(mat[1][3], mat[3][1])) +\
  104.                 wp[5]*(df(mat[0][1], mat[1][0]) + df(mat[2][3], mat[3][2]));
  105.  
  106.     float dw2 = wp[0]*(df(mat[0][1], mat[1][2]) + df(mat[1][2], mat[2][3]) + df(mat[1][0], mat[2][1]) + df(mat[2][1], mat[3][2])) +\
  107.                 wp[1]*(df(mat[0][0], mat[1][1]) + df(mat[2][2], mat[3][3])) +\
  108.                 wp[2]*(df(mat[0][0], mat[2][2]) + df(mat[1][1], mat[3][3])) +\
  109.                 wp[3]*df(mat[1][1], mat[2][2]) +\
  110.                 wp[4]*(df(mat[1][0], mat[3][2]) + df(mat[0][1], mat[2][3])) +\
  111.                 wp[5]*(df(mat[0][2], mat[1][3]) + df(mat[2][0], mat[3][1]));
  112.  
  113.     return (dw1 - dw2);
  114. }
  115.  
  116. // Not used yet...
  117. float cross_edge(float mat[][4], float *wp) {
  118.     float hvw1 = wp[3] * (df(mat[1][1], mat[2][1]) + df(mat[1][2], mat[2][2])) + \
  119.                  wp[0] * (df(mat[0][1], mat[1][1]) + df(mat[2][1], mat[3][1]) + df(mat[0][2], mat[1][2]) + df(mat[2][2], mat[3][2])) + \
  120.                  wp[2] * (df(mat[0][1], mat[2][1]) + df(mat[1][1], mat[3][1]) + df(mat[0][2], mat[2][2]) + df(mat[1][2], mat[3][2]));
  121.  
  122.     float hvw2 = wp[3] * (df(mat[1][1], mat[1][2]) + df(mat[2][1], mat[2][2])) + \
  123.                  wp[0] * (df(mat[1][0], mat[1][1]) + df(mat[2][0], mat[2][1]) + df(mat[1][2], mat[1][3]) + df(mat[2][2], mat[2][3])) + \
  124.                  wp[2] * (df(mat[1][0], mat[1][2]) + df(mat[1][1], mat[1][3]) + df(mat[2][0], mat[2][2]) + df(mat[2][1], mat[2][3]));
  125.  
  126.     return (hvw1 - hvw2);
  127. }
  128.  
  129.  
  130.  
  131.  
  132.  
  133. ///////////////////////// Super-xBR scaling
  134. // perform super-xbr (fast shader version) scaling by factor f=2 only.
  135. template<int f>
  136. void scaleSuperXBRT(u32* data, u32* out, int w, int h) {
  137.     int outw = w*f, outh = h*f;
  138.  
  139.     float wp[6] = { 2.0f, 1.0f, -1.0f, 4.0f, -1.0f, 1.0f };
  140.  
  141.     // First Pass
  142.     for (int y = 0; y < outh; ++y) {
  143.         for (int x = 0; x < outw; ++x) {
  144.             float r[4][4], g[4][4], b[4][4], a[4][4], Y[4][4];
  145.             int cx = x / f, cy = y / f; // central pixels on original images
  146.             // sample supporting pixels in original image
  147.             for (int sx = -1; sx <= 2; ++sx) {
  148.                 for (int sy = -1; sy <= 2; ++sy) {
  149.                     // clamp pixel locations
  150.                     int csy = clamp(sy + cy, 0, h - 1);
  151.                     int csx = clamp(sx + cx, 0, w - 1);
  152.                     // sample & add weighted components
  153.                     u32 sample = data[csy*w + csx];
  154.                     r[sx + 1][sy + 1] = (float)R(sample);
  155.                     g[sx + 1][sy + 1] = (float)G(sample);
  156.                     b[sx + 1][sy + 1] = (float)B(sample);
  157.                     a[sx + 1][sy + 1] = (float)A(sample);
  158.                     Y[sx + 1][sy + 1] = (float)(0.2126*r[sx + 1][sy + 1] + 0.7152*g[sx + 1][sy + 1] + 0.0722*b[sx + 1][sy + 1]);
  159.                 }
  160.             }
  161.             float min_r_sample = min4(r[1][1], r[2][1], r[1][2], r[2][2]);
  162.             float min_g_sample = min4(g[1][1], g[2][1], g[1][2], g[2][2]);
  163.             float min_b_sample = min4(b[1][1], b[2][1], b[1][2], b[2][2]);
  164.             float min_a_sample = min4(a[1][1], a[2][1], a[1][2], a[2][2]);
  165.             float max_r_sample = max4(r[1][1], r[2][1], r[1][2], r[2][2]);
  166.             float max_g_sample = max4(g[1][1], g[2][1], g[1][2], g[2][2]);
  167.             float max_b_sample = max4(b[1][1], b[2][1], b[1][2], b[2][2]);
  168.             float max_a_sample = max4(a[1][1], a[2][1], a[1][2], a[2][2]);
  169.             float d_edge = diagonal_edge(Y, &wp[0]);
  170.             float r1, g1, b1, a1, r2, g2, b2, a2, rf, gf, bf, af;
  171.             r1 = (float)w1*(r[0][3] + r[3][0]) + (float)w2*(r[1][2] + r[2][1]);
  172.             g1 = (float)w1*(g[0][3] + g[3][0]) + (float)w2*(g[1][2] + g[2][1]);
  173.             b1 = (float)w1*(b[0][3] + b[3][0]) + (float)w2*(b[1][2] + b[2][1]);
  174.             a1 = (float)w1*(a[0][3] + a[3][0]) + (float)w2*(a[1][2] + a[2][1]);
  175.             r2 = (float)w1*(r[0][0] + r[3][3]) + (float)w2*(r[1][1] + r[2][2]);
  176.             g2 = (float)w1*(g[0][0] + g[3][3]) + (float)w2*(g[1][1] + g[2][2]);
  177.             b2 = (float)w1*(b[0][0] + b[3][3]) + (float)w2*(b[1][1] + b[2][2]);
  178.             a2 = (float)w1*(a[0][0] + a[3][3]) + (float)w2*(a[1][1] + a[2][2]);
  179.             // generate and write result
  180.             if (d_edge <= 0.0f) { rf = r1; gf = g1; bf = b1; af = a1; }
  181.             else { rf = r2; gf = g2; bf = b2; af = a2; }
  182.             // anti-ringing, clamp.
  183.             rf = clamp(rf, min_r_sample, max_r_sample);
  184.             gf = clamp(gf, min_g_sample, max_g_sample);
  185.             bf = clamp(bf, min_b_sample, max_b_sample);
  186.             af = clamp(af, min_a_sample, max_a_sample);
  187.             int ri = clamp(static_cast<int>(ceilf(rf)), 0, 255);
  188.             int gi = clamp(static_cast<int>(ceilf(gf)), 0, 255);
  189.             int bi = clamp(static_cast<int>(ceilf(bf)), 0, 255);
  190.             int ai = clamp(static_cast<int>(ceilf(af)), 0, 255);
  191.             out[y*outw + x] = out[y*outw + x + 1] = out[(y + 1)*outw + x] = data[cy*w + cx];
  192.             out[(y+1)*outw + x+1] = (ai << 24) | (bi << 16) | (gi << 8) | ri;
  193.             ++x;
  194.         }
  195.         ++y;
  196.     }
  197.    
  198.  
  199.  
  200.     // Second Pass
  201.     wp[0] = 2.0f;
  202.     wp[1] = 0.0f;
  203.     wp[2] = 0.0f;
  204.     wp[3] = 0.0f;
  205.     wp[4] = 0.0f;
  206.     wp[5] = 0.0f;
  207.  
  208.     for (int y = 0; y < outh; ++y) {
  209.         for (int x = 0; x < outw; ++x) {
  210.             float r[4][4], g[4][4], b[4][4], a[4][4], Y[4][4];
  211.             // sample supporting pixels in original image
  212.             for (int sx = -1; sx <= 2; ++sx) {
  213.                 for (int sy = -1; sy <= 2; ++sy) {
  214.                     // clamp pixel locations
  215.                     int csy = clamp(sx - sy + y, 0, f*h - 1);
  216.                     int csx = clamp(sx + sy + x, 0, f*w - 1);
  217.                     // sample & add weighted components
  218.                     u32 sample = out[csy*outw + csx];
  219.                     r[sx + 1][sy + 1] = (float)R(sample);
  220.                     g[sx + 1][sy + 1] = (float)G(sample);
  221.                     b[sx + 1][sy + 1] = (float)B(sample);
  222.                     a[sx + 1][sy + 1] = (float)A(sample);
  223.                     Y[sx + 1][sy + 1] = (float)(0.2126*r[sx + 1][sy + 1] + 0.7152*g[sx + 1][sy + 1] + 0.0722*b[sx + 1][sy + 1]);
  224.                 }
  225.             }
  226.             float min_r_sample = min4(r[1][1], r[2][1], r[1][2], r[2][2]);
  227.             float min_g_sample = min4(g[1][1], g[2][1], g[1][2], g[2][2]);
  228.             float min_b_sample = min4(b[1][1], b[2][1], b[1][2], b[2][2]);
  229.             float min_a_sample = min4(a[1][1], a[2][1], a[1][2], a[2][2]);
  230.             float max_r_sample = max4(r[1][1], r[2][1], r[1][2], r[2][2]);
  231.             float max_g_sample = max4(g[1][1], g[2][1], g[1][2], g[2][2]);
  232.             float max_b_sample = max4(b[1][1], b[2][1], b[1][2], b[2][2]);
  233.             float max_a_sample = max4(a[1][1], a[2][1], a[1][2], a[2][2]);
  234.             float d_edge = diagonal_edge(Y, &wp[0]);
  235.             float r1, g1, b1, a1, r2, g2, b2, a2, rf, gf, bf, af;
  236.             r1 = (float)w3*(r[0][3] + r[3][0]) + (float)w4*(r[1][2] + r[2][1]);
  237.             g1 = (float)w3*(g[0][3] + g[3][0]) + (float)w4*(g[1][2] + g[2][1]);
  238.             b1 = (float)w3*(b[0][3] + b[3][0]) + (float)w4*(b[1][2] + b[2][1]);
  239.             a1 = (float)w3*(a[0][3] + a[3][0]) + (float)w4*(a[1][2] + a[2][1]);
  240.             r2 = (float)w3*(r[0][0] + r[3][3]) + (float)w4*(r[1][1] + r[2][2]);
  241.             g2 = (float)w3*(g[0][0] + g[3][3]) + (float)w4*(g[1][1] + g[2][2]);
  242.             b2 = (float)w3*(b[0][0] + b[3][3]) + (float)w4*(b[1][1] + b[2][2]);
  243.             a2 = (float)w3*(a[0][0] + a[3][3]) + (float)w4*(a[1][1] + a[2][2]);
  244.             // generate and write result
  245.             if (d_edge <= 0.0f) { rf = r1; gf = g1; bf = b1; af = a1; }
  246.             else { rf = r2; gf = g2; bf = b2; af = a2; }
  247.             // anti-ringing, clamp.
  248.             rf = clamp(rf, min_r_sample, max_r_sample);
  249.             gf = clamp(gf, min_g_sample, max_g_sample);
  250.             bf = clamp(bf, min_b_sample, max_b_sample);
  251.             af = clamp(af, min_a_sample, max_a_sample);
  252.             int ri = clamp(static_cast<int>(ceilf(rf)), 0, 255);
  253.             int gi = clamp(static_cast<int>(ceilf(gf)), 0, 255);
  254.             int bi = clamp(static_cast<int>(ceilf(bf)), 0, 255);
  255.             int ai = clamp(static_cast<int>(ceilf(af)), 0, 255);
  256.             out[y*outw + x + 1] = (ai << 24) | (bi << 16) | (gi << 8) | ri;
  257.  
  258.             for (int sx = -1; sx <= 2; ++sx) {
  259.                 for (int sy = -1; sy <= 2; ++sy) {
  260.                     // clamp pixel locations
  261.                     int csy = clamp(sx - sy + 1 + y, 0, f*h - 1);
  262.                     int csx = clamp(sx + sy - 1 + x, 0, f*w - 1);
  263.                     // sample & add weighted components
  264.                     u32 sample = out[csy*outw + csx];
  265.                     r[sx + 1][sy + 1] = (float)R(sample);
  266.                     g[sx + 1][sy + 1] = (float)G(sample);
  267.                     b[sx + 1][sy + 1] = (float)B(sample);
  268.                     a[sx + 1][sy + 1] = (float)A(sample);
  269.                     Y[sx + 1][sy + 1] = (float)(0.2126*r[sx + 1][sy + 1] + 0.7152*g[sx + 1][sy + 1] + 0.0722*b[sx + 1][sy + 1]);
  270.                 }
  271.             }
  272.             d_edge = diagonal_edge(Y, &wp[0]);
  273.             r1 = (float)w3*(r[0][3] + r[3][0]) + (float)w4*(r[1][2] + r[2][1]);
  274.             g1 = (float)w3*(g[0][3] + g[3][0]) + (float)w4*(g[1][2] + g[2][1]);
  275.             b1 = (float)w3*(b[0][3] + b[3][0]) + (float)w4*(b[1][2] + b[2][1]);
  276.             a1 = (float)w3*(a[0][3] + a[3][0]) + (float)w4*(a[1][2] + a[2][1]);
  277.             r2 = (float)w3*(r[0][0] + r[3][3]) + (float)w4*(r[1][1] + r[2][2]);
  278.             g2 = (float)w3*(g[0][0] + g[3][3]) + (float)w4*(g[1][1] + g[2][2]);
  279.             b2 = (float)w3*(b[0][0] + b[3][3]) + (float)w4*(b[1][1] + b[2][2]);
  280.             a2 = (float)w3*(a[0][0] + a[3][3]) + (float)w4*(a[1][1] + a[2][2]);
  281.             // generate and write result
  282.             if (d_edge <= 0.0f) { rf = r1; gf = g1; bf = b1; af = a1; }
  283.             else { rf = r2; gf = g2; bf = b2; af = a2; }
  284.             // anti-ringing, clamp.
  285.             rf = clamp(rf, min_r_sample, max_r_sample);
  286.             gf = clamp(gf, min_g_sample, max_g_sample);
  287.             bf = clamp(bf, min_b_sample, max_b_sample);
  288.             af = clamp(af, min_a_sample, max_a_sample);
  289.             ri = clamp(static_cast<int>(ceilf(rf)), 0, 255);
  290.             gi = clamp(static_cast<int>(ceilf(gf)), 0, 255);
  291.             bi = clamp(static_cast<int>(ceilf(bf)), 0, 255);
  292.             ai = clamp(static_cast<int>(ceilf(af)), 0, 255);
  293.             out[(y+1)*outw + x] = (ai << 24) | (bi << 16) | (gi << 8) | ri;
  294.             ++x;
  295.         }
  296.         ++y;
  297.     }
  298.  
  299.     // Third Pass
  300.     wp[0] =  2.0f;
  301.     wp[1] =  1.0f;
  302.     wp[2] = -1.0f;
  303.     wp[3] =  4.0f;
  304.     wp[4] = -1.0f;
  305.     wp[5] =  1.0f;
  306.  
  307.     for (int y = outh - 1; y >= 0; --y) {
  308.         for (int x = outw - 1; x >= 0; --x) {
  309.             float r[4][4], g[4][4], b[4][4], a[4][4], Y[4][4];
  310.             for (int sx = -2; sx <= 1; ++sx) {
  311.                 for (int sy = -2; sy <= 1; ++sy) {
  312.                     // clamp pixel locations
  313.                     int csy = clamp(sy + y, 0, f*h - 1);
  314.                     int csx = clamp(sx + x, 0, f*w - 1);
  315.                     // sample & add weighted components
  316.                     u32 sample = out[csy*outw + csx];
  317.                     r[sx + 2][sy + 2] = (float)R(sample);
  318.                     g[sx + 2][sy + 2] = (float)G(sample);
  319.                     b[sx + 2][sy + 2] = (float)B(sample);
  320.                     a[sx + 2][sy + 2] = (float)A(sample);
  321.                     Y[sx + 2][sy + 2] = (float)(0.2126*r[sx + 2][sy + 2] + 0.7152*g[sx + 2][sy + 2] + 0.0722*b[sx + 2][sy + 2]);
  322.                 }
  323.             }
  324.             float min_r_sample = min4(r[1][1], r[2][1], r[1][2], r[2][2]);
  325.             float min_g_sample = min4(g[1][1], g[2][1], g[1][2], g[2][2]);
  326.             float min_b_sample = min4(b[1][1], b[2][1], b[1][2], b[2][2]);
  327.             float min_a_sample = min4(a[1][1], a[2][1], a[1][2], a[2][2]);
  328.             float max_r_sample = max4(r[1][1], r[2][1], r[1][2], r[2][2]);
  329.             float max_g_sample = max4(g[1][1], g[2][1], g[1][2], g[2][2]);
  330.             float max_b_sample = max4(b[1][1], b[2][1], b[1][2], b[2][2]);
  331.             float max_a_sample = max4(a[1][1], a[2][1], a[1][2], a[2][2]);
  332.             float d_edge = diagonal_edge(Y, &wp[0]);
  333.             float r1, g1, b1, a1, r2, g2, b2, a2, rf, gf, bf, af;
  334.             r1 = (float)w1*(r[0][3] + r[3][0]) + (float)w2*(r[1][2] + r[2][1]);
  335.             g1 = (float)w1*(g[0][3] + g[3][0]) + (float)w2*(g[1][2] + g[2][1]);
  336.             b1 = (float)w1*(b[0][3] + b[3][0]) + (float)w2*(b[1][2] + b[2][1]);
  337.             a1 = (float)w1*(a[0][3] + a[3][0]) + (float)w2*(a[1][2] + a[2][1]);
  338.             r2 = (float)w1*(r[0][0] + r[3][3]) + (float)w2*(r[1][1] + r[2][2]);
  339.             g2 = (float)w1*(g[0][0] + g[3][3]) + (float)w2*(g[1][1] + g[2][2]);
  340.             b2 = (float)w1*(b[0][0] + b[3][3]) + (float)w2*(b[1][1] + b[2][2]);
  341.             a2 = (float)w1*(a[0][0] + a[3][3]) + (float)w2*(a[1][1] + a[2][2]);
  342.             // generate and write result
  343.             if (d_edge <= 0.0f) { rf = r1; gf = g1; bf = b1; af = a1; }
  344.             else { rf = r2; gf = g2; bf = b2; af = a2; }
  345.             // anti-ringing, clamp.
  346.             rf = clamp(rf, min_r_sample, max_r_sample);
  347.             gf = clamp(gf, min_g_sample, max_g_sample);
  348.             bf = clamp(bf, min_b_sample, max_b_sample);
  349.             af = clamp(af, min_a_sample, max_a_sample);
  350.             int ri = clamp(static_cast<int>(ceilf(rf)), 0, 255);
  351.             int gi = clamp(static_cast<int>(ceilf(gf)), 0, 255);
  352.             int bi = clamp(static_cast<int>(ceilf(bf)), 0, 255);
  353.             int ai = clamp(static_cast<int>(ceilf(af)), 0, 255);
  354.             out[y*outw + x] = (ai << 24) | (bi << 16) | (gi << 8) | ri;
  355.         }
  356.     }
  357.  
  358. }
  359.  
  360.  
  361. //// *** Super-xBR code ends here - MIT LICENSE *** ///
  362.  
  363.  
  364. void scaleSuperXBR(int factor, u32* data, u32* out, int w, int h) {
  365. /* Needs implementation.*/
  366.     switch (factor) {
  367.     case 2: scaleSuperXBRT<2>(data, out, w, h); break;
  368.     default: ERROR_LOG(VIDEO, "Super-xBR upsampling only implemented for factor 2");
  369.     }
  370.  
  371. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement