-
Notifications
You must be signed in to change notification settings - Fork 10
/
niftiio.c
268 lines (224 loc) · 6.97 KB
/
niftiio.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/* niftiio.c
*
* Copyright 2011 Simon Fristed Eskildsen, Vladimir Fonov,
* Pierrick Coupé, Jose V. Manjon
*
* This file is part of mincbeast.
*
* mincbeast is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mincbeast is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mincbeast. If not, see <http://www.gnu.org/licenses/>.
*
* For questions and feedback, please contact:
* Simon Fristed Eskildsen <[email protected]>
*/
#include <nifti1_io.h>
#include "niftiio.h"
//#define DEBUG
image_metadata * read_nifti(char *filename, float **image, int *sizes){
nifti_image *nim;
float *data;
int x,y,z,index;
image_metadata *meta;
meta = (image_metadata *)calloc( 1 , sizeof(image_metadata) ) ;
meta->start = calloc(3,sizeof(float));
meta->step = calloc(3,sizeof(float));
meta->length = calloc(3,sizeof(int));
nim = nifti_image_read( filename , 1 ) ;
if( nim == NULL ) return NULL;
#ifdef DEBUG
fprintf(stderr,"%s\n",nifti_image_to_ascii(nim));
#endif
data = (float *)nim->data;
*image = malloc(nim->nvox*sizeof(**image));
for (z=0;z<nim->nz;z++)
for (y=0;y<nim->ny;y++)
for (x=0;x<nim->nx;x++){
index = z*(nim->ny)*(nim->nx) + y*(nim->nx) + x;
(*image)[index]=data[index]*nim->scl_slope + nim->scl_inter;
}
meta->length[0]=sizes[0] = nim->nz;
meta->length[1]=sizes[1] = nim->ny;
meta->length[2]=sizes[2] = nim->nx;
meta->step[0]=nim->dz;
meta->step[1]=nim->dy;
meta->step[2]=nim->dx;
if (nim->sform_code){
meta->start[0]=nim->sto_xyz.m[2][3];
meta->start[1]=nim->sto_xyz.m[1][3];
meta->start[2]=nim->sto_xyz.m[0][3];
} else {
meta->start[0]=0;
meta->start[1]=0;
meta->start[2]=0;
}
nifti_image_free(nim) ;
return meta;
}
int write_nifti_generic(char *filename, float *image, image_metadata *meta)
{
nifti_image *nim;
int ll,ntot;
//float *data;
#ifdef DEBUG
fprintf(stderr,"nifti WRITE: Dimension sizes: %d, %d, %d\n",meta->length[0],meta->length[1],meta->length[2]);
fprintf(stderr,"nifti WRITE: Start coordinates: %f, %f, %f\n",meta->start[0],meta->start[1],meta->start[2]);
fprintf(stderr,"nifti WRITE: Step values: %f, %f, %f\n",meta->step[0],meta->step[1],meta->step[2]);
#endif
nim = nifti_simple_init_nim();
nim->ndim=nim->dim[0]=3;
nim->nx=nim->dim[1]=meta->length[2];
nim->ny=nim->dim[2]=meta->length[1];
nim->nz=nim->dim[3]=meta->length[0];
nim->dx=meta->step[2];
nim->dy=meta->step[1];
nim->dz=meta->step[0];
nim->sform_code = 1;
nim->sto_xyz.m[0][0] = 1;
nim->sto_xyz.m[0][1] = 0;
nim->sto_xyz.m[0][2] = 0;
nim->sto_xyz.m[0][3] = meta->start[2];
nim->sto_xyz.m[1][0] = 0;
nim->sto_xyz.m[1][1] = 1;
nim->sto_xyz.m[1][2] = 0;
nim->sto_xyz.m[1][3] = meta->start[1];
nim->sto_xyz.m[2][0] = 0;
nim->sto_xyz.m[2][1] = 0;
nim->sto_xyz.m[2][2] = 1;
nim->sto_xyz.m[2][3] = meta->start[0];
nim->sto_xyz.m[3][0] = 0;
nim->sto_xyz.m[3][1] = 0;
nim->sto_xyz.m[3][2] = 0;
nim->sto_xyz.m[3][3] = 1;
nim->sto_ijk.m[0][0] = 1;
nim->sto_ijk.m[0][1] = 0;
nim->sto_ijk.m[0][2] = 0;
nim->sto_ijk.m[0][3] = -meta->start[2];
nim->sto_ijk.m[1][0] = 0;
nim->sto_ijk.m[1][1] = 1;
nim->sto_ijk.m[1][2] = 0;
nim->sto_ijk.m[1][3] = -meta->start[1];
nim->sto_ijk.m[2][0] = 0;
nim->sto_ijk.m[2][1] = 0;
nim->sto_ijk.m[2][2] = 1;
nim->sto_ijk.m[2][3] = -meta->start[0];
nim->sto_ijk.m[3][0] = 0;
nim->sto_ijk.m[3][1] = 0;
nim->sto_ijk.m[3][2] = 0;
nim->sto_ijk.m[3][3] = 1;
nim->nvox=nim->nx*nim->ny*nim->nz;
nim->nbyper=4;
nim->datatype=16;
nim->scl_slope=1;
nim->scl_inter=0;
ntot = nifti_get_volsize(nim);
nim->data = (void *)calloc(1,ntot); /* create image memory */
bcopy(image, nim->data, ntot); /* copy data */
ll = strlen(filename) ;
nim->fname = (char *)calloc(1,ll+1) ; strcpy(nim->fname,filename) ;
nim->iname = (char *)calloc(1,ll+1) ; strcpy(nim->iname,filename) ;
#ifdef DEBUG
fprintf(stderr,"%s\n",nifti_image_to_ascii(nim));
#endif
nifti_image_write(nim) ;
nifti_image_free(nim) ;
return 0;
}
int write_nifti(char *filename, nifti_image *nim, float *image)
{
int ll, outmode=-1, usegzip=0;
char *tmpstr;
char fname[255];
int x,y,z,index;
float *data;
data = (float *)nim->data;
for (z=0;z<nim->nz;z++)
for (y=0;y<nim->ny;y++)
for (x=0;x<nim->nx;x++){
index = z*(nim->ny)*(nim->nx) + y*(nim->nx) + x;
data[index]=(image[index] - nim->scl_inter)/nim->scl_slope;
}
/* copy the filename */
sprintf(fname,"%s",filename);
/* check if the file must be zipped */
if (!strcmp("gz",filename + strlen(filename)-2)){
/* strip the .gz off the filename */
fname[strlen(filename)-3] = 0;
usegzip = 1;
}
/* check the format */
if (!strcmp("hdr",fname + strlen(fname)-3)){
outmode=0;
}
if (!strcmp("img",fname + strlen(fname)-3)){
outmode=0;
}
if (!strcmp("nii",fname + strlen(fname)-3)){
outmode=1;
}
if (!strcmp("nia",fname + strlen(fname)-3)){
outmode=3;
}
if (outmode==-1){
fprintf(stderr,"Error: Unknown format!\n");
return 1;
}
fname[strlen(fname)-4] = 0;
nim->nifti_type = outmode ;
if( nim->fname != NULL ) free(nim->fname) ;
if( nim->iname != NULL ) free(nim->iname) ;
ll = strlen(fname) ;
tmpstr = nifti_makebasename(fname);
nim->fname = (char *)calloc(1,ll+8) ; strcpy(nim->fname,tmpstr) ;
nim->iname = (char *)calloc(1,ll+8) ; strcpy(nim->iname,tmpstr) ;
free(tmpstr);
if( nim->nifti_type == 1 ){
strcat(nim->fname,".nii") ;
strcat(nim->iname,".nii") ;
} else if ( nim->nifti_type == 3 ){
strcat(nim->fname,".nia") ;
strcat(nim->iname,".nia") ;
} else {
strcat(nim->fname,".hdr") ;
strcat(nim->iname,".img") ;
}
if (usegzip) {
strcat(nim->fname,".gz");
strcat(nim->iname,".gz");
}
nifti_image_write( nim ) ;
nifti_image_free( nim ) ;
return 0;
}
char * get_datatype(int datatype){
switch (datatype) {
case 0: return "UNKNOWN"; break;
case 1: return "BINARY"; break;
case 2: return "UINT8"; break;
case 4: return "INT16"; break;
case 8: return "INT32"; break;
case 16: return "FLOAT32"; break;
case 32: return "COMPLEX64"; break;
case 64: return "FLOAT64"; break;
case 128: return "RGB24"; break;
case 256: return "INT8"; break;
case 512: return "UINT16"; break;
case 768: return "UINT32"; break;
case 1024: return "INT64"; break;
case 1280: return "UINT64"; break;
case 1536: return "FLOAT128"; break;
case 1792: return "COMPLEX128"; break;
case 2048: return "COMPLEX256"; break;
case 2304: return "RGBA32"; break;
}
return "REALLY UNKNOWN";
}