idamnjanovic@70
|
1 /**************************************************************************
|
idamnjanovic@70
|
2 *
|
idamnjanovic@70
|
3 * File name: im2colstep.c
|
idamnjanovic@70
|
4 *
|
idamnjanovic@70
|
5 * Ron Rubinstein
|
idamnjanovic@70
|
6 * Computer Science Department
|
idamnjanovic@70
|
7 * Technion, Haifa 32000 Israel
|
idamnjanovic@70
|
8 * ronrubin@cs
|
idamnjanovic@70
|
9 *
|
idamnjanovic@70
|
10 * Last Updated: 31.8.2009
|
idamnjanovic@70
|
11 *
|
idamnjanovic@70
|
12 *************************************************************************/
|
idamnjanovic@70
|
13
|
idamnjanovic@70
|
14
|
idamnjanovic@70
|
15 #include "mex.h"
|
idamnjanovic@70
|
16 #include <string.h>
|
idamnjanovic@70
|
17
|
idamnjanovic@70
|
18
|
idamnjanovic@70
|
19 /* Input Arguments */
|
idamnjanovic@70
|
20
|
idamnjanovic@70
|
21 #define X_IN prhs[0]
|
idamnjanovic@70
|
22 #define SZ_IN prhs[1]
|
idamnjanovic@70
|
23 #define S_IN prhs[2]
|
idamnjanovic@70
|
24
|
idamnjanovic@70
|
25
|
idamnjanovic@70
|
26 /* Output Arguments */
|
idamnjanovic@70
|
27
|
idamnjanovic@70
|
28 #define B_OUT plhs[0]
|
idamnjanovic@70
|
29
|
idamnjanovic@70
|
30
|
idamnjanovic@70
|
31 void mexFunction(int nlhs, mxArray *plhs[],
|
idamnjanovic@70
|
32 int nrhs, const mxArray*prhs[])
|
idamnjanovic@70
|
33
|
idamnjanovic@70
|
34 {
|
idamnjanovic@70
|
35 double *x, *b, *s;
|
idamnjanovic@70
|
36 mwSize sz[3], stepsize[3], n[3], ndims;
|
idamnjanovic@70
|
37 mwIndex i, j, k, l, m, blocknum;
|
idamnjanovic@70
|
38
|
idamnjanovic@70
|
39
|
idamnjanovic@70
|
40 /* Check for proper number of arguments */
|
idamnjanovic@70
|
41
|
idamnjanovic@70
|
42 if (nrhs < 2 || nrhs > 3) {
|
idamnjanovic@70
|
43 mexErrMsgTxt("Invalid number of input arguments.");
|
idamnjanovic@70
|
44 } else if (nlhs > 1) {
|
idamnjanovic@70
|
45 mexErrMsgTxt("Too many output arguments.");
|
idamnjanovic@70
|
46 }
|
idamnjanovic@70
|
47
|
idamnjanovic@70
|
48
|
idamnjanovic@70
|
49 /* Check the the input dimensions */
|
idamnjanovic@70
|
50
|
idamnjanovic@70
|
51 ndims = mxGetNumberOfDimensions(X_IN);
|
idamnjanovic@70
|
52
|
idamnjanovic@70
|
53 if (!mxIsDouble(X_IN) || mxIsComplex(X_IN) || ndims>3) {
|
idamnjanovic@70
|
54 mexErrMsgTxt("X should be a 2-D or 3-D double matrix.");
|
idamnjanovic@70
|
55 }
|
idamnjanovic@70
|
56 if (!mxIsDouble(SZ_IN) || mxIsComplex(SZ_IN) || mxGetNumberOfDimensions(SZ_IN)>2 || mxGetM(SZ_IN)*mxGetN(SZ_IN)!=ndims) {
|
idamnjanovic@70
|
57 mexErrMsgTxt("Invalid block size.");
|
idamnjanovic@70
|
58 }
|
idamnjanovic@70
|
59 if (nrhs == 3) {
|
idamnjanovic@70
|
60 if (!mxIsDouble(S_IN) || mxIsComplex(S_IN) || mxGetNumberOfDimensions(S_IN)>2 || mxGetM(S_IN)*mxGetN(S_IN)!=ndims) {
|
idamnjanovic@70
|
61 mexErrMsgTxt("Invalid step size.");
|
idamnjanovic@70
|
62 }
|
idamnjanovic@70
|
63 }
|
idamnjanovic@70
|
64
|
idamnjanovic@70
|
65
|
idamnjanovic@70
|
66 /* Get parameters */
|
idamnjanovic@70
|
67
|
idamnjanovic@70
|
68 s = mxGetPr(SZ_IN);
|
idamnjanovic@70
|
69 if (s[0]<1 || s[1]<1 || (ndims==3 && s[2]<1)) {
|
idamnjanovic@70
|
70 mexErrMsgTxt("Invalid block size.");
|
idamnjanovic@70
|
71 }
|
idamnjanovic@70
|
72 sz[0] = (mwSize)(s[0] + 0.01);
|
idamnjanovic@70
|
73 sz[1] = (mwSize)(s[1] + 0.01);
|
idamnjanovic@70
|
74 sz[2] = ndims==3 ? (mwSize)(s[2] + 0.01) : 1;
|
idamnjanovic@70
|
75
|
idamnjanovic@70
|
76 if (nrhs == 3) {
|
idamnjanovic@70
|
77 s = mxGetPr(S_IN);
|
idamnjanovic@70
|
78 if (s[0]<1 || s[1]<1 || (ndims==3 && s[2]<1)) {
|
idamnjanovic@70
|
79 mexErrMsgTxt("Invalid step size.");
|
idamnjanovic@70
|
80 }
|
idamnjanovic@70
|
81 stepsize[0] = (mwSize)(s[0] + 0.01);
|
idamnjanovic@70
|
82 stepsize[1] = (mwSize)(s[1] + 0.01);
|
idamnjanovic@70
|
83 stepsize[2] = ndims==3 ? (mwSize)(s[2] + 0.01) : 1;
|
idamnjanovic@70
|
84 }
|
idamnjanovic@70
|
85 else {
|
idamnjanovic@70
|
86 stepsize[0] = stepsize[1] = stepsize[2] = 1;
|
idamnjanovic@70
|
87 }
|
idamnjanovic@70
|
88
|
idamnjanovic@70
|
89 n[0] = (mxGetDimensions(X_IN))[0];
|
idamnjanovic@70
|
90 n[1] = (mxGetDimensions(X_IN))[1];
|
idamnjanovic@70
|
91 n[2] = ndims==3 ? (mxGetDimensions(X_IN))[2] : 1;
|
idamnjanovic@70
|
92
|
idamnjanovic@70
|
93 if (n[0]<sz[0] || n[1]<sz[1] || (ndims==3 && n[2]<sz[2])) {
|
idamnjanovic@70
|
94 mexErrMsgTxt("Block size too large.");
|
idamnjanovic@70
|
95 }
|
idamnjanovic@70
|
96
|
idamnjanovic@70
|
97
|
idamnjanovic@70
|
98 /* Create a matrix for the return argument */
|
idamnjanovic@70
|
99
|
idamnjanovic@70
|
100 B_OUT = mxCreateDoubleMatrix(sz[0]*sz[1]*sz[2], ((n[0]-sz[0])/stepsize[0]+1)*((n[1]-sz[1])/stepsize[1]+1)*((n[2]-sz[2])/stepsize[2]+1), mxREAL);
|
idamnjanovic@70
|
101
|
idamnjanovic@70
|
102
|
idamnjanovic@70
|
103 /* Assign pointers */
|
idamnjanovic@70
|
104
|
idamnjanovic@70
|
105 x = mxGetPr(X_IN);
|
idamnjanovic@70
|
106 b = mxGetPr(B_OUT);
|
idamnjanovic@70
|
107
|
idamnjanovic@70
|
108
|
idamnjanovic@70
|
109 /* Do the actual computation */
|
idamnjanovic@70
|
110
|
idamnjanovic@70
|
111 blocknum = 0;
|
idamnjanovic@70
|
112
|
idamnjanovic@70
|
113 /* iterate over all blocks */
|
idamnjanovic@70
|
114 for (k=0; k<=n[2]-sz[2]; k+=stepsize[2]) {
|
idamnjanovic@70
|
115 for (j=0; j<=n[1]-sz[1]; j+=stepsize[1]) {
|
idamnjanovic@70
|
116 for (i=0; i<=n[0]-sz[0]; i+=stepsize[0]) {
|
idamnjanovic@70
|
117
|
idamnjanovic@70
|
118 /* copy single block */
|
idamnjanovic@70
|
119 for (m=0; m<sz[2]; m++) {
|
idamnjanovic@70
|
120 for (l=0; l<sz[1]; l++) {
|
idamnjanovic@70
|
121 memcpy(b + blocknum*sz[0]*sz[1]*sz[2] + m*sz[0]*sz[1] + l*sz[0], x+(k+m)*n[0]*n[1]+(j+l)*n[0]+i, sz[0]*sizeof(double));
|
idamnjanovic@70
|
122 }
|
idamnjanovic@70
|
123 }
|
idamnjanovic@70
|
124 blocknum++;
|
idamnjanovic@70
|
125
|
idamnjanovic@70
|
126 }
|
idamnjanovic@70
|
127 }
|
idamnjanovic@70
|
128 }
|
idamnjanovic@70
|
129
|
idamnjanovic@70
|
130 return;
|
idamnjanovic@70
|
131 }
|