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 <string.h>
8 #include "mrilib.h"
9
10 /*** global data ***/
11
12 #define MAX_NAME 64
13
14 static float FT_tval = 32000.0 ;
15 static float FT_bval = 0.0 ;
16 static float FT_sval = 0.0 ;
17 static int FT_start = 1 ;
18 static int FT_step = 1 ;
19 static int FT_nsize = 0 ;
20
21 static char FT_pname[MAX_NAME] = "sh." ;
22 static char FT_sname[MAX_NAME] = "\0" ;
23
24 static MRI_IMARR * FT_imts = NULL ;
25
26 /*** prototypes ***/
27
28 void FTOSH_syntax( char * ) ;
29 void FTOSH_getopts( int , char * argv[] ) ;
30
31 /*** actual program code ***/
32
main(int argc,char * argv[])33 int main( int argc , char *argv[] )
34 {
35 int ii , nx , ny , npix , kk ;
36 MRI_IMAGE * tim , * sim ;
37 float * tar ;
38 short * sar ;
39 char name[256] ;
40
41 /*----- read inputs -----*/
42
43 printf(
44 "ftosh: convert float images to shorts, by RW Cox\n") ;
45
46 machdep() ;
47
48 if( argc < 2 || strcmp(argv[1],"-help") == 0 ) FTOSH_syntax(NULL) ;
49
50 FTOSH_getopts( argc , argv ) ;
51
52 /*----- convert and write each image -----*/
53
54 for( kk=0 ; kk < FT_imts->num ; kk++ ){
55 tim = FT_imts->imarr[kk] ; tar = mri_data_pointer( tim ) ;
56 nx = tim->nx ; ny = tim->ny ; npix = nx*ny ;
57 sim = mri_new( nx , ny , MRI_short ) ; sar = mri_data_pointer( sim ) ;
58
59 for( ii=0 ; ii < npix ; ii++ )
60 sar[ii] = (short) ( FT_sval * ( tar[ii] - FT_bval ) + 0.499 ) ;
61
62 mri_free( tim ) ;
63 if( FT_nsize ){
64 tim = mri_nsize( sim ) ; mri_free( sim ) ; sim = tim ;
65 }
66
67 if( strlen(FT_pname) > 0 && strlen(FT_sname) > 0 ){
68 sprintf( name , "%s%04d.%s" ,
69 FT_pname , FT_start + kk*FT_step , FT_sname ) ;
70 } else if( strlen(FT_pname) > 0 ){
71 sprintf( name , "%s%04d" , FT_pname , FT_start + kk*FT_step ) ;
72 } else if( strlen(FT_sname) > 0 ){
73 sprintf( name , "%04d.%s" , FT_start + kk*FT_step , FT_sname ) ;
74 } else {
75 sprintf( name , "%04d" , FT_start + kk*FT_step ) ;
76 }
77
78 printf("-- writing %dx%d image %s\n",nx,ny,name) ;
79 mri_write( name , sim ) ;
80 mri_free( sim ) ;
81 }
82
83 exit(0) ;
84 }
85
86 /*----------------------------------------------------------------------*/
87
FTOSH_getopts(int argc,char * argv[])88 void FTOSH_getopts( int argc , char *argv[] )
89 {
90 int nopt = 1 , kk , nx,ny , ii,npix ;
91 MRI_IMAGE * tim ;
92 float * tar ;
93 float bmax , bdif ;
94
95 /*---- scan options that start with a - -----*/
96
97 while( nopt < argc && argv[nopt][0] == '-' ){
98
99 /** -prefix pname **/
100
101 if( strncmp(argv[nopt],"-prefix",5) == 0 ){
102 if( ++nopt >= argc ) FTOSH_syntax("-prefix needs a name!") ;
103 strcpy( FT_pname , argv[nopt] ) ;
104 kk = strlen(FT_pname) ;
105 if( kk > 0 && FT_pname[kk-1] != '.' ){
106 FT_pname[kk] = '.' ;
107 FT_pname[kk+1] = '\0' ;
108 }
109 nopt++ ; continue ; /* skip to next arg */
110 }
111
112 /** -suffix pname **/
113
114 if( strncmp(argv[nopt],"-suffix",5) == 0 ){
115 if( ++nopt >= argc ) FTOSH_syntax("-suffix needs a name!") ;
116 strcpy( FT_sname , argv[nopt] ) ;
117 nopt++ ; continue ; /* skip to next arg */
118 }
119
120 /** -scale sval **/
121
122 if( strncmp(argv[nopt],"-scale",5) == 0 ){
123 char * ch ;
124 if( ++nopt >= argc ) FTOSH_syntax("-scale needs a value!");
125 FT_sval = strtod( argv[nopt] , &ch ) ;
126 if( *ch != '\0' || FT_sval == 0.0 )
127 FTOSH_syntax("value after -scale is illegal!") ;
128 nopt++ ; continue ; /* skip to next arg */
129 }
130
131 /** -base bval **/
132
133 if( strncmp(argv[nopt],"-base",5) == 0 ){
134 char * ch ;
135 if( ++nopt >= argc ) FTOSH_syntax("-base needs a value!");
136 FT_bval = strtod( argv[nopt] , &ch ) ;
137 if( *ch != '\0' ) FTOSH_syntax("value after -base is illegal!") ;
138 nopt++ ; continue ; /* skip to next arg */
139 }
140
141 /** -top tval **/
142
143 if( strncmp(argv[nopt],"-top",4) == 0 ){
144 char * ch ;
145 if( ++nopt >= argc ) FTOSH_syntax("-top needs a value!");
146 FT_tval = strtod( argv[nopt] , &ch ) ;
147 if( *ch != '\0' || FT_tval == 0.0 )
148 FTOSH_syntax("value after -top is illegal!") ;
149 nopt++ ; continue ; /* skip to next arg */
150 }
151
152 /** -start si **/
153
154 if( strncmp(argv[nopt],"-start",5) == 0 ){
155 char * ch ;
156 if( ++nopt >= argc ) FTOSH_syntax("-start needs a value!");
157 FT_start = strtol( argv[nopt] , &ch , 10 ) ;
158 if( *ch != '\0' || FT_start < 0 )
159 FTOSH_syntax("value after -start is illegal!") ;
160 nopt++ ; continue ; /* skip to next arg */
161 }
162
163 /** -step ss **/
164
165 if( strncmp(argv[nopt],"-step",5) == 0 ){
166 char * ch ;
167 if( ++nopt >= argc ) FTOSH_syntax("-step needs a value!");
168 FT_step = strtol( argv[nopt] , &ch , 10 ) ;
169 if( *ch != '\0' || FT_step < 0 )
170 FTOSH_syntax("value after -step is illegal!") ;
171 nopt++ ; continue ; /* skip to next arg */
172 }
173
174 /** -nsize **/
175
176 if( strncmp(argv[nopt],"-nsize",5) == 0 ){
177 FT_nsize = 1 ;
178 nopt++ ; continue ; /* skip to next arg */
179 }
180
181 /** illegal option **/
182
183 fprintf(stderr,"*** illegal command line option: %s\n",argv[nopt]) ;
184 FTOSH_syntax("type ftosh -help for more details") ;
185
186 }
187
188 /*----- rest of inputs are image files -----*/
189
190 FT_imts = mri_read_many_files( argc-nopt , argv+nopt ) ;
191 if( FT_imts == NULL ) FTOSH_syntax("cannot continue without input images!") ;
192
193 #if 0
194 /* check images for consistency */
195
196 nx = FT_imts->imarr[0]->nx ;
197 ny = FT_imts->imarr[0]->ny ;
198
199 for( kk=1 ; kk < FT_imts->num ; kk++ ){
200 if( nx != FT_imts->imarr[kk]->nx || ny != FT_imts->imarr[kk]->ny ){
201 fprintf(stderr,"*** image %d not conformant to image 0\n",kk) ;
202 FTOSH_syntax("cannot continue with images whose sizes differ!") ;
203 }
204 }
205 #endif
206
207 /* convert all input images to floats, if needed; ALSO, if */
208 /* FT_sval is zero, must find the maximum abs(input-FT_bval) */
209
210 bmax = 0.0 ;
211 for( kk=0 ; kk < FT_imts->num ; kk++ ){
212
213 if( FT_imts->imarr[kk]->kind != MRI_float ){
214 tim = mri_to_float( FT_imts->imarr[kk] ) ;
215 mri_free( FT_imts->imarr[kk] ) ;
216 FT_imts->imarr[kk] = tim ;
217 }
218
219 if( FT_sval == 0.0 ){
220 tim = FT_imts->imarr[kk] ; tar = mri_data_pointer( tim ) ;
221 nx = tim->nx ; ny = tim->ny ; npix = nx*ny ;
222 for( ii=0 ; ii < npix ; ii++ ){
223 bdif = fabs( tar[ii]-FT_bval ) ;
224 if( bmax < bdif ) bmax = bdif ;
225 }
226 }
227 }
228
229 if( FT_sval == 0.0 ){
230 if( bmax == 0.0 ){
231 fprintf(stderr,"*** all input images are == %g\n",FT_bval) ;
232 FTOSH_syntax("can't autoscale them!") ;
233 }
234 FT_sval = FT_tval / bmax ;
235 printf("** max abs(input-%g) = %g --> scale factor = %g/%g = %g\n",
236 FT_bval , bmax , FT_tval,bmax , FT_sval ) ;
237 }
238
239 return ;
240 }
241
242 /*----------------------------------------------------------------------*/
243
FTOSH_syntax(char * str)244 void FTOSH_syntax( char * str )
245 {
246 if( str != NULL ){
247 fprintf(stderr,"*** %s\a\n",str) ;
248 exit(-1) ;
249 }
250
251 printf(
252 "Usage: ftosh [options] image_files ...\n"
253 "\n"
254 " where the image_files are in the same format to3d accepts\n"
255 " and where the options are\n"
256 "\n"
257 " -prefix pname: The output files will be named in the format\n"
258 " -suffix sname: 'pname.index.sname' where 'pname' and 'sname'\n"
259 " -start si: are strings given by the first 2 options.\n"
260 " -step ss: 'index' is a number, given by 'si+(i-1)*ss'\n"
261 " for the i-th output file, for i=1,2,...\n"
262 " *** Default pname = 'sh'\n"
263 " *** Default sname = nothing at all\n"
264 " *** Default si = 1\n"
265 " *** Default ss = 1\n"
266 "\n"
267 " -nsize: Enforce the 'normal size' option, to make\n"
268 " the output images 64x64, 128x128, or 256x256.\n"
269 "\n"
270 " -scale sval: 'sval' and 'bval' are numeric values; if\n"
271 " -base bval: sval is given, then the output images are\n"
272 " -top tval: formed by scaling the inputs by the formula\n"
273 " 'output = sval*(input-bval)'.\n"
274 " *** Default sval is determined by finding\n"
275 " V = largest abs(input-bval) in all the input\n"
276 " images and then sval = tval / V.\n"
277 " *** Default tval is 32000; note that tval is only\n"
278 " used if sval is not given on the command line.\n"
279 " *** Default bval is 0.\n"
280 ) ;
281 exit(0) ;
282 }
283