Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
3dAFNItoNIFTI.c
Go to the documentation of this file.00001 #include "mrilib.h"
00002 #include "thd_niftiwrite.h"
00003
00004 int main( int argc , char *argv[] )
00005 {
00006 THD_3dim_dataset *dset ;
00007 char *prefix=NULL , *fname ;
00008 int narg=1 , flags=0 , ii , verb=0 , newid=1 , denote=0 , floatize=0 ;
00009 niftiwr_opts_t options ;
00010
00011 PRINT_VERSION("3dAFNItoNIFTI");
00012
00013 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
00014 printf("Usage: 3dAFNItoNIFTI [options] dataset\n"
00015 "Reads an AFNI dataset, writes it out as a NIfTI-1.1 (.nii) file.\n"
00016 "\n"
00017 "NOTES:\n"
00018 "* The nifti_tool program can be used to manipulate\n"
00019 " the contents of a NIfTI-1.1 file.\n"
00020 "* The input dataset can actually be in any input format\n"
00021 " that AFNI can read directly (e.g., MINC-1).\n"
00022 "* There is no 3dNIFTItoAFNI program, since AFNI programs\n"
00023 " can directly read .nii files. If you wish to make such\n"
00024 " a conversion anyway, one way to do so is like so:\n"
00025 " 3dcalc -a ppp.nii -prefix ppp -expr 'a'\n"
00026 "\n"
00027 "OPTIONS:\n"
00028 " -prefix ppp = Write the NIfTI-1.1 file as 'ppp.nii'.\n"
00029 " Default: the dataset's prefix is used.\n"
00030 #ifdef HAVE_ZLIB
00031 " If you want a compressed file, try\n"
00032 " using a prefix like 'ppp.nii.gz'.\n"
00033 #endif
00034 " -verb = Be verbose = print progress messages.\n"
00035 " Repeating this increases the verbosity\n"
00036 " (maximum setting is 3 '-verb' options).\n"
00037 " -float = Force the output dataset to be 32-bit\n"
00038 " floats. This option should be used when\n"
00039 " the input AFNI dataset has different\n"
00040 " float scale factors for different sub-bricks,\n"
00041 " an option that NIfTI-1.1 does not support.\n"
00042 "\n"
00043 "The following options affect the contents of the AFNI extension\n"
00044 "field that is written by default into the NIfTI-1.1 header:\n"
00045 "\n"
00046 " -pure = Do NOT write an AFNI extension field into\n"
00047 " the output file. Only use this option if\n"
00048 " needed. You can also use the 'nifti_tool'\n"
00049 " program to strip extensions from a file.\n"
00050 " -denote = When writing the AFNI extension field, remove\n"
00051 " text notes that might contain subject\n"
00052 " identifying information.\n"
00053 " -oldid = Give the new dataset the input dataset's\n"
00054 " AFNI ID code.\n"
00055 " -newid = Give the new dataset a new AFNI ID code, to\n"
00056 " distinguish it from the input dataset.\n"
00057 " **** N.B.: -newid is now the default action.\n"
00058 ) ;
00059 exit(0) ;
00060 }
00061
00062 mainENTRY("3dAFNItoNIFTI main"); machdep();
00063
00064
00065
00066 while( narg < argc && argv[narg][0] == '-' ){
00067
00068 if( strcmp(argv[narg],"-newid") == 0 ){
00069 newid = 1 ; narg++ ; continue ;
00070 }
00071
00072 if( strcmp(argv[narg],"-oldid") == 0 ){
00073 newid = 0 ; narg++ ; continue ;
00074 }
00075
00076 if( strcmp(argv[narg],"-denote") == 0 ){
00077 denote = 1 ; narg++ ; continue ;
00078 }
00079
00080 if( strcmp(argv[narg],"-float") == 0 ){
00081 floatize = 1 ; narg++ ; continue ;
00082 }
00083
00084 if( strcmp(argv[narg],"-pure") == 0 ){
00085 putenv("AFNI_NIFTI_NOEXT=YES") ;
00086 narg++ ; continue ;
00087 }
00088
00089 if( strcmp(argv[narg],"-prefix") == 0 ){
00090 prefix = argv[++narg] ;
00091 if( !THD_filename_ok(prefix) || prefix[0] == '-' )
00092 ERROR_exit("-prefix is illegal: %s\n",prefix) ;
00093 narg++ ; continue ;
00094 }
00095
00096 if( strcmp(argv[narg],"-verb") == 0 ){
00097 verb++ ; narg++ ; continue ;
00098 }
00099
00100 ERROR_exit("Unknown option: %s\n",argv[narg]);
00101 }
00102
00103
00104
00105 if( narg >= argc )
00106 ERROR_exit("No dataset on command line?\n");
00107
00108 dset = THD_open_dataset( argv[narg] ) ;
00109 if( !ISVALID_DSET(dset) )
00110 ERROR_exit("Can't open dataset %s\n",argv[narg]);
00111
00112
00113
00114 if( prefix == NULL ) prefix = DSET_PREFIX(dset) ;
00115
00116 if( newid ) dset->idcode = MCW_new_idcode() ;
00117
00118 fname = malloc( strlen(prefix)+16 ) ;
00119 strcpy(fname,prefix) ;
00120 if( strstr(fname,".nii") == NULL ) strcat(fname,".nii") ;
00121
00122 options.infile_name = nifti_strdup(fname) ;
00123 options.debug_level = verb ;
00124
00125
00126
00127 if( floatize ){
00128 int nvals=DSET_NVALS(dset) , nxyz=DSET_NVOX(dset) , tt ;
00129 float fac , *far ;
00130
00131 DSET_mallocize(dset); DSET_load(dset);
00132 if( !DSET_LOADED(dset) ) ERROR_exit("Can't load input dataset from disk") ;
00133 if( verb ) INFO_message("Converting dataset to floats (in memory)") ;
00134
00135
00136
00137 for( ii=0 ; ii < nvals ; ii++ ){
00138 fac = DSET_BRICK_FACTOR(dset,ii) ; if( fac == 0.0f ) fac = 1.0f ;
00139 tt = DSET_BRICK_TYPE(dset,ii) ;
00140
00141
00142
00143 if( fac == 1.0f && (tt == MRI_float || tt == MRI_complex) ) continue ;
00144
00145
00146
00147 far = (float *)calloc( nxyz , sizeof(float) ) ;
00148
00149
00150
00151 EDIT_coerce_scale_type( nxyz , fac ,
00152 DSET_BRICK_TYPE(dset,ii),DSET_BRICK_ARRAY(dset,ii) ,
00153 MRI_float , far ) ;
00154
00155
00156
00157 EDIT_substitute_brick( dset , ii , MRI_float , far ) ;
00158 EDIT_BRICK_FACTOR( dset , ii , 0.0 ) ;
00159 }
00160 }
00161
00162
00163
00164 if( denote ) THD_anonymize_write(1) ;
00165 ii = THD_write_nifti( dset , options ) ;
00166 exit(0) ;
00167 }