Advertisement
kujikita

Untitled

Feb 2nd, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 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.             double[][] p = new double[4][];
  7.             p[0] = new double[4];
  8.             p[1] = new double[4];
  9.             p[2] = new double[4];
  10.             p[3] = new double[4];
  11.            
  12.             long pix0 = 0;
  13.             long pix1 = 0;
  14.             for (byte c = 0; c < 4; c++)
  15.                 for (int y = 0; y < h_new; y++)
  16.                     for (int x = 0; x < w_new; x++)
  17.                     {
  18.                         pix0 = (long)(x / scalex) + (long)(y / scaley) * (long)(w_new / scalex);
  19.                         for (byte k = 0; k < 4; k++)
  20.                             for (byte i = 0; i < 4; i++)
  21.                             {
  22.                                 pix1 = ((long)(pix0 + i + k * (w_new / scalex)) << 2) | c;
  23.                                 if (pix1 < Data.LongLength) p[i][k] = Data[pix1];
  24.                                 else                        p[i][k] = 0;
  25.                             }
  26.                         NewData[((x + y * w_new) << 2) | c] =
  27.                             (byte)BicubicInterpolate(p, x - (long)(x / scalex), y - (long)(y / scaley));
  28.                     }
  29.             return NewData;
  30.         }
  31.  
  32.         private double BicubicInterpolate(double[][] p, double x, double y)
  33.         {
  34.             double[] arr = new double[4];
  35.             arr[0] = CubicInterpolate(p[0], y);
  36.             arr[1] = CubicInterpolate(p[1], y);
  37.             arr[2] = CubicInterpolate(p[2], y);
  38.             arr[3] = CubicInterpolate(p[3], y);
  39.             return CubicInterpolate(arr, x);
  40.         }
  41.  
  42.         private double CubicInterpolate(double[] p, double x) => p[1] + 0.5 * x * (p[2] - p[0] + x *
  43.             (2.0 * p[0] - 5.0 * p[1] + 4.0 * p[2] - p[3] + x * (3.0 * (p[1] - p[2]) + p[3] - p[0])));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement