Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
ftosh.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include <string.h>
00008 #include "mrilib.h"
00009
00010
00011
00012 #define MAX_NAME 64
00013
00014 static float FT_tval = 32000.0 ;
00015 static float FT_bval = 0.0 ;
00016 static float FT_sval = 0.0 ;
00017 static int FT_start = 1 ;
00018 static int FT_step = 1 ;
00019 static int FT_nsize = 0 ;
00020
00021 static char FT_pname[MAX_NAME] = "sh." ;
00022 static char FT_sname[MAX_NAME] = "\0" ;
00023
00024 static MRI_IMARR * FT_imts = NULL ;
00025
00026
00027
00028 void FTOSH_syntax( char * ) ;
00029 void FTOSH_getopts( int , char * argv[] ) ;
00030
00031
00032
00033 int main( int argc , char *argv[] )
00034 {
00035 int ii , nx , ny , npix , kk ;
00036 MRI_IMAGE * tim , * sim ;
00037 float * tar ;
00038 short * sar ;
00039 char name[256] ;
00040
00041
00042
00043 printf(
00044 "ftosh: convert float images to shorts, by RW Cox\n") ;
00045
00046 machdep() ;
00047
00048 if( argc < 2 || strcmp(argv[1],"-help") == 0 ) FTOSH_syntax(NULL) ;
00049
00050 FTOSH_getopts( argc , argv ) ;
00051
00052
00053
00054 for( kk=0 ; kk < FT_imts->num ; kk++ ){
00055 tim = FT_imts->imarr[kk] ; tar = mri_data_pointer( tim ) ;
00056 nx = tim->nx ; ny = tim->ny ; npix = nx*ny ;
00057 sim = mri_new( nx , ny , MRI_short ) ; sar = mri_data_pointer( sim ) ;
00058
00059 for( ii=0 ; ii < npix ; ii++ )
00060 sar[ii] = (short) ( FT_sval * ( tar[ii] - FT_bval ) + 0.499 ) ;
00061
00062 mri_free( tim ) ;
00063 if( FT_nsize ){
00064 tim = mri_nsize( sim ) ; mri_free( sim ) ; sim = tim ;
00065 }
00066
00067 if( strlen(FT_pname) > 0 && strlen(FT_sname) > 0 ){
00068 sprintf( name , "%s%04d.%s" ,
00069 FT_pname , FT_start + kk*FT_step , FT_sname ) ;
00070 } else if( strlen(FT_pname) > 0 ){
00071 sprintf( name , "%s%04d" , FT_pname , FT_start + kk*FT_step ) ;
00072 } else if( strlen(FT_sname) > 0 ){
00073 sprintf( name , "%04d.%s" , FT_start + kk*FT_step , FT_sname ) ;
00074 } else {
00075 sprintf( name , "%04d" , FT_start + kk*FT_step ) ;
00076 }
00077
00078 printf("-- writing %dx%d image %s\n",nx,ny,name) ;
00079 mri_write( name , sim ) ;
00080 mri_free( sim ) ;
00081 }
00082
00083 exit(0) ;
00084 }
00085
00086
00087
00088 void FTOSH_getopts( int argc , char *argv[] )
00089 {
00090 int nopt = 1 , kk , nx,ny , ii,npix ;
00091 MRI_IMAGE * tim ;
00092 float * tar ;
00093 float bmax , bdif ;
00094
00095
00096
00097 while( nopt < argc && argv[nopt][0] == '-' ){
00098
00099
00100
00101 if( strncmp(argv[nopt],"-prefix",5) == 0 ){
00102 if( ++nopt >= argc ) FTOSH_syntax("-prefix needs a name!") ;
00103 strcpy( FT_pname , argv[nopt] ) ;
00104 kk = strlen(FT_pname) ;
00105 if( kk > 0 && FT_pname[kk-1] != '.' ){
00106 FT_pname[kk] = '.' ;
00107 FT_pname[kk+1] = '\0' ;
00108 }
00109 nopt++ ; continue ;
00110 }
00111
00112
00113
00114 if( strncmp(argv[nopt],"-suffix",5) == 0 ){
00115 if( ++nopt >= argc ) FTOSH_syntax("-suffix needs a name!") ;
00116 strcpy( FT_sname , argv[nopt] ) ;
00117 nopt++ ; continue ;
00118 }
00119
00120
00121
00122 if( strncmp(argv[nopt],"-scale",5) == 0 ){
00123 char * ch ;
00124 if( ++nopt >= argc ) FTOSH_syntax("-scale needs a value!");
00125 FT_sval = strtod( argv[nopt] , &ch ) ;
00126 if( *ch != '\0' || FT_sval == 0.0 )
00127 FTOSH_syntax("value after -scale is illegal!") ;
00128 nopt++ ; continue ;
00129 }
00130
00131
00132
00133 if( strncmp(argv[nopt],"-base",5) == 0 ){
00134 char * ch ;
00135 if( ++nopt >= argc ) FTOSH_syntax("-base needs a value!");
00136 FT_bval = strtod( argv[nopt] , &ch ) ;
00137 if( *ch != '\0' ) FTOSH_syntax("value after -base is illegal!") ;
00138 nopt++ ; continue ;
00139 }
00140
00141
00142
00143 if( strncmp(argv[nopt],"-top",4) == 0 ){
00144 char * ch ;
00145 if( ++nopt >= argc ) FTOSH_syntax("-top needs a value!");
00146 FT_tval = strtod( argv[nopt] , &ch ) ;
00147 if( *ch != '\0' || FT_tval == 0.0 )
00148 FTOSH_syntax("value after -top is illegal!") ;
00149 nopt++ ; continue ;
00150 }
00151
00152
00153
00154 if( strncmp(argv[nopt],"-start",5) == 0 ){
00155 char * ch ;
00156 if( ++nopt >= argc ) FTOSH_syntax("-start needs a value!");
00157 FT_start = strtol( argv[nopt] , &ch , 10 ) ;
00158 if( *ch != '\0' || FT_start < 0 )
00159 FTOSH_syntax("value after -start is illegal!") ;
00160 nopt++ ; continue ;
00161 }
00162
00163
00164
00165 if( strncmp(argv[nopt],"-step",5) == 0 ){
00166 char * ch ;
00167 if( ++nopt >= argc ) FTOSH_syntax("-step needs a value!");
00168 FT_step = strtol( argv[nopt] , &ch , 10 ) ;
00169 if( *ch != '\0' || FT_step < 0 )
00170 FTOSH_syntax("value after -step is illegal!") ;
00171 nopt++ ; continue ;
00172 }
00173
00174
00175
00176 if( strncmp(argv[nopt],"-nsize",5) == 0 ){
00177 FT_nsize = 1 ;
00178 nopt++ ; continue ;
00179 }
00180
00181
00182
00183 fprintf(stderr,"*** illegal command line option: %s\n",argv[nopt]) ;
00184 FTOSH_syntax("type ftosh -help for more details") ;
00185
00186 }
00187
00188
00189
00190 FT_imts = mri_read_many_files( argc-nopt , argv+nopt ) ;
00191 if( FT_imts == NULL ) FTOSH_syntax("cannot continue without input images!") ;
00192
00193 #if 0
00194
00195
00196 nx = FT_imts->imarr[0]->nx ;
00197 ny = FT_imts->imarr[0]->ny ;
00198
00199 for( kk=1 ; kk < FT_imts->num ; kk++ ){
00200 if( nx != FT_imts->imarr[kk]->nx || ny != FT_imts->imarr[kk]->ny ){
00201 fprintf(stderr,"*** image %d not conformant to image 0\n",kk) ;
00202 FTOSH_syntax("cannot continue with images whose sizes differ!") ;
00203 }
00204 }
00205 #endif
00206
00207
00208
00209
00210 bmax = 0.0 ;
00211 for( kk=0 ; kk < FT_imts->num ; kk++ ){
00212
00213 if( FT_imts->imarr[kk]->kind != MRI_float ){
00214 tim = mri_to_float( FT_imts->imarr[kk] ) ;
00215 mri_free( FT_imts->imarr[kk] ) ;
00216 FT_imts->imarr[kk] = tim ;
00217 }
00218
00219 if( FT_sval == 0.0 ){
00220 tim = FT_imts->imarr[kk] ; tar = mri_data_pointer( tim ) ;
00221 nx = tim->nx ; ny = tim->ny ; npix = nx*ny ;
00222 for( ii=0 ; ii < npix ; ii++ ){
00223 bdif = fabs( tar[ii]-FT_bval ) ;
00224 if( bmax < bdif ) bmax = bdif ;
00225 }
00226 }
00227 }
00228
00229 if( FT_sval == 0.0 ){
00230 if( bmax == 0.0 ){
00231 fprintf(stderr,"*** all input images are == %g\n",FT_bval) ;
00232 FTOSH_syntax("can't autoscale them!") ;
00233 }
00234 FT_sval = FT_tval / bmax ;
00235 printf("** max abs(input-%g) = %g --> scale factor = %g/%g = %g\n",
00236 FT_bval , bmax , FT_tval,bmax , FT_sval ) ;
00237 }
00238
00239 return ;
00240 }
00241
00242
00243
00244 void FTOSH_syntax( char * str )
00245 {
00246 if( str != NULL ){
00247 fprintf(stderr,"*** %s\a\n",str) ;
00248 exit(-1) ;
00249 }
00250
00251 printf(
00252 "Usage: ftosh [options] image_files ...\n"
00253 "\n"
00254 " where the image_files are in the same format to3d accepts\n"
00255 " and where the options are\n"
00256 "\n"
00257 " -prefix pname: The output files will be named in the format\n"
00258 " -suffix sname: 'pname.index.sname' where 'pname' and 'sname'\n"
00259 " -start si: are strings given by the first 2 options.\n"
00260 " -step ss: 'index' is a number, given by 'si+(i-1)*ss'\n"
00261 " for the i-th output file, for i=1,2,...\n"
00262 " *** Default pname = 'sh'\n"
00263 " *** Default sname = nothing at all\n"
00264 " *** Default si = 1\n"
00265 " *** Default ss = 1\n"
00266 "\n"
00267 " -nsize: Enforce the 'normal size' option, to make\n"
00268 " the output images 64x64, 128x128, or 256x256.\n"
00269 "\n"
00270 " -scale sval: 'sval' and 'bval' are numeric values; if\n"
00271 " -base bval: sval is given, then the output images are\n"
00272 " -top tval: formed by scaling the inputs by the formula\n"
00273 " 'output = sval*(input-bval)'.\n"
00274 " *** Default sval is determined by finding\n"
00275 " V = largest abs(input-bval) in all the input\n"
00276 " images and then sval = tval / V.\n"
00277 " *** Default tval is 32000; note that tval is only\n"
00278 " used if sval is not given on the command line.\n"
00279 " *** Default bval is 0.\n"
00280 ) ;
00281 exit(0) ;
00282 }