/**************************************************
 Mex C Code for computing the sigmoid transformation
***************************************************/
# include <stdio.h>
# include <stdlib.h>
# include "mex.h"        /* the algorithm is connect to matlab */
# include "math.h"
# define PI 3.1415926
# define ABS(x) ((x)>0? (x):(-(x)))
# define MAX(x, y) ((x)>(y)? (x):(y))
# define MIN(x, y) ((x)<(y)? (x):(y))

/* getting the index of matlab image, (x,y) location, (sx, sy) sizes */
int px(int x, int y, int bx, int by)    
{            
   return (x + (y-1)*bx - 1); 
 }

 /* variables */

 double *I, *J;                /* filtered images */
 int sx, sy, sx1, sy1;                 /* sizes of image */  
 int Fx, Fy; 
 
 
void Cmp()
{
   
   int x, y, here, there, x1, y1; 
   
   for (x=1; x<=sx; x++)
       for (y=1; y<=sy; y++)
       {
        here = px(x, y, sx, sy); 
        x1 = Fx+x; y1 = Fy+y; 
        if ((x1>=1)&&(x1<=sx1)&&(y1>=1)&&(y1<=sy1))
        {
           there = px(x1, y1, sx1, sy1); 
           I[here] = J[there];
        }
        else
            I[here] = 0.; 
        }
}
   


/* mex function is used to pass on the pointers and scalars from matlab, 
   so that heavy computation can be done by C, which puts the results into 
   some of the pointers. After that, matlab can then use these results. 
   
   So matlab is very much like a managing platform for organizing the 
   experiments, and mex C is like a work enginee for fast computation. */

void mexFunction(int nlhs, mxArray *plhs[], 
                 int nrhs, const mxArray *prhs[])                
{
 int c; 
 
 c = 0; /* counter for input pointers and scalars */
 I = mxGetPr(prhs[c++]);
 J = mxGetPr(prhs[c++]);
 Fx = floor(mxGetScalar(prhs[c++])+.5);
 Fy = floor(mxGetScalar(prhs[c++])+.5); 
 sx = floor(mxGetScalar(prhs[c++])+.5);
 sy = floor(mxGetScalar(prhs[c++])+.5);   /* size of images */
 sx1 = floor(mxGetScalar(prhs[c++])+.5);
 sy1 = floor(mxGetScalar(prhs[c++])+.5);   /* size of images */
 Cmp();
}

     



 

                    