Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
imstack.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include "mrilib.h"
00008
00009 int main( int argc , char * argv[] )
00010 {
00011 char prefix[255]="obi-wan-kenobi" , fname[255] ;
00012 int datum = -1 ;
00013 int iarg = 1 ;
00014 int gnim , nx , ny , nz , kz ;
00015 char ** gname ;
00016 MRI_IMARR * arr ;
00017 MRI_IMAGE * im , * qim ;
00018 FILE * fp ;
00019
00020
00021
00022 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00023 printf(
00024 "Usage: imstack [options] image_filenames ...\n"
00025 "Stacks up a set of 2D images into one big file (a la MGH).\n"
00026 "Options:\n"
00027 " -datum type Converts the output data file to be 'type',\n"
00028 " which is either 'short' or 'float'.\n"
00029 " The default type is the type of the first image.\n"
00030 " -prefix name Names the output files to be 'name'.b'type' and 'name'.hdr.\n"
00031 " The default name is 'obi-wan-kenobi'.\n"
00032 ) ;
00033
00034 exit(0) ;
00035 }
00036
00037 machdep() ;
00038
00039
00040
00041 while( iarg < argc && argv[iarg][0] == '-' ){
00042
00043
00044
00045 if( strcmp(argv[iarg],"-datum") == 0 ){
00046 if( ++iarg >= argc ){
00047 fprintf(stderr,"-datum needs a type!\n") ; exit(1) ;
00048 }
00049 if( strcmp(argv[iarg],"short") == 0 ){
00050 datum = MRI_short ;
00051 } else if( strcmp(argv[iarg],"float") == 0 ){
00052 datum = MRI_float ;
00053 } else {
00054 fprintf(stderr,"-datum %s is illegal!\n",argv[iarg]) ; exit(1) ;
00055 }
00056 iarg++ ; continue ;
00057 }
00058
00059
00060
00061 if( strcmp(argv[iarg],"-prefix") == 0 ){
00062 if( ++iarg >= argc ){
00063 fprintf(stderr,"-prefix needs a name!\n") ; exit(1) ;
00064 }
00065 strcpy(prefix,argv[iarg]) ;
00066 iarg++ ; continue ;
00067 }
00068
00069
00070
00071 fprintf(stderr,"Unrecognized option: %s\n",argv[iarg]) ; exit(1) ;
00072 }
00073
00074
00075
00076 if( iarg >= argc ){
00077 fprintf(stderr,"No input image filenames?!\n") ; exit(1) ;
00078 }
00079
00080
00081
00082 MCW_warn_expand(1) ;
00083 MCW_file_expand( argc - iarg , argv + iarg , &gnim , &gname ) ;
00084 MCW_warn_expand(0) ;
00085
00086 if( gnim < 1 ){
00087 fprintf(stderr,"Filename expansion fails on input filenames?!\n") ; exit(1) ;
00088 }
00089
00090
00091
00092 arr = mri_read_many_files( gnim , gname ) ;
00093 if( arr == NULL || IMARR_COUNT(arr) <= 0 ){
00094 fprintf(stderr,"Can't read input files?!\n") ; exit(1) ;
00095 }
00096 MCW_free_expand( gnim , gname ) ;
00097 fprintf(stderr,"Read in %d 2D slices\n",IMARR_COUNT(arr)) ;
00098
00099
00100
00101 if( datum < 0 ){
00102 datum = IMARR_SUBIMAGE(arr,0)->kind ;
00103
00104 if( datum != MRI_short && datum != MRI_float ){
00105 fprintf(stderr,"Input image type is %s -- you must use -datum!\n",
00106 MRI_TYPE_name[datum]) ;
00107 exit(1) ;
00108 }
00109 }
00110
00111
00112
00113 nx = IMARR_SUBIMAGE(arr,0)->nx ;
00114 ny = IMARR_SUBIMAGE(arr,0)->ny ;
00115 nz = IMARR_COUNT(arr) ;
00116
00117 for( kz=1 ; kz < nz ; kz++ ){
00118 if( IMARR_SUBIMAGE(arr,kz)->nx != nx ||
00119 IMARR_SUBIMAGE(arr,kz)->ny != ny ){
00120
00121 fprintf(stderr,"All images must be the same size (%d x %d)\n",nx,ny) ;
00122 exit(1) ;
00123 }
00124 }
00125
00126
00127
00128 sprintf(fname,"%s.b%s",prefix,MRI_TYPE_name[datum]) ;
00129 fp = fopen( fname , "w" ) ;
00130 if( fp == NULL ){
00131 fprintf(stderr,"Can't open output file %s\n",fname) ; exit(1) ;
00132 }
00133
00134 for( kz=0 ; kz < nz ; kz++ ){
00135
00136 im = IMARR_SUBIMAGE(arr,kz) ;
00137 if( im->kind != datum ) qim = mri_to_mri( datum , im ) ;
00138 else qim = im ;
00139
00140 fwrite( mri_data_pointer(qim) , qim->pixel_size , qim->nx * qim->ny , fp ) ;
00141
00142 if( qim != im ) mri_free(qim) ;
00143 mri_free(im);
00144 }
00145 fclose( fp ) ;
00146 fprintf(stderr,"Wrote output brick %s\n",fname) ;
00147
00148
00149
00150 sprintf(fname,"%s.hdr",prefix) ;
00151 fp = fopen( fname , "w" ) ;
00152 if( fp == NULL ){
00153 fprintf(stderr,"Can't open output file %s\n",fname) ; exit(1) ;
00154 }
00155
00156 fprintf( fp , "%d %d %d 0\n" , nx,ny,nz ) ;
00157 fclose(fp) ;
00158 fprintf(stderr,"Wrote output header %s: %d %d %d\n",fname,nx,ny,nz) ;
00159
00160 exit(0) ;
00161 }