00001 #include "nifti1_io.h" /* directly include I/O library functions */ 00002 00003 /*-----------------------------------------------*/ 00004 /* cc -o nifti1_test -O2 nifti1_test.c -lm */ 00005 /*-----------------------------------------------*/ 00006 00007 /****************************************************************************/ 00008 00009 int main( int argc , char *argv[] ) 00010 { 00011 nifti_image *nim ; 00012 int iarg=1 , outmode , ll , usegzip, debug = 0; 00013 char *tmpstr; 00014 00015 if( argc < 2 || strcmp(argv[1],"-help") == 0 ){ 00016 printf( 00017 "Usage: nifti1_test [-d level] [-n2|-n1|-na|-a2] infile [prefix]\n" 00018 "Usage: nifti1_test -nifti_hist\n" 00019 "\n" 00020 " If prefix is given, then the options mean:\n" 00021 " -a2 ==> write an ANALYZE 7.5 file pair: prefix.hdr/prefix.img\n" 00022 " -n2 ==> write a NIFTI-1 file pair: prefix.hdr/prefix.img\n" 00023 " -n1 ==> write a NIFTI-1 single file: prefix.nii\n" 00024 " -na ==> write a NIFTI-1 ASCII+binary file: prefix.nia\n" 00025 " -za2 ==> write an ANALYZE 7.5 file pair: prefix.hdr.gz/prefix.img.gz\n" 00026 " -zn2 ==> write a NIFTI-1 file pair: prefix.hdr.gz/prefix.img.gz\n" 00027 " -zn1 ==> write a NIFTI-1 single file: prefix.nii.gz\n" 00028 " -zna ==> write a NIFTI-1 ASCII+binary file: prefix.nia.gz\n" 00029 "\n" 00030 " The default is '-n1'.\n" 00031 "\n" 00032 " -d level : specify a debug level (0-3)\n" 00033 " -nifti_hist : display the nifti library modification history\n" 00034 " -nifti_ver : display the nifti library version number\n" 00035 "\n" 00036 " If prefix is not given, then the header info from infile\n" 00037 " file is printed to stdout.\n" 00038 "\n" 00039 " Please note that the '.nia' format is NOT part of the\n" 00040 " NIFTI-1 specification, but is provided mostly for ease\n" 00041 " of visualization (e.g., you can edit a .nia file and\n" 00042 " change some header fields, then rewrite it as .nii)\n" 00043 ) ; 00044 printf("\nsizeof(nifti_1_header)=%u\n",(unsigned int)sizeof(nifti_1_header)) ; 00045 exit(0) ; 00046 } 00047 00048 usegzip = 0; 00049 outmode = 1; 00050 while ( (iarg < argc) && (argv[iarg][0] == '-') ) { 00051 if ( ! strncmp(argv[iarg], "-a2", 3 ) ) outmode = 0; 00052 else if ( ! strncmp(argv[iarg], "-n1", 3 ) ) outmode = 1; 00053 else if ( ! strncmp(argv[iarg], "-n2", 3 ) ) outmode = 2; 00054 else if ( ! strncmp(argv[iarg], "-na", 3 ) ) outmode = 3; 00055 else if ( ! strncmp(argv[iarg], "-za2", 4 ) ){ outmode = 0; usegzip = 1; } 00056 else if ( ! strncmp(argv[iarg], "-zn1", 4 ) ){ outmode = 1; usegzip = 1; } 00057 else if ( ! strncmp(argv[iarg], "-zn2", 4 ) ){ outmode = 2; usegzip = 1; } 00058 else if ( ! strncmp(argv[iarg], "-zna", 4 ) ){ outmode = 3; usegzip = 1; } 00059 else if ( ! strncmp(argv[iarg], "-nifti_hist", 8 ) ) { 00060 nifti_disp_lib_hist(); 00061 exit(0); 00062 } 00063 else if ( ! strncmp(argv[iarg], "-nifti_ver", 8 ) ) { 00064 nifti_disp_lib_version(); 00065 exit(0); 00066 } 00067 else if ( ! strncmp(argv[iarg], "-d", 2 ) ) { 00068 iarg++; 00069 if( iarg >= argc ){ 00070 fprintf(stderr,"** ERROR: missing debug level\n"); exit(1); 00071 } 00072 debug = atoi(argv[iarg]); 00073 nifti_set_debug_level(debug); 00074 } 00075 else { 00076 fprintf(stderr,"** ERROR: invalid option '%s'\n", argv[iarg]); 00077 exit(1); 00078 } 00079 00080 iarg++; 00081 } 00082 00083 if( iarg >= argc ){ 00084 fprintf(stderr,"** ERROR: no input file on command line!?\n"); exit(1); 00085 } 00086 00087 nim = nifti_image_read( argv[iarg++] , 1 ) ; 00088 if( nim == NULL ){ fprintf(stderr,"** image read failure\n"); exit(1) ; } 00089 00090 /* if no output prefix, dump the image to the screen */ 00091 if( iarg >= argc ){ nifti_image_infodump(nim); exit(0); } 00092 00093 if( debug > 0 ){ 00094 if( nim->fname ) 00095 fprintf(stderr,"-d freeing header filename '%s'\n",nim->fname); 00096 if( nim->iname ) 00097 fprintf(stderr,"-d freeing image filename '%s'\n",nim->iname); 00098 } 00099 00100 nim->nifti_type = outmode ; 00101 if( nim->fname != NULL ) free(nim->fname) ; 00102 if( nim->iname != NULL ) free(nim->iname) ; 00103 00104 ll = strlen(argv[iarg]) ; 00105 tmpstr = nifti_makebasename(argv[iarg]); 00106 nim->fname = (char *)calloc(1,ll+8) ; strcpy(nim->fname,tmpstr) ; 00107 nim->iname = (char *)calloc(1,ll+8) ; strcpy(nim->iname,tmpstr) ; 00108 free(tmpstr); 00109 if( nim->nifti_type == 1 ){ 00110 strcat(nim->fname,".nii") ; 00111 strcat(nim->iname,".nii") ; 00112 } else if ( nim->nifti_type == 3 ){ 00113 strcat(nim->fname,".nia") ; 00114 strcat(nim->iname,".nia") ; 00115 } else { 00116 strcat(nim->fname,".hdr") ; 00117 strcat(nim->iname,".img") ; 00118 } 00119 if (usegzip) { 00120 strcat(nim->fname,".gz"); 00121 strcat(nim->iname,".gz"); 00122 } 00123 if (debug > 0) 00124 { 00125 fprintf(stderr,"+d writing nifti output to '%s'", nim->fname); 00126 if ( nim->nifti_type == 2 ) fprintf(stderr," and '%s'", nim->iname); 00127 fputc('\n', stderr); 00128 } 00129 00130 nifti_image_write( nim ) ; 00131 exit(0) ; 00132 }