Debian Codegraph @

conversion/ana2mnc/steve_smith_ana2mnc/minc2analyze.c

raw
1 /* {{{ Copyright etc. */
2
3 /**********************************************\
4
5 minc2analyze 0.4 by Stephen Smith
6
7 Copyright 1997-1998 Stephen Smith
8
9 Some elements are Copyright 1986-1995
10 Biomedical Imaging Resource Mayo Foundation
11
12 Oxford Centre for Functional Magnetic Resonance Imaging of the Brain,
13 Department of Clinical Neurology, Oxford University, Oxford, UK
14 Email: [email protected]
15 WWW: http://www.fmrib.ox.ac.uk/~steve
16
17 This program should be considered a beta test version and must not
18 be used for any clinical purposes.
19
20 This code is written using an emacs folding mode, making moving
21 around the different sections very easy. This is why there are
22 various marks within comments and why comments are indented.
23
24 Requires:
25 minctoraw (setup the macro MINCTORAW below)
26 mincinfo (setup the macro MINCINFO below)
27
28 Compile with:
29 cc -o minc2analyze minc2analyze.c
30
31 History:
32 0.4 January 20 1999 fixed L-R error
33 0.3 September 28 1998 altered reversing and added reordering
34 0.2 July 17 1998 added 4D functionality and more data types
35 0.1 June 1 1998
36
37 \**********************************************/
38
39 /* }}} */
40
41 #define MINCTORAW "/usr/local/mni/bin/minctoraw"
42 #define MINCINFO "/usr/local/mni/bin/mincinfo"
43
44 /* {{{ includes and defines */
45
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <math.h>
50 #include <sys/file.h>
51 #include <malloc.h>
52 #include "dbh.h"
53
54 typedef unsigned char uchar;
55
56 #define FTOI(a) ( (a) < 0 ? ((int)((a)-0.5)) : ((int)((a)+0.5)) )
57
58 /* }}} */
59 /* {{{ num_test */
60
61 float num_test(sinput,fdefault)
62 char *sinput;
63 float fdefault;
64 {
65 float finput=atof(sinput);
66
67 if ( (finite(finput)==1) && (strcmp(sinput,"unknown")!=0) )
68 {
69 /*printf("string=%s atof=%f default=%f return=%f\n",sinput,finput,fdefault,finput);*/
70 return(finput);
71 }
72 else
73 {
74 /*printf("string=%s atof=%f default=%f return=%f\n",sinput,finput,fdefault,fdefault);*/
75 return(fdefault);
76 }
77 }
78
79 /* }}} */
80 /* {{{ usage */
81
82 void usage()
83 {
84 printf("Usage: minc2analyze <minc_input_root> [-z]\n");
85 printf("E.g.: if input file is image.mnc, use \"minc2analyze image\"\n");
86 printf("By default, co-ordinates and voxel dimensions will be the same after conversion;\nalternatively, -z zeros origin offset and makes all voxel dimensions positive\n");
87
88 exit(1);
89 }
90
91 /* }}} */
92
93 int main(argc,argv)
94 int argc;
95 char *argv[];
96 {
97 /* {{{ vars */
98
99 uchar *buffera, *bufferb;
100 char string_a[1000], string_array_a[100][1000], type_string[1000], *tcp;
101 short short_array_a[1000], x_dim_short, y_dim_short, z_dim_short;
102 int x,y,z, i, ko=1, datatype, volumes=1, bitpix, bytepix, argindex, order[3], glmin, glmax, pos=0;
103 static int DataTypeSizes[32] = {0,1,8,0,16,0,0,0,32,0,0,0,0,0,0,0,32};
104 float x_pix_dim_float, y_pix_dim_float, z_pix_dim_float, x_origin_float, y_origin_float, z_origin_float;
105 struct dsr hdr;
106 FILE *fd, *fd2;
107
108 /* }}} */
109
110 /* {{{ usage */
111
112 if (argc==1)
113 usage();
114
115 argindex=2;
116
117 while (argindex < argc)
118 {
119 tcp = argv[argindex];
120 if (*tcp == '-')
121 switch (*++tcp)
122 {
123 case 'z': /* zero origin offset */
124 ko=0;
125 break;
126 case 'p': /* make voxel dimensions positive */
127 pos=1;
128 break;
129 default:
130 usage();
131 }
132 else
133 usage();
134 argindex++;
135 }
136
137 /* }}} */
138 /* {{{ read file type */
139
140 sprintf(string_a,"%s %s.mnc | tail +2 | head -1 > %s.tmp",MINCINFO,argv[1],argv[1]);
141 system(string_a);
142 sprintf(string_a,"%s.tmp",argv[1]);
143 fd2=fopen(string_a,"r");
144 fscanf(fd2,"%s %s %s",string_a,(char *)&string_array_a[0],(char *)&string_array_a[1]);
145 fclose(fd2);
146
147 datatype=DT_SIGNED_SHORT;
148 sprintf(type_string," ");
149 if (strncmp(string_array_a[1],"byt",3)==0)
150 {
151 if (strncmp(string_array_a[0],"sig",3)==0) { datatype=DT_SIGNED_SHORT; sprintf(type_string,"-short -signed ");}
152 else { datatype=DT_UNSIGNED_CHAR; sprintf(type_string,"-byte ");}
153 }
154 if (strncmp(string_array_a[1],"sho",3)==0)
155 {
156 if (strncmp(string_array_a[0],"sig",3)!=0) { datatype=DT_SIGNED_INT; sprintf(type_string,"-long -signed ");}
157 else { datatype=DT_SIGNED_SHORT; sprintf(type_string,"-short -signed ");}
158 }
159 if (strncmp(string_array_a[1],"lon",3)==0)
160 {
161 if (strncmp(string_array_a[0],"sig",3)==0) { datatype=DT_SIGNED_INT; sprintf(type_string,"-long -signed ");}
162 else { datatype=DT_FLOAT; sprintf(type_string,"float ");}
163 }
164 if (strncmp(string_array_a[1],"flo",3)==0) { datatype=DT_FLOAT; sprintf(type_string,"-float "); }
165
166 printf("datatype=%d\n",datatype);
167 bitpix=DataTypeSizes[datatype];
168 bytepix=bitpix/8;
169
170 /* }}} */
171 /* {{{ read x,y,z,t dimensions and image range etc. */
172
173 sprintf(string_a,"%s %s.mnc | grep image: > %s.tmp",MINCINFO,argv[1],argv[1]);
174 system(string_a);
175 sprintf(string_a,"%s.tmp",argv[1]);
176 fd2=fopen(string_a,"r");
177 fscanf(fd2,"%s %s %s %s %s %s",(char *)&string_array_a[0],(char *)&string_array_a[1],(char *)&string_array_a[2],
178 (char *)&string_array_a[3],(char *)&string_array_a[4],(char *)&string_array_a[5]);
179 if (strcmp("to",string_array_a[4])==0)
180 i=4;
181 else
182 i=3;
183 glmin=atoi(string_array_a[i-1]);
184 glmax=atoi(string_array_a[i+1]);
185 printf("Image range = %d to %d\n",glmin,glmax);
186
187 fscanf(fd2,"%s %s %s %s",string_a,(char *)&string_array_a[0],(char *)&string_array_a[1],(char *)&string_array_a[2]);
188 z_dim_short=num_test(string_array_a[0],(float)256);
189 z_pix_dim_float=num_test(string_array_a[1],(float)1);
190 z_origin_float=-((float)num_test(string_array_a[2],(float)(z_dim_short/2)))/fabs(z_pix_dim_float);
191 fclose(fd2);
192
193 sprintf(string_a,"%s %s.mnc | tail -3 | grep zspace > %s.tmp",MINCINFO,argv[1],argv[1]);
194 system(string_a);
195 sprintf(string_a,"%s.tmp",argv[1]);
196 fd2=fopen(string_a,"r");
197 fscanf(fd2,"%s %s %s %s",string_a,(char *)&string_array_a[0],(char *)&string_array_a[1],(char *)&string_array_a[2]);
198 z_dim_short=num_test(string_array_a[0],(float)256);
199 z_pix_dim_float=num_test(string_array_a[1],(float)1);
200 z_origin_float=-((float)num_test(string_array_a[2],(float)(z_dim_short/2)))/fabs(z_pix_dim_float);
201 fclose(fd2);
202
203 sprintf(string_a,"%s %s.mnc | tail -3 | grep yspace > %s.tmp",MINCINFO,argv[1],argv[1]);
204 system(string_a);
205 sprintf(string_a,"%s.tmp",argv[1]);
206 fd2=fopen(string_a,"r");
207 fscanf(fd2,"%s %s %s %s",string_a,(char *)&string_array_a[0],(char *)&string_array_a[1],(char *)&string_array_a[2]);
208 y_dim_short=num_test(string_array_a[0],(float)256);
209 y_pix_dim_float=num_test(string_array_a[1],(float)1);
210 y_origin_float=-((float)num_test(string_array_a[2],(float)(y_dim_short/2)))/fabs(y_pix_dim_float);
211 fclose(fd2);
212
213 sprintf(string_a,"%s %s.mnc | tail -3 | grep xspace > %s.tmp",MINCINFO,argv[1],argv[1]);
214 system(string_a);
215 sprintf(string_a,"%s.tmp",argv[1]);
216 fd2=fopen(string_a,"r");
217 fscanf(fd2,"%s %s %s %s",string_a,(char *)&string_array_a[0],(char *)&string_array_a[1],(char *)&string_array_a[2]);
218 x_dim_short=num_test(string_array_a[0],(float)256);
219 x_pix_dim_float=num_test(string_array_a[1],(float)1);
220 x_origin_float=-((float)num_test(string_array_a[2],(float)(x_dim_short/2)))/fabs(x_pix_dim_float);
221 fclose(fd2);
222
223 sprintf(string_a,"%s %s.mnc | grep dimensions | grep time > %s.tmp",MINCINFO,argv[1],argv[1]);
224 system(string_a);
225 sprintf(string_a,"%s.tmp",argv[1]);
226 fd2=fopen(string_a,"r");
227 fscanf(fd2,"%s %s %s",string_a,(char *)&string_array_a[0],(char *)&string_array_a[1]);
228 if (strncmp(string_array_a[1],"time",4)==0)
229 {
230 sprintf(string_a,"%s %s.mnc | tail -4 | grep time > %s.tmp",MINCINFO,argv[1],argv[1]);
231 system(string_a);
232 sprintf(string_a,"%s.tmp",argv[1]);
233 fd2=fopen(string_a,"r");
234 fscanf(fd2,"%s %s %s %s",string_a,(char *)&string_array_a[0],(char *)&string_array_a[1],(char *)&string_array_a[2]);
235 volumes=num_test(string_array_a[0],(float)1);
236 fclose(fd2);
237 /*printf("4D file of %d volumes\n",volumes);*/
238 }
239
240 sprintf(string_a,"/bin/rm %s.tmp",argv[1]);
241 system(string_a);
242
243 /* }}} */
244 /* {{{ create raw image */
245
246 sprintf(string_a,"%s -normalize %s %s.mnc > %s.img",MINCTORAW,type_string,argv[1],argv[1]);
247 printf("%s\n",string_a);
248 system(string_a);
249
250 /* }}} */
251 /* {{{ input image */
252
253 buffera = (uchar*)malloc(volumes*x_dim_short*y_dim_short*z_dim_short*bytepix);
254 sprintf(string_a,"%s.img",argv[1]);
255 fd=fopen(string_a,"r");
256 fread(buffera,bytepix,volumes*x_dim_short*y_dim_short*z_dim_short,fd);
257 fclose(fd);
258
259 bufferb = (uchar*)malloc(volumes*x_dim_short*y_dim_short*z_dim_short*bytepix);
260
261 /* }}} */
262 /* {{{ reorder x,y,z ? */
263
264 /* {{{ read x,y,z,t ordering */
265
266 sprintf(string_a,"%s %s.mnc | tail -3 > %s.tmp",MINCINFO,argv[1],argv[1]);
267 system(string_a);
268 sprintf(string_a,"%s.tmp",argv[1]);
269 fd2=fopen(string_a,"r");
270 fscanf(fd2,"%s %s %s %s",(char *)&string_array_a[2],string_a,string_a,string_a);
271 fscanf(fd2,"%s %s %s %s",(char *)&string_array_a[1],string_a,string_a,string_a);
272 fscanf(fd2,"%s %s %s %s",(char *)&string_array_a[0],string_a,string_a,string_a);
273 fclose(fd2);
274
275 for(i=0; i<3; i++)
276 {
277 if (strncmp(string_array_a[i],"z",1)==0)
278 order[i]=2;
279 else
280 {
281 if (strncmp(string_array_a[i],"y",1)==0)
282 order[i]=1;
283 else
284 order[i]=0;
285 }
286 }
287 printf("Order: %s (%d) %s (%d) %s (%d)\n",string_array_a[0],order[0],string_array_a[1],order[1],string_array_a[2],order[2]);
288
289 sprintf(string_a,"/bin/rm %s.tmp",argv[1]);
290 system(string_a);
291
292 /* }}} */
293
294 if ( (order[0]!=0) || (order[1]!=1) || (order[2]!=2) )
295 {
296 int counters[3];
297 short dims[3];
298
299 dims[0]=x_dim_short;
300 dims[1]=y_dim_short;
301 dims[2]=z_dim_short;
302
303 printf("Reordering dimensions.\n");
304
305 for(i=0; i<volumes; i++)
306 for(counters[2]=0; counters[2]<z_dim_short; counters[2]++)
307 for(counters[1]=0; counters[1]<y_dim_short; counters[1]++)
308 for(counters[0]=0; counters[0]<x_dim_short; counters[0]++)
309 memcpy(bufferb+( ( i*z_dim_short*y_dim_short*x_dim_short +
310 counters[2]*y_dim_short*x_dim_short +
311 counters[1]*x_dim_short +
312 counters[0]) * bytepix ),
313 buffera +( ( i*z_dim_short*y_dim_short*x_dim_short +
314 counters[order[2]]*dims[order[1]]*dims[order[0]] +
315 counters[order[1]]*dims[order[0]] +
316 counters[order[0]]) * bytepix ),
317 bytepix);
318
319 memcpy(buffera,bufferb,bytepix*volumes*x_dim_short*y_dim_short*z_dim_short);
320 }
321
322 /* }}} */
323 /* {{{ COMMENT reverse x ? ORIG */
324
325 #ifdef FoldingComment
326
327 if (x_pix_dim_float<0)
328 {
329 printf("Reversing x direction.\n");
330
331 x_origin_float += ((float)x_dim_short);
332 x_pix_dim_float = -x_pix_dim_float;
333
334 for(i=0; i<volumes; i++)
335 for(z=0; z<z_dim_short; z++)
336 for(y=0; y<y_dim_short; y++)
337 for(x=0; x<x_dim_short; x++)
338 memcpy(bufferb+( ( i*z_dim_short*y_dim_short*x_dim_short +
339 z*y_dim_short*x_dim_short +
340 y*x_dim_short +
341 x) * bytepix ),
342 buffera +( ( i*z_dim_short*y_dim_short*x_dim_short +
343 z*y_dim_short*x_dim_short +
344 y*x_dim_short +
345 (x_dim_short-x-1) ) * bytepix ),
346 bytepix);
347
348 memcpy(buffera,bufferb,bytepix*volumes*x_dim_short*y_dim_short*z_dim_short);
349 }
350
351 #endif
352
353 /* }}} */
354 /* {{{ reverse x ? */
355
356 if (x_pix_dim_float>0) /* this is necessary because of the MINC<->MEDx L<->R inconsistency */
357 {
358 printf("Reversing x direction (data).\n");
359
360 for(i=0; i<volumes; i++)
361 for(z=0; z<z_dim_short; z++)
362 for(y=0; y<y_dim_short; y++)
363 for(x=0; x<x_dim_short; x++)
364 memcpy(bufferb+( ( i*z_dim_short*y_dim_short*x_dim_short +
365 z*y_dim_short*x_dim_short +
366 y*x_dim_short +
367 x) * bytepix ),
368 buffera +( ( i*z_dim_short*y_dim_short*x_dim_short +
369 z*y_dim_short*x_dim_short +
370 y*x_dim_short +
371 (x_dim_short-x-1) ) * bytepix ),
372 bytepix);
373
374 memcpy(buffera,bufferb,bytepix*volumes*x_dim_short*y_dim_short*z_dim_short);
375 }
376 else
377 {
378 printf("Reversing x direction (header).\n");
379
380 x_origin_float += ((float)x_dim_short);
381 x_pix_dim_float = -x_pix_dim_float;
382
383 /* this used to have data reversal in here but moved it up as in this case we'd want to reverse twice */
384 }
385
386 if (ko) /* this is necessary because of the MINC<->MEDx L<->R inconsistency */
387 x_pix_dim_float = -x_pix_dim_float;
388
389 /* }}} */
390 /* {{{ reverse y ? */
391
392 if (y_pix_dim_float<0)
393 {
394 printf("Reversing y direction.\n");
395
396 y_origin_float += ((float)y_dim_short);
397 y_pix_dim_float = -y_pix_dim_float;
398
399 for(i=0; i<volumes; i++)
400 for(z=0; z<z_dim_short; z++)
401 for(y=0; y<y_dim_short; y++)
402 memcpy(bufferb+( ( i*z_dim_short*y_dim_short*x_dim_short +
403 z*y_dim_short*x_dim_short +
404 y*x_dim_short ) * bytepix ),
405 buffera +( ( i*z_dim_short*y_dim_short*x_dim_short +
406 z*y_dim_short*x_dim_short +
407 (y_dim_short-1-y)*x_dim_short ) * bytepix ),
408 bytepix*x_dim_short);
409
410 memcpy(buffera,bufferb,bytepix*volumes*x_dim_short*y_dim_short*z_dim_short);
411 }
412
413 /* }}} */
414 /* {{{ reverse z ? */
415
416 if (z_pix_dim_float<0)
417 {
418 printf("Reversing z direction.\n");
419
420 z_origin_float += ((float)z_dim_short);
421 z_pix_dim_float = -z_pix_dim_float;
422
423 for(i=0; i<volumes; i++)
424 for(z=0; z<z_dim_short; z++)
425 memcpy(bufferb+( ( i*z_dim_short*y_dim_short*x_dim_short +
426 z*y_dim_short*x_dim_short ) * bytepix ),
427 buffera +( ( i*z_dim_short*y_dim_short*x_dim_short +
428 (z_dim_short-1-z)*y_dim_short*x_dim_short ) * bytepix ),
429 bytepix*x_dim_short*y_dim_short);
430
431 memcpy(buffera,bufferb,bytepix*volumes*x_dim_short*y_dim_short*z_dim_short);
432 }
433
434 /* }}} */
435 /* {{{ output image */
436
437 sprintf(string_a,"%s.img",argv[1]);
438 fd=fopen(string_a,"w");
439 fwrite(buffera,bytepix,volumes*x_dim_short*y_dim_short*z_dim_short,fd);
440 fclose(fd);
441
442 /* }}} */
443 /* {{{ open output (header) file */
444
445 sprintf(string_a,"%s.hdr",argv[1]);
446
447 if((fd=fopen(string_a,"w"))==0)
448 {
449 printf("unable to create: %s\n",argv[1]);
450 exit(0);
451 }
452
453 /* }}} */
454 /* {{{ create header struc */
455
456 hdr.hk.sizeof_hdr = sizeof(struct dsr);
457
458 sprintf(hdr.hk.data_type,"dsr ");
459 hdr.hk.data_type[9]=0;
460
461 sprintf(string_a,"%s ",argv[1]);
462 string_a[17]=0;
463 strcpy(hdr.hk.db_name,string_a);
464
465 hdr.hk.extents=0;
466
467 hdr.hk.session_error=0;
468
469 hdr.hk.regular = 'r';
470
471 hdr.hk.hkey_un0 = '0';
472
473 hdr.dime.dim[0] = 4; /* all Analyze images are taken as 4 dimensional */
474 hdr.dime.dim[1] = x_dim_short; /* slice width in pixels */
475 hdr.dime.dim[2] = y_dim_short; /* slice height in pixels */
476 hdr.dime.dim[3] = z_dim_short; /* volume depth in slices */
477 hdr.dime.dim[4] = volumes; /* number of volumes per file */
478 hdr.dime.dim[5] = 0;
479 hdr.dime.dim[6] = 0;
480 hdr.dime.dim[7] = 0;
481
482 /* up to 3 characters for the voxels units label; i.e. mm., um., cm. */
483 strcpy(hdr.dime.vox_units,"mm");
484
485 /* up to 7 characters for the calibration units label; i.e. HU */
486 strcpy(hdr.dime.cal_units," ");
487
488 hdr.dime.unused1=0;
489
490 hdr.dime.datatype = datatype;
491
492 hdr.dime.bitpix = bitpix;
493
494 hdr.dime.dim_un0=0;
495
496 /* 1,2,3 = pixel dimensions */
497 hdr.dime.pixdim[0]=0;
498 hdr.dime.pixdim[1]=x_pix_dim_float;
499 hdr.dime.pixdim[2]=y_pix_dim_float;
500 hdr.dime.pixdim[3]=z_pix_dim_float;
501 hdr.dime.pixdim[4]=0;
502 hdr.dime.pixdim[5]=0;
503 hdr.dime.pixdim[6]=0;
504 hdr.dime.pixdim[7]=0;
505
506 hdr.dime.vox_offset = 0.0;
507
508 hdr.dime.funused1 = 1.0; /* scaling */
509
510 hdr.dime.funused2 = 0.0;
511
512 hdr.dime.funused3 = 0.0;
513
514 hdr.dime.cal_max = 0.0;
515
516 hdr.dime.cal_min = 0.0;
517
518 hdr.dime.compressed=0;
519
520 hdr.dime.verified=0;
521
522 hdr.dime.glmax = glmax;
523 hdr.dime.glmin = glmin;
524
525 /* Planar Orientation;
526 Movie flag FALSE: 0 = transverse, 1 = coronal, 2 = sagittal
527 Movie flag TRUE: 3 = transverse, 4 = coronal, 5 = sagittal */
528 hdr.hist.orient = 0;
529
530 /* origin */
531 if (ko==1)
532 {
533 short_array_a[0]=(short)FTOI(x_origin_float);
534 short_array_a[1]=(short)FTOI(y_origin_float);
535 short_array_a[2]=(short)FTOI(z_origin_float);
536 printf("origin = %d %d %d\n",short_array_a[0],short_array_a[1],short_array_a[2]);
537 }
538 else
539 {
540 short_array_a[0]=0;
541 short_array_a[1]=0;
542 short_array_a[2]=0;
543 }
544 short_array_a[3]=0;
545 short_array_a[4]=0;
546 memcpy(hdr.hist.originator,short_array_a,5*sizeof(short));
547
548 /* }}} */
549 /* {{{ output header */
550
551 fwrite(&hdr,sizeof(struct dsr),1,fd);
552 fclose(fd);
553
554 /* }}} */
555 return(0);
556 }