Attachment 'mri_convert.c'

Download

   1 //
   2 // mri_convert.c
   3 // original: written by Bruce Fischl (Apr 16, 1997)
   4 //
   5 // Warning: Do not edit the following four lines.  CVS maintains them.
   6 // Revision Author: $Author: greve $
   7 // Revision Date  : $Date: 2006/01/15 05:26:50 $
   8 // Revision       : $Revision: 1.116 $
   9 
  10 #include <stdio.h>
  11 #include <stdlib.h>
  12 #include <unistd.h>
  13 #include <string.h>
  14 #include <errno.h>
  15 #include "mri.h"
  16 #include "fmriutils.h"
  17 #include "error.h"
  18 #include "mri_identify.h"
  19 #include "gcamorph.h"
  20 #include "DICOMRead.h"
  21 #include "unwarpGradientNonlinearity.h"
  22 #include "version.h"
  23 
  24 /* ----- determines tolerance of non-orthogonal basis vectors ----- */
  25 #define CLOSE_ENOUGH  (5e-3)
  26 
  27 void get_ints(int argc, char *argv[], int *pos, int *vals, int nvals);
  28 void get_floats(int argc, char *argv[], int *pos, float *vals, int nvals);
  29 void get_string(int argc, char *argv[], int *pos, char *val);
  30 void usage_message(FILE *stream);
  31 void usage(FILE *stream);
  32 float findMinSize(MRI *mri, int *conform_width);
  33 int   findRightSize(MRI *mri, float conform_size);
  34 
  35 int debug=0;
  36 
  37 extern int errno;
  38 
  39 char *Progname;
  40 
  41 int main(int argc, char *argv[])
  42 {
  43   int nargs = 0;
  44   MRI *mri_unwarped;
  45   MRI *mri, *mri2, *template, *mri_in_like;
  46   int i;
  47   int reorder_vals[3];
  48   float invert_val;
  49   int in_info_flag, out_info_flag;
  50   int template_info_flag;
  51   int nochange_flag ;
  52   int conform_flag;
  53   int conform_min;  // conform to the smallest dimension
  54   int conform_width;
  55   int parse_only_flag;
  56   int reorder_flag;
  57   int in_stats_flag, out_stats_flag;
  58   int read_only_flag, no_write_flag;
  59   char in_name[STRLEN], out_name[STRLEN];
  60   int in_volume_type, out_volume_type;
  61   char resample_type[STRLEN];
  62   int resample_type_val;
  63   int in_i_size_flag, in_j_size_flag, in_k_size_flag, crop_flag = FALSE;
  64   int out_i_size_flag, out_j_size_flag, out_k_size_flag;
  65   float in_i_size, in_j_size, in_k_size;
  66   float out_i_size, out_j_size, out_k_size;
  67   int crop_center[3], sizes_good_flag, crop_size[3] ;
  68   float in_i_directions[3], in_j_directions[3], in_k_directions[3];
  69   float out_i_directions[3], out_j_directions[3], out_k_directions[3];
  70   int in_i_direction_flag, in_j_direction_flag, in_k_direction_flag;
  71   int out_i_direction_flag, out_j_direction_flag, out_k_direction_flag;
  72   int  in_orientation_flag = FALSE;
  73   char in_orientation_string[STRLEN];
  74   int  out_orientation_flag = FALSE;
  75   char out_orientation_string[STRLEN];
  76   char *errmsg = NULL;
  77   int in_tr_flag = 0;
  78   float in_tr = 0;
  79   int in_ti_flag = 0;
  80   float in_ti = 0;
  81   int in_te_flag = 0;
  82   float in_te = 0;
  83   int in_flip_angle_flag = 0;
  84   float in_flip_angle = 0;
  85   float magnitude;
  86   float i_dot_j, i_dot_k, j_dot_k;
  87   float in_center[3], out_center[3];
  88   int in_center_flag, out_center_flag;
  89   int out_data_type;
  90   char out_data_type_string[STRLEN];
  91   int out_n_i, out_n_j, out_n_k;
  92   int out_n_i_flag, out_n_j_flag, out_n_k_flag;
  93   float fov_x, fov_y, fov_z;
  94   int force_in_type_flag, force_out_type_flag;
  95   int forced_in_type, forced_out_type;
  96   char in_type_string[STRLEN], out_type_string[STRLEN];
  97   char subject_name[STRLEN];
  98   int force_template_type_flag;
  99   int forced_template_type;
 100   char template_type_string[STRLEN];
 101   char reslice_like_name[STRLEN];
 102   int reslice_like_flag;
 103   int frame_flag;
 104   int frame;
 105   char in_name_only[STRLEN];
 106   char transform_fname[STRLEN];
 107   int transform_flag, invert_transform_flag;
 108   LTA *lta_transform;
 109   MRI *mri_transformed = NULL;
 110   int transform_type;
 111   MATRIX *inverse_transform_matrix;
 112   int smooth_parcellation_flag, smooth_parcellation_count;
 113   int in_like_flag;
 114   char in_like_name[STRLEN];
 115   char out_like_name[STRLEN];
 116   int out_like_flag=FALSE;
 117   int in_n_i, in_n_j, in_n_k;
 118   int in_n_i_flag, in_n_j_flag, in_n_k_flag;
 119   int fill_parcellation_flag;
 120   int read_parcellation_volume_flag;
 121   int zero_outlines_flag;
 122   int read_otl_flags;
 123   int color_file_flag;
 124   char color_file_name[STRLEN];
 125   int no_scale_flag;
 126   int temp_type;
 127   int roi_flag;
 128   FILE *fptmp;
 129   int j,translate_labels_flag;
 130   int force_ras_good = FALSE;
 131   char gdf_image_stem[STRLEN];
 132   int in_matrix_flag, out_matrix_flag;
 133   float conform_size;
 134   int zero_ge_z_offset_flag = FALSE; //E/
 135   int nskip = 0; // number of frames to skip from start
 136   int ndrop = 0; // number of frames to skip from end
 137   VOL_GEOM vgtmp;
 138   LT *lt = 0;
 139   int DevXFM = 0;
 140   char devxfm_subject[STRLEN];
 141   MATRIX *T;
 142   float scale_factor ;
 143   int nthframe=-1; 
 144   char cmdline[STRLEN] ;
 145         
 146   make_cmd_version_string 
 147     (argc, argv, 
 148     "$Id: mri_convert.c,v 1.116 2006/01/15 05:26:50 greve Exp $", "$Name:  $",
 149      cmdline);
 150 
 151   for(i=0;i<argc;i++) printf("%s ",argv[i]);
 152   printf("\n");
 153   fflush(stdout);
 154 
 155   crop_size[0] = crop_size[1] = crop_size[2] = 256 ;
 156   crop_center[0] = crop_center[1] = crop_center[2] = 128 ;
 157   for(i=0;i<argc;i++){
 158     if(strcmp(argv[i],"--debug")==0){
 159       fptmp = fopen("debug.gdb","w");
 160       fprintf(fptmp,"# source this file in gdb to debug\n");
 161       fprintf(fptmp,"file %s \n",argv[0]);
 162       fprintf(fptmp,"run ");
 163       for(j=1;j<argc;j++){
 164         if(strcmp(argv[j],"--debug")!=0)
 165           fprintf(fptmp,"%s ",argv[j]);
 166       }
 167       fprintf(fptmp,"\n");
 168       fclose(fptmp);
 169       break;
 170     }
 171   }
 172 
 173   /* ----- keep the compiler quiet ----- */
 174   mri2 = NULL;
 175   forced_in_type = forced_out_type = 
 176     forced_template_type = MRI_VOLUME_TYPE_UNKNOWN;
 177   invert_transform_flag = FALSE;
 178 
 179   /* ----- get the program name ----- */
 180   Progname = strrchr(argv[0], '/');
 181   Progname = (Progname == NULL ? argv[0] : Progname + 1);
 182 
 183   /* ----- pass the command line to mriio ----- */
 184   mriio_command_line(argc, argv);
 185 
 186   /* ----- catch no arguments here ----- */
 187   if(argc == 1)
 188     {
 189       usage(stdout);
 190       exit(1);
 191     }
 192 
 193   /* ----- initialize values ----- */
 194   scale_factor = 1 ;
 195   in_name[0] = '\0';
 196   out_name[0] = '\0';
 197   invert_val = -1.0;
 198   in_info_flag = FALSE;
 199   out_info_flag = FALSE;
 200   conform_flag = FALSE; // TRUE;
 201   nochange_flag = FALSE ;
 202   parse_only_flag = FALSE;
 203   reorder_flag = FALSE;
 204   in_stats_flag = FALSE;
 205   out_stats_flag = FALSE;
 206   read_only_flag = FALSE;
 207   no_write_flag = FALSE;
 208   resample_type_val = SAMPLE_TRILINEAR;
 209   in_i_size_flag = in_j_size_flag = in_k_size_flag = FALSE;
 210   out_i_size_flag = out_j_size_flag = out_k_size_flag = FALSE;
 211   in_i_direction_flag = in_j_direction_flag = in_k_direction_flag = FALSE;
 212   out_i_direction_flag = out_j_direction_flag = out_k_direction_flag = FALSE;
 213   in_center_flag = FALSE;
 214   out_center_flag = FALSE;
 215   out_data_type = -1;
 216   out_n_i_flag = out_n_j_flag = out_n_k_flag = FALSE;
 217   template_info_flag = FALSE;
 218   out_volume_type = MRI_VOLUME_TYPE_UNKNOWN;
 219   force_in_type_flag = force_out_type_flag = force_template_type_flag = FALSE;
 220   subject_name[0] = '\0';
 221   reslice_like_flag = FALSE;
 222   frame_flag = FALSE;
 223   transform_flag = FALSE;
 224   smooth_parcellation_flag = FALSE;
 225   in_like_flag = FALSE;
 226   in_n_i_flag = in_n_j_flag = in_n_k_flag = FALSE;
 227   fill_parcellation_flag = FALSE;
 228   zero_outlines_flag = FALSE;
 229   color_file_flag = FALSE;
 230   no_scale_flag = FALSE;
 231   roi_flag = FALSE;
 232   translate_labels_flag = TRUE;
 233   gdf_image_stem[0] = '\0';
 234   in_matrix_flag = FALSE;
 235   out_matrix_flag = FALSE;
 236   conform_min = FALSE;
 237   conform_size = 1.0;
 238   nskip = 0;
 239   ndrop = 0;
 240 
 241   /* rkt: check for and handle version tag */
 242   nargs = 
 243     handle_version_option 
 244     (
 245      argc, argv, 
 246      "$Id: mri_convert.c,v 1.116 2006/01/15 05:26:50 greve Exp $", "$Name:  $"
 247      );
 248   if (nargs && argc - nargs == 1)
 249     exit (0);
 250   argc -= nargs;
 251 
 252   for(i = 1;i < argc;i++)
 253     {
 254       if(strcmp(argv[i], "-version2") == 0)
 255         exit(97);
 256       if(strcmp(argv[i], "-r") == 0 || strcmp(argv[i], "--reorder") == 0)
 257         {
 258           get_ints(argc, argv, &i, reorder_vals, 3);
 259           reorder_flag = TRUE;
 260         }
 261       else if(strcmp(argv[i], "--debug") == 0) debug = 1;
 262       else if(strcmp(argv[i], "--invert_contrast") == 0)
 263         get_floats(argc, argv, &i, &invert_val, 1);
 264       else if(strcmp(argv[i], "-i") == 0 || 
 265               strcmp(argv[i], "--input_volume") == 0)
 266         get_string(argc, argv, &i, in_name);
 267       else if(strcmp(argv[i], "-o") == 0 || 
 268               strcmp(argv[i], "--output_volume") == 0)
 269         get_string(argc, argv, &i, out_name);
 270       else if (strcmp(argv[i], "-c") == 0 || 
 271                strcmp(argv[i], "--conform") == 0)
 272         conform_flag = TRUE;
 273       else if (strcmp(argv[i], "-nc") == 0 ||
 274                strcmp(argv[i], "--nochange") == 0)
 275         nochange_flag = TRUE;
 276       else if (strcmp(argv[i], "-cm") == 0 || 
 277                strcmp(argv[i], "--conform_min") == 0)
 278         {
 279           conform_min = TRUE;
 280           conform_flag = TRUE;
 281         }
 282       else if (strcmp(argv[i], "-cs") == 0 
 283                || strcmp(argv[i], "--conform_size") == 0)
 284         {
 285           get_floats(argc, argv, &i, &conform_size, 1);
 286           conform_flag = TRUE;
 287         }
 288       else if(strcmp(argv[i], "-po") == 0 || 
 289               strcmp(argv[i], "--parse_only") == 0)
 290         parse_only_flag = TRUE;
 291       else if(strcmp(argv[i], "-ii") == 0 ||
 292               strcmp(argv[i], "--in_info") == 0)
 293         in_info_flag = TRUE;
 294       else if(strcmp(argv[i], "-oi") == 0 || 
 295               strcmp(argv[i], "--out_info") == 0)
 296         out_info_flag = TRUE;
 297       else if(strcmp(argv[i], "-ti") == 0 
 298               || strcmp(argv[i], "--template_info") == 0)
 299         template_info_flag = TRUE;
 300       else if(strcmp(argv[i], "-is") == 0 ||
 301               strcmp(argv[i], "--in_stats") == 0)
 302         in_stats_flag = TRUE;
 303       else if(strcmp(argv[i], "-os") == 0 ||
 304               strcmp(argv[i], "--out_stats") == 0)
 305         out_stats_flag = TRUE;
 306       else if(strcmp(argv[i], "-ro") == 0 ||
 307               strcmp(argv[i], "--read_only") == 0)
 308         read_only_flag = TRUE;
 309       else if(strcmp(argv[i], "-nw") == 0 ||
 310               strcmp(argv[i], "--no_write") == 0)
 311         no_write_flag = TRUE;
 312       else if(strcmp(argv[i], "-im") == 0 ||
 313               strcmp(argv[i], "--in_matrix") == 0)
 314         in_matrix_flag = TRUE;
 315       else if(strcmp(argv[i], "-om") == 0 ||
 316               strcmp(argv[i], "--out_matrix") == 0)
 317         out_matrix_flag = TRUE;
 318       else if(strcmp(argv[i], "--force_ras_good") == 0) 
 319         force_ras_good = TRUE;
 320       // transform related things here /////////////////////
 321       else if(strcmp(argv[i], "-at") == 0 || 
 322               strcmp(argv[i], "--apply_transform") == 0 || 
 323               strcmp(argv[i], "-T") == 0)
 324         {
 325           get_string(argc, argv, &i, transform_fname);
 326           transform_flag = TRUE;
 327           invert_transform_flag = FALSE;
 328         }
 329       else if (strcmp(argv[i], "--like")==0)
 330         {
 331           get_string(argc, argv, &i, out_like_name);
 332           out_like_flag = TRUE;
 333         }
 334       else if (strcmp(argv[i], "--crop")==0)
 335         {
 336           crop_flag = TRUE ;
 337           get_ints(argc, argv, &i, crop_center, 3);
 338         }
 339       else if (strcmp(argv[i], "--cropsize")==0)
 340         {
 341           crop_flag = TRUE ;
 342           get_ints(argc, argv, &i, crop_size, 3);
 343         }
 344       else if(strcmp(argv[i], "--devolvexfm") == 0){
 345         /* devolve xfm to account for cras != 0 */
 346         get_string(argc, argv, &i, devxfm_subject);
 347         DevXFM = 1;
 348       }
 349       else if(strcmp(argv[i], "-ait") == 0 || 
 350               strcmp(argv[i], "--apply_inverse_transform") == 0){
 351         get_string(argc, argv, &i, transform_fname);
 352         transform_flag = TRUE;
 353         invert_transform_flag = TRUE;
 354       }
 355       ///////////////////////////////////////////////////////////
 356       else if(strcmp(argv[i], "-iis") == 0 || 
 357               strcmp(argv[i], "--in_i_size") == 0)
 358         {
 359           get_floats(argc, argv, &i, &in_i_size, 1);
 360           in_i_size_flag = TRUE;
 361         }
 362       else if(strcmp(argv[i], "-ijs") == 0 || 
 363               strcmp(argv[i], "--in_j_size") == 0)
 364         {
 365           get_floats(argc, argv, &i, &in_j_size, 1);
 366           in_j_size_flag = TRUE;
 367         }
 368       else if(strcmp(argv[i], "-iks") == 0 ||
 369               strcmp(argv[i], "--in_k_size") == 0)
 370         {
 371           get_floats(argc, argv, &i, &in_k_size, 1);
 372           in_k_size_flag = TRUE;
 373         }
 374       else if(strcmp(argv[i], "-ois") == 0 ||
 375               strcmp(argv[i], "--out_i_size") == 0)
 376         {
 377           get_floats(argc, argv, &i, &out_i_size, 1);
 378           out_i_size_flag = TRUE;
 379         }
 380       else if(strcmp(argv[i], "-ojs") == 0 ||
 381               strcmp(argv[i], "--out_j_size") == 0)
 382         {
 383           get_floats(argc, argv, &i, &out_j_size, 1);
 384           out_j_size_flag = TRUE;
 385         }
 386       else if(strcmp(argv[i], "-oks") == 0 ||
 387               strcmp(argv[i], "--out_k_size") == 0)
 388         {
 389           get_floats(argc, argv, &i, &out_k_size, 1);
 390           out_k_size_flag = TRUE;
 391         }
 392       else if(strcmp(argv[i], "-iid") == 0 
 393               || strcmp(argv[i], "--in_i_direction") == 0)
 394         {
 395           get_floats(argc, argv, &i, in_i_directions, 3);
 396           magnitude = sqrt(in_i_directions[0]*in_i_directions[0] + 
 397                            in_i_directions[1]*in_i_directions[1] + 
 398                            in_i_directions[2]*in_i_directions[2]);
 399           if(magnitude == 0.0)
 400             {
 401               fprintf(stderr, "\n%s: directions must have non-zero magnitude; "
 402                       "in_i_direction = (%g, %g, %g)\n", 
 403                       Progname, 
 404                       in_i_directions[0], 
 405                       in_i_directions[1], 
 406                       in_i_directions[2]);
 407               usage_message(stdout);
 408               exit(1);
 409             }
 410           if(magnitude != 1.0)
 411             {
 412               printf("normalizing in_i_direction: (%g, %g, %g) -> ", 
 413                      in_i_directions[0], 
 414                      in_i_directions[1], 
 415                      in_i_directions[2]);
 416               in_i_directions[0] = in_i_directions[0] / magnitude;
 417               in_i_directions[1] = in_i_directions[1] / magnitude;
 418               in_i_directions[2] = in_i_directions[2] / magnitude;
 419               printf("(%g, %g, %g)\n", 
 420                      in_i_directions[0], 
 421                      in_i_directions[1], 
 422                      in_i_directions[2]);
 423             }
 424           in_i_direction_flag = TRUE;
 425         }
 426       else if(strcmp(argv[i], "-ijd") == 0 
 427               || strcmp(argv[i], "--in_j_direction") == 0)
 428         {
 429           get_floats(argc, argv, &i, in_j_directions, 3);
 430           magnitude = sqrt(in_j_directions[0]*in_j_directions[0] + 
 431                            in_j_directions[1]*in_j_directions[1] + 
 432                            in_j_directions[2]*in_j_directions[2]);
 433           if(magnitude == 0.0)
 434             {
 435               fprintf(stderr, "\n%s: directions must have non-zero magnitude; "
 436                       "in_j_direction = (%g, %g, %g)\n", 
 437                       Progname, 
 438                       in_j_directions[0], 
 439                       in_j_directions[1], 
 440                       in_j_directions[2]);
 441               usage_message(stdout);
 442               exit(1);
 443             }
 444           if(magnitude != 1.0)
 445             {
 446               printf("normalizing in_j_direction: (%g, %g, %g) -> ", 
 447                      in_j_directions[0], 
 448                      in_j_directions[1], 
 449                      in_j_directions[2]);
 450               in_j_directions[0] = in_j_directions[0] / magnitude;
 451               in_j_directions[1] = in_j_directions[1] / magnitude;
 452               in_j_directions[2] = in_j_directions[2] / magnitude;
 453               printf("(%g, %g, %g)\n", 
 454                      in_j_directions[0],
 455                      in_j_directions[1], 
 456                      in_j_directions[2]);
 457             }
 458           in_j_direction_flag = TRUE;
 459         }
 460       else if(strcmp(argv[i], "-ikd") == 0 || 
 461               strcmp(argv[i], "--in_k_direction") == 0)
 462         {
 463           get_floats(argc, argv, &i, in_k_directions, 3);
 464           magnitude = sqrt(in_k_directions[0]*in_k_directions[0] + 
 465                            in_k_directions[1]*in_k_directions[1] + 
 466                            in_k_directions[2]*in_k_directions[2]);
 467           if(magnitude == 0.0)
 468             {
 469               fprintf(stderr, "\n%s: directions must have non-zero magnitude; "
 470                       "in_k_direction = (%g, %g, %g)\n", 
 471                       Progname, 
 472                       in_k_directions[0], 
 473                       in_k_directions[1],
 474                       in_k_directions[2]);
 475               usage_message(stdout);
 476               exit(1);
 477             }
 478           if(magnitude != 1.0)
 479             {
 480               printf("normalizing in_k_direction: (%g, %g, %g) -> ", 
 481                      in_k_directions[0], 
 482                      in_k_directions[1],
 483                      in_k_directions[2]);
 484               in_k_directions[0] = in_k_directions[0] / magnitude;
 485               in_k_directions[1] = in_k_directions[1] / magnitude;
 486               in_k_directions[2] = in_k_directions[2] / magnitude;
 487               printf("(%g, %g, %g)\n", 
 488                      in_k_directions[0], 
 489                      in_k_directions[1], 
 490                      in_k_directions[2]);
 491             }
 492           in_k_direction_flag = TRUE;
 493         }
 494 
 495       else if(strcmp(argv[i], "--in_orientation") == 0)
 496         {
 497           get_string(argc, argv, &i, in_orientation_string);
 498           errmsg = MRIcheckOrientationString(in_orientation_string);
 499           if(errmsg){
 500             printf("ERROR: with in orientation string %s\n",
 501                    in_orientation_string);
 502             printf("%s\n",errmsg);
 503             exit(1);
 504           }
 505           in_orientation_flag = TRUE;
 506         }
 507 
 508       else if(strcmp(argv[i], "--out_orientation") == 0)
 509         {
 510           get_string(argc, argv, &i, out_orientation_string);
 511           errmsg = MRIcheckOrientationString(out_orientation_string);
 512           if(errmsg){
 513             printf("ERROR: with out_orientation string %s\n",
 514                    out_orientation_string);
 515             printf("%s\n",errmsg);
 516             exit(1);
 517           }
 518           out_orientation_flag = TRUE;
 519         }
 520 
 521       else if(strcmp(argv[i], "-oid") == 0 || 
 522               strcmp(argv[i], "--out_i_direction") == 0)
 523         {
 524           get_floats(argc, argv, &i, out_i_directions, 3);
 525           magnitude = sqrt(out_i_directions[0]*out_i_directions[0] + 
 526                            out_i_directions[1]*out_i_directions[1] + 
 527                            out_i_directions[2]*out_i_directions[2]);
 528           if(magnitude == 0.0)
 529             {
 530               fprintf(stderr, "\n%s: directions must have non-zero magnitude; "
 531                       "out_i_direction = (%g, %g, %g)\n", Progname, 
 532                       out_i_directions[0], 
 533                       out_i_directions[1], 
 534                       out_i_directions[2]);
 535               usage_message(stdout);
 536               exit(1);
 537             }
 538           if(magnitude != 1.0)
 539             {
 540               printf("normalizing out_i_direction: (%g, %g, %g) -> ", 
 541                      out_i_directions[0], 
 542                      out_i_directions[1],
 543                      out_i_directions[2]);
 544               out_i_directions[0] = out_i_directions[0] / magnitude;
 545               out_i_directions[1] = out_i_directions[1] / magnitude;
 546               out_i_directions[2] = out_i_directions[2] / magnitude;
 547               printf("(%g, %g, %g)\n", 
 548                      out_i_directions[0], 
 549                      out_i_directions[1], 
 550                      out_i_directions[2]);
 551             }
 552           out_i_direction_flag = TRUE;
 553         }
 554       else if(strcmp(argv[i], "-ojd") == 0 || 
 555               strcmp(argv[i], "--out_j_direction") == 0)
 556         {
 557           get_floats(argc, argv, &i, out_j_directions, 3);
 558           magnitude = sqrt(out_j_directions[0]*out_j_directions[0] + 
 559                            out_j_directions[1]*out_j_directions[1] + 
 560                            out_j_directions[2]*out_j_directions[2]);
 561           if(magnitude == 0.0)
 562             {
 563               fprintf(stderr, "\n%s: directions must have non-zero magnitude; "
 564                       "out_j_direction = (%g, %g, %g)\n", 
 565                       Progname, 
 566                       out_j_directions[0], 
 567                       out_j_directions[1], 
 568                       out_j_directions[2]);
 569               usage_message(stdout);
 570               exit(1);
 571             }
 572           if(magnitude != 1.0)
 573             {
 574               printf("normalizing out_j_direction: (%g, %g, %g) -> ", 
 575                      out_j_directions[0], 
 576                      out_j_directions[1], 
 577                      out_j_directions[2]);
 578               out_j_directions[0] = out_j_directions[0] / magnitude;
 579               out_j_directions[1] = out_j_directions[1] / magnitude;
 580               out_j_directions[2] = out_j_directions[2] / magnitude;
 581               printf("(%g, %g, %g)\n", 
 582                      out_j_directions[0],
 583                      out_j_directions[1],
 584                      out_j_directions[2]);
 585             }
 586           out_j_direction_flag = TRUE;
 587         }
 588       else if(strcmp(argv[i], "-okd") == 0 || 
 589               strcmp(argv[i], "--out_k_direction") == 0)
 590         {
 591           get_floats(argc, argv, &i, out_k_directions, 3);
 592           magnitude = sqrt(out_k_directions[0]*out_k_directions[0] + 
 593                            out_k_directions[1]*out_k_directions[1] + 
 594                            out_k_directions[2]*out_k_directions[2]);
 595           if(magnitude == 0.0)
 596             {
 597               fprintf(stderr, "\n%s: directions must have non-zero magnitude; "
 598                       "out_k_direction = (%g, %g, %g)\n", 
 599                       Progname, 
 600                       out_k_directions[0], 
 601                       out_k_directions[1], 
 602                       out_k_directions[2]);
 603               usage_message(stdout);
 604               exit(1);
 605             }
 606           if(magnitude != 1.0)
 607             {
 608               printf("normalizing out_k_direction: (%g, %g, %g) -> ", 
 609                      out_k_directions[0],
 610                      out_k_directions[1],
 611                      out_k_directions[2]);
 612               out_k_directions[0] = out_k_directions[0] / magnitude;
 613               out_k_directions[1] = out_k_directions[1] / magnitude;
 614               out_k_directions[2] = out_k_directions[2] / magnitude;
 615               printf("(%g, %g, %g)\n", 
 616                      out_k_directions[0], 
 617                      out_k_directions[1],
 618                      out_k_directions[2]);
 619             }
 620           out_k_direction_flag = TRUE;
 621         }
 622       else if(strcmp(argv[i], "-ic") == 0 ||
 623               strcmp(argv[i], "--in_center") == 0)
 624         {
 625           get_floats(argc, argv, &i, in_center, 3);
 626           in_center_flag = TRUE;
 627         }
 628       else if(strcmp(argv[i], "-oc") == 0 || 
 629               strcmp(argv[i], "--out_center") == 0)
 630         {
 631           get_floats(argc, argv, &i, out_center, 3);
 632           out_center_flag = TRUE;
 633         }
 634       else if(strcmp(argv[i], "-oni") == 0 || 
 635               strcmp(argv[i], "-oic") == 0 || 
 636               strcmp(argv[i], "--out_i_count") == 0)
 637         {
 638           get_ints(argc, argv, &i, &out_n_i, 1);
 639           out_n_i_flag = TRUE;
 640         }
 641       else if(strcmp(argv[i], "-onj") == 0 || 
 642               strcmp(argv[i], "-ojc") == 0 || 
 643               strcmp(argv[i], "--out_j_count") == 0)
 644         {
 645           get_ints(argc, argv, &i, &out_n_j, 1);
 646           out_n_j_flag = TRUE;
 647         }
 648       else if(strcmp(argv[i], "-onk") == 0 || 
 649               strcmp(argv[i], "-okc") == 0 || 
 650               strcmp(argv[i], "--out_k_count") == 0)
 651         {
 652           get_ints(argc, argv, &i, &out_n_k, 1);
 653           out_n_k_flag = TRUE;
 654         }
 655       else if(strcmp(argv[i], "-ini") == 0 || 
 656               strcmp(argv[i], "-iic") == 0 || 
 657               strcmp(argv[i], "--in_i_count") == 0)
 658         {
 659           get_ints(argc, argv, &i, &in_n_i, 1);
 660           in_n_i_flag = TRUE;
 661         }
 662       else if(strcmp(argv[i], "-inj") == 0 || 
 663               strcmp(argv[i], "-ijc") == 0 || 
 664               strcmp(argv[i], "--in_j_count") == 0)
 665         {
 666           get_ints(argc, argv, &i, &in_n_j, 1);
 667           in_n_j_flag = TRUE;
 668         }
 669       else if(strcmp(argv[i], "-ink") == 0 || 
 670               strcmp(argv[i], "-ikc") == 0 || 
 671               strcmp(argv[i], "--in_k_count") == 0)
 672         {
 673           get_ints(argc, argv, &i, &in_n_k, 1);
 674           in_n_k_flag = TRUE;
 675         }
 676       else if( strcmp(argv[i], "-tr") == 0 )
 677         {
 678           get_floats(argc, argv, &i, &in_tr, 1);
 679           in_tr_flag = TRUE;
 680         }
 681       else if( strcmp(argv[i], "-TI") == 0 )
 682         {
 683           get_floats(argc, argv, &i, &in_ti, 1);
 684           in_ti_flag = TRUE;
 685         }
 686       else if( strcmp(argv[i], "-te") == 0 )
 687         {
 688           get_floats(argc, argv, &i, &in_te, 1);
 689           in_te_flag = TRUE;
 690         }
 691       else if( strcmp(argv[i], "-flip_angle") == 0 )
 692         {
 693           get_floats(argc, argv, &i, &in_flip_angle, 1);
 694           in_flip_angle_flag = TRUE;
 695         }
 696 
 697       else if(strcmp(argv[i], "-odt") == 0 || 
 698               strcmp(argv[i], "--out_data_type") == 0)
 699         {
 700           get_string(argc, argv, &i, out_data_type_string);
 701           if(strcmp(StrLower(out_data_type_string), "uchar") == 0)
 702             out_data_type = MRI_UCHAR;
 703           else if(strcmp(StrLower(out_data_type_string), "short") == 0)
 704             out_data_type = MRI_SHORT;
 705           else if(strcmp(StrLower(out_data_type_string), "int") == 0)
 706             out_data_type = MRI_INT;
 707           else if(strcmp(StrLower(out_data_type_string), "float") == 0)
 708             out_data_type = MRI_FLOAT;
 709           else
 710             {
 711               fprintf(stderr, "\n%s: unknown data type \"%s\"\n", 
 712                       Progname, argv[i]);
 713               usage_message(stdout);
 714               exit(1);
 715             }
 716         }
 717       else if(strcmp(argv[i], "-sc") == 0 || strcmp(argv[i], "--scale") == 0)
 718         {
 719           scale_factor = atof(argv[i+1]) ;
 720           i++ ;
 721         }
 722       else if(strcmp(argv[i], "-rt") == 0 || 
 723               strcmp(argv[i], "--resample_type") == 0)
 724         {
 725           get_string(argc, argv, &i, resample_type);
 726           if(strcmp(StrLower(resample_type), "interpolate") == 0)
 727             resample_type_val = SAMPLE_TRILINEAR;
 728           else if(strcmp(StrLower(resample_type), "nearest") == 0)
 729             resample_type_val = SAMPLE_NEAREST;
 730           else if(strcmp(StrLower(resample_type), "weighted") == 0)
 731             resample_type_val = SAMPLE_WEIGHTED;
 732           else if(strcmp(StrLower(resample_type), "sinc") == 0)
 733             resample_type_val = SAMPLE_SINC;
 734           else if(strcmp(StrLower(resample_type), "cubic") == 0)
 735             resample_type_val = SAMPLE_CUBIC;
 736           else
 737             {
 738               fprintf(stderr, "\n%s: unknown resample type \"%s\"\n", 
 739                       Progname, argv[i]);
 740               usage_message(stdout);
 741               exit(1);
 742             }
 743         }
 744       else if(strcmp(argv[i], "-it") == 0 || strcmp(argv[i], "--in_type") == 0)
 745         {
 746           get_string(argc, argv, &i, in_type_string);
 747           forced_in_type = string_to_type(in_type_string);
 748           force_in_type_flag = TRUE;
 749         }
 750       else if(strcmp(argv[i], "-ot") == 0 ||
 751               strcmp(argv[i], "--out_type") == 0)
 752         {
 753           get_string(argc, argv, &i, out_type_string);
 754           forced_out_type = string_to_type(out_type_string);
 755           /* see mri_identify.c */
 756           force_out_type_flag = TRUE;
 757         }
 758       else if(strcmp(argv[i], "-tt") == 0 || 
 759               strcmp(argv[i], "--template_type") == 0)
 760         {
 761           get_string(argc, argv, &i, template_type_string);
 762           forced_template_type = string_to_type(template_type_string);
 763           force_template_type_flag = TRUE;
 764         }
 765       else if(strcmp(argv[i], "-sn") == 0 || 
 766               strcmp(argv[i], "--subject_name") == 0)
 767         {
 768           get_string(argc, argv, &i, subject_name);
 769         }
 770       else if(strcmp(argv[i], "-gis") == 0 || 
 771               strcmp(argv[i], "--gdf_image_stem") == 0)
 772         {
 773           get_string(argc, argv, &i, gdf_image_stem);
 774           if(strlen(gdf_image_stem) == 0)
 775             {
 776               fprintf(stderr, "\n%s: zero length GDF image stem given\n", 
 777                       Progname);
 778               usage_message(stdout);
 779               exit(1);
 780             }
 781         }
 782       else if(strcmp(argv[i], "-rl") == 0 || 
 783               strcmp(argv[i], "--reslice_like") == 0)
 784         {
 785           get_string(argc, argv, &i, reslice_like_name);
 786           reslice_like_flag = TRUE;
 787         }
 788       else if(strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--frame") == 0)
 789         {
 790           get_ints(argc, argv, &i, &frame, 1);
 791           frame_flag = TRUE;
 792         }
 793       else if(strcmp(argv[i], "-il") == 0 || strcmp(argv[i], "--in_like") == 0)
 794         {
 795           get_string(argc, argv, &i, in_like_name);
 796           in_like_flag = TRUE;
 797         }
 798       else if(strcmp(argv[i], "-roi") == 0 || strcmp(argv[i], "--roi") == 0)
 799         {
 800           roi_flag = TRUE;
 801         }
 802       else if(strcmp(argv[i], "-fp") == 0 || 
 803               strcmp(argv[i], "--fill_parcellation") == 0)
 804         {
 805           fill_parcellation_flag = TRUE;
 806         }
 807       else if(strcmp(argv[i], "-zo") == 0 || 
 808               strcmp(argv[i], "--zero_outlines") == 0)
 809         {
 810           zero_outlines_flag = TRUE;
 811         }
 812       else if(strcmp(argv[i], "-sp") == 0 || 
 813               strcmp(argv[i], "--smooth_parcellation") == 0)
 814         {
 815           get_ints(argc, argv, &i, &smooth_parcellation_count, 1);
 816           if(smooth_parcellation_count < 14 || smooth_parcellation_count > 26)
 817             {
 818               fprintf(stderr, "\n%s: clean parcellation count must "
 819                       "be between 14 and 26, inclusive\n", Progname);
 820               usage_message(stdout);
 821               exit(1);
 822             }
 823           smooth_parcellation_flag = TRUE;
 824         }
 825       else if(strcmp(argv[i], "-cf") == 0 || 
 826               strcmp(argv[i], "--color_file") == 0)
 827         {
 828           get_string(argc, argv, &i, color_file_name);
 829           color_file_flag = TRUE;
 830         }
 831       else if(strcmp(argv[i], "-nt") == 0 || 
 832               strcmp(argv[i], "--no_translate") == 0)
 833         {
 834           translate_labels_flag = FALSE;
 835         }
 836       else if(strcmp(argv[i], "-ns") == 0 || 
 837               strcmp(argv[i], "--no_scale") == 0)
 838         {
 839           get_ints(argc, argv, &i, &no_scale_flag, 1);
 840           no_scale_flag = (no_scale_flag == 0 ? FALSE : TRUE);
 841         }
 842       else if (strcmp(argv[i], "-nth") == 0 || 
 843                strcmp(argv[i], "--nth_frame") == 0)
 844         {
 845           get_ints(argc, argv, &i, &nthframe, 1);
 846         }
 847       else if(strcmp(argv[i], "--unwarp_gradient_nonlinearity") == 0){
 848         /* !@# start */
 849         unwarp_flag = 1;
 850       
 851         /* Determine gradient type: sonata or allegra */
 852         get_string(argc, argv, &i, unwarp_gradientType);
 853         if( (strcmp(unwarp_gradientType, "sonata")  != 0) &&
 854             (strcmp(unwarp_gradientType, "allegra") != 0) &&
 855             (strcmp(unwarp_gradientType, "GE") != 0) )
 856           {
 857             fprintf(stderr, "\n%s: must specify gradient type "
 858                     "('sonata' or 'allegra' or 'GE')\n", Progname);
 859             usage_message(stdout);
 860             exit(1);
 861           }
 862       
 863         /* Determine whether or not to do a partial unwarp */
 864         get_string(argc, argv, &i, unwarp_partialUnwarp);
 865         if( (strcmp(unwarp_partialUnwarp, "fullUnwarp") != 0) &&
 866             (strcmp(unwarp_partialUnwarp, "through-plane")  != 0) )
 867           {
 868             fprintf(stderr, "\n%s: must specify unwarping type "
 869                     "('fullUnwarp' or 'through-plane')\n", Progname);
 870             usage_message(stdout);
 871             exit(1);
 872           }
 873       
 874         /* Determine whether or not to do jacobian correction */
 875         get_string(argc, argv, &i, unwarp_jacobianCorrection);
 876         if( (strcmp(unwarp_jacobianCorrection, "JacobianCorrection")  != 0) &&
 877             (strcmp(unwarp_jacobianCorrection, "noJacobianCorrection") != 0) )
 878           {
 879             fprintf(stderr, "\n%s: must specify intensity correction type "
 880                     "('JacobianCorrection' or 'noJacobianCorrection')\n",
 881                     Progname);
 882             usage_message(stdout);
 883             exit(1);
 884           }
 885       
 886         /* Determine interpolation type: linear or sinc */
 887         get_string(argc, argv, &i, unwarp_interpType);
 888         if( (strcmp(unwarp_interpType, "linear") != 0) &&
 889             (strcmp(unwarp_interpType, "sinc")   != 0) )
 890           {
 891             fprintf(stderr, "\n%s: must specify interpolation type "
 892                     "('linear' or 'sinc')\n", Progname);
 893             usage_message(stdout);
 894             exit(1);
 895           }
 896 
 897         /* Get the HW for sinc interpolation (if linear interpolation,
 898            this integer value is not used) */
 899         get_ints(argc, argv, &i, &unwarp_sincInterpHW, 1);
 900       
 901         /* OPTIONS THAT THERE ARE NO PLANS TO SUPPORT */
 902         /* Jacobian correction with through-plane only correction */ 
 903         if( (strcmp(unwarp_jacobianCorrection, "JacobianCorrection") == 0) &&
 904             (strcmp(unwarp_partialUnwarp, "through-plane") == 0) )      
 905           {
 906             fprintf(stderr, "\n%s: Jacobian correction not valid for "
 907                     "'through-plane' only unwarping)\n", Progname);
 908             exit(1);
 909           }
 910       
 911         /* OPTIONS NOT CURRENTLY SUPPORTED (BUT W/ PLANS TO SUPPORT) */
 912         /* 1) GE unwarping not supported until we have offset data */
 913         if( strcmp(unwarp_gradientType, "GE") == 0 ){
 914           fprintf(stderr, "\n%s: unwarping data from GE scanners not "
 915                   "supported at present.\n", Progname);
 916           exit(1);
 917         }    
 918       
 919         /* 2) for GE: through-plane correction requires rewarping the
 920            in-plane unwarped image, which requires map inversion */
 921         if( strcmp(unwarp_partialUnwarp, "through-plane") == 0 )      
 922           {
 923             fprintf(stderr, "\n%s: through-plane only unwarping not "
 924                     "supported at present.\n", Progname);
 925             exit(1);
 926           }
 927         /* !@# end */
 928       
 929       }
 930       else if((strcmp(argv[i], "-u") == 0)
 931               || (strcmp(argv[i], "--usage") == 0)
 932               || (strcmp(argv[i], "--help") == 0))
 933         {
 934           usage(stdout);
 935           exit(0);
 936         }
 937       /*-------------------------------------------------------------*/
 938       else if(strcmp(argv[i], "--status") == 0 || 
 939               strcmp(argv[i], "--statusfile") == 0)
 940         {
 941           /* File name to write percent complete for Siemens DICOM */
 942           if( (argc-1) - i < 1 ){
 943             fprintf(stderr,"ERROR: option --statusfile "
 944                     "requires one argument\n");
 945             exit(1);
 946           }
 947           i++;
 948           SDCMStatusFile = (char *) calloc(strlen(argv[i])+1,sizeof(char));
 949           memcpy(SDCMStatusFile,argv[i],strlen(argv[i]));
 950           fptmp = fopen(SDCMStatusFile,"w");
 951           if(fptmp == NULL){
 952             fprintf(stderr,"ERROR: could not open %s for writing\n",
 953                     SDCMStatusFile);
 954             exit(1);
 955           }
 956           fprintf(fptmp,"0\n");
 957           fclose(fptmp);
 958         }
 959       /*-------------------------------------------------------------*/
 960       else if(strcmp(argv[i], "--sdcmlist") == 0)
 961         {
 962           /* File name that contains a list of Siemens DICOM files
 963              that are in the same run as the one listed on the
 964              command-line. If not present, the directory will be scanned,
 965              but this can take a while.
 966           */
 967           if( (argc-1) - i < 1 ){
 968             fprintf(stderr,
 969                     "ERROR: option --sdcmlist requires one argument\n");
 970             exit(1);
 971           }
 972           i++;
 973           SDCMListFile = (char *) calloc(strlen(argv[i])+1,sizeof(char));
 974           memcpy(SDCMListFile,argv[i],strlen(argv[i]));
 975           fptmp = fopen(SDCMListFile,"r");
 976           if(fptmp == NULL){
 977             fprintf(stderr,"ERROR: could not open %s for reading\n",
 978                     SDCMListFile);
 979             exit(1);
 980           }
 981           fclose(fptmp);
 982         }
 983       /*-------------------------------------------------------------*/
 984       else if( (strcmp(argv[i], "--nspmzeropad") == 0) ||
 985                (strcmp(argv[i], "--out_nspmzeropad") == 0))
 986         {
 987           /* Choose the amount of zero padding for spm output files */
 988           if( (argc-1) - i < 1 ){
 989             fprintf(stderr,"ERROR: option --out_nspmzeropad "
 990                     "requires one argument\n");
 991             exit(1);
 992           }
 993           i++;
 994           sscanf(argv[i],"%d",&N_Zero_Pad_Output);
 995         }
 996       /*-------------------------------------------------------------*/
 997       else if( (strcmp(argv[i], "--in_nspmzeropad") == 0))
 998         {
 999           /* Choose the amount of zero padding for spm input files */
1000           if( (argc-1) - i < 1 ){
1001             fprintf(stderr,"ERROR: option --in_nspmzeropad "
1002                     "requires one argument\n");
1003             exit(1);
1004           }
1005           i++;
1006           sscanf(argv[i],"%d",&N_Zero_Pad_Input);
1007         }
1008       /*-----------------------------------------------------*/ //E/
1009       else if(strcmp(argv[i], "-zgez") == 0 || 
1010               strcmp(argv[i], "--zero_ge_z_offset") == 0)
1011         {
1012           zero_ge_z_offset_flag = TRUE;
1013         }
1014       /*-----------------------------------------------------*/ //E/
1015       else if(strcmp(argv[i], "-nozgez") == 0 || 
1016               strcmp(argv[i], "--no_zero_ge_z_offset") == 0)
1017         {
1018           zero_ge_z_offset_flag = FALSE;
1019         }
1020       else if(strcmp(argv[i], "--nskip") == 0 ){
1021         get_ints(argc, argv, &i, &nskip, 1);
1022         printf("nskip = %d\n",nskip);
1023         if(nskip < 0){
1024           printf("ERROR: nskip cannot be negative\n");
1025           exit(1);
1026         }
1027       }
1028       else if(strcmp(argv[i], "--ndrop") == 0 ){
1029         get_ints(argc, argv, &i, &ndrop, 1);
1030         printf("ndrop = %d\n",ndrop);
1031         if(ndrop < 0){
1032           printf("ERROR: ndrop cannot be negative\n");
1033           exit(1);
1034         }
1035       }
1036       /*-------------------------------------------------------------*/
1037       else
1038         {
1039           if(argv[i][0] == '-')
1040             {
1041               fprintf(stderr, 
1042                       "\n%s: unknown flag \"%s\"\n", Progname, argv[i]);
1043               usage_message(stdout);
1044               exit(1);
1045             }
1046           else
1047             {
1048               if(in_name[0] == '\0')
1049                 strcpy(in_name, argv[i]);
1050               else if(out_name[0] == '\0')
1051                 strcpy(out_name, argv[i]);
1052               else
1053                 {
1054                   if(i + 1 == argc)
1055                     fprintf(stderr, "\n%s: extra argument (\"%s\")\n", 
1056                             Progname, argv[i]);
1057                   else
1058                     fprintf(stderr, "\n%s: extra arguments (\"%s\" and "
1059                             "following)\n", Progname, argv[i]);
1060                   usage_message(stdout);
1061                   exit(1);
1062                 }
1063             }
1064 
1065         }
1066     }
1067   /**** Finished parsing command line ****/
1068   /* option inconsistency checks */
1069   if(force_ras_good && (in_i_direction_flag || in_j_direction_flag ||
1070                         in_k_direction_flag)){
1071     fprintf(stderr, "ERROR: cannot use --force_ras_good and "
1072             "--in_?_direction_flag\n");
1073     exit(1);
1074   }
1075   if (conform_flag == FALSE && conform_min == TRUE)
1076     {
1077       fprintf(stderr, "In order to use -cm (--conform_min), "
1078               "you must set -c (--conform)  at the same time.\n");
1079       exit(1);
1080     }
1081 
1082   /* ----- catch zero or negative voxel dimensions ----- */
1083 
1084   sizes_good_flag = TRUE;
1085 
1086   if((in_i_size_flag && in_i_size <= 0.0) || 
1087      (in_j_size_flag && in_j_size <= 0.0) || 
1088      (in_k_size_flag && in_k_size <= 0.0) || 
1089      (out_i_size_flag && out_i_size <= 0.0) || 
1090      (out_j_size_flag && out_j_size <= 0.0) || 
1091      (out_k_size_flag && out_k_size <= 0.0))
1092     {
1093       fprintf(stderr, "\n%s: voxel sizes must be "
1094               "greater than zero\n", Progname);
1095       sizes_good_flag = FALSE;
1096     }
1097 
1098   if(in_i_size_flag && in_i_size <= 0.0)
1099     fprintf(stderr, "in i size = %g\n", in_i_size);
1100   if(in_j_size_flag && in_j_size <= 0.0)
1101     fprintf(stderr, "in j size = %g\n", in_j_size);
1102   if(in_k_size_flag && in_k_size <= 0.0)
1103     fprintf(stderr, "in k size = %g\n", in_k_size);
1104   if(out_i_size_flag && out_i_size <= 0.0)
1105     fprintf(stderr, "out i size = %g\n", out_i_size);
1106   if(out_j_size_flag && out_j_size <= 0.0)
1107     fprintf(stderr, "out j size = %g\n", out_j_size);
1108   if(out_k_size_flag && out_k_size <= 0.0)
1109     fprintf(stderr, "out k size = %g\n", out_k_size);
1110 
1111   if(!sizes_good_flag)
1112     {
1113       usage_message(stdout);
1114       exit(1);
1115     }
1116 
1117   /* ----- catch missing input or output volume name ----- */
1118   if(in_name[0] == '\0')
1119     {
1120       fprintf(stderr, "\n%s: missing input volume name\n", Progname);
1121       usage_message(stdout);
1122       exit(1);
1123     }
1124 
1125   if(out_name[0] == '\0' && !(read_only_flag || no_write_flag))
1126     {
1127       fprintf(stderr, "\n%s: missing output volume name\n", Progname);
1128       usage_message(stdout);
1129       exit(1);
1130     }
1131 
1132   /* ----- copy file name (only -- strip '@' and '#') ----- */
1133   MRIgetVolumeName(in_name, in_name_only);
1134 
1135   /* ----- catch unknown volume types ----- */
1136   if(force_in_type_flag && forced_in_type == MRI_VOLUME_TYPE_UNKNOWN)
1137     {
1138       fprintf(stderr, "\n%s: unknown input "
1139               "volume type %s\n", Progname, in_type_string);
1140       usage_message(stdout);
1141       exit(1);
1142     }
1143 
1144   if(force_template_type_flag && 
1145      forced_template_type == MRI_VOLUME_TYPE_UNKNOWN)
1146     {
1147       fprintf(stderr, "\n%s: unknown template volume type %s\n", 
1148               Progname, template_type_string);
1149       usage_message(stdout);
1150       exit(1);
1151     }
1152 
1153   if(force_out_type_flag && forced_out_type == MRI_VOLUME_TYPE_UNKNOWN)
1154     {
1155       fprintf(stderr, "\n%s: unknown output volume type %s\n", 
1156               Progname, out_type_string);
1157       usage_message(stdout);
1158       exit(1);
1159     }
1160 
1161   /* ----- warn if read only is desired and an 
1162      output volume is specified or the output info flag is set ----- */
1163   if(read_only_flag && out_name[0] != '\0')
1164     fprintf(stderr, 
1165             "%s: warning: read only flag is set; "
1166             "nothing will be written to %s\n", Progname, out_name);
1167   if(read_only_flag && (out_info_flag || out_matrix_flag))
1168     fprintf(stderr, "%s: warning: read only flag is set; "
1169             "no output information will be printed\n", Progname);
1170 
1171 
1172   /* ----- get the type of the output ----- */
1173   if (!read_only_flag)
1174     {
1175       if(!force_out_type_flag)
1176         {
1177           // if(!read_only_flag && !no_write_flag)  
1178           // because conform_flag value changes depending on type below
1179           {
1180             out_volume_type = mri_identify(out_name);
1181             if(out_volume_type == MRI_VOLUME_TYPE_UNKNOWN)
1182               {
1183                 fprintf(stderr, 
1184                         "%s: can't determine type of output volume\n", 
1185                         Progname);
1186                 exit(1);
1187               }
1188           }
1189         }
1190       else
1191         out_volume_type = forced_out_type;
1192 
1193       // if output type is COR, then it is always conformed
1194       if (out_volume_type == MRI_CORONAL_SLICE_DIRECTORY)
1195         conform_flag = TRUE;
1196     }
1197 
1198   /* ----- catch the parse-only flag ----- */
1199   if(parse_only_flag)
1200     {
1201       printf("input volume name: %s\n", in_name);
1202       printf("input name only: %s\n", in_name_only);
1203       printf("output volume name: %s\n", out_name);
1204       printf("parse_only_flag = %d\n", parse_only_flag);
1205       printf("conform_flag = %d\n", conform_flag);
1206       printf("conform_size = %f\n", conform_size);
1207       printf("in_info_flag = %d\n", in_info_flag);
1208       printf("out_info_flag = %d\n", out_info_flag);
1209       printf("in_matrix_flag = %d\n", in_matrix_flag);
1210       printf("out_matrix_flag = %d\n", out_matrix_flag);
1211 
1212       if(force_in_type_flag)
1213         printf("input type is %d\n", forced_in_type);
1214       if(force_out_type_flag)
1215         printf("output type is %d\n", forced_out_type);
1216 
1217       if(subject_name[0] != '\0')
1218         printf("subject name is %s\n", subject_name);
1219 
1220       if(invert_val >= 0)
1221         printf("inversion, value is %g\n", invert_val);
1222 
1223       if(reorder_flag)
1224         printf("reordering, values are %d %d %d\n", 
1225                reorder_vals[0], reorder_vals[1], reorder_vals[2]);
1226 
1227       printf("translation of otl labels is %s\n", 
1228              translate_labels_flag ? "on" : "off");
1229 
1230       exit(0);
1231     }
1232 
1233 
1234   /* ----- check for a gdf image stem if the output type is gdf ----- */
1235   if(out_volume_type == GDF_FILE && strlen(gdf_image_stem) == 0)
1236     {
1237       fprintf(stderr, "%s: GDF output type, "
1238               "but no GDF image file stem\n", Progname);
1239       exit(1);
1240     }
1241 
1242   /* ----- read the in_like volume ----- */
1243   if(in_like_flag)
1244     {
1245       printf("reading info from %s...\n", in_like_name);
1246       mri_in_like = MRIreadInfo(in_like_name);
1247       if(mri_in_like == NULL)
1248         exit(1);
1249     }
1250 
1251   /* ----- read the volume ----- */
1252   if(force_in_type_flag)
1253     in_volume_type = forced_in_type;
1254   else
1255     in_volume_type = mri_identify(in_name_only);
1256 
1257   if(in_volume_type == MRI_VOLUME_TYPE_UNKNOWN)
1258     {
1259       errno = 0;
1260       ErrorPrintf(ERROR_BADFILE, "unknown file type for file %s",
1261                   in_name_only);
1262       if(in_like_flag)
1263         MRIfree(&mri_in_like);
1264       exit(1);
1265     }
1266 
1267   if(roi_flag && in_volume_type != GENESIS_FILE)
1268     {
1269       errno = 0;
1270       ErrorPrintf(ERROR_BADPARM, "rois must be in GE format");
1271       if(in_like_flag) MRIfree(&mri_in_like);
1272       exit(1);
1273     }
1274 
1275   if (zero_ge_z_offset_flag && in_volume_type != DICOM_FILE) //E/
1276     {
1277       zero_ge_z_offset_flag = FALSE ;
1278       fprintf(stderr, "Not a GE dicom volume: -zgez "
1279               "= --zero_ge_z_offset option ignored.\n");
1280     }
1281 
1282   printf("reading from %s...\n", in_name_only);
1283 
1284   if(in_volume_type == OTL_FILE)
1285     {
1286 
1287       if(!in_like_flag && !in_n_k_flag)
1288         {
1289           errno = 0;
1290           ErrorPrintf(ERROR_BADPARM, "parcellation read: must specify"
1291                       "a volume depth with either in_like or in_k_count");
1292           exit(1);
1293         }
1294 
1295       if(!color_file_flag)
1296         {
1297           errno = 0;
1298           ErrorPrintf(ERROR_BADPARM, "parcellation read: must specify a"
1299                       "color file name");
1300           if(in_like_flag)
1301             MRIfree(&mri_in_like);
1302           exit(1);
1303         }
1304 
1305       read_parcellation_volume_flag = TRUE;
1306       if(read_only_flag && 
1307          (in_info_flag || in_matrix_flag) && 
1308          !in_stats_flag)
1309         read_parcellation_volume_flag = FALSE;
1310 
1311       read_otl_flags = 0x00;
1312 
1313       if(read_parcellation_volume_flag)
1314         read_otl_flags |= READ_OTL_READ_VOLUME_FLAG;
1315 
1316       if(fill_parcellation_flag)
1317         read_otl_flags |= READ_OTL_FILL_FLAG;
1318       else
1319         {
1320           printf("notice: unfilled parcellations "
1321                  "currently unimplemented\n");
1322           printf("notice: filling outlines\n");
1323           read_otl_flags |= READ_OTL_FILL_FLAG;
1324         }
1325 
1326       if(translate_labels_flag)
1327         read_otl_flags |= READ_OTL_TRANSLATE_LABELS_FLAG;
1328 
1329       if(zero_outlines_flag)
1330         {
1331           read_otl_flags |= READ_OTL_ZERO_OUTLINES_FLAG;
1332         }
1333 
1334       if(in_like_flag)
1335         mri = MRIreadOtl(in_name, mri_in_like->width, mri_in_like->height, 
1336                          mri_in_like->depth,
1337                          color_file_name, read_otl_flags);
1338       else
1339         mri = MRIreadOtl(in_name, 0, 0, in_n_k, 
1340                          color_file_name, read_otl_flags);
1341 
1342       if(mri == NULL)
1343         {
1344           if(in_like_flag)
1345             MRIfree(&mri_in_like);
1346           exit(1);
1347         }
1348 
1349       /* ----- smooth the parcellation if requested ----- */
1350       if(smooth_parcellation_flag)
1351         {
1352           printf("smoothing parcellation...\n");
1353           mri2 = MRIsmoothParcellation(mri, smooth_parcellation_count);
1354           if(mri2 == NULL)
1355             {
1356               if(in_like_flag)
1357                 MRIfree(&mri_in_like);
1358               exit(1);
1359             }
1360           MRIfree(&mri);
1361           mri = mri2;
1362         }
1363 
1364       resample_type_val = SAMPLE_NEAREST;
1365       no_scale_flag = TRUE;
1366 
1367     }
1368   else if(roi_flag)
1369     {
1370       if(!in_like_flag && !in_n_k_flag)
1371         {
1372           errno = 0;
1373           ErrorPrintf(ERROR_BADPARM, "roi read: must specify a volume"
1374                       "depth with either in_like or in_k_count");
1375           if(in_like_flag)
1376             MRIfree(&mri_in_like);
1377           exit(1);
1378         }
1379 
1380       if(in_like_flag)
1381         mri = MRIreadGeRoi(in_name, mri_in_like->depth);
1382       else
1383         mri = MRIreadGeRoi(in_name, in_n_k);
1384 
1385       if(mri == NULL)
1386         {
1387           if(in_like_flag)
1388             MRIfree(&mri_in_like);
1389           exit(1);
1390         }
1391 
1392       resample_type_val = SAMPLE_NEAREST;
1393       no_scale_flag = TRUE;
1394     }
1395   else
1396     {
1397       if(read_only_flag && 
1398          (in_info_flag || in_matrix_flag) && 
1399          !in_stats_flag)
1400         {
1401           if(force_in_type_flag)
1402             mri = MRIreadHeader(in_name, in_volume_type);
1403           else
1404             mri = MRIreadInfo(in_name);
1405         }
1406       else
1407         {
1408           if(force_in_type_flag){
1409             //printf("MRIreadType()\n");
1410             mri = MRIreadType(in_name, in_volume_type);
1411           }
1412           else
1413             {
1414               if (nthframe < 0)
1415                 mri = MRIread(in_name);
1416               else
1417                 mri = MRIreadEx(in_name, nthframe);
1418             }
1419         }
1420 
1421     }
1422 
1423 
1424   if(mri == NULL)
1425     {
1426       if(in_like_flag) MRIfree(&mri_in_like);
1427       exit(1);
1428     }
1429 
1430   MRIaddCommandLine(mri, cmdline) ;
1431   if (!FEQUAL(scale_factor,1.0))
1432     {
1433       printf("scaling input volume by %2.3f\n", scale_factor) ;
1434       MRIscalarMul(mri, mri, scale_factor) ;
1435     }
1436 
1437   if(zero_ge_z_offset_flag) //E/
1438     mri->c_s = 0.0;
1439 
1440   if(unwarp_flag)
1441     {
1442       /* if unwarp_flag is true, unwarp the distortions due
1443          to gradient coil nonlinearities */
1444       printf("INFO: unwarping ... ");
1445       mri_unwarped = unwarpGradientNonlinearity(mri, 
1446                                                 unwarp_gradientType, 
1447                                                 unwarp_partialUnwarp,
1448                                                 unwarp_jacobianCorrection,
1449                                                 unwarp_interpType,
1450                                                 unwarp_sincInterpHW);
1451       MRIfree(&mri);
1452       mri = mri_unwarped;
1453       printf("done \n ");      
1454     }
1455 
1456   printf("TR=%2.2f, TE=%2.2f, TI=%2.2f, flip angle=%2.2f\n",
1457          mri->tr, mri->te, mri->ti, DEGREES(mri->flip_angle)) ;
1458   if(in_volume_type != OTL_FILE)
1459     {
1460       if(fill_parcellation_flag)
1461         printf("fill_parcellation flag ignored on a "
1462                "non-parcellation read\n");
1463       if(smooth_parcellation_flag)
1464         printf("smooth_parcellation flag ignored on a "
1465                "non-parcellation read\n");
1466     }
1467 
1468   /* ----- apply the in_like volume if it's been read ----- */
1469   if(in_like_flag)
1470     {
1471       if(mri->width   != mri_in_like->width ||
1472          mri->height  != mri_in_like->height ||
1473          mri->depth   != mri_in_like->depth ||
1474          mri->nframes != mri_in_like->nframes)
1475         {
1476           errno = 0;
1477           ErrorPrintf(ERROR_BADPARM, "volume sizes do not match\n");
1478           ErrorPrintf(ERROR_BADPARM, "%s: (width, height, depth, frames) "
1479                       "= (%d, %d, %d, %d)\n", 
1480                       in_name, 
1481                       mri->width, mri->height, mri->depth, mri->nframes);
1482           ErrorPrintf(ERROR_BADPARM, "%s: (width, height, depth, frames) "
1483                       "= (%d, %d, %d, %d)\n", 
1484                       in_like_name, mri_in_like->width, 
1485                       mri_in_like->height, mri_in_like->depth, 
1486                       mri_in_like->nframes);
1487           MRIfree(&mri);
1488           MRIfree(&mri_in_like);
1489           exit(1);
1490         }
1491 
1492       temp_type = mri->type;
1493 
1494       if(MRIcopyHeader(mri_in_like, mri) != NO_ERROR)
1495         {
1496           errno = 0;
1497           ErrorPrintf(ERROR_BADPARM, "error copying information from "
1498                       "%s structure to %s structure\n", 
1499                       in_like_name, in_name);
1500           MRIfree(&mri);
1501           MRIfree(&mri_in_like);
1502           exit(1);
1503         }
1504 
1505       mri->type = temp_type;
1506 
1507       MRIfree(&mri_in_like);
1508 
1509     }
1510 
1511   if(mri->ras_good_flag == 0){
1512     printf("WARNING: it does not appear that there "
1513            "was sufficient information\n"
1514            "in the input to assign orientation to the volume... \n");
1515     if(force_ras_good){
1516       printf("However, you have specified that the "
1517              "default orientation should\n"
1518              "be used with by adding --force_ras_good "
1519              "on the command-line.\n");
1520       mri->ras_good_flag = 1;
1521     }
1522     if(in_i_direction_flag || in_j_direction_flag || in_k_direction_flag){
1523       printf("However, you have specified one or more "
1524              "orientations on the \n"
1525              "command-line using -i?d or --in-?-direction (?=i,j,k).\n");
1526       mri->ras_good_flag = 1;
1527     }
1528   }
1529 
1530   /* ----- apply command-line parameters ----- */
1531   if(in_i_size_flag)    mri->xsize = in_i_size;
1532   if(in_j_size_flag)    mri->ysize = in_j_size;
1533   if(in_k_size_flag)    mri->zsize = in_k_size;
1534   if(in_i_direction_flag)
1535     {
1536       mri->x_r = in_i_directions[0];
1537       mri->x_a = in_i_directions[1];
1538       mri->x_s = in_i_directions[2];
1539       mri->ras_good_flag = 1;
1540     }
1541   if(in_j_direction_flag)
1542     {
1543       mri->y_r = in_j_directions[0];
1544       mri->y_a = in_j_directions[1];
1545       mri->y_s = in_j_directions[2];
1546       mri->ras_good_flag = 1;
1547     }
1548   if(in_k_direction_flag)
1549     {
1550       mri->z_r = in_k_directions[0];
1551       mri->z_a = in_k_directions[1];
1552       mri->z_s = in_k_directions[2];
1553       mri->ras_good_flag = 1;
1554     }
1555   if(in_orientation_flag){
1556     printf("Setting input orientation to %s\n",in_orientation_string);
1557     MRIorientationStringToDircos(mri, in_orientation_string);
1558     mri->ras_good_flag = 1;
1559   }
1560   if(in_center_flag)
1561     {
1562       mri->c_r = in_center[0];
1563       mri->c_a = in_center[1];
1564       mri->c_s = in_center[2];
1565     }
1566   if(subject_name[0] != '\0')
1567     strcpy(mri->subject_name, subject_name);
1568 
1569   if(in_tr_flag) mri->tr = in_tr;
1570   if(in_ti_flag) mri->ti = in_ti;
1571   if(in_te_flag) mri->te = in_te;
1572   if(in_flip_angle_flag) mri->flip_angle = in_flip_angle;
1573 
1574   /* ----- correct starts, ends, and fov if necessary ----- */
1575   if(in_i_size_flag || in_j_size_flag || in_k_size_flag)
1576     {
1577 
1578       fov_x = mri->xsize * mri->width;
1579       fov_y = mri->ysize * mri->height;
1580       fov_z = mri->zsize * mri->depth;
1581 
1582       mri->xend = fov_x / 2.0;
1583       mri->xstart = -mri->xend;
1584       mri->yend = fov_y / 2.0;
1585       mri->ystart = -mri->yend;
1586       mri->zend = fov_z / 2.0;
1587       mri->zstart = -mri->zend;
1588 
1589       mri->fov = (fov_x > fov_y ? 
1590                   (fov_x > fov_z ? fov_x : fov_z) : 
1591                   (fov_y > fov_z ? fov_y : fov_z) );
1592 
1593     }
1594 
1595   /* ----- give a warning for non-orthogonal directions ----- */
1596   i_dot_j = mri->x_r * 
1597     mri->y_r + mri->x_a * 
1598     mri->y_a + mri->x_s * 
1599     mri->y_s;
1600   i_dot_k = mri->x_r * 
1601     mri->z_r + mri->x_a * 
1602     mri->z_a + mri->x_s * 
1603     mri->z_s;
1604   j_dot_k = mri->y_r * 
1605     mri->z_r + mri->y_a * 
1606     mri->z_a + mri->y_s * 
1607     mri->z_s;
1608   if(fabs(i_dot_j) > CLOSE_ENOUGH || 
1609      fabs(i_dot_k) > CLOSE_ENOUGH || 
1610      fabs(i_dot_k) > CLOSE_ENOUGH)
1611     {
1612       printf("warning: input volume axes are not orthogonal:"
1613              "i_dot_j = %.6f, i_dot_k = %.6f, j_dot_k = %.6f\n",
1614              i_dot_j, i_dot_k, j_dot_k);
1615     }
1616   printf("i_ras = (%g, %g, %g)\n", mri->x_r, mri->x_a, mri->x_s);
1617   printf("j_ras = (%g, %g, %g)\n", mri->y_r, mri->y_a, mri->y_s);
1618   printf("k_ras = (%g, %g, %g)\n", mri->z_r, mri->z_a, mri->z_s);
1619 
1620   /* ----- catch the in info flag ----- */
1621   if(in_info_flag)
1622     {
1623       printf("input structure:\n");
1624       MRIdump(mri, stdout);
1625     }
1626 
1627   if(in_matrix_flag)
1628     {
1629       MATRIX *i_to_r;
1630       i_to_r = extract_i_to_r(mri);
1631       if(i_to_r != NULL)
1632         {
1633           printf("input ijk -> ras:\n");
1634           MatrixPrint(stdout, i_to_r);
1635           MatrixFree(&i_to_r);
1636         }
1637       else
1638         printf("error getting input matrix\n");
1639     }
1640 
1641   /* ----- catch the in stats flag ----- */
1642   if(in_stats_flag)
1643     MRIprintStats(mri, stdout);
1644 
1645   /* ----- apply a transformation if requested ----- */
1646   if(transform_flag)
1647     {
1648 
1649       printf("INFO: Applying transformation from file %s...\n", 
1650              transform_fname);
1651 
1652       if(!FileExists(transform_fname))
1653         {
1654           fprintf(stderr,"ERROR: cannot find transform file %s\n",
1655                   transform_fname);
1656           exit(1);
1657         }
1658 
1659       transform_type = TransformFileNameType(transform_fname);
1660       if(transform_type == MNI_TRANSFORM_TYPE || 
1661          transform_type == TRANSFORM_ARRAY_TYPE ||
1662          transform_type == REGISTER_DAT ||
1663          transform_type == FSLREG_TYPE)
1664         {
1665           printf("Reading transform\n");
1666           // lta_transform = LTAread(transform_fname);
1667           lta_transform = LTAreadEx(transform_fname);
1668           if(lta_transform  == NULL){
1669             fprintf(stderr, "ERROR: Reading transform from file %s\n", 
1670                     transform_fname);
1671             exit(1);
1672           }
1673       
1674           if (transform_type == FSLREG_TYPE)
1675             {
1676               MRI *tmp = 0;
1677               if (out_like_flag == 0)
1678                 {
1679                   fprintf(stderr, "ERROR: fslmat does not have the "
1680                           "information on the dst volume\n");
1681                   fprintf(stderr, "ERROR: you must give option "
1682                           "'--like volume' to specify the"
1683                           " dst volume info\n");
1684                   MRIfree(&mri);
1685                   exit(1);
1686                 }
1687               // now setup dst volume info
1688               tmp = MRIreadHeader(out_like_name, MRI_VOLUME_TYPE_UNKNOWN); 
1689               // flsmat does not contain src and dst info
1690               LTAmodifySrcDstGeom(lta_transform, mri, tmp); 
1691               // add src and dst information
1692               LTAchangeType(lta_transform, LINEAR_VOX_TO_VOX);
1693               MRIfree(&tmp);
1694             }
1695 
1696           if(DevXFM){
1697             printf("INFO: devolving XFM (%s)\n",devxfm_subject);
1698             printf("-------- before ---------\n");
1699             MatrixPrint(stdout,lta_transform->xforms[0].m_L);
1700             T = DevolveXFM(devxfm_subject, 
1701                            lta_transform->xforms[0].m_L, NULL);
1702             if(T==NULL) exit(1);
1703             printf("-------- after ---------\n");
1704             MatrixPrint(stdout,lta_transform->xforms[0].m_L);
1705             printf("-----------------\n");
1706           }
1707       
1708           if(invert_transform_flag)
1709             {
1710               inverse_transform_matrix = 
1711                 MatrixInverse(lta_transform->xforms[0].m_L,NULL);
1712               if(inverse_transform_matrix == NULL)
1713                 {
1714                   fprintf(stderr, "ERROR: inverting transform\n");
1715                   MatrixPrint(stdout,lta_transform->xforms[0].m_L);
1716                   exit(1);
1717                 }
1718         
1719               MatrixFree(&(lta_transform->xforms[0].m_L));
1720               lta_transform->xforms[0].m_L = inverse_transform_matrix;
1721               // reverse src and dst target info.
1722               // since it affects the c_ras values of the result
1723               // in LTAtransform()
1724               // question is what to do when transform src info is invalid.
1725               lt = &lta_transform->xforms[0];
1726               if (lt->src.valid==0)
1727                 {
1728                   char buf[512];
1729                   char *p;
1730                   MRI *mriOrig;
1731 
1732                   fprintf(stderr, "INFO: Trying to get the source "
1733                           "volume information from the transform name\n");
1734                   // copy transform filename
1735                   strcpy(buf, transform_fname);
1736                   // reverse look for the first '/' 
1737                   p = strrchr(buf, '/');
1738                   if (p != 0)
1739                     {
1740                       p++;
1741                       *p = '\0'; 
1742                       // set the terminator. i.e.  
1743                       // ".... mri/transforms" from "
1744                       //..../mri/transforms/talairach.xfm"
1745                     }
1746                   else // no / present means only a filename is given
1747                     {
1748                       strcpy(buf, "./");
1749                     }  
1750                   strcat(buf, "../orig"); // go to mri/orig from 
1751                   // mri/transforms/
1752                   // check whether we can read header info or not
1753                   mriOrig = MRIreadHeader(buf, MRI_VOLUME_TYPE_UNKNOWN);
1754                   if (mriOrig)
1755                     {
1756                       getVolGeom(mriOrig, &lt->src);
1757                       fprintf(stderr, "INFO: Succeeded in retrieving "
1758                               "the source volume info.\n");
1759                     }
1760                   else
1761                     fprintf(stderr, "INFO: Failed to find %s as a "
1762                             "source volume. The inverse c_(ras) may "
1763                             "not be valid.\n",
1764                             buf);
1765 
1766                 }
1767               copyVolGeom(&lt->dst, &vgtmp);
1768               copyVolGeom(&lt->src, &lt->dst);
1769               copyVolGeom(&vgtmp, &lt->src);
1770             }
1771       
1772           /* Think about calling MRIlinearTransform() here; need vox2vox
1773              transform. Can create NN version. In theory, LTAtransform()
1774              can handle multiple transforms, but the inverse assumes only
1775              one. NN is good for ROI*/
1776       
1777           printf("---------------------------------\n");
1778           printf("INFO: Transform Matrix \n");
1779           MatrixPrint(stdout,lta_transform->xforms[0].m_L);
1780           printf("---------------------------------\n");
1781                         
1782           /* LTAtransform() runs either MRIapplyRASlinearTransform() 
1783              for RAS2RAS or MRIlinearTransform() for Vox2Vox. */
1784           /* MRIlinearTransform() calls MRIlinearTransformInterp() */
1785           if (out_like_flag == 1)
1786             {
1787               MRI *tmp = 0;
1788               printf("INFO: transform dst into the like-volume\n");
1789               tmp = MRIreadHeader(out_like_name, MRI_VOLUME_TYPE_UNKNOWN);
1790               mri_transformed = 
1791                 MRIalloc(tmp->width, tmp->height, tmp->depth, mri->type);
1792               if (!mri_transformed)
1793                 {
1794                   ErrorExit(ERROR_NOMEMORY, "could not allocate memory");
1795                 }
1796               MRIcopyHeader(tmp, mri_transformed);
1797               MRIfree(&tmp); tmp = 0;
1798               mri_transformed = 
1799                 LTAtransform(mri, mri_transformed, lta_transform);
1800             }
1801           else{
1802             printf("Applying LTA transform (resample_type %d)\n",
1803                    resample_type_val);
1804             mri_transformed = 
1805               LTAtransformInterp(mri, NULL, 
1806                                  lta_transform,resample_type_val);
1807           }
1808           if(mri_transformed == NULL){
1809             fprintf(stderr, "ERROR: applying transform to volume\n");
1810             exit(1);
1811           }
1812           LTAfree(&lta_transform);
1813           MRIfree(&mri);
1814           mri = mri_transformed;
1815         }
1816       else if(transform_type == MORPH_3D_TYPE)
1817         // this is a non-linear vox-to-vox transform
1818         {
1819           //note that in this case trilinear 
1820           // interpolation is always used, and -rt
1821           // option has no effect! -xh
1822           TRANSFORM *tran = TransformRead(transform_fname);
1823           if (invert_transform_flag == 0)
1824             mri_transformed = 
1825               GCAMmorphToAtlas(mri, (GCA_MORPH *)tran->xform, NULL, 0) ;
1826           else // invert 
1827             {
1828               mri_transformed = MRIclone(mri, NULL);
1829               mri_transformed = 
1830                 GCAMmorphFromAtlas(mri, 
1831                                    (GCA_MORPH *)tran->xform, 
1832                                    mri_transformed);
1833             }
1834           TransformFree(&tran);
1835           MRIfree(&mri);
1836           mri = mri_transformed;
1837         }
1838       else
1839         {
1840           fprintf(stderr, "unknown transform type in file %s\n", 
1841                   transform_fname);
1842           exit(1);
1843         }
1844 
1845     }
1846   else if (out_like_flag) // flag set but no transform
1847     {
1848       // modify direction cosines to use the 
1849       // out-like volume direction cosines
1850       //
1851       //   src --> RAS
1852       //    |       |
1853       //    |       |
1854       //    V       V
1855       //   dst --> RAS   where dst i_to_r is taken from out volume
1856       MRI *tmp = 0;
1857       MATRIX *src2dst = 0;
1858 
1859       printf("INFO: transform src into the like-volume: %s\n", 
1860              out_like_name);
1861       tmp = MRIreadHeader(out_like_name, MRI_VOLUME_TYPE_UNKNOWN);
1862       mri_transformed = 
1863         MRIalloc(tmp->width, tmp->height, tmp->depth, mri->type);
1864       if (!mri_transformed)
1865         {
1866           ErrorExit(ERROR_NOMEMORY, "could not allocate memory");
1867         }
1868       MRIcopyHeader(tmp, mri_transformed);
1869       MRIfree(&tmp); tmp = 0;
1870       // just to make sure
1871       if (mri->i_to_r__)
1872         mri->i_to_r__ = extract_i_to_r(mri);
1873       if (mri_transformed->r_to_i__)
1874         mri_transformed->r_to_i__ = extract_r_to_i(mri_transformed);
1875       // got the transform
1876       src2dst = MatrixMultiply(mri_transformed->r_to_i__,
1877                                mri->i_to_r__, NULL);
1878       // now get the values (tri-linear)
1879       MRIlinearTransform(mri, mri_transformed, src2dst);
1880       MatrixFree(&src2dst);
1881       MRIfree(&mri);
1882       mri=mri_transformed;
1883     } 
1884 
1885   if(reslice_like_flag)
1886     {
1887 
1888       if(force_template_type_flag)
1889         {
1890           printf("reading template info from (type %s) volume %s...\n", 
1891                  template_type_string, reslice_like_name);
1892           template = 
1893             MRIreadHeader(reslice_like_name, forced_template_type);
1894           if(template == NULL)
1895             {
1896               fprintf(stderr, "error reading from volume %s\n", 
1897                       reslice_like_name);
1898               exit(1);
1899             }
1900         }
1901       else
1902         {
1903           printf("reading template info from volume %s...\n", 
1904                  reslice_like_name);
1905           template = MRIreadInfo(reslice_like_name);
1906           if(template == NULL)
1907             {
1908               fprintf(stderr, "error reading from volume %s\n", 
1909                       reslice_like_name);
1910               exit(1);
1911             }
1912         }
1913 
1914     }
1915   else
1916     {
1917       template = 
1918         MRIallocHeader(mri->width, mri->height, mri->depth, mri->type);
1919       MRIcopyHeader(mri, template);
1920       if(conform_flag)
1921         {
1922           conform_width = 256;
1923           if (conform_min == TRUE)
1924             {
1925               conform_size = findMinSize(mri, &conform_width);
1926             }
1927           else
1928             {
1929               conform_width = findRightSize(mri, conform_size);
1930             }
1931           template->width = 
1932             template->height = template->depth = conform_width;
1933           template->imnr0 = 1;
1934           template->imnr1 = conform_width;
1935           template->type = MRI_UCHAR;
1936           template->thick = conform_size;
1937           template->ps = conform_size;
1938           template->xsize = 
1939             template->ysize = template->zsize = conform_size;
1940           printf("Original Data has (%g, %g, %g) mm size "
1941                  "and (%d, %d, %d) voxels.\n",
1942                  mri->xsize, mri->ysize, mri->zsize, 
1943                  mri->width, mri->height, mri->depth);
1944           printf("Data is conformed to %g mm size and "
1945                  "%d voxels for all directions\n", 
1946                  conform_size, conform_width); 
1947           template->xstart = template->ystart = 
1948             template->zstart = - conform_width/2;
1949           template->xend = 
1950             template->yend = template->zend = conform_width/2;
1951           template->x_r = -1.0;  
1952           template->x_a =  0.0; 
1953           template->x_s =  0.0;
1954           template->y_r =  0.0; 
1955           template->y_a =  0.0;  
1956           template->y_s = -1.0;
1957           template->z_r =  0.0; 
1958           template->z_a =  1.0; 
1959           template->z_s =  0.0;
1960         }
1961     }
1962 
1963   /* ----- apply command-line parameters ----- */
1964   if(out_i_size_flag)
1965     template->xsize = out_i_size;
1966   if(out_j_size_flag)
1967     template->ysize = out_j_size;
1968   if(out_k_size_flag)
1969     template->zsize = out_k_size;
1970   if(out_n_i_flag)
1971     template->width = out_n_i;
1972   if(out_n_j_flag)
1973     template->height = out_n_j;
1974   if(out_n_k_flag)
1975     {
1976       template->depth = out_n_k;
1977       template->imnr1 = template->imnr0 + out_n_k - 1;
1978     }
1979   if(out_i_direction_flag)
1980     {
1981       template->x_r = out_i_directions[0];
1982       template->x_a = out_i_directions[1];
1983       template->x_s = out_i_directions[2];
1984     }
1985   if(out_j_direction_flag)
1986     {
1987       template->y_r = out_j_directions[0];
1988       template->y_a = out_j_directions[1];
1989       template->y_s = out_j_directions[2];
1990     }
1991   if(out_k_direction_flag)
1992     {
1993       template->z_r = out_k_directions[0];
1994       template->z_a = out_k_directions[1];
1995       template->z_s = out_k_directions[2];
1996     }
1997   if(out_orientation_flag){
1998     printf("Setting output orientation to %s\n",out_orientation_string);
1999     MRIorientationStringToDircos(template, out_orientation_string);
2000   }
2001   if(out_center_flag)
2002     {
2003       template->c_r = out_center[0];
2004       template->c_a = out_center[1];
2005       template->c_s = out_center[2];
2006     }
2007 
2008   /* ----- correct starts, ends, and fov if necessary ----- */
2009   if(out_i_size_flag || out_j_size_flag || out_k_size_flag ||
2010      out_n_i_flag    || out_n_j_flag    || out_n_k_flag)
2011     {
2012 
2013       fov_x = template->xsize * template->width;
2014       fov_y = template->ysize * template->height;
2015       fov_z = template->zsize * template->depth;
2016       template->xend = fov_x / 2.0;
2017       template->xstart = -template->xend;
2018       template->yend = fov_y / 2.0;
2019       template->ystart = -template->yend;
2020       template->zend = fov_z / 2.0;
2021       template->zstart = -template->zend;
2022 
2023       template->fov = (fov_x > fov_y ? 
2024                        (fov_x > fov_z ? fov_x : fov_z) : 
2025                        (fov_y > fov_z ? fov_y : fov_z) );
2026 
2027     }
2028 
2029   /* ----- give a warning for non-orthogonal directions ----- */
2030   i_dot_j = template->x_r * 
2031     template->y_r + template->x_a * 
2032     template->y_a + template->x_s * 
2033     template->y_s;
2034   i_dot_k = template->x_r * 
2035     template->z_r + template->x_a * 
2036     template->z_a + template->x_s * 
2037     template->z_s;
2038   j_dot_k = template->y_r * 
2039     template->z_r + template->y_a * 
2040     template->z_a + template->y_s * 
2041     template->z_s;
2042   if(fabs(i_dot_j) > CLOSE_ENOUGH || 
2043      fabs(i_dot_k) > CLOSE_ENOUGH || 
2044      fabs(i_dot_k) > CLOSE_ENOUGH)
2045     {
2046       printf("warning: output volume axes are not orthogonal:"
2047              "i_dot_j = %.6f, i_dot_k = %.6f, j_dot_k = %.6f\n",
2048              i_dot_j, i_dot_k, j_dot_k);
2049       printf("i_ras = (%g, %g, %g)\n", 
2050              template->x_r, template->x_a, template->x_s);
2051       printf("j_ras = (%g, %g, %g)\n", 
2052              template->y_r, template->y_a, template->y_s);
2053       printf("k_ras = (%g, %g, %g)\n", 
2054              template->z_r, template->z_a, template->z_s);
2055     }
2056   if(out_data_type >= 0)
2057     template->type = out_data_type;
2058 
2059   /* ----- catch the template info flag ----- */
2060   if(template_info_flag)
2061     {
2062       printf("template structure:\n");
2063       MRIdump(template, stdout);
2064     }
2065 
2066   /* ----- exit here if read only is desired ----- */
2067   if(read_only_flag)  exit(0);
2068 
2069   /* ----- change type if necessary ----- */
2070   if(mri->type != template->type && nochange_flag == FALSE)
2071     {
2072       printf("changing data type from %d to %d (noscale = %d)...\n",
2073              mri->type,template->type,no_scale_flag);
2074       mri2 = 
2075         MRISeqchangeType(mri, template->type, 0.0, 0.999, no_scale_flag);
2076       if(mri2 == NULL) {
2077         printf("ERROR: MRISeqchangeType\n");
2078         exit(1);
2079       }
2080       MRIfree(&mri);
2081       mri = mri2;
2082     }
2083 
2084   /* ----- reslice if necessary ----- */
2085   if(mri->xsize != template->xsize || 
2086      mri->ysize != template->ysize || 
2087      mri->zsize != template->zsize ||
2088      mri->width != template->width || 
2089      mri->height != template->height || 
2090      mri->depth != template->depth ||
2091      mri->x_r != template->x_r || 
2092      mri->x_a != template->x_a || 
2093      mri->x_s != template->x_s ||
2094      mri->y_r != template->y_r || 
2095      mri->y_a != template->y_a || 
2096      mri->y_s != template->y_s ||
2097      mri->z_r != template->z_r || 
2098      mri->z_a != template->z_a || 
2099      mri->z_s != template->z_s ||
2100      mri->c_r != template->c_r || 
2101      mri->c_a != template->c_a || 
2102      mri->c_s != template->c_s)
2103     {
2104       printf("Reslicing using ");
2105       switch(resample_type_val){
2106       case SAMPLE_TRILINEAR: printf("trilinear interpolation \n"); break;
2107       case SAMPLE_NEAREST:     printf("nearest \n"); break;
2108       case SAMPLE_SINC:        printf("sinc \n"); break;
2109       case SAMPLE_CUBIC:       printf("cubic \n"); break;
2110       case SAMPLE_WEIGHTED:    printf("weighted \n"); break;
2111       }
2112       mri2 = MRIresample(mri, template, resample_type_val);
2113       if(mri2 == NULL) exit(1);
2114       MRIfree(&mri);
2115       mri = mri2;
2116     }
2117 
2118 
2119   /* ----- invert contrast if necessary ----- */
2120   if(invert_val >= 0)
2121     {
2122       printf("inverting contrast...\n");
2123       mri2 = MRIinvertContrast(mri, NULL, invert_val);
2124       if(mri2 == NULL)
2125         exit(1);
2126       MRIfree(&mri);
2127       mri = mri2;
2128     }
2129 
2130   /* ----- reorder if necessary ----- */
2131   if(reorder_flag){
2132     printf("reordering axes...\n");
2133     mri2 = MRIreorder(mri, NULL, reorder_vals[0], reorder_vals[1], 
2134                       reorder_vals[2]);
2135     if(mri2 == NULL){
2136       fprintf(stderr, "error reordering axes\n");
2137       exit(1);
2138     }
2139     MRIfree(&mri);
2140     mri = mri2;
2141   }
2142 
2143   /* ----- store the gdf file stem ----- */
2144   strcpy(mri->gdf_image_stem, gdf_image_stem);
2145 
2146   /* ----- catch the out info flag ----- */
2147   if(out_info_flag){
2148     printf("output structure:\n");
2149     MRIdump(mri, stdout);
2150   }
2151 
2152   /* ----- catch the out matrix flag ----- */
2153   if(out_matrix_flag)
2154     {
2155       MATRIX *i_to_r;
2156       i_to_r = extract_i_to_r(mri);
2157       if(i_to_r != NULL)
2158         {
2159           printf("output ijk -> ras:\n");
2160           MatrixPrint(stdout, i_to_r);
2161           MatrixFree(&i_to_r);
2162         }
2163       else
2164         printf("error getting output matrix\n");
2165     }
2166 
2167   /* ----- catch the out stats flag ----- */
2168   if(out_stats_flag) MRIprintStats(mri, stdout);
2169 
2170   /* ----- drop the last ndrop frames (drop before skip) ------ */
2171   if(ndrop > 0){
2172     printf("Dropping last %d frames\n",ndrop);
2173     if(mri->nframes <= ndrop){
2174       printf("   ERROR: can't drop, volume only has %d frames\n",
2175              mri->nframes);
2176       exit(1);
2177     }
2178     mri2 = fMRIndrop(mri, ndrop, NULL);
2179     if(mri2 == NULL) exit(1);
2180     MRIfree(&mri);
2181     mri = mri2;
2182   }
2183 
2184   /* ----- skip the first nskip frames ------ */
2185   if(nskip > 0){
2186     printf("Skipping %d frames\n",nskip);
2187     if(mri->nframes <= nskip){
2188       printf("   ERROR: can't skip, volume only has %d frames\n",
2189              mri->nframes);
2190       exit(1);
2191     }
2192     mri2 = fMRInskip(mri, nskip, NULL);
2193     if(mri2 == NULL) exit(1);
2194     MRIfree(&mri);
2195     mri = mri2;
2196   }
2197 
2198   /* ----- crops ---------*/
2199   if (crop_flag == TRUE){
2200     MRI *mri_tmp ;
2201     int   x0, y0, z0 ;
2202     
2203     x0 = crop_center[0] ; y0 = crop_center[1] ; z0 = crop_center[2] ; 
2204     
2205     x0 -= crop_size[0]/2 ; y0 -= crop_size[1]/2 ; z0 -= crop_size[2]/2 ;
2206     mri_tmp = MRIextract(mri, NULL, x0, y0, z0, crop_size[0], 
2207                          crop_size[1], crop_size[2]) ;
2208     MRIfree(&mri) ;
2209     mri = mri_tmp ;
2210   }
2211 
2212   /*------ Finally, write the output -----*/
2213   if(!no_write_flag)
2214     {
2215       printf("writing to %s...\n", out_name);
2216       if(force_out_type_flag){
2217         if(MRIwriteType(mri, out_name, out_volume_type) != NO_ERROR){
2218           printf("ERROR: failure writing %s as volume type %d\n",
2219                  out_name,out_volume_type);
2220           exit(1);
2221         }
2222       }
2223       else{
2224         if(MRIwrite(mri, out_name) != NO_ERROR){
2225           printf("ERROR: failure writing %s\n",out_name);
2226           exit(1);
2227         }
2228       }
2229     }
2230   // free memory
2231   MRIfree(&mri);
2232   MRIfree(&template);
2233 
2234   exit(0);
2235 
2236 } /* end main() */
2237   /*----------------------------------------------------------------------*/
2238 
2239 void get_ints(int argc, char *argv[], int *pos, int *vals, int nvals)
2240 {
2241 
2242   char *ep;
2243   int i;
2244 
2245   if(*pos + nvals >= argc)
2246     {
2247       fprintf(stderr, "\n%s: argument %s expects %d integers; "
2248               "only %d arguments after flag\n", 
2249               Progname, argv[*pos], nvals, argc - *pos - 1);
2250       usage_message(stdout);
2251       exit(1);
2252     }
2253 
2254   for(i = 0;i < nvals;i++)
2255     {
2256       if(argv[*pos+i+1][0] == '\0')
2257         {
2258           fprintf(stderr, "\n%s: argument to %s flag is null\n", 
2259                   Progname, argv[*pos]);
2260           usage_message(stdout);
2261           exit(1);
2262         }
2263 
2264       vals[i] = (int)strtol(argv[*pos+i+1], &ep, 10);
2265 
2266       if(*ep != '\0')
2267         {
2268           fprintf(stderr, "\n%s: error converting \"%s\" to an "
2269                   "integer for %s flag\n", 
2270                   Progname, argv[*pos+i+1], argv[*pos]);
2271           usage_message(stdout);
2272           exit(1);
2273         }
2274 
2275     }
2276 
2277   *pos += nvals;
2278 
2279 } /* end get_ints() */
2280 
2281 void get_floats(int argc, char *argv[], int *pos, float *vals, int nvals)
2282 {
2283 
2284   char *ep;
2285   int i;
2286 
2287   if(*pos + nvals >= argc)
2288     {
2289       fprintf(stderr, "\n%s: argument %s expects %d floats; "
2290               "only %d arguments after flag\n", 
2291               Progname, argv[*pos], nvals, argc - *pos - 1);
2292       usage_message(stdout);
2293       exit(1);
2294     }
2295 
2296   for(i = 0;i < nvals;i++)
2297     {
2298       if(argv[*pos+i+1][0] == '\0')
2299         {
2300           fprintf(stderr, "\n%s: argument to %s flag is null\n", 
2301                   Progname, argv[*pos]);
2302           usage_message(stdout);
2303           exit(1);
2304         }
2305 
2306       vals[i] = (float)strtod(argv[*pos+i+1], &ep);
2307 
2308       if(*ep != '\0')
2309         {
2310           fprintf(stderr, "\n%s: error converting \"%s\" to a "
2311                   "float for %s flag\n", 
2312                   Progname, argv[*pos+i+1], argv[*pos]);
2313           usage_message(stdout);
2314           exit(1);
2315         }
2316 
2317     }
2318 
2319   *pos += nvals;
2320 
2321 } /* end get_floats() */
2322 
2323 void get_string(int argc, char *argv[], int *pos, char *val)
2324 {
2325 
2326   if(*pos + 1 >= argc)
2327     {
2328       fprintf(stderr, "\n%s: argument %s expects an extra argument; "
2329               "none found\n", Progname, argv[*pos]);
2330       usage_message(stdout);
2331       exit(1);
2332     }  
2333 
2334   strcpy(val, argv[*pos+1]);
2335 
2336   (*pos)++;
2337 
2338 } /* end get_string() */
2339 
2340 void usage_message(FILE *stream)
2341 {
2342 
2343   fprintf(stream, "\n");
2344   fprintf(stream, "type %s -u for usage\n", Progname);
2345   fprintf(stream, "\n");
2346 
2347 } /* end usage_message() */
2348 
2349 void usage(FILE *stream)
2350 {
2351 
2352   fprintf(stream, "\n");
2353   fprintf(stream, "usage: %s [options] <in volume> <out volume>\n", 
2354           Progname);
2355   fprintf(stream, "\n");
2356   fprintf(stream, "options are:\n");
2357   fprintf(stream, "\n");
2358   fprintf(stream, "  -ro, --read_only\n");
2359   fprintf(stream, "  -nw, --no_write\n");
2360   fprintf(stream, "\n");
2361   fprintf(stream, "  -ii, --in_info\n");
2362   fprintf(stream, "  -oi, --out_info\n");
2363   fprintf(stream, "  -is, --in_stats\n");
2364   fprintf(stream, "  -os, --out_stats\n");
2365   fprintf(stream, "  -im, --in_matrix\n");
2366   fprintf(stream, "  -om, --out_matrix\n");
2367   fprintf(stream, "\n");
2368   fprintf(stream, "  -iis, --in_i_size <size>\n");
2369   fprintf(stream, "  -ijs, --in_j_size <size>\n");
2370   fprintf(stream, "  -iks, --in_k_size <size>\n");
2371   fprintf(stream, "  --force_ras_good : "
2372           "use default when orientation info absent\n");
2373   fprintf(stream, "\n");
2374   fprintf(stream, "  -iid, --in_i_direction "
2375           "<R direction> <A direction> <S direction>\n");
2376   fprintf(stream, "  -ijd, --in_j_direction "
2377           "<R direction> <A direction> <S direction>\n");
2378   fprintf(stream, "  -ikd, --in_k_direction "
2379           "<R direction> <A direction> <S direction>\n");
2380   fprintf(stream, "  --in_orientation orientation-string "
2381           ": see SETTING ORIENTATION\n");
2382   fprintf(stream, "\n");
2383   fprintf(stream, "  -ic, --in_center "
2384           "<R coordinate> <A coordinate> <S coordinate>\n");
2385   fprintf(stream, "\n");
2386   fprintf(stream, "  -oni, -oic, --out_i_count <count>\n");
2387   fprintf(stream, "  -onj, -ojc, --out_j_count <count>\n");
2388   fprintf(stream, "  -onk, -okc, --out_k_count <count>\n");
2389   fprintf(stream, "\n");
2390   fprintf(stream, "  -ois, --out_i_size <size>\n");
2391   fprintf(stream, "  -ojs, --out_j_size <size>\n");
2392   fprintf(stream, "  -oks, --out_k_size <size>\n");
2393   fprintf(stream, " --nskip n : skip the first n frames\n");
2394   fprintf(stream, " --ndrop n : drop the last n frames\n");
2395   fprintf(stream, "\n");
2396   fprintf(stream, "  -oid, --out_i_direction "
2397           "<R direction> <A direction> <S direction>\n");
2398   fprintf(stream, "  -ojd, --out_j_direction "
2399           "<R direction> <A direction> <S direction>\n");
2400   fprintf(stream, "  -okd, --out_k_direction "
2401           "<R direction> <A direction> <S direction>\n");
2402   fprintf(stream, "  --out_orientation orientation-string "
2403           ": see SETTING ORIENTATION\n");
2404   fprintf(stream, "\n");
2405   fprintf(stream, "  -oc, --out_center "
2406           "<R coordinate> <A coordinate> <S coordinate>\n");
2407   fprintf(stream, "\n");
2408   fprintf(stream, "  -odt, --out_data_type <uchar|short|int|float>\n");
2409   fprintf(stream, "\n");
2410   fprintf(stream, "  -rt, --resample_type "
2411           "<interpolate|weighted|nearest|sinc|cubic> "
2412           "(default is interpolate)\n");
2413   fprintf(stream, "\n");
2414   fprintf(stream, "  --no_scale flag <-ns>: 1 = dont rescale "
2415           "values for COR\n");
2416   fprintf(stream, "\n");
2417   fprintf(stream, "  -nc --nochange - don't change type of "
2418           "input to that of template\n");
2419   fprintf(stream, "\n");
2420   fprintf(stream, "\n");
2421   fprintf(stream, "  -tr TR : TR in msec\n");
2422   fprintf(stream, "  -te TE : TE in msec\n");
2423   fprintf(stream, "  -TI TI : TI in msec (note upper case flag)\n");
2424   fprintf(stream, "  -flip_angle flip angle : radians \n");
2425 
2426   fprintf(stream, "\n");
2427   fprintf(stream, "  --unwarp_gradient_nonlinearity \n"
2428           "      <sonata | allegra | GE> \n"
2429           "      <fullUnwarp | through-plane> \n"
2430           "      <JacobianCorrection | noJacobianCorrection> \n"
2431           "      <linear | sinc> \n"
2432           "      <sincInterpHW>  \n"); 
2433   fprintf(stream, "\n");
2434   fprintf(stream, "APPLYING TRANSFORMS (INCLUDING TALAIRACH)\n");
2435   fprintf(stream, "\n");
2436   fprintf(stream, "  --apply_transform xfmfile (-T or -at)\n");
2437   fprintf(stream, "  --apply_inverse_transform xfmfile (-ait)\n");
2438   fprintf(stream, "  --devolvexfm subjectid : \n");
2439   fprintf(stream, "  --crop <x> <y> <z> crop to 256 around "
2440           "center (x,y,z) \n");
2441   fprintf(stream, "  --cropsize <dx> <dy> <dz> crop to size "
2442           "<dx, dy, dz> \n");
2443   fprintf(stream, "  --like vol: output is embedded in "
2444           "a volume like vol\n");
2445   fprintf(stream, "\n");
2446 
2447   fprintf(stream,
2448           " The volume can be resampled into another space "
2449           "by supplying a \n"
2450           " transform using the --apply_transform flag. This reads the \n"
2451           " transform file and applies the transform "
2452           "(when --apply_inverse_transform \n"
2453           " is used, the transform is inverted and then applied). "
2454           "An example of a \n"
2455           " transform file is talairach.xfm as found in "
2456           "subjectid/mri/transforms. To \n"
2457           " convert a subject's orig volume to "
2458           "talairach space, execute the \n"
2459           " following lines:\n"
2460           "\n"
2461           "  cd subjectid/mri\n"
2462           "  mkdir talairach\n"
2463           "  mri_convert --apply_transform transforms/talariach.xfm \n"
2464           "     --devolvexfm subjectid -ic 0 0 0 \n"
2465           "     orig talairach\n"
2466           "\n"
2467           " This properly accounts for the case where the "
2468           "input volume does not \n"
2469           " have it's coordinate center at 0.\n"
2470           "\n"
2471           " To evaluate the result, run:\n"
2472           "\n"
2473           "    tkmedit -f $SUBJECTS_DIR/talairach/mri/orig "
2474           "-aux talairach\n"
2475           "\n"
2476           " The main and aux volumes should overlap very closely. "
2477           "If they do not, \n"
2478           " use tkregister2 to fix it (run tkregister --help for docs).\n"
2479           "\n");
2480 
2481   fprintf(stream, 
2482 
2483           "\n"
2484           "SPECIFYING THE INPUT AND OUTPUT FILE TYPES\n"
2485           "\n"
2486           "The file type can be specified in two ways. First, "
2487           "mri_convert will try \n"
2488           "to figure it out on its own from the format "
2489           "of the file name (eg, files that\n"
2490           "end in .img are assumed to be in spm analyze format). "
2491           "Second, the user can \n"
2492           "explicity set the type of file using --in_type "
2493           "and/or --out_type.\n"
2494           "\n"
2495           "Legal values for --in_type (-it) and --out_type (-ot) are:\n"
2496           "\n"
2497           "  cor           - MGH-NMR COR format (deprecated)\n"
2498           "  mgh           - MGH-NMR format\n"
2499           "  mgz           - MGH-NMR gzipped (compressed) mgh format\n"
2500           "  minc          - MNI's Medical Imaging NetCDF format "
2501           "(output may not work)\n"
2502           "  analyze       - 3D analyze (same as spm)\n"
2503           "  analyze4d     - 4D analyze \n"
2504           "  spm           - SPM Analyze format "
2505           "(same as analyze and analyze3d)\n"
2506           "  ge            - GE Genesis format (input only)\n"
2507           "  gelx          - GE LX (input only)\n"
2508           "  lx            - same as gelx\n"
2509           "  ximg          - GE XIMG variant (input only)\n"
2510           "  siemens       - Siemens IMA (input only)\n"
2511           "  dicom         - generic DICOM Format (input only)\n"
2512           "  siemens_dicom - Siemens DICOM Format (input only)\n"
2513           "  afni          - AFNI format\n"
2514           "  brik          - same as afni\n"
2515           "  bshort        - MGH-NMR bshort format\n"
2516           "  bfloat        - MGH-NMR bfloat format\n"
2517           "  sdt           - Varian (?)\n"
2518           "  outline       - MGH-NMR Outline format\n"
2519           "  otl           - same as outline\n"
2520           "  gdf           - GDF volume (requires image stem "
2521           "for output; use -gis)\n"
2522           "  nifti1        - NIfTI-1 volume (separate image "
2523           "and header files)\n"
2524           "  nii           - NIfTI-1 volume (single file)\n"
2525           "    if the input/output has extension .nii.gz, then compressed is used\n"
2526           "\n"
2527           "CONVERTING TO SPM-ANALYZE FORMAT \n"
2528           "\n"
2529           "Converting to SPM-Analyze format can be done in two ways, "
2530           "depending upon\n"
2531           "whether a single frame or multiple frames are desired. "
2532           "For a single frame,\n"
2533           "simply specify the output file name with a .img extension, "
2534           "and mri_convert \n"
2535           "will save the first frame into the file.  "
2536           "For multiple frames, specify the \n"
2537           "base as the output file name and add --out_type spm. "
2538           "This will save each \n"
2539           "frame as baseXXX.img where XXX is the three-digit, "
2540           "zero-padded frame number.\n"
2541           "Frame numbers begin at one. By default, the width "
2542           "the of zero padding is 3.\n"
2543           "This can be controlled with --in_nspmzeropad N where "
2544           "N is the new width.\n"
2545           "\n"  );
2546 
2547   printf("\n");
2548   printf("Other options\n");
2549   printf("\n");
2550   printf("  -r, --reorder olddim1 olddim2 olddim3\n");
2551   printf("\n");
2552   printf("  Reorders axes such that olddim1 is the "
2553          "new column dimension,\n");
2554   printf("  olddim2 is the new row dimension, "
2555          "olddim3 is the new slice \n");
2556   printf("  dimension. Example: 2 1 3 will swap rows and cols.\n");
2557   printf("\n");
2558   printf("  --invert_contrast threshold\n");
2559   printf("\n");
2560   printf("  All voxels in volume greater than threshold are replaced\n");
2561   printf("  with 255-value. Only makes sense for 8 bit images.\n");
2562   printf("  Only operates on the first frame.\n");
2563   printf("\n");
2564   printf("  -i, --input_volume\n");
2565   printf("  -o, --output_volume\n");
2566   printf("  -c, --conform\n");
2567   printf("            conform to 1mm voxel size in coronal "
2568          "slice direction with 256^3 or more\n"); 
2569   printf("  -cm, --conform_min\n");
2570   printf("            conform to the src min direction size\n");
2571   printf("  -cs, --conform_size size_in_mm\n");
2572   printf("            conform to the size given in mm\n");
2573   printf("  -po, --parse_only\n");
2574   printf("  -is, --in_stats\n");
2575   printf("  -os, --out_stats\n");
2576   printf("  -ro, --read_only\n");
2577   printf("  -nw, --no_write\n");
2578   printf("  -sn, --subject_name\n");
2579   printf("  -rl, --reslice_like\n");
2580   printf("  -tt, --template_type <type> (see above)\n");
2581   printf("  -f,  --frame\n");
2582   printf("  -sc, --scale <scale factor>\n") ;
2583   printf("  -il, --in_like\n");
2584   printf("  -roi\n");
2585   printf("  -fp, --fill_parcellation\n");
2586   printf("  -sp, --smooth_parcellation\n");
2587   printf("  -zo, --zero_outlines\n");
2588   printf("  -cf, --color_file\n");
2589   printf("  -nt, --no_translate\n");
2590   printf("  --status (status file for DICOM conversion)\n");
2591   printf("  --sdcmlist (list of DICOM files for conversion)\n");
2592   printf("  -ti, --template_info : dump info about template\n");
2593   printf("  -gis <gdf image file stem>\n");
2594   printf("  -zgez, --zero_ge_z_offset\n");
2595   printf("            set c_s=0 (appropriate for dicom "
2596          "files from GE machines with isocenter scanning)\n");
2597   //E/  printf("  -nozgez, --no_zero_ge_z_offset\n");
2598   //E/  printf("            don't set c_s=0, even if a GE volume\n");
2599   printf("\n");
2600   printf(
2601          "SPECIFYING THE ORIENTATION\n"
2602          "\n"
2603          "Ideally, the orientation information is derived from a "
2604          "DICOM file so that\n"
2605          "you have some confidence that it is correct. "
2606          "It is generally pretty easy\n"
2607          "to determine which direction Anterior/Posterior or "
2608          "Inferior/Superior are.\n"
2609          "Left/Right is very difficult. However, if you have "
2610          "some way of knowing which\n"
2611          "direction is which, you can use these options to "
2612          "incorporate this information \n"
2613          "into the header of the output format. For analyze files, "
2614          "it will be stored in\n"
2615          "the output.mat file. For NIFTI, it is stored as the "
2616          "qform matrix. For bshort/ \n"
2617          "bfloat, it is stored in the .bhdr file. "
2618          "For mgh/mgz it is internal.\n"
2619          "\n"
2620          "First of all, determining and setting the orientation "
2621          "is hard. Don't fool yourself\n"
2622          "into thinking otherwise. Second, don't think you are "
2623          "going to learn all you need\n"
2624          "to know from this documentation. Finally, you risk "
2625          "incorporating a left-right\n"
2626          "flip in your data if you do it incorrectly. OK, "
2627          "there are two ways to specify this \n"
2628          "information on the command-line. "
2629          "(1) explicitly specify the direction cosines\n"
2630          "with -iid, -ijd, -ikd. If you don't know what "
2631          "a direction cosine is, don't use\n"
2632          "this method. Instead, (2) specify an orientation string. \n"
2633          "\n"
2634          "--in-orientation  ostring\n"
2635          "--out-orientation ostring\n"
2636          "  \n"
2637          "  Supply the orientation information in the form "
2638          "of an orientation string \n"
2639          "  (ostring). The ostring is three letters that "
2640          "roughly describe how the volume\n"
2641          "  is oriented. This is usually described by the direction "
2642          "cosine information\n"
2643          "  as originally derived from the dicom but might not "
2644          "be available in all data\n"
2645          "  sets. You'll have to determine the correct "
2646          "ostring for your data.\n"
2647          "  The first  character of ostring determines the "
2648          "direction of increasing column.\n"
2649          "  The second character of ostring determines the "
2650          "direction of increasing row.\n"
2651          "  The third  character of ostring determines the "
2652          "direction of increasing slice.\n"
2653          "  Eg, if the volume is axial starting inferior and "
2654          "going superior the slice \n"
2655          "  is oriented such that nose is pointing up and "
2656          "the right side of the subject\n"
2657          "  is on the left side of the image, then this "
2658          "would correspond to LPS, ie,\n"
2659          "  as the column increases, you move to the patients left; "
2660          "as the row increases,\n"
2661          "  you move posteriorly, and as the slice increases, "
2662          "you move superiorly. Valid\n"
2663          "  letters are L, R, P, A, I, and S. There are 48 "
2664          "valid combinations (eg, RAS\n"
2665          "  LPI, SRI). Some invalid ones are DPS (D is not a "
2666          "valid letter), RRS (can't\n"
2667          "  specify R twice), RAP (A and P refer to the same axis). "
2668          "Invalid combinations\n"
2669          "  are detected immediately, an error printed, "
2670          "and the program exits. Case-insensitive.\n"
2671          "  Note: you can use tkregister2 to help determine "
2672          "the correct orientation string.\n"
2673          );
2674 
2675   printf("\n");
2676   printf("Notes: \n");
2677   printf("\n");
2678   printf("If the user specifies any of the direction "
2679          "cosines or orientation string,\n");
2680   printf("the ras_good_flag is set.\n");
2681   printf("\n");
2682 
2683 } /* end usage() */
2684 
2685 float findMinSize(MRI *mri, int *conform_width)
2686 {
2687   double xsize, ysize, zsize, minsize;
2688   double fwidth, fheight, fdepth, fmax;
2689   xsize = mri->xsize;
2690   ysize = mri->ysize;
2691   zsize = mri->zsize;
2692   // there are 3! = 6 ways of ordering
2693   //             xy  yz  zx
2694   // x > y > z    z min
2695   // x > z > y    y min  
2696   // z > x > y    y min
2697   //////////////////////////
2698   // y > x > z    z min
2699   // y > z > x    x min
2700   // z > y > x    x min
2701   if (xsize > ysize)
2702     minsize = (ysize > zsize) ? zsize : ysize;
2703   else
2704     minsize = (zsize > xsize) ? xsize : zsize;
2705 
2706   // now decide the conformed_width
2707   // algorighm ----------------------------------------------
2708   // calculate the size in mm for all three directions
2709   fwidth = mri->xsize*mri->width;
2710   fheight = mri->ysize*mri->height;
2711   fdepth = mri->zsize*mri->depth;
2712   // pick the largest
2713   if (fwidth> fheight)
2714     fmax = (fwidth > fdepth) ? fwidth : fdepth;
2715   else
2716     fmax = (fdepth > fheight) ? fdepth : fheight;
2717 
2718   *conform_width = (int) ceil(fmax/minsize);
2719   // just to make sure that if smaller than 256, use 256 anyway
2720   if (*conform_width < 256)
2721     *conform_width = 256;
2722 
2723   return (float) minsize;
2724 }
2725 /* EOF */
2726 
2727 // this function is called when conform is done
2728 int findRightSize(MRI *mri, float conform_size)
2729 {
2730   // user gave the conform_size
2731   double xsize, ysize, zsize;
2732   double fwidth, fheight, fdepth, fmax;
2733   int conform_width;
2734 
2735   xsize = mri->xsize;
2736   ysize = mri->ysize;
2737   zsize = mri->zsize;
2738 
2739   // now decide the conformed_width
2740   // calculate the size in mm for all three directions
2741   fwidth = mri->xsize*mri->width;
2742   fheight = mri->ysize*mri->height;
2743   fdepth = mri->zsize*mri->depth;
2744   // pick the largest
2745   if (fwidth> fheight)
2746     fmax = (fwidth > fdepth) ? fwidth : fdepth;
2747   else
2748     fmax = (fdepth > fheight) ? fdepth : fheight;
2749   // get the width with conform_size
2750   conform_width = (int) ceil(fmax/conform_size);
2751 
2752   // just to make sure that if smaller than 256, use 256 anyway
2753   if (conform_width < 256)
2754     conform_width = 256;
2755   // conform_width >= 256.   allow 10% leeway
2756   else if ((conform_width -256.)/256. < 0.1)
2757     conform_width = 256;
2758 
2759   // if more than 256, warn users
2760   if (conform_width > 256)
2761     {
2762       fprintf(stderr, "WARNING =================="
2763               "++++++++++++++++++++++++"
2764               "=======================================\n");
2765       fprintf(stderr, "The physical sizes are "
2766               "(%.2f mm, %.2f mm, %.2f mm), "
2767               "which cannot fit in 256^3 mm^3 volume.\n",
2768               fwidth, fheight, fdepth);
2769       fprintf(stderr, "The resulting volume will have %d slices.\n", 
2770               conform_width);
2771       fprintf(stderr, "The freesurfer tools should be "
2772               "able to handle more than 256 slices.\n");
2773       fprintf(stderr, "If you find problems, please let us know "
2774               "(freesurfer@nmr.mgh.harvard.edu).\n");
2775       fprintf(stderr, "=================================================="
2776               "++++++++++++++++++++++++"
2777               "===============\n\n");
2778     }
2779   return conform_width;
2780 }

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.