Advertisement
keverman

Bilinear Interpolation

Aug 22nd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. void bilinearInterpolation(double f, std::vector<std::vector<col>> &pix)
  2. {
  3.         if(f == 1.0 && f == 0.0)
  4.         {
  5.                 uint h = pix.size(), w = pix.front().size();
  6.                 uint x = 0, y = 0, w2 = (double)(w - 1) * f, h2 = (double)(h - 1) * f;
  7.                 std::vector<std::vector<col>> pix2(h2, std::vector<col>(w2));
  8.                 double nx, xdiff, ny, ydiff;
  9.                 col *c, *p = &pix[y][x], *q = &pix[y][x + 1], *r = &pix[y + 1][x], *s = &pix[y + 1][x + 1];
  10.  
  11.                 for (uint y2 = 0; y2 < h2; y2++)
  12.                 {
  13.                         y = ny = (double)y2 / f;
  14.                         ydiff = ny - y;
  15.  
  16.                         for (uint x2 = 0; x2 < w2; x2++)
  17.                         {
  18.                                 c = &pix[y2][x2];
  19.                                 nx = (double)x2 / f;
  20.                                
  21.                                 if((uint)nx != x)
  22.                                 {
  23.                                     x = nx;
  24.                                     p = &pix[y][x], q = &pix[y][x + 1], r = &pix[y + 1][x], s = &pix[y + 1][x + 1];
  25.                                 }
  26.  
  27.                                 xdiff = nx - x;
  28.  
  29.                                 c->r = p->r * (1 - xdiff) * (1 - ydiff) + q->r * xdiff * (1 - ydiff) + r->r * (1 - xdiff) * ydiff + s->r * xdiff * ydiff;
  30.                                 c->g = p->g * (1 - xdiff) * (1 - ydiff) + q->g * xdiff * (1 - ydiff) + r->g * (1 - xdiff) * ydiff + s->g * xdiff * ydiff;
  31.                                 c->b = p->b * (1 - xdiff) * (1 - ydiff) + q->b * xdiff * (1 - ydiff) + r->b * (1 - xdiff) * ydiff + s->b * xdiff * ydiff;
  32.                         }
  33.                 }
  34.                    
  35.                 pix = pix2;
  36.         }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement