-
Notifications
You must be signed in to change notification settings - Fork 1
/
spm_add.c
235 lines (204 loc) · 5 KB
/
spm_add.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef lint
static char sccsid[]="@(#)spm_add.c 2.9 John Ashburner & Jean-Baptiste Poline 99/05/18";
#endif
/*
% add a series of images - a compiled routine
% FORMAT s = spm_add(V,Q,flags)
% V - Vector of mapped volumes (from spm_map or spm_vol).
% Q - Filename for averaged image
% flags - Flags can be:
% 'f' - writes floating point output image.
% 'm' - masks the mean to zero or NaN wherever
% a zero occurs in the input images.
% s - Scalefactor for output image.
%_______________________________________________________________________
%
% spm_add computes a sum of a set of image volumes to produce an
% integral image that is written to a named file (Q).
%
% The image is written as signed short (16 bit) unless the `f' flag
% is specified.
%
% A mean can be effected by scaling the output image via it's
% scalefactor (see spm_mean for an example). A weighted sum can be
% effected by weighting the image scalefactors appropriately.
%
%_______________________________________________________________________
*/
#include <math.h>
#include "spm_sys_deps.h"
#include "spm_mapping.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
MAPTYPE *maps, *get_maps();
double *sptr, *scales, *image, scale;
short **dptr;
int ni, nj, nk, i, j, k;
static double mat[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
int mask0flag = 0, floatflag = 0;
double NaN;
mxArray *wplane_args[3];
int maxval, minval;
int dtype;
NaN = mxGetNaN();
if ((nrhs != 2 && nrhs != 3) || nlhs > 1)
mexErrMsgTxt("Inappropriate usage.");
if (nrhs == 3)
{
if (!mxIsChar(prhs[2]))
mexErrMsgTxt("Inappropriate usage.");
else
{
char *buf;
int buflen;
buflen = mxGetN(prhs[2])*mxGetM(prhs[2])+1;
buf = mxCalloc(buflen,sizeof(char));
if (mxGetString(prhs[2],buf,buflen))
{
mxFree(buf);
mexErrMsgTxt("Cant get flags.");
}
for (i=0; i<buflen; i++)
{
if (buf[i] == 'm') mask0flag = 1;
/* if (buf[i] == 'f') floatflag = 1; */
}
mxFree(buf);
}
}
maps = get_maps(prhs[0], &ni);
for(i=1; i<ni; i++)
{
if ( maps[i].dim[0] != maps[0].dim[0] ||
maps[i].dim[1] != maps[0].dim[1] ||
maps[i].dim[2] != maps[0].dim[2])
{
free_maps(maps, ni);
mexErrMsgTxt("Incompatible image dimensions.");
}
}
dtype = get_dtype(prhs[1]);
if (dtype > 256)
dtype>>=8;
if (dtype == 2)
{
maxval = 255;
minval = 0;
floatflag = 0;
}
else if (dtype == 4)
{
maxval = 32767;
minval = -32768;
floatflag = 0;
}
else if (dtype == 8)
{
maxval = 2147483647;
minval = -2147483647-1;
floatflag = 0;
}
else
{
floatflag = 1;
}
nj = maps[0].dim[2];
nk = maps[0].dim[0]*maps[0].dim[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(maps[0].dim[0],maps[0].dim[1],mxREAL);
wplane_args[2] = mxCreateDoubleMatrix(1,1,mxREAL);
sptr = mxGetPr(wplane_args[1]);
image = (double *)mxCalloc(nk, sizeof(double));
if (!floatflag)
{
scales = (double *)mxCalloc(nj, sizeof(double));
dptr = (short **)mxCalloc(nj, sizeof(double));
}
for(j=0; j<maps[0].dim[2]; j++)
{
double mx, mn;
mat[14] = j+1.0;
for(k=0; k<nk; k++)
sptr[k] = 0.0;
for(i=0; i<ni; i++)
{
/* compiler complains here about 5th arg - was "maps[i]" */
slice(mat, image, maps[i].dim[0],maps[i].dim[1], &maps[i], 0, 0.0);
if (mask0flag &&
(maps[i].dtype == 2 || maps[i].dtype == 4 || maps[i].dtype == 8 ||
maps[i].dtype == 512 || maps[i].dtype == 1024 || maps[i].dtype == 2048))
{
for(k=0; k<nk; k++)
{
if (image[k] != 0)
sptr[k] += image[k];
else
sptr[k] = NaN;
}
}
else
{
for(k=0; k<nk; k++)
sptr[k] += image[k];
}
}
if (floatflag)
{
mxGetPr(wplane_args[2])[0] = j+1.0;
mexCallMATLAB(0, NULL, 3, wplane_args, "spm_write_plane");
}
else
{
/* Determine maximum and minimum */
mx = -9e99;
mn = 9e99;
for(k=0; k<nk; k++)
{
if (!floatflag && !finite(sptr[k])) sptr[k] = 0.0;
if (sptr[k]>mx) mx=sptr[k];
if (sptr[k]<mn) mn=sptr[k];
}
if (mx > -mn)
scales[j] = mx/32767.0;
else
scales[j] = -mn/32768.0;
dptr[j] = (short *)mxCalloc(nk, sizeof(short));
for(k=0; k<nk; k++)
{
dptr[j][k] = (short)rint(sptr[k]/scales[j]);
}
}
}
if (!floatflag)
{
scale = 0.0;
for(j=0; j<nj; j++)
{
if (scales[j] > scale)
scale = scales[j];
}
scale = scale*32767/maxval; /* should really also use minval */
for(j=0; j<nj; j++)
{
for(k=0; k<nk; k++)
sptr[k] = dptr[j][k]*(scales[j]/scale);
mxGetPr(wplane_args[2])[0] = j+1.0;
mexCallMATLAB(0, NULL, 3, wplane_args, "spm_write_plane");
mxFree((char *)(dptr[j]));
}
mxFree((char *)scales);
mxFree((char *)dptr);
}
else
{
scale = 1.0;
}
mxFree((char *)image);
free_maps(maps, ni);
if (nlhs == 1)
{
plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL);
mxGetPr(plhs[0])[0] = scale;
}
}