Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

3dAFNItoANALYZE.c

Go to the documentation of this file.
00001 #include "mrilib.h"
00002 
00003 #ifndef ORCODE
00004 # define ORCODE(aa)                         \
00005    ( ((aa)=='R'||(aa)=='r') ? ORI_R2L_TYPE  \
00006     :((aa)=='L'||(aa)=='l') ? ORI_L2R_TYPE  \
00007     :((aa)=='P'||(aa)=='p') ? ORI_P2A_TYPE  \
00008     :((aa)=='A'||(aa)=='a') ? ORI_A2P_TYPE  \
00009     :((aa)=='I'||(aa)=='i') ? ORI_I2S_TYPE  \
00010     :((aa)=='S'||(aa)=='s') ? ORI_S2I_TYPE : ILLEGAL_TYPE )
00011 
00012 # define OR3OK(x,y,z) ( ((x)&6) + ((y)&6) + ((z)&6) == 6 )
00013 #endif
00014 
00015 int main( int argc , char *argv[] )
00016 {
00017    char *aname ;
00018    THD_3dim_dataset *dset ;
00019    int ii , scl ;
00020    MRI_IMAGE *im , *qim ;
00021    char *fname ;
00022    float fac ;
00023 
00024    int do_4D=0 , iarg=1 ;    /* 30 Sep 2002 */
00025    FILE *ifp=NULL ;
00026 
00027    int xxor=-1,yyor,zzor , xdir=0,ydir,zdir;  /* 19 Mar 2003 */
00028    float                   xdel  ,ydel,zdel;
00029    char orient_code[4] ;
00030 
00031    /*-- help me if you can --*/
00032 
00033    if( argc < 3 || strcmp(argv[1],"-help") == 0 ){
00034       printf("Usage: 3dAFNItoANALYZE [-4D] [-orient code] aname dset\n"
00035              "Writes AFNI dataset 'dset' to 1 or more ANALYZE 7.5 format\n"
00036              ".hdr/.img file pairs (one pair for each sub-brick in the\n"
00037              "AFNI dataset).  The ANALYZE files will be named\n"
00038              "  aname_0000.hdr aname_0000.img   for sub-brick #0\n"
00039              "  aname_0001.hdr aname_0001.img   for sub-brick #1\n"
00040              "and so forth.  Each file pair will contain a single 3D array.\n"
00041              "\n"
00042              "* If the AFNI dataset does not include sub-brick scale\n"
00043              "  factors, then the ANALYZE files will be written in the\n"
00044              "  datum type of the AFNI dataset.\n"
00045              "* If the AFNI dataset does have sub-brick scale factors,\n"
00046              "  then each sub-brick will be scaled to floating format\n"
00047              "  and the ANALYZE files will be written as floats.\n"
00048              "* The .hdr and .img files are written in the native byte\n"
00049              "  order of the computer on which this program is executed.\n"
00050              "\n"
00051              "Options\n"
00052              "-------\n"
00053              "-4D [30 Sep 2002]:\n"
00054              " If you use this option, then all the data will be written to\n"
00055              " one big ANALYZE file pair named aname.hdr/aname.img, rather\n"
00056              " than a series of 3D files.  Even if you only have 1 sub-brick,\n"
00057              " you may prefer this option, since the filenames won't have\n"
00058              " the '_0000' appended to 'aname'.\n"
00059              "\n"
00060              "-orient code [19 Mar 2003]:\n"
00061              " This option lets you flip the dataset to a different orientation\n"
00062              " when it is written to the ANALYZE files.  The orientation code is\n"
00063              " formed as follows:\n"
00064              "   The code must be 3 letters, one each from the\n"
00065              "   pairs {R,L} {A,P} {I,S}.  The first letter gives\n"
00066              "   the orientation of the x-axis, the second the\n"
00067              "   orientation of the y-axis, the third the z-axis:\n"
00068              "      R = Right-to-Left          L = Left-to-Right\n"
00069              "      A = Anterior-to-Posterior  P = Posterior-to-Anterior\n"
00070              "      I = Inferior-to-Superior   S = Superior-to-Inferior\n"
00071              "   For example, 'LPI' means\n"
00072              "      -x = Left       +x = Right\n"
00073              "      -y = Posterior  +y = Anterior\n"
00074              "      -z = Inferior   +z = Superior\n"
00075              " * For display in SPM, 'LPI' or 'RPI' seem to work OK.\n"
00076              "    Be careful with this: you don't want to confuse L and R\n"
00077              "    in the SPM display!\n"
00078              " * If you DON'T use this option, the dataset will be written\n"
00079              "    out in the orientation in which it is stored in AFNI\n"
00080              "    (e.g., the output of '3dinfo dset' will tell you this.)\n"
00081              " * The dataset orientation is NOT stored in the .hdr file.\n"
00082              " * AFNI and ANALYZE data are stored in files with the x-axis\n"
00083              "    varying most rapidly and the z-axis most slowly.\n"
00084              " * Note that if you read an ANALYZE dataset into AFNI for\n"
00085              "    display, AFNI assumes the LPI orientation, unless you\n"
00086              "    set environment variable AFNI_ANALYZE_ORIENT.\n"
00087             ) ;
00088       exit(0) ;
00089    }
00090 
00091    mainENTRY("3dAFNItoANALYZE main"); machdep(); PRINT_VERSION("3dAFNItoANALYZE");
00092 
00093    /*-- read inputs --*/
00094 
00095    while( iarg < argc && argv[iarg][0] == '-' ){
00096 
00097      if( strcmp(argv[iarg],"-4D") == 0 ){    /* 30 Sep 2002 */
00098        do_4D = 1 ; iarg++ ; continue ;
00099      }
00100 
00101      if( strcmp(argv[iarg],"-orient") == 0 ){ /* 19 Mar 2003 */
00102        char acod ;
00103 
00104        if( iarg+1 >= argc ){
00105          fprintf(stderr,"** Need something after -orient!\n"); exit(1);
00106        }
00107 
00108        MCW_strncpy(orient_code,argv[++iarg],4) ;
00109        if( strlen(orient_code) != 3 ){
00110          fprintf(stderr,"** Illegal code '%s' after -orient!\n",argv[iarg]); exit(1);
00111        }
00112 
00113        acod = toupper(orient_code[0]) ; xxor = ORCODE(acod) ;
00114        acod = toupper(orient_code[1]) ; yyor = ORCODE(acod) ;
00115        acod = toupper(orient_code[2]) ; zzor = ORCODE(acod) ;
00116 
00117        if( xxor<0 || yyor<0 || zzor<0 || !OR3OK(xxor,yyor,zzor) ){
00118          fprintf(stderr,"** Unusable code after -orient!\n"); exit(1);
00119        }
00120        iarg++ ; continue ;
00121      }
00122 
00123      fprintf(stderr,"** Illegal option: %s\n",argv[iarg]); exit(1);
00124    }
00125 
00126    if( iarg >= argc-1 ){
00127      fprintf(stderr,"** Not enough arguments on command line!\n"); exit(1);
00128    }
00129 
00130    aname = argv[iarg++] ;
00131    if( !THD_filename_ok(aname) ){
00132      fprintf(stderr,"** Illegal aname string %s\n",aname) ;
00133      exit(1) ;
00134    }
00135    fname = malloc( strlen(aname)+16 ) ;
00136 
00137    dset = THD_open_dataset( argv[iarg++] ) ;
00138    if( dset == NULL ){
00139      fprintf(stderr,"** Can't open dataset %s\n",argv[iarg-1]) ;
00140      exit(1) ;
00141    }
00142 
00143    if( xxor >= 0 ){  /* 19 Mar 2003: figure how to flip */
00144      xdir = THD_get_axis_direction( dset->daxes , xxor ) ;
00145      ydir = THD_get_axis_direction( dset->daxes , yyor ) ;
00146      zdir = THD_get_axis_direction( dset->daxes , zzor ) ;
00147      if(              ydir == 0 || zdir == 0 ) xdir = 0 ;
00148      if( xdir == 1 && ydir == 2 && zdir == 3 ) xdir = 0 ;
00149    }
00150    if( xdir != 0 ){
00151      float dx=fabs(DSET_DX(dset)) ,
00152            dy=fabs(DSET_DY(dset)) ,
00153            dz=fabs(DSET_DZ(dset))  ;
00154      DSET_mallocize(dset) ;
00155      switch( xdir ){
00156        case 1: case -1: xdel = dx ; break ;
00157        case 2: case -2: xdel = dy ; break ;
00158        case 3: case -3: xdel = dz ; break ;
00159      }
00160      switch( ydir ){
00161        case 1: case -1: ydel = dx ; break ;
00162        case 2: case -2: ydel = dy ; break ;
00163        case 3: case -3: ydel = dz ; break ;
00164      }
00165      switch( zdir ){
00166        case 1: case -1: zdel = dx ; break ;
00167        case 2: case -2: zdel = dy ; break ;
00168        case 3: case -3: zdel = dz ; break ;
00169      }
00170    } else {
00171      xdel = fabs(DSET_DX(dset)) ;
00172      ydel = fabs(DSET_DY(dset)) ;
00173      zdel = fabs(DSET_DZ(dset)) ;
00174    }
00175 
00176    DSET_load(dset) ;
00177    if( !DSET_LOADED(dset) ){
00178      fprintf(stderr,"** Can't load dataset %s\n",argv[iarg-1]) ;
00179      exit(1) ;
00180    }
00181 
00182    /* determine if we scale to floats */
00183 
00184    scl = THD_need_brick_factor( dset ) ;
00185 
00186    /* 30 Sep 2002: if doing a 4D file, write single .hdr now */
00187 
00188    if( do_4D ){
00189      im = mri_empty_conforming( DSET_BRICK(dset,0) ,
00190                                 (scl) ? MRI_float
00191                                       : DSET_BRICK_TYPE(dset,0) ) ;
00192 
00193      if( xdir != 0 ){
00194        qim = mri_flip3D( xdir,ydir,zdir , im ) ;
00195        if( qim == NULL){
00196          fprintf(stderr,"mri_flip3D fails?!\n"); exit(1);
00197        }
00198        mri_free(im); im = qim;
00199      }
00200 
00201      im->dx = xdel ;                    /* load voxel sizes */
00202      im->dy = ydel ;
00203      im->dz = zdel ;
00204      im->dw = 1.0 ;
00205 
00206      if( AFNI_yesenv("AFNI_ANALYZE_ORIGINATOR") ){
00207        im->xo = dset->daxes->xxorg ;                    /* load voxel origin */
00208        im->yo = dset->daxes->yyorg ;                    /* 03/11/04 KRH added this bit for SPM */
00209        im->zo = dset->daxes->zzorg ;
00210        if( ORIENT_sign[dset->daxes->xxorient] == '-' ){
00211          im->dx = -im->dx ;
00212          /* im->xo = -im->xo ; */
00213        }
00214        if( ORIENT_sign[dset->daxes->yyorient] == '-' ){
00215          im->dy = -im->dy ;
00216          /* im->yo = -im->yo ; */
00217        }
00218        if( ORIENT_sign[dset->daxes->zzorient] == '-' ){
00219          im->dz = -im->dz ;
00220          /* im->zo = -im->zo ; */
00221        }
00222      }
00223 
00224      im->nt = DSET_NVALS(dset) ;        /* add a time axis */
00225      im->dt = DSET_TR(dset) ;
00226      if( im->dt <= 0.0 ) im->dt = 1.0 ;
00227      if( DSET_TIMEUNITS(dset) == UNITS_MSEC_TYPE ) im->dt *= 0.001 ;  /* 05 Jul 2005 - oopsie */
00228 
00229      mri_write_analyze( aname , im ) ;  /* output 4D .hdr file */
00230      mri_free(im) ;
00231 
00232      sprintf(fname,"%s.img",aname) ;    /* open output .img file */
00233      ifp = fopen( fname , "wb" ) ;
00234      if( ifp == NULL ){
00235        fprintf(stderr,"** Can't open file %s for output!\n",fname) ;
00236        exit(1) ;
00237      }
00238    }
00239 
00240    /* loop over sub-bricks */
00241 
00242    for( ii=0 ; ii < DSET_NVALS(dset) ; ii++ ){
00243 
00244       im = DSET_BRICK(dset,ii) ;             /* get the sub-brick */
00245 
00246       if( scl ){                             /* scale it to floats */
00247         fac = DSET_BRICK_FACTOR(dset,ii) ;
00248         if( fac == 0.0 ) fac = 1.0 ;
00249         qim = mri_scale_to_float( fac , im ) ;
00250       } else {
00251         qim = im ;
00252       }
00253 
00254       if( xdir != 0 ){                       /* 19 Mar 2003: flip it */
00255         MRI_IMAGE *fim ;
00256         fim = mri_flip3D( xdir,ydir,zdir , qim ) ;
00257         if( fim == NULL ){
00258           fprintf(stderr,"mri_flip3D fails at ii=%d ?!\n",ii); exit(1);
00259         }
00260         if( qim != im ) mri_free(qim) ;
00261         qim = fim ;
00262       }
00263 
00264       if( do_4D ){                           /* 30 Sep 2002: write into 4D .img file */
00265 
00266         fwrite( mri_data_pointer(qim) , qim->nvox , qim->pixel_size , ifp ) ;
00267 
00268       } else {                               /* write separate 3D .hdr/.img files */
00269 
00270         qim->dx = xdel ;    /* load voxel sizes */
00271         qim->dy = ydel ;
00272         qim->dz = zdel ;
00273         qim->dw = 1.0 ;
00274 
00275         if( AFNI_yesenv("AFNI_ANALYZE_ORIGINATOR") ){
00276           qim->xo = dset->daxes->xxorg ;                    /* load voxel origin */
00277           qim->yo = dset->daxes->yyorg ;                    /* 03/11/04 KRH added this bit for SPM */
00278           qim->zo = dset->daxes->zzorg ;
00279 
00280           if( ORIENT_sign[dset->daxes->xxorient] == '-' ){
00281             qim->dx   = -qim->dx   ;
00282             /* qim->xo = -qim->xo ; */
00283           }
00284           if( ORIENT_sign[dset->daxes->yyorient] == '-' ){
00285             qim->dy   = -qim->dy   ;
00286             /* qim->yo = -qim->yo ; */
00287           }
00288           if( ORIENT_sign[dset->daxes->zzorient] == '-' ){
00289             qim->dz   = -qim->dz   ;
00290             /* qim->zo = -qim->zo ; */
00291           }
00292         }
00293 
00294         sprintf(fname,"%s_%04d",aname,ii) ;  /* make up a filename */
00295         mri_write_analyze( fname , qim ) ;   /* do the real work */
00296       }
00297 
00298       if( qim != im ) mri_free(qim) ;
00299       DSET_unload_one(dset,ii) ;             /* clean up the trash */
00300    }
00301 
00302    if( ifp != NULL ) fclose(ifp) ;           /* 30 Sep 2002 */
00303 
00304    free(fname) ; exit(0) ;
00305 }
 

Powered by Plone

This site conforms to the following standards: