corrDn.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. RES = corrDn(IM, FILT, EDGES, STEP, START, STOP);
  3. >>> See corrDn.m for documentation <<<
  4. This is a matlab interface to the internal_reduce function.
  5. EPS, 7/96.
  6. */
  7. #define V4_COMPAT
  8. #include <matrix.h> /* Matlab matrices */
  9. #include <mex.h>
  10. #include "convolve.h"
  11. #define notDblMtx(it) (!mxIsNumeric(it) || !mxIsDouble(it) || mxIsSparse(it) || mxIsComplex(it))
  12. void mexFunction(int nlhs, /* Num return vals on lhs */
  13. mxArray *plhs[], /* Matrices on lhs */
  14. int nrhs, /* Num args on rhs */
  15. const mxArray *prhs[] /* Matrices on rhs */
  16. )
  17. {
  18. double *image,*filt, *temp, *result;
  19. int x_fdim, y_fdim, x_idim, y_idim;
  20. int x_rdim, y_rdim;
  21. int x_start = 1;
  22. int x_step = 1;
  23. int y_start = 1;
  24. int y_step = 1;
  25. int x_stop, y_stop;
  26. const mxArray *arg0,*arg1,*arg3,*arg4;
  27. double *mxMat;
  28. char edges[15] = "reflect1";
  29. if (nrhs<2) mexErrMsgTxt("requres at least 2 args.");
  30. /* ARG 1: IMAGE */
  31. arg0 = prhs[0];
  32. if notDblMtx(arg0) mexErrMsgTxt("IMAGE arg must be a non-sparse double float matrix.");
  33. image = mxGetPr(arg0);
  34. x_idim = (int) mxGetM(arg0); /* X is inner index! */
  35. y_idim = (int) mxGetN(arg0);
  36. /* ARG 2: FILTER */
  37. arg1 = prhs[1];
  38. if notDblMtx(arg1) mexErrMsgTxt("FILTER arg must be non-sparse double float matrix.");
  39. filt = mxGetPr(arg1);
  40. x_fdim = (int) mxGetM(arg1);
  41. y_fdim = (int) mxGetN(arg1);
  42. if ((x_fdim > x_idim) || (y_fdim > y_idim))
  43. {
  44. mexPrintf("Filter: [%d %d], Image: [%d %d]\n",x_fdim,y_fdim,x_idim,y_idim);
  45. mexErrMsgTxt("FILTER dimensions larger than IMAGE dimensions.");
  46. }
  47. /* ARG 3 (optional): EDGES */
  48. if (nrhs>2)
  49. {
  50. if (!mxIsChar(prhs[2]))
  51. mexErrMsgTxt("EDGES arg must be a string.");
  52. mxGetString(prhs[2],edges,15);
  53. }
  54. /* ARG 4 (optional): STEP */
  55. if (nrhs>3)
  56. {
  57. arg3 = prhs[3];
  58. if notDblMtx(arg3) mexErrMsgTxt("STEP arg must be a double float matrix.");
  59. if (mxGetM(arg3) * mxGetN(arg3) != 2)
  60. mexErrMsgTxt("STEP arg must contain two elements.");
  61. mxMat = mxGetPr(arg3);
  62. x_step = (int) mxMat[0];
  63. y_step = (int) mxMat[1];
  64. if ((x_step<1) || (y_step<1))
  65. mexErrMsgTxt("STEP values must be greater than zero.");
  66. }
  67. /* ARG 5 (optional): START */
  68. if (nrhs>4)
  69. {
  70. arg4 = prhs[4];
  71. if notDblMtx(arg4) mexErrMsgTxt("START arg must be a double float matrix.");
  72. if (mxGetM(arg4) * mxGetN(arg4) != 2)
  73. mexErrMsgTxt("START arg must contain two elements.");
  74. mxMat = mxGetPr(arg4);
  75. x_start = (int) mxMat[0];
  76. y_start = (int) mxMat[1];
  77. if ((x_start<1) || (x_start>x_idim) ||
  78. (y_start<1) || (y_start>y_idim))
  79. mexErrMsgTxt("START values must lie between 1 and the image dimensions.");
  80. }
  81. x_start--; /* convert from Matlab to standard C indexes */
  82. y_start--;
  83. /* ARG 6 (optional): STOP */
  84. if (nrhs>5)
  85. {
  86. if notDblMtx(prhs[5]) mexErrMsgTxt("STOP arg must be double float matrix.");
  87. if (mxGetM(prhs[5]) * mxGetN(prhs[5]) != 2)
  88. mexErrMsgTxt("STOP arg must contain two elements.");
  89. mxMat = mxGetPr(prhs[5]);
  90. x_stop = (int) mxMat[0];
  91. y_stop = (int) mxMat[1];
  92. if ((x_stop<x_start) || (x_stop>x_idim) ||
  93. (y_stop<y_start) || (y_stop>y_idim))
  94. mexErrMsgTxt("STOP values must lie between START and the image dimensions.");
  95. }
  96. else
  97. {
  98. x_stop = x_idim;
  99. y_stop = y_idim;
  100. }
  101. x_rdim = (x_stop-x_start+x_step-1) / x_step;
  102. y_rdim = (y_stop-y_start+y_step-1) / y_step;
  103. /* mxFreeMatrix(plhs[0]); */
  104. plhs[0] = (mxArray *) mxCreateDoubleMatrix(x_rdim,y_rdim,mxREAL);
  105. if (plhs[0] == NULL) mexErrMsgTxt("Cannot allocate result matrix");
  106. result = mxGetPr(plhs[0]);
  107. temp = mxCalloc(x_fdim*y_fdim, sizeof(double));
  108. if (temp == NULL)
  109. mexErrMsgTxt("Cannot allocate necessary temporary space");
  110. /*
  111. printf("i(%d, %d), f(%d, %d), r(%d, %d), X(%d, %d, %d), Y(%d, %d, %d), %s\n",
  112. x_idim,y_idim,x_fdim,y_fdim,x_rdim,y_rdim,
  113. x_start,x_step,x_stop,y_start,y_step,y_stop,edges);
  114. */
  115. if (strcmp(edges,"circular") == 0)
  116. internal_wrap_reduce(image, x_idim, y_idim, filt, x_fdim, y_fdim,
  117. x_start, x_step, x_stop, y_start, y_step, y_stop,
  118. result);
  119. else internal_reduce(image, x_idim, y_idim, filt, temp, x_fdim, y_fdim,
  120. x_start, x_step, x_stop, y_start, y_step, y_stop,
  121. result, edges);
  122. mxFree((char *) temp);
  123. return;
  124. }