Advertisement
kujikita

Untitled

Feb 2nd, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.50 KB | None | 0 0
  1.         private byte[] BicubicResize(byte[] Data, int w, int h, double scalex, double scaley)
  2.         {
  3.             int w_new = (int)(w * scalex);
  4.             int h_new = (int)(h * scaley);
  5.             byte[] NewData = new byte[w_new * h_new * 4];
  6.             byte c;
  7.             int x, y;
  8.  
  9.             BicubicInterpolation interpolation = new BicubicInterpolation(scalex, scaley, w_new, h_new);
  10.             for (c = 0; c < 4; c++)
  11.                 for (y = 0; y < h_new; y++)
  12.                     for (x = 0; x < w_new; x++)
  13.                         NewData[((x + y * w_new) << 2) | c] = interpolation.Bicubic(x, y, c, Data);
  14.             return NewData;
  15.         }
  16.  
  17.         public class BicubicInterpolation
  18.         {
  19.             double dx;
  20.             double dy;
  21.             double p0;
  22.             double p1;
  23.             double p2;
  24.             double p3;
  25.             double[] Rdx;
  26.             double[] Rdy;
  27.             double s;
  28.             byte k;
  29.             byte i;
  30.             long pix0;
  31.             long pix1;
  32.             double[][] p;
  33.             double scalex;
  34.             double scaley;
  35.             int w;
  36.             int h;
  37.  
  38.             public BicubicInterpolation(double scalex = 1, double scaley = 1, int w = 0, int h = 0)
  39.             {
  40.                 p = new double[4][];
  41.                 p[0] = new double[4];
  42.                 p[1] = new double[4];
  43.                 p[2] = new double[4];
  44.                 p[3] = new double[4];
  45.                 pix0 = 0;
  46.                 pix1 = 0;
  47.  
  48.                 this.scalex = scalex;
  49.                 this.scaley = scaley;
  50.                 this.w = w;
  51.                 this.h = h;
  52.             }
  53.  
  54.             public byte Bicubic(int x, int y, byte c, byte[] Data)
  55.             {
  56.                 pix0 = (long)(x / scalex) + (long)(y / scaley) * (long)(w / scalex);
  57.                 for (k = 0; k < 4; k++)
  58.                     for (i = 0; i < 4; i++)
  59.                     {
  60.                         pix1 = ((long)(pix0 + i + k * (w / scalex)) << 2) | c;
  61.                         if (pix1 < Data.LongLength) p[i][k] = Data[pix1];
  62.                         else p[i][k] = 0;
  63.                     }
  64.  
  65.                 //Temp = BicubicInterpolate(p, x - (long)(x / scalex), y - (long)(y / scaley));
  66.                 return (byte)Interpolation(x, y, p);
  67.             }
  68.  
  69.             private double Interpolation(double tx, double ty, double[][] P)
  70.             {
  71.                 dx = F(tx);
  72.                 dy = F(ty);
  73.  
  74.                 Rdx = new double[4];
  75.  
  76.                 Rdy = new double[4];
  77.                 for (int n = 0; n < 4; n++)
  78.                 {
  79.                     Rdx[n] = R(n - 1 - dx);
  80.                     Rdy[n] = R(n - 1 - dy);
  81.                 }
  82.  
  83.                 s = 0;
  84.  
  85.                 for (byte k = 0; k < 4; k++)
  86.                     for (byte i = 0; i < 4; i++)
  87.                         s += P[k][i] * Rdx[i] * Rdy[k];
  88.                 return s;
  89.             }
  90.  
  91.             private double F(double x) => x - Math.Floor(x);
  92.             private double P(double x) => (x > 0.0) ? x : 0.0;
  93.             private double R(double x)
  94.             {
  95.                 //return (Math.Pow(P(x + 2), 3) - 4 * Math.Pow(P(x + 1), 3) +
  96.                 //    6 * Math.Pow(P(x), 3) - 4 * Math.Pow(P(x - 1), 3)) / 6;
  97.                 p0 = P(x + 2);
  98.                 p1 = P(x + 1);
  99.                 p2 = P(x    );
  100.                 p3 = P(x - 1);
  101.                 return ((p0 * p0 * p0) - 4 * (p1 * p1 * p1) + 6 * (p2 * p2 * p2) - 4 * (p3 * p3 * p3)) / 6.0;
  102.             }
  103.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement