/*
 * MLS_applyDeform.c
 *
 * Code generation for function 'MLS_applyDeform'
 *
 */

/* Include files */
#include "rt_nonfinite.h"
#include "MLS_applyDeform.h"
#include "MLS_applyDeform_data.h"

/* Variable Definitions */
static emlrtRSInfo emlrtRSI = { 52,    /* lineNo */
  "MLS_applyDeform",                   /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m"/* pathName */
};

static emlrtRSInfo b_emlrtRSI = { 144, /* lineNo */
  "MLS_applyDeform",                   /* fcnName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m"/* pathName */
};

static emlrtRTEInfo emlrtRTEI = { 27,  /* lineNo */
  9,                                   /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m"/* pName */
};

static emlrtRTEInfo b_emlrtRTEI = { 28,/* lineNo */
  13,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m"/* pName */
};

static emlrtDCInfo emlrtDCI = { 80,    /* lineNo */
  42,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo emlrtBCI = { 1,     /* iFirst */
  360,                                 /* iLast */
  80,                                  /* lineNo */
  42,                                  /* colNo */
  "f",                                 /* aName */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo b_emlrtDCI = { 80,  /* lineNo */
  44,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo b_emlrtBCI = { 1,   /* iFirst */
  640,                                 /* iLast */
  80,                                  /* lineNo */
  44,                                  /* colNo */
  "f",                                 /* aName */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo c_emlrtDCI = { 80,  /* lineNo */
  54,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo c_emlrtBCI = { 1,   /* iFirst */
  640,                                 /* iLast */
  80,                                  /* lineNo */
  54,                                  /* colNo */
  "f",                                 /* aName */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo d_emlrtDCI = { 80,  /* lineNo */
  63,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo d_emlrtBCI = { 1,   /* iFirst */
  360,                                 /* iLast */
  80,                                  /* lineNo */
  63,                                  /* colNo */
  "f",                                 /* aName */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtRTEInfo c_emlrtRTEI = { 87,/* lineNo */
  9,                                   /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m"/* pName */
};

static emlrtRTEInfo d_emlrtRTEI = { 92,/* lineNo */
  13,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m"/* pName */
};

static emlrtBCInfo e_emlrtBCI = { 1,   /* iFirst */
  360,                                 /* iLast */
  165,                                 /* lineNo */
  16,                                  /* colNo */
  "oldImg",                            /* aName */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo e_emlrtDCI = { 165, /* lineNo */
  16,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo f_emlrtBCI = { 1,   /* iFirst */
  640,                                 /* iLast */
  164,                                 /* lineNo */
  21,                                  /* colNo */
  "oldImg",                            /* aName */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  0                                    /* checkKind */
};

static emlrtDCInfo f_emlrtDCI = { 164, /* lineNo */
  21,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtDCInfo g_emlrtDCI = { 163, /* lineNo */
  21,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtDCInfo h_emlrtDCI = { 163, /* lineNo */
  16,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtBCInfo g_emlrtBCI = { 1,   /* iFirst */
  360,                                 /* iLast */
  161,                                 /* lineNo */
  12,                                  /* colNo */
  "newImg",                            /* aName */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  3                                    /* checkKind */
};

static emlrtDCInfo i_emlrtDCI = { 161, /* lineNo */
  12,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

static emlrtDCInfo j_emlrtDCI = { 161, /* lineNo */
  18,                                  /* colNo */
  "MLS_applyDeform",                   /* fName */
  "C:\\Users\\tomas\\Documents\\BP Matlab\\gui\\MLS_applyDeform.m",/* pName */
  1                                    /* checkKind */
};

/* Function Declarations */
static void Interpolation(const emlrtStack *sp, real_T i, real_T j, real_T ni,
  real_T nj, real_T x2, real_T y2, const real_T f[460800], real_T *finterp,
  uint8_T newImg[691200], const uint8_T oldImg[691200]);

/* Function Definitions */
static void Interpolation(const emlrtStack *sp, real_T i, real_T j, real_T ni,
  real_T nj, real_T x2, real_T y2, const real_T f[460800], real_T *finterp,
  uint8_T newImg[691200], const uint8_T oldImg[691200])
{
  int32_T ixstart;
  real_T fx;
  int32_T ix;
  boolean_T exitg1;
  real_T x[6];
  real_T slope[6];
  real_T varargin_1[12];
  real_T nx;
  real_T step;
  real_T b_x;
  real_T y;
  real_T py_idx_0;
  real_T py_idx_1;
  real_T px_idx_0;
  real_T px_idx_1;
  int32_T i2;
  int32_T b_y;
  real_T fy;
  real_T ny;
  real_T f_nx;
  real_T f_ny;
  real_T fpx;
  real_T y11;
  real_T y22;
  int32_T layer;
  int32_T i3;
  uint8_T u0;
  uint8_T u1;
  int32_T i4;
  uint8_T u2;
  emlrtStack st;
  st.prev = sp;
  st.tls = sp->tls;
  *finterp = 0.0;

  /*  Interpolation */
  /*  calculate step form slopes */
  /*  try  */
  ixstart = 1;
  fx = x2;
  if (muDoubleScalarIsNaN(x2)) {
    ix = 2;
    exitg1 = false;
    while ((!exitg1) && (ix < 3)) {
      ixstart = 2;
      if (!muDoubleScalarIsNaN(y2)) {
        fx = y2;
        exitg1 = true;
      } else {
        ix = 3;
      }
    }
  }

  if ((ixstart < 2) && (y2 < fx)) {
    fx = y2;
  }

  if (j != (int32_T)muDoubleScalarFloor(j)) {
    emlrtIntegerCheckR2012b(j, &emlrtDCI, sp);
  }

  ixstart = (int32_T)j;
  if (!((ixstart >= 1) && (ixstart <= 360))) {
    emlrtDynamicBoundsCheckR2012b(ixstart, 1, 360, &emlrtBCI, sp);
  }

  if (i != (int32_T)muDoubleScalarFloor(i)) {
    emlrtIntegerCheckR2012b(i, &b_emlrtDCI, sp);
  }

  ixstart = (int32_T)i;
  if (!((ixstart >= 1) && (ixstart <= 640))) {
    emlrtDynamicBoundsCheckR2012b(ixstart, 1, 640, &b_emlrtBCI, sp);
  }

  if (ni != (int32_T)muDoubleScalarFloor(ni)) {
    emlrtIntegerCheckR2012b(ni, &c_emlrtDCI, sp);
  }

  ixstart = (int32_T)ni;
  if (!((ixstart >= 1) && (ixstart <= 640))) {
    emlrtDynamicBoundsCheckR2012b(ixstart, 1, 640, &c_emlrtBCI, sp);
  }

  if (nj != (int32_T)muDoubleScalarFloor(nj)) {
    emlrtIntegerCheckR2012b(nj, &d_emlrtDCI, sp);
  }

  ixstart = (int32_T)nj;
  if (!((ixstart >= 1) && (ixstart <= 360))) {
    emlrtDynamicBoundsCheckR2012b(ixstart, 1, 360, &d_emlrtBCI, sp);
  }

  x[0] = f[((int32_T)j + 360 * ((int32_T)i - 1)) - 1] - f[((int32_T)j + 360 *
    ((int32_T)ni - 1)) - 1];
  x[1] = f[((int32_T)j + 360 * ((int32_T)i - 1)) - 1] - f[((int32_T)nj + 360 *
    ((int32_T)i - 1)) - 1];
  x[2] = f[((int32_T)j + 360 * ((int32_T)i - 1)) - 1] - f[((int32_T)nj + 360 *
    ((int32_T)ni - 1)) - 1];
  x[3] = f[((int32_T)j + 360 * ((int32_T)ni - 1)) - 1] - f[((int32_T)nj + 360 *
    ((int32_T)i - 1)) - 1];
  x[4] = f[((int32_T)j + 360 * ((int32_T)ni - 1)) - 1] - f[((int32_T)nj + 360 *
    ((int32_T)ni - 1)) - 1];
  x[5] = f[((int32_T)nj + 360 * ((int32_T)i - 1)) - 1] - f[((int32_T)nj + 360 *
    ((int32_T)ni - 1)) - 1];
  for (ixstart = 0; ixstart < 6; ixstart++) {
    slope[ixstart] = muDoubleScalarAbs(x[ixstart]);
  }

  x[0] = f[((int32_T)j + 360 * ((int32_T)i - 1)) + 230399] - f[((int32_T)j + 360
    * ((int32_T)ni - 1)) + 230399];
  x[1] = f[((int32_T)j + 360 * ((int32_T)i - 1)) + 230399] - f[((int32_T)nj +
    360 * ((int32_T)i - 1)) + 230399];
  x[2] = f[((int32_T)j + 360 * ((int32_T)i - 1)) + 230399] - f[((int32_T)nj +
    360 * ((int32_T)ni - 1)) + 230399];
  x[3] = f[((int32_T)j + 360 * ((int32_T)ni - 1)) + 230399] - f[((int32_T)nj +
    360 * ((int32_T)i - 1)) + 230399];
  x[4] = f[((int32_T)j + 360 * ((int32_T)ni - 1)) + 230399] - f[((int32_T)nj +
    360 * ((int32_T)ni - 1)) + 230399];
  x[5] = f[((int32_T)nj + 360 * ((int32_T)i - 1)) + 230399] - f[((int32_T)nj +
    360 * ((int32_T)ni - 1)) + 230399];
  for (ixstart = 0; ixstart < 6; ixstart++) {
    varargin_1[ixstart] = slope[ixstart];
    varargin_1[ixstart + 6] = muDoubleScalarAbs(x[ixstart]);
  }

  ixstart = 1;
  nx = varargin_1[0];
  if (muDoubleScalarIsNaN(varargin_1[0])) {
    ix = 2;
    exitg1 = false;
    while ((!exitg1) && (ix < 13)) {
      ixstart = ix;
      if (!muDoubleScalarIsNaN(varargin_1[ix - 1])) {
        nx = varargin_1[ix - 1];
        exitg1 = true;
      } else {
        ix++;
      }
    }
  }

  if (ixstart < 12) {
    while (ixstart + 1 < 13) {
      if (varargin_1[ixstart] > nx) {
        nx = varargin_1[ixstart];
      }

      ixstart++;
    }
  }

  step = fx / nx;

  /*  catch e */
  /*      f */
  /*      fprintf('Size f: %f×%f×%f, i:%f, ni:%f, j:%f, nj:%f',size(f),i,ni,j,nj) */
  /*      rethrow e */
  /*  end */
  ixstart = (int32_T)((x2 + step) / step);
  emlrtForLoopVectorCheckR2012b(0.0, step, x2, mxDOUBLE_CLASS, ixstart,
    &c_emlrtRTEI, sp);
  ix = 0;
  while (ix <= ixstart - 1) {
    b_x = (real_T)ix * step;

    /*  fill deformation function for area between control points (1/2) */
    y = b_x / x2;
    py_idx_0 = f[((int32_T)j + 360 * ((int32_T)i - 1)) - 1] * (1.0 - y) + f
      [((int32_T)j + 360 * ((int32_T)ni - 1)) - 1] * y;
    py_idx_1 = f[((int32_T)nj + 360 * ((int32_T)i - 1)) - 1] * (1.0 - y) + f
      [((int32_T)nj + 360 * ((int32_T)ni - 1)) - 1] * y;
    y = b_x / x2;
    px_idx_0 = f[((int32_T)j + 360 * ((int32_T)i - 1)) + 230399] * (1.0 - y) +
      f[((int32_T)j + 360 * ((int32_T)ni - 1)) + 230399] * y;
    px_idx_1 = f[((int32_T)nj + 360 * ((int32_T)i - 1)) + 230399] * (1.0 - y) +
      f[((int32_T)nj + 360 * ((int32_T)ni - 1)) + 230399] * y;
    i2 = (int32_T)((y2 + step) / step);
    emlrtForLoopVectorCheckR2012b(0.0, step, y2, mxDOUBLE_CLASS, i2,
      &d_emlrtRTEI, sp);
    b_y = 0;
    while (b_y <= i2 - 1) {
      y = (real_T)b_y * step;
      fx = y / y2;
      fy = py_idx_0 * (1.0 - fx) + py_idx_1 * fx;
      fx = y / y2;
      fx = px_idx_0 * (1.0 - fx) + px_idx_1 * fx;

      /*           finterp.x = [finterp.x;x+i]; */
      /*           finterp.y = [finterp.y;y+j]; */
      /*           finterp.zX = [finterp.zX;fx]; */
      /*           finterp.zY = [finterp.zY;fy]; */
      /*  check borders         */
      nx = fx;
      ny = fy;
      if (fx > 640.0) {
        nx = 640.0;
      }

      if (nx < 1.0) {
        nx = 1.0;
      }

      if (fy > 360.0) {
        ny = 360.0;
      }

      if (ny < 1.0) {
        ny = 1.0;
      }

      /*  round dest. pixels */
      f_nx = muDoubleScalarRound(nx);
      f_ny = muDoubleScalarRound(ny);

      /*  float from rounding */
      fpx = muDoubleScalarRound(nx - (i + b_x)) - (fx - (i + b_x));
      nx = muDoubleScalarRound(ny - (j + y)) - (fy - (j + y));

      /*  find closest source pixels 1*1 square */
      ny = muDoubleScalarFloor((i + b_x) + fpx);
      if (ny < 1.0) {
        ny = 1.0;
        fy = 1.0;
        fpx = 0.0;
      } else if (ny > 640.0) {
        ny = 640.0;
        fy = 640.0;
        fpx = 0.0;
      } else {
        fy = muDoubleScalarCeil((i + b_x) + fpx);
        fpx = ((i + b_x) + fpx) - ny;

        /*  distance on unit square for interpolation */
      }

      y11 = muDoubleScalarFloor((j + y) + nx);
      if (y11 < 1.0) {
        y11 = 1.0;
        y22 = 1.0;
        nx = 0.0;
      } else if (y11 > 360.0) {
        y11 = 360.0;
        y22 = 360.0;
        nx = 0.0;
      } else {
        y22 = muDoubleScalarCeil((j + y) + nx);
        nx = ((j + y) + nx) - y11;
      }

      st.site = &b_emlrtRSI;
      if (fy > 640.0) {
        fy = 640.0;
      }

      if (y22 > 360.0) {
        y22 = 360.0;
      }

      layer = 0;
      while (layer <= 2) {
        /*  interpolate the pixel at x+x2+float,y+y2+float and */
        /*  move it to nx,ny */
        /* try */
        if (y11 != (int32_T)y11) {
          emlrtIntegerCheckR2012b(y11, &h_emlrtDCI, &st);
        }

        if (ny != (int32_T)ny) {
          emlrtIntegerCheckR2012b(ny, &g_emlrtDCI, &st);
        }

        if (fy != (int32_T)fy) {
          emlrtIntegerCheckR2012b(fy, &f_emlrtDCI, &st);
        }

        i3 = (int32_T)fy;
        if (!((i3 >= 1) && (i3 <= 640))) {
          emlrtDynamicBoundsCheckR2012b(i3, 1, 640, &f_emlrtBCI, &st);
        }

        if (y22 != (int32_T)y22) {
          emlrtIntegerCheckR2012b(y22, &e_emlrtDCI, &st);
        }

        i3 = (int32_T)y22;
        if (!((i3 >= 1) && (i3 <= 360))) {
          emlrtDynamicBoundsCheckR2012b(i3, 1, 360, &e_emlrtBCI, &st);
        }

        /*  bilinear interpolation for 1*1 square, */
        /*  bilinear interpolation for 1*1 square, if you pass x=x/x2 and y=y/y2 you */
        /*  get bilinear interpolation for rectangle in begining of cordinate system */
        if (f_ny != (int32_T)f_ny) {
          emlrtIntegerCheckR2012b(f_ny, &i_emlrtDCI, &st);
        }

        i3 = (int32_T)f_ny;
        if (!((i3 >= 1) && (i3 <= 360))) {
          emlrtDynamicBoundsCheckR2012b(i3, 1, 360, &g_emlrtBCI, &st);
        }

        if (f_nx != (int32_T)f_nx) {
          emlrtIntegerCheckR2012b(f_nx, &j_emlrtDCI, &st);
        }

        fx = muDoubleScalarRound((real_T)oldImg[(((int32_T)y11 + 360 * ((int32_T)
          ny - 1)) + 230400 * layer) - 1] * (1.0 - fpx));
        if (fx < 256.0) {
          if (fx >= 0.0) {
            u0 = (uint8_T)fx;
          } else {
            u0 = 0;
          }
        } else if (fx >= 256.0) {
          u0 = MAX_uint8_T;
        } else {
          u0 = 0;
        }

        fx = muDoubleScalarRound((real_T)oldImg[(((int32_T)y11 + 360 * ((int32_T)
          fy - 1)) + 230400 * layer) - 1] * fpx);
        if (fx < 256.0) {
          if (fx >= 0.0) {
            u1 = (uint8_T)fx;
          } else {
            u1 = 0;
          }
        } else if (fx >= 256.0) {
          u1 = MAX_uint8_T;
        } else {
          u1 = 0;
        }

        i4 = (int32_T)((uint32_T)u0 + u1);
        if ((uint32_T)i4 > 255U) {
          i4 = 255;
        }

        fx = muDoubleScalarRound((real_T)i4 * (1.0 - nx));
        if (fx < 256.0) {
          if (fx >= 0.0) {
            u0 = (uint8_T)fx;
          } else {
            u0 = 0;
          }
        } else if (fx >= 256.0) {
          u0 = MAX_uint8_T;
        } else {
          u0 = 0;
        }

        fx = muDoubleScalarRound((real_T)oldImg[(((int32_T)y22 + 360 * ((int32_T)
          ny - 1)) + 230400 * layer) - 1] * (1.0 - fpx));
        if (fx < 256.0) {
          if (fx >= 0.0) {
            u1 = (uint8_T)fx;
          } else {
            u1 = 0;
          }
        } else if (fx >= 256.0) {
          u1 = MAX_uint8_T;
        } else {
          u1 = 0;
        }

        fx = muDoubleScalarRound((real_T)oldImg[(((int32_T)y22 + 360 * ((int32_T)
          fy - 1)) + 230400 * layer) - 1] * fpx);
        if (fx < 256.0) {
          if (fx >= 0.0) {
            u2 = (uint8_T)fx;
          } else {
            u2 = 0;
          }
        } else if (fx >= 256.0) {
          u2 = MAX_uint8_T;
        } else {
          u2 = 0;
        }

        i4 = (int32_T)((uint32_T)u1 + u2);
        if ((uint32_T)i4 > 255U) {
          i4 = 255;
        }

        fx = muDoubleScalarRound((real_T)i4 * nx);
        if (fx < 256.0) {
          if (fx >= 0.0) {
            u1 = (uint8_T)fx;
          } else {
            u1 = 0;
          }
        } else if (fx >= 256.0) {
          u1 = MAX_uint8_T;
        } else {
          u1 = 0;
        }

        i4 = (int32_T)((uint32_T)u0 + u1);
        if ((uint32_T)i4 > 255U) {
          i4 = 255;
        }

        newImg[((i3 + 360 * ((int32_T)f_nx - 1)) + 230400 * layer) - 1] =
          (uint8_T)i4;

        /* catch */
        /*     fprintf('f_ny: %f, f_nx: %f, layer: %f, x11: %f, x22: %f, y11: %f,y22: %f',f_ny,f_nx,x11,x22,y11,y22) */
        /* end */
        layer++;
        if (*emlrtBreakCheckR2012bFlagVar != 0) {
          emlrtBreakCheckR2012b(&st);
        }
      }

      b_y++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }

    ix++;
    if (*emlrtBreakCheckR2012bFlagVar != 0) {
      emlrtBreakCheckR2012b(sp);
    }
  }
}

void MLS_applyDeform(const emlrtStack *sp, const uint8_T oldImg[691200], const
                     real_T f[460800], const real_T v_data[], const int32_T
                     v_size[2], const real_T grid_size[2], uint8_T newImg[691200],
                     real_T *finterp)
{
  int32_T i0;
  int32_T i1;
  real_T stepX;
  real_T stepY;
  int32_T i;
  real_T b_i;
  int32_T j;
  real_T b_j;
  real_T ni;
  real_T nj;
  real_T x2;
  real_T y2;
  real_T b_finterp;
  emlrtStack st;
  (void)v_data;
  (void)v_size;
  st.prev = sp;
  st.tls = sp->tls;

  /* UNTITLED2 Summary of this function goes here */
  /*    f(v) - deformation vector */
  /*    v - point of evaluation */
  /*    grid_size - loop info for v points */
  /*  newImg = ones(imh,imw,imd); */
  for (i0 = 0; i0 < 640; i0++) {
    for (i1 = 0; i1 < 360; i1++) {
      newImg[i1 + 360 * i0] = 50;
      newImg[230400 + (i1 + 360 * i0)] = 205;
      newImg[460800 + (i1 + 360 * i0)] = 50;
    }
  }

  /*  finterp.x = []; */
  /*  finterp.y = []; */
  /*  finterp.zX = []; */
  /*  finterp.zY = []; */
  *finterp = 0.0;

  /*  iterate over evaluated points */
  /*  +--------> x,i=width */
  /*  | */
  /*  | */
  /*  | */
  /*  V y,j=height */
  stepX = grid_size[1];
  stepY = grid_size[0];

  /*  go over grid */
  i0 = (int32_T)((640.0 + (grid_size[1] - 1.0)) / grid_size[1]);
  emlrtForLoopVectorCheckR2012b(1.0, grid_size[1], 640.0, mxDOUBLE_CLASS, i0,
    &emlrtRTEI, sp);
  i = 0;
  while (i <= i0 - 1) {
    b_i = 1.0 + (real_T)i * stepX;
    i1 = (int32_T)((360.0 + (stepY - 1.0)) / stepY);
    emlrtForLoopVectorCheckR2012b(1.0, stepY, 360.0, mxDOUBLE_CLASS, i1,
      &b_emlrtRTEI, sp);
    j = 0;
    while (j <= i1 - 1) {
      b_j = 1.0 + (real_T)j * stepY;

      /*  next i,j to create square for bilinear interp */
      ni = b_i + stepX;
      nj = b_j + stepY;

      /*  length of square sides */
      x2 = stepX;
      y2 = stepY;

      /*  check image borders */
      if (ni > 640.0) {
        ni = 640.0;
        x2 = 640.0 - b_i;
      }

      if (nj > 360.0) {
        nj = 360.0;
        y2 = 360.0 - b_j;
      }

      /*    y A            ni,nj */
      /*      |            x2,y2 */
      /*  f01 |-----------+  f11 */
      /*      |           | */
      /*      |           | */
      /*      |           | */
      /*      |           | */
      /*  f00 +----------------> x */
      /*    x1,y1 = 0      f10 */
      /*    i,j            ni,j */
      /*  call interpolation */
      b_finterp = 0.0;
      st.site = &emlrtRSI;
      Interpolation(&st, b_i, b_j, ni, nj, x2, y2, f, &b_finterp, newImg, oldImg);
      j++;
      if (*emlrtBreakCheckR2012bFlagVar != 0) {
        emlrtBreakCheckR2012b(sp);
      }
    }

    i++;
    if (*emlrtBreakCheckR2012bFlagVar != 0) {
      emlrtBreakCheckR2012b(sp);
    }
  }
}

/* End of code generation (MLS_applyDeform.c) */
