Attachment 'mri_info.c'

Download

   1 ////////////////////////////////////////////////////////////////////
   2 // mri_info.c
   3 //
   4 // Warning: Do not edit the following three lines.  CVS maintains them.
   5 // Revision Author: $Author: nicks $
   6 // Revision Date  : $Date: 2006/01/17 21:28:56 $
   7 // Revision       : $Revision: 1.43 $
   8 //
   9 ////////////////////////////////////////////////////////////////////
  10 
  11 char *MRI_INFO_VERSION = "$Revision: 1.43 $";
  12 
  13 #include <stdio.h>
  14 #include <sys/stat.h>
  15 #include <unistd.h>
  16 #include <string.h>
  17 #include <ctype.h>
  18 #include <time.h>
  19 #include "const.h"
  20 #include "machine.h"
  21 #include "fio.h"
  22 #include "utils.h"
  23 #include "mri.h"
  24 #include "gcamorph.h"
  25 #include "volume_io.h"
  26 #include "analyze.h"
  27 #include "mri_identify.h"
  28 #include "error.h"
  29 #include "diag.h"
  30 #include "version.h"
  31 #include "mghendian.h"
  32 #include "fio.h"
  33 
  34 static void do_file(char *fname);
  35 static int  parse_commandline(int argc, char **argv);
  36 static void check_options(void);
  37 static void print_usage(void) ;
  38 static void usage_exit(void);
  39 static void print_help(void) ;
  40 static void print_version(void) ;
  41 
  42 static char vcid[] = "$Id: mri_info.c,v 1.43 2006/01/17 21:28:56 nicks Exp $";
  43 
  44 char *Progname ;
  45 char *inputlist[100];
  46 int nthinput=0;
  47 int PrintTR=0;
  48 int PrintTE=0;
  49 int PrintTI=0;
  50 int PrintFlipAngle=0;
  51 int PrintCRes = 0;
  52 int PrintRRes = 0;
  53 int PrintSRes = 0;
  54 int PrintNCols = 0;
  55 int PrintNRows = 0;
  56 int PrintNSlices = 0;
  57 int PrintNFrames = 0;
  58 int PrintFormat = 0;
  59 int PrintColDC   = 0;
  60 int PrintRowDC   = 0;
  61 int PrintSliceDC = 0;
  62 int PrintVox2RAS = 0;
  63 int PrintRAS2Vox = 0;
  64 int PrintVox2RAStkr = 0;
  65 int PrintDet = 0;
  66 int PrintOrientation = 0;
  67 int PrintSliceDirection = 0;
  68 FILE *fpout;
  69 int PrintToFile = 0;
  70 char *outfile = NULL;
  71 int debug = 0;
  72 
  73 /***-------------------------------------------------------****/
  74 int main(int argc, char *argv[])
  75 {
  76   int nargs, index;
  77 
  78   /* rkt: check for and handle version tag */
  79   nargs = handle_version_option (argc, argv, vcid, "$Name:  $");
  80   if (nargs && argc - nargs == 1)
  81     exit (0);
  82   argc -= nargs;
  83 
  84   Progname = argv[0] ;
  85   argc --;
  86   argv++;
  87   ErrorInit(NULL, NULL, NULL) ;
  88   DiagInit(NULL, NULL, NULL) ;
  89   if(argc == 0) usage_exit();
  90 
  91   parse_commandline(argc, argv);
  92   check_options();
  93 
  94   if(PrintToFile){
  95     fpout = fopen(outfile,"w");
  96     if(fpout == NULL){
  97       printf("ERROR: could not open %s\n",outfile);
  98       exit(1);
  99     }
 100   }
 101   else fpout = stdout;
 102 
 103   for(index=0;index<nthinput;index++) {
 104     if(debug) printf("%d %s ----- \n",index,inputlist[index]);
 105     do_file(inputlist[index]);
 106   }
 107   if(PrintToFile) fclose(fpout);
 108 
 109   exit(0);
 110 
 111 } /* end main() */
 112 
 113 /* ------------------------------------------------------------------ */
 114 static int parse_commandline(int argc, char **argv)
 115 {
 116   int  nargc , nargsused;
 117   char **pargv, *option ;
 118 
 119   if(argc < 1) usage_exit();
 120 
 121   nargc = argc;
 122   pargv = argv;
 123   while(nargc > 0){
 124 
 125     option = pargv[0];
 126     if(debug) printf("%d %s\n",nargc,option);
 127     nargc -= 1;
 128     pargv += 1;
 129 
 130     nargsused = 0;
 131 
 132     if (!strcasecmp(option, "--help"))  print_help() ;
 133     else if (!strcasecmp(option, "--version")) print_version() ;
 134     else if (!strcasecmp(option, "--debug"))   debug = 1;
 135     else if (!strcasecmp(option, "--tr"))   PrintTR = 1;
 136     else if (!strcasecmp(option, "--te"))   PrintTE = 1;
 137     else if (!strcasecmp(option, "--ti"))   PrintTI = 1;
 138     else if (!strcasecmp(option, "--fa"))   PrintFlipAngle = 1;
 139     else if (!strcasecmp(option, "--flip_angle"))   PrintFlipAngle = 1;
 140 
 141     else if (!strcasecmp(option, "--cres"))    PrintCRes = 1;
 142     else if (!strcasecmp(option, "--xsize"))   PrintCRes = 1;
 143     else if (!strcasecmp(option, "--rres"))    PrintRRes = 1;
 144     else if (!strcasecmp(option, "--ysize"))   PrintCRes = 1;
 145     else if (!strcasecmp(option, "--sres"))    PrintSRes = 1;
 146     else if (!strcasecmp(option, "--zsize"))   PrintCRes = 1;
 147 
 148     else if (!strcasecmp(option, "--ncols"))     PrintNCols = 1;
 149     else if (!strcasecmp(option, "--width"))     PrintNCols = 1;
 150     else if (!strcasecmp(option, "--nrows"))     PrintNRows = 1;
 151     else if (!strcasecmp(option, "--height"))    PrintNRows = 1;
 152     else if (!strcasecmp(option, "--nslices"))   PrintNSlices = 1;
 153     else if (!strcasecmp(option, "--depth"))     PrintNSlices = 1;
 154 
 155     else if (!strcasecmp(option, "--cdc"))       PrintColDC = 1;
 156     else if (!strcasecmp(option, "--rdc"))       PrintRowDC = 1;
 157     else if (!strcasecmp(option, "--sdc"))       PrintSliceDC = 1;
 158     else if (!strcasecmp(option, "--vox2ras"))   PrintVox2RAS = 1;
 159     else if (!strcasecmp(option, "--ras2vox"))   PrintRAS2Vox = 1;
 160     else if (!strcasecmp(option, "--vox2ras-tkr")) PrintVox2RAStkr = 1;
 161 
 162     else if (!strcasecmp(option, "--det"))     PrintDet = 1;
 163 
 164     else if (!strcasecmp(option, "--nframes"))   PrintNFrames = 1;
 165     else if (!strcasecmp(option, "--format")) PrintFormat = 1;
 166     else if (!strcasecmp(option, "--orientation")) PrintOrientation = 1;
 167     else if (!strcasecmp(option, "--slicedirection")) PrintSliceDirection = 1;
 168     else if (!strcasecmp(option, "--o")){
 169       PrintToFile = 1;
 170       outfile = pargv[0];
 171       nargc --;
 172       pargv ++;
 173     }
 174     else{
 175       // Must be an input volume
 176       inputlist[nthinput] = option;
 177       nthinput++;
 178     }
 179     nargc -= nargsused;
 180     pargv += nargsused;
 181   }
 182   return(0);
 183 }
 184 /* --------------------------------------------- */
 185 static void print_usage(void)
 186 {
 187   printf("USAGE: %s fname1 <fname2> <options> \n",Progname) ;
 188   printf("\n");
 189   printf("   --tr : print TR to stdout\n");
 190   printf("   --te : print TE to stdout\n");
 191   printf("   --ti : print TI to stdout\n");
 192   printf("   --fa : print flip angle to stdout\n");
 193   printf("   --cres : print column voxel size (xsize) to stdout\n");
 194   printf("   --rres : print row    voxel size (ysize) to stdout\n");
 195   printf("   --sres : print slice  voxel size (zsize) to stdout\n");
 196   printf("   --ncols : print number of columns (width) to stdout\n");
 197   printf("   --nrows : print number of rows (height) to stdout\n");
 198   printf("   --nslices : print number of columns (depth) to stdout\n");
 199   printf("   --cdc : print column direction cosine (x_{r,a,s})\n");
 200   printf("   --rdc : print row    direction cosine (y_{r,a,s})\n");
 201   printf("   --sdc : print slice  direction cosine (z_{r,a,s})\n");
 202   printf("   --vox2ras : print the the native/qform vox2ras matrix\n");
 203   printf("   --ras2vox : print the the native/qform ras2vox matrix\n");
 204   printf("   --vox2ras-tkr : print the the tkregister vox2ras matrix\n");
 205   printf("   --det : print the determinant of the vox2ras matrix\n");
 206   printf("   --nframes : print number of frames to stdout\n");
 207   printf("   --format : file format\n");
 208   printf("   --orientation : orientation string (eg, LPS, RAS, RPI)\n");
 209   printf("   --slicedirection : primary slice direction (eg, axial)\n");
 210   printf("   --o file : print flagged results to file \n");
 211   printf("\n");
 212   //printf("   --svol svol.img (structural volume)\n");
 213 }
 214 /* --------------------------------------------- */
 215 static void print_help(void)
 216 {
 217   print_usage() ;
 218   printf(
 219          "\n"
 220          "Dumps information about the volume to stdout. Specific pieces \n"
 221          "of information can be printed out as well by specifying the proper\n"
 222          "flag (eg, --tr for TR). Time is in msec. Distance is in MM. Angles\n"
 223          "are in radians.\n"
 224          "\n"
 225          "The direction cosine outputs (--cdc, --rdc, --sdc) correspond to \n"
 226          "mri_convert flags -iid, -ijd, -ikd.\n"
 227          );
 228 
 229 
 230   exit(1) ;
 231 }
 232 /* --------------------------------------------- */
 233 static void print_version(void)
 234 {
 235   printf("%s\n", vcid) ;
 236   exit(1) ;
 237 }
 238 /* --------------------------------------------- */
 239 static void check_options(void)
 240 {
 241   if(nthinput == 0){
 242     printf("ERROR: no input volume supplied\n");
 243     exit(1);
 244   }
 245   return;
 246 }
 247 /* ------------------------------------------------------ */
 248 static void usage_exit(void)
 249 {
 250   print_usage() ;
 251   exit(1) ;
 252 }
 253 
 254 /***-------------------------------------------------------****/
 255 int PrettyMatrixPrint(MATRIX *mat)
 256 {
 257   int row;
 258 
 259   if (mat == NULL)
 260     ErrorReturn(ERROR_BADPARM,(ERROR_BADPARM, "mat = NULL!")) ;
 261 
 262   if (mat->type != MATRIX_REAL)
 263     ErrorReturn(ERROR_BADPARM,(ERROR_BADPARM, "mat is not Real type")) ;
 264 
 265   if (mat->rows != 4 || mat->cols != 4)
 266     ErrorReturn(ERROR_BADPARM,(ERROR_BADPARM, "mat is not of 4 x 4")) ;
 267 
 268   for (row=1; row < 5; ++row)
 269     printf("              %8.4f %8.4f %8.4f %10.4f\n",
 270            mat->rptr[row][1], mat->rptr[row][2],
 271            mat->rptr[row][3], mat->rptr[row][4]);
 272   return (NO_ERROR);
 273 }
 274 
 275 /***-------------------------------------------------------****/
 276 static void do_file(char *fname)
 277 {
 278   MRI *mri ;
 279   MATRIX *m, *minv ;
 280   int r,c;
 281   char ostr[5];
 282   GCA_MORPH *gcam;
 283   ostr[4] = '\0';
 284 
 285   if(!(strstr(fname, ".m3d") == 0 && strstr(fname, ".m3z") == 0
 286        && strstr(fname, ".M3D") == 0 && strstr(fname, ".M3Z") == 0)
 287      ){
 288     fprintf(fpout,"Input file is a 3D morph.\n");
 289 
 290     gcam = NULL;
 291     gcam = GCAMread(fname);
 292     if(!gcam) return;
 293     fprintf(fpout,"3D morph source geometry:\n");
 294     vg_print(&gcam->src);
 295     fprintf(fpout,"3D morph atlas geometry:\n");
 296     vg_print(&gcam->atlas);
 297     GCAMfree(&gcam);
 298     return;
 299   }
 300 
 301   if(PrintFormat){
 302     fprintf(fpout,"%s\n", type_to_string(mri_identify(fname)));
 303     return;
 304   }
 305   mri = MRIreadHeader(fname, MRI_VOLUME_TYPE_UNKNOWN) ;
 306   if(!mri) return;
 307 
 308   if(PrintTR){
 309     fprintf(fpout,"%g\n",mri->tr);
 310     return;
 311   }
 312   if(PrintTE){
 313     fprintf(fpout,"%g\n",mri->te);
 314     return;
 315   }
 316   if(PrintTI){
 317     fprintf(fpout,"%g\n",mri->ti);
 318     return;
 319   }
 320   if(PrintFlipAngle){
 321     fprintf(fpout,"%g\n",mri->flip_angle);
 322     return;
 323   }
 324   if(PrintCRes){
 325     fprintf(fpout,"%g\n",mri->xsize);
 326     return;
 327   }
 328   if(PrintRRes){
 329     fprintf(fpout,"%g\n",mri->ysize);
 330     return;
 331   }
 332   if(PrintSRes){
 333     fprintf(fpout,"%g\n",mri->zsize);
 334     return;
 335   }
 336   if(PrintNCols){
 337     fprintf(fpout,"%d\n",mri->width);
 338     return;
 339   }
 340   if(PrintNRows){
 341     fprintf(fpout,"%d\n",mri->height);
 342     return;
 343   }
 344   if(PrintNSlices){
 345     fprintf(fpout,"%d\n",mri->depth);
 346     return;
 347   }
 348   if(PrintNFrames){
 349     fprintf(fpout,"%d\n",mri->nframes);
 350     return;
 351   }
 352   if(PrintColDC){
 353     fprintf(fpout,"%g %g %g\n",mri->x_r,mri->x_a,mri->x_s);
 354     return;
 355   }
 356   if(PrintRowDC){
 357     fprintf(fpout,"%g %g %g\n",mri->y_r,mri->y_a,mri->y_s);
 358     return;
 359   }
 360   if(PrintSliceDC){
 361     fprintf(fpout,"%g %g %g\n",mri->z_r,mri->z_a,mri->z_s);
 362     return;
 363   }
 364   if(PrintDet){
 365     m = MRIgetVoxelToRasXform(mri) ;
 366     fprintf(fpout,"%g\n",MatrixDeterminant(m));
 367     MatrixFree(&m) ;
 368     return;
 369   }
 370   if(PrintVox2RAS){
 371     m = MRIgetVoxelToRasXform(mri) ;
 372     for(r=1; r<=4; r++){
 373       for(c=1; c<=4; c++){
 374         fprintf(fpout,"%10.5f ",m->rptr[r][c]);
 375       }
 376       fprintf(fpout,"\n");
 377     }
 378     MatrixFree(&m) ;
 379     return;
 380   }
 381   if(PrintRAS2Vox){
 382     m = MRIgetVoxelToRasXform(mri) ;
 383     minv = MatrixInverse(m,NULL);
 384     for(r=1; r<=4; r++){
 385       for(c=1; c<=4; c++){
 386         fprintf(fpout,"%10.5f ",minv->rptr[r][c]);
 387       }
 388       fprintf(fpout,"\n");
 389     }
 390     MatrixFree(&m) ;
 391     MatrixFree(&minv) ;
 392     return;
 393   }
 394   if(PrintVox2RAStkr){
 395     m = MRIxfmCRS2XYZtkreg(mri);
 396     for(r=1; r<=4; r++){
 397       for(c=1; c<=4; c++){
 398         fprintf(fpout,"%10.5f ",m->rptr[r][c]);
 399       }
 400       fprintf(fpout,"\n");
 401     }
 402     MatrixFree(&m) ;
 403     return;
 404   }
 405   if(PrintOrientation){
 406     MRIdircosToOrientationString(mri,ostr);
 407     fprintf(fpout,"%s\n",ostr);
 408     return;
 409   }
 410   if(PrintSliceDirection){
 411     fprintf(fpout,"%s\n",MRIsliceDirectionName(mri));
 412     return;
 413   }
 414 
 415   fprintf(fpout,"Volume information for %s\n", fname);
 416   // mri_identify has been called but the result is not stored
 417   // and thus I have to call it again
 418   printf("          type: %s\n", type_to_string(mri_identify(fname)));
 419   if (mri->nframes > 1)
 420     printf("    dimensions: %d x %d x %d x %d\n",
 421            mri->width, mri->height, mri->depth, mri->nframes) ;
 422   else
 423     printf("    dimensions: %d x %d x %d\n",
 424            mri->width, mri->height, mri->depth) ;
 425   printf("   voxel sizes: %6.4f, %6.4f, %6.4f\n",
 426          mri->xsize, mri->ysize, mri->zsize) ;
 427   printf("          type: %s (%d)\n",
 428          mri->type == MRI_UCHAR   ? "UCHAR" :
 429          mri->type == MRI_SHORT   ? "SHORT" :
 430          mri->type == MRI_INT     ? "INT" :
 431          mri->type == MRI_LONG    ? "LONG" :
 432          mri->type == MRI_BITMAP  ? "BITMAP" :
 433          mri->type == MRI_TENSOR  ? "TENSOR" :
 434          mri->type == MRI_FLOAT   ? "FLOAT" : "UNKNOWN", mri->type) ;
 435   printf("           fov: %2.3f\n", mri->fov) ;
 436   printf("        xstart: %2.1f, xend: %2.1f\n",
 437          mri->xstart*mri->xsize, mri->xend*mri->xsize) ;
 438   printf("        ystart: %2.1f, yend: %2.1f\n",
 439          mri->ystart*mri->ysize, mri->yend*mri->ysize) ;
 440   printf("        zstart: %2.1f, zend: %2.1f\n",
 441          mri->zstart*mri->zsize, mri->zend*mri->zsize) ;
 442   printf("            TR: %2.2f msec, TE: %2.2f msec, TI: %2.2f msec, "
 443          "flip angle: %2.2f degrees\n",
 444          mri->tr, mri->te, mri->ti, DEGREES(mri->flip_angle)) ;
 445   printf("       nframes: %d\n", mri->nframes) ;
 446   printf("ras xform %spresent\n", mri->ras_good_flag ? "" : "not ") ;
 447   printf("    xform info: x_r = %8.4f, y_r = %8.4f, z_r = %8.4f, "
 448          "c_r = %10.4f\n",
 449          mri->x_r, mri->y_r, mri->z_r, mri->c_r);
 450   printf("              : x_a = %8.4f, y_a = %8.4f, z_a = %8.4f, "
 451          "c_a = %10.4f\n",
 452          mri->x_a, mri->y_a, mri->z_a, mri->c_a);
 453   printf("              : x_s = %8.4f, y_s = %8.4f, z_s = %8.4f, "
 454          "c_s = %10.4f\n",
 455          mri->x_s, mri->y_s, mri->z_s, mri->c_s);
 456 
 457   if (fio_IsDirectory(fname))
 458     printf("\ntalairach xfm : %s\n", mri->transform_fname);
 459   else
 460     {
 461       char *ext = 0;
 462       ext = fio_extension(fname);
 463       if (ext)
 464         {
 465           if (strcmp(ext, "mgz") == 0 || strcmp(ext, "mgh")==0)
 466             printf("\ntalairach xfm : %s\n", mri->transform_fname);
 467           free(ext);
 468         }
 469     }
 470   MRIdircosToOrientationString(mri,ostr);
 471   printf("Orientation   : %s\n",ostr);
 472   printf("Primary Slice Direction: %s\n",MRIsliceDirectionName(mri));
 473   m = MRIgetVoxelToRasXform(mri) ; // extract_i_to_r(mri) (just macto)
 474   printf("\nvoxel to ras transform:\n") ; PrettyMatrixPrint(m) ;
 475   printf("\nvoxel-to-ras determinant %g\n",MatrixDeterminant(m));
 476   MatrixFree(&m) ;
 477   m = extract_r_to_i(mri);
 478   printf("\nras to voxel transform:\n"); PrettyMatrixPrint(m);
 479   MatrixFree(&m);
 480   MRIfree(&mri);
 481 
 482   return;
 483 
 484 } /* end do_file */
 485 

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.