/* * $Id: spm_conv_vol.c 4452 2011-09-02 10:45:26Z guillaume $ * John Ashburner */ #include #include "mex.h" #include "spm_mapping.h" #include "spm_datatypes.h" #define RINT(A) floor((A)+0.5) static void convxy(out, xdim, ydim, filtx, filty, fxdim, fydim, xoff, yoff, buff) int xdim, ydim, fxdim, fydim, xoff, yoff; double out[], filtx[], filty[], buff[]; { int x,y,k; for(y=0; y= xdim) ? x-xdim-xoff+1 : 0); fend = ((x-(xoff+fxdim) < 0) ? x-xoff+1 : fxdim); for(k=fstart; k= ydim) ? y-ydim-yoff+1 : 0); fend = ((y-(yoff+fydim) < 0) ? y-yoff+1 : fydim); for(k=fstart; kdim[0]; ydim = vol->dim[1]; zdim = vol->dim[2]; tmp = (double *)mxCalloc(xdim*ydim*fzdim,sizeof(double)); buff = (double *)mxCalloc(((ydim>xdim) ? ydim : xdim),sizeof(double)); sortedv = (double **)mxCalloc(fzdim, sizeof(double *)); startz = ((fzdim+zoff-1<0) ? fzdim+zoff-1 : 0); endz = zdim+fzdim+zoff-1; for (z=startz; z= 0 && zdim[0], vol->dim[1], vol, 0, 0); convxy(tmp+((z%fzdim)*xdim*ydim),xdim, ydim, filtx, filty, fxdim, fydim, xoff, yoff, buff); } if (z-fzdim-zoff+1>=0 && z-fzdim-zoff+1= zdim) ? z-zdim+1 : 0); fend = ((z-fzdim < 0) ? z+1 : fzdim); for(k=0; k2147483647.0) tmp = 2147483647.0; obuf[xy] = RINT(tmp); } } else for(xy=0; xy4294967295.0) tmp = 4294967295.0; obuf[xy] = RINT(tmp); } } else for(xy=0; xy32767) tmp = 32767; obuf[xy] = RINT(tmp); } } else for(xy=0; xy65535) tmp = 65535; obuf[xy] = RINT(tmp); } } else for(xy=0; xy127) tmp = 127; obuf[xy] = RINT(tmp); } } else for(xy=0; xy255) tmp = 255; obuf[xy] = RINT(tmp); } } else for(xy=0; xy 0) { mexErrMsgTxt("Incorrect usage."); } map=get_maps(prhs[0], &k); if (k!=1) { free_maps(map, k); mexErrMsgTxt("Too many images to smooth at once."); } if (!mxIsNumeric(prhs[1])) { /* The compiler doesn't like this line - but I think it's OK */ wplane_args[0] = (struct mxArray_tag *)prhs[1]; wplane_args[1] = mxCreateDoubleMatrix(map->dim[0],map->dim[1],mxREAL); wplane_args[2] = mxCreateDoubleMatrix(1,1,mxREAL); oVol = (double *)0; } else { if ( mxIsComplex(prhs[1]) || mxIsSparse(prhs[1]) || mxGetM(prhs[1])*mxGetN(prhs[1]) != map->dim[0]*map->dim[1]*map->dim[2]) { free_maps(map, 1); mexErrMsgTxt("Bad output array"); } oVol = (double *)mxGetPr(prhs[1]); if (mxIsDouble(prhs[1])) dtype = SPM_DOUBLE; else if (mxIsSingle(prhs[1])) dtype = SPM_FLOAT; else if (mxIsInt32 (prhs[1])) dtype = SPM_SIGNED_INT; else if (mxIsUint32(prhs[1])) dtype = SPM_UNSIGNED_INT; else if (mxIsInt16 (prhs[1])) dtype = SPM_SIGNED_SHORT; else if (mxIsUint16(prhs[1])) dtype = SPM_UNSIGNED_SHORT; else if (mxIsInt8 (prhs[1])) dtype = SPM_SIGNED_CHAR; else if (mxIsUint8 (prhs[1])) dtype = SPM_UNSIGNED_CHAR; else mexErrMsgTxt("Unknown output datatype."); } for(k=2; k<=5; k++) { if (!mxIsNumeric(prhs[k]) || mxIsComplex(prhs[k]) || mxIsSparse(prhs[k]) || !mxIsDouble(prhs[k])) { free_maps(map, 1); mexErrMsgTxt("Functions must be numeric, real, full and double."); } } if (mxGetM(prhs[5])*mxGetN(prhs[5]) != 3) { free_maps(map, 1); mexErrMsgTxt("Offsets must have three values."); } offsets = mxGetPr(prhs[5]); if (convxyz(map, mxGetPr(prhs[2]), mxGetPr(prhs[3]), mxGetPr(prhs[4]), mxGetM(prhs[2])*mxGetN(prhs[2]), mxGetM(prhs[3])*mxGetN(prhs[3]), mxGetM(prhs[4])*mxGetN(prhs[4]), (int)floor(offsets[0]), (int)floor(offsets[1]), (int)floor(offsets[2]), oVol, wplane_args, dtype) != 0) { free_maps(map, 1); mexErrMsgTxt("Error writing data."); } free_maps(map, 1); if (!oVol) { mxDestroyArray(wplane_args[1]); mxDestroyArray(wplane_args[2]); } }