upConv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. RES = upConv(IM, FILT, EDGES, STEP, START, STOP, RES);
  3. >>> See upConv.m for documentation <<<
  4. This is a matlab interface to the internal_expand 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, *orig_filt;
  19. int x_fdim, y_fdim, x_idim, y_idim;
  20. int orig_x = 0, orig_y, x, y;
  21. int x_rdim, y_rdim;
  22. int x_start = 1;
  23. int x_step = 1;
  24. int y_start = 1;
  25. int y_step = 1;
  26. int x_stop, y_stop;
  27. const mxArray *arg0,*arg1,*arg3,*arg4,*arg6;
  28. double *mxMat;
  29. char edges[15] = "reflect1";
  30. if (nrhs<2) mexErrMsgTxt("requres at least 2 args.");
  31. /* ARG 1: IMAGE */
  32. arg0 = prhs[0];
  33. if notDblMtx(arg0) mexErrMsgTxt("IMAGE arg must be a non-sparse double float matrix.");
  34. image = mxGetPr(arg0);
  35. x_idim = (int) mxGetM(arg0); /* X is inner index! */
  36. y_idim = (int) mxGetN(arg0);
  37. /* ARG 2: FILTER */
  38. arg1 = prhs[1];
  39. if notDblMtx(arg1) mexErrMsgTxt("FILTER arg must be non-sparse double float matrix."); filt = mxGetPr(arg1);
  40. x_fdim = (int) mxGetM(arg1);
  41. y_fdim = (int) mxGetN(arg1);
  42. /* ARG 3 (optional): EDGES */
  43. if (nrhs>2)
  44. {
  45. if (!mxIsChar(prhs[2]))
  46. mexErrMsgTxt("EDGES arg must be a string.");
  47. mxGetString(prhs[2],edges,15);
  48. }
  49. /* ARG 4 (optional): STEP */
  50. if (nrhs>3)
  51. {
  52. arg3 = prhs[3];
  53. if notDblMtx(arg3) mexErrMsgTxt("STEP arg must be double float matrix.");
  54. if (mxGetM(arg3) * mxGetN(arg3) != 2)
  55. mexErrMsgTxt("STEP arg must contain two elements.");
  56. mxMat = mxGetPr(arg3);
  57. x_step = (int) mxMat[0];
  58. y_step = (int) mxMat[1];
  59. if ((x_step<1) || (y_step<1))
  60. mexErrMsgTxt("STEP values must be greater than zero.");
  61. }
  62. /* ARG 5 (optional): START */
  63. if (nrhs>4)
  64. {
  65. arg4 = prhs[4];
  66. if notDblMtx(arg4) mexErrMsgTxt("START arg must be double float matrix.");
  67. if (mxGetM(arg4) * mxGetN(arg4) != 2)
  68. mexErrMsgTxt("START arg must contain two elements.");
  69. mxMat = mxGetPr(arg4);
  70. x_start = (int) mxMat[0];
  71. y_start = (int) mxMat[1];
  72. if ((x_start<1) || (y_start<1))
  73. mexErrMsgTxt("START values must be greater than zero.");
  74. }
  75. x_start--; /* convert to standard C indexes */
  76. y_start--;
  77. /* ARG 6 (optional): STOP */
  78. if (nrhs>5)
  79. {
  80. if notDblMtx(prhs[5]) mexErrMsgTxt("STOP arg must be double float matrix.");
  81. if (mxGetM(prhs[5]) * mxGetN(prhs[5]) != 2)
  82. mexErrMsgTxt("STOP arg must contain two elements.");
  83. mxMat = mxGetPr(prhs[5]);
  84. x_stop = (int) mxMat[0];
  85. y_stop = (int) mxMat[1];
  86. if ((x_stop<x_start) || (y_stop<y_start))
  87. mexErrMsgTxt("STOP values must be greater than START values.");
  88. }
  89. else
  90. { /* default: make res dims a multiple of STEP size */
  91. x_stop = x_step * ((x_start/x_step) + x_idim);
  92. y_stop = y_step * ((y_start/y_step) + y_idim);
  93. }
  94. /* ARG 6 (optional): RESULT image */
  95. if (nrhs>6)
  96. {
  97. arg6 = prhs[6];
  98. if notDblMtx(arg6) mexErrMsgTxt("RES arg must be double float matrix.");
  99. /* 7/10/97: Returning one of the args causes problems with Matlab's memory
  100. manager, so we don't return anything if the result image is passed */
  101. /* plhs[0] = arg; */
  102. result = mxGetPr(arg6);
  103. x_rdim = (int) mxGetM(arg6); /* X is inner index! */
  104. y_rdim = (int) mxGetN(arg6);
  105. if ((x_stop>x_rdim) || (y_stop>y_rdim))
  106. mexErrMsgTxt("STOP values must within image dimensions.");
  107. }
  108. else
  109. {
  110. x_rdim = x_stop;
  111. y_rdim = y_stop;
  112. /* x_rdim = x_step * ((x_stop+x_step-1)/x_step);
  113. y_rdim = y_step * ((y_stop+y_step-1)/y_step); */
  114. plhs[0] = (mxArray *) mxCreateDoubleMatrix(x_rdim,y_rdim,mxREAL);
  115. if (plhs[0] == NULL) mexErrMsgTxt("Cannot allocate result matrix");
  116. result = mxGetPr(plhs[0]);
  117. }
  118. if ( (((x_stop-x_start+x_step-1) / x_step) != x_idim) ||
  119. (((y_stop-y_start+y_step-1) / y_step) != y_idim) )
  120. {
  121. mexPrintf("Im dims: [%d %d]\n",x_idim,y_idim);
  122. mexPrintf("Start: [%d %d]\n",x_start,y_start);
  123. mexPrintf("Step: [%d %d]\n",x_step,y_step);
  124. mexPrintf("Stop: [%d %d]\n",x_stop,y_stop);
  125. mexPrintf("Res dims: [%d %d]\n",x_rdim,y_rdim);
  126. mexErrMsgTxt("Image sizes and upsampling args are incompatible!");
  127. }
  128. /* upConv has a bug for even-length kernels when using the
  129. reflect1, extend, or repeat edge-handlers */
  130. if ((!strcmp(edges,"reflect1") || !strcmp(edges,"extend") || !strcmp(edges,"repeat"))
  131. &&
  132. ((x_fdim%2 == 0) || (y_fdim%2 == 0)))
  133. {
  134. orig_filt = filt;
  135. orig_x = x_fdim;
  136. orig_y = y_fdim;
  137. x_fdim = 2*(orig_x/2)+1;
  138. y_fdim = 2*(orig_y/2)+1;
  139. filt = mxCalloc(x_fdim*y_fdim, sizeof(double));
  140. if (filt == NULL)
  141. mexErrMsgTxt("Cannot allocate necessary temporary space");
  142. for (y=0; y<orig_y; y++)
  143. for (x=0; x<orig_x; x++)
  144. filt[y*x_fdim + x] = orig_filt[y*orig_x + x];
  145. }
  146. if ((x_fdim > x_rdim) || (y_fdim > y_rdim))
  147. {
  148. mexPrintf("Filter: [%d %d], ",x_fdim,y_fdim);
  149. mexPrintf("Result: [%d %d]\n",x_rdim,y_rdim);
  150. mexErrMsgTxt("FILTER dimensions larger than RESULT dimensions.");
  151. }
  152. temp = mxCalloc(x_fdim*y_fdim, sizeof(double));
  153. if (temp == NULL)
  154. mexErrMsgTxt("Cannot allocate necessary temporary space");
  155. /*
  156. printf("(%d, %d), (%d, %d), (%d, %d), (%d, %d), (%d, %d), %s\n",
  157. x_idim,y_idim,x_fdim,y_fdim,x_rdim,y_rdim,
  158. x_start,x_step,y_start,y_step,edges);
  159. */
  160. if (strcmp(edges,"circular") == 0)
  161. internal_wrap_expand(image, filt, x_fdim, y_fdim,
  162. x_start, x_step, x_stop, y_start, y_step, y_stop,
  163. result, x_rdim, y_rdim);
  164. else internal_expand(image, filt, temp, x_fdim, y_fdim,
  165. x_start, x_step, x_stop, y_start, y_step, y_stop,
  166. result, x_rdim, y_rdim, edges);
  167. if (orig_x) mxFree((char *) filt);
  168. mxFree((char *) temp);
  169. return;
  170. }