1 /*****************************************************************************
2 Major portions of this software are copyrighted by the Medical College
3 of Wisconsin, 1994-2000, and are released under the Gnu General Public
4 License, Version 2. See the file README.Copyright for details.
5 ******************************************************************************/
6
7 #include "mrilib.h"
8
main(int argc,char * argv[])9 int main( int argc , char * argv[] )
10 {
11 char prefix[255]="obi-wan-kenobi" , fname[255] ;
12 int datum = -1 ;
13 int iarg = 1 ;
14 int gnim , nx , ny , nz , kz ;
15 char ** gname ;
16 MRI_IMARR * arr ;
17 MRI_IMAGE * im , * qim ;
18 FILE * fp ;
19
20 /***** help? *****/
21
22 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
23 printf(
24 "Usage: imstack [options] image_filenames ...\n"
25 "Stacks up a set of 2D images into one big file (a la MGH).\n"
26 "Options:\n"
27 " -datum type Converts the output data file to be 'type',\n"
28 " which is either 'short' or 'float'.\n"
29 " The default type is the type of the first image.\n"
30 " -prefix name Names the output files to be 'name'.b'type' and 'name'.hdr.\n"
31 " The default name is 'obi-wan-kenobi'.\n"
32 ) ;
33
34 exit(0) ;
35 }
36
37 machdep() ;
38
39 /***** scan for option *****/
40
41 while( iarg < argc && argv[iarg][0] == '-' ){
42
43 /*** -datum ***/
44
45 if( strcmp(argv[iarg],"-datum") == 0 ){
46 if( ++iarg >= argc ){
47 fprintf(stderr,"-datum needs a type!\n") ; exit(1) ;
48 }
49 if( strcmp(argv[iarg],"short") == 0 ){
50 datum = MRI_short ;
51 } else if( strcmp(argv[iarg],"float") == 0 ){
52 datum = MRI_float ;
53 } else {
54 fprintf(stderr,"-datum %s is illegal!\n",argv[iarg]) ; exit(1) ;
55 }
56 iarg++ ; continue ;
57 }
58
59 /*** -prefix ***/
60
61 if( strcmp(argv[iarg],"-prefix") == 0 ){
62 if( ++iarg >= argc ){
63 fprintf(stderr,"-prefix needs a name!\n") ; exit(1) ;
64 }
65 strcpy(prefix,argv[iarg]) ;
66 iarg++ ; continue ;
67 }
68
69 /*** ERROR ***/
70
71 fprintf(stderr,"Unrecognized option: %s\n",argv[iarg]) ; exit(1) ;
72 }
73
74 /***** Check if any filenames left *****/
75
76 if( iarg >= argc ){
77 fprintf(stderr,"No input image filenames?!\n") ; exit(1) ;
78 }
79
80 /***** Perform filename expansion on the input list *****/
81
82 MCW_warn_expand(1) ;
83 MCW_file_expand( argc - iarg , argv + iarg , &gnim , &gname ) ;
84 MCW_warn_expand(0) ;
85
86 if( gnim < 1 ){
87 fprintf(stderr,"Filename expansion fails on input filenames?!\n") ; exit(1) ;
88 }
89
90 /***** Read all files *****/
91
92 arr = mri_read_many_files( gnim , gname ) ;
93 if( arr == NULL || IMARR_COUNT(arr) <= 0 ){
94 fprintf(stderr,"Can't read input files?!\n") ; exit(1) ;
95 }
96 MCW_free_expand( gnim , gname ) ;
97 fprintf(stderr,"Read in %d 2D slices\n",IMARR_COUNT(arr)) ;
98
99 /***** Set output datum, if not already fixed *****/
100
101 if( datum < 0 ){
102 datum = IMARR_SUBIMAGE(arr,0)->kind ;
103
104 if( datum != MRI_short && datum != MRI_float ){
105 fprintf(stderr,"Input image type is %s -- you must use -datum!\n",
106 MRI_TYPE_name[datum]) ;
107 exit(1) ;
108 }
109 }
110
111 /***** Check images for equal sizes *****/
112
113 nx = IMARR_SUBIMAGE(arr,0)->nx ;
114 ny = IMARR_SUBIMAGE(arr,0)->ny ;
115 nz = IMARR_COUNT(arr) ;
116
117 for( kz=1 ; kz < nz ; kz++ ){
118 if( IMARR_SUBIMAGE(arr,kz)->nx != nx ||
119 IMARR_SUBIMAGE(arr,kz)->ny != ny ){
120
121 fprintf(stderr,"All images must be the same size (%d x %d)\n",nx,ny) ;
122 exit(1) ;
123 }
124 }
125
126 /***** Write the output brick *****/
127
128 sprintf(fname,"%s.b%s",prefix,MRI_TYPE_name[datum]) ;
129 fp = fopen( fname , "w" ) ;
130 if( fp == NULL ){
131 fprintf(stderr,"Can't open output file %s\n",fname) ; exit(1) ;
132 }
133
134 for( kz=0 ; kz < nz ; kz++ ){
135
136 im = IMARR_SUBIMAGE(arr,kz) ;
137 if( im->kind != datum ) qim = mri_to_mri( datum , im ) ;
138 else qim = im ;
139
140 fwrite( mri_data_pointer(qim) , qim->pixel_size , qim->nx * qim->ny , fp ) ;
141
142 if( qim != im ) mri_free(qim) ;
143 mri_free(im);
144 }
145 fclose( fp ) ;
146 fprintf(stderr,"Wrote output brick %s\n",fname) ;
147
148 /***** Write the output header *****/
149
150 sprintf(fname,"%s.hdr",prefix) ;
151 fp = fopen( fname , "w" ) ;
152 if( fp == NULL ){
153 fprintf(stderr,"Can't open output file %s\n",fname) ; exit(1) ;
154 }
155
156 fprintf( fp , "%d %d %d 0\n" , nx,ny,nz ) ;
157 fclose(fp) ;
158 fprintf(stderr,"Wrote output header %s: %d %d %d\n",fname,nx,ny,nz) ;
159
160 exit(0) ;
161 }
162