#include "nifti1_io.h"   /* directly include I/O library functions */

/*-----------------------------------------------*/
/*    cc -o nifti1_test -O2 nifti1_test.c -lm    */
/*-----------------------------------------------*/

/****************************************************************************/

int main( int argc , char *argv[] )
{
   nifti_image *nim ;
   int iarg=1 , outmode , ll , usegzip, debug = 0;
   char *tmpstr;
   
   if( argc < 2 || strcmp(argv[1],"-help") == 0 ){
     printf(
            "Usage: nifti1_test [-d level] [-n2|-n1|-na|-a2] infile [prefix]\n"
            "Usage: nifti1_test -nifti_hist\n"
            "\n"
            " If prefix is given, then the options mean:\n"
            "  -a2 ==> write an ANALYZE 7.5 file pair: prefix.hdr/prefix.img\n"
            "  -n2 ==> write a NIFTI-1 file pair: prefix.hdr/prefix.img\n"
            "  -n1 ==> write a NIFTI-1 single file: prefix.nii\n"
            "  -na ==> write a NIFTI-1 ASCII+binary file: prefix.nia\n"
            "  -za2 ==> write an ANALYZE 7.5 file pair: prefix.hdr.gz/prefix.img.gz\n"
            "  -zn2 ==> write a NIFTI-1 file pair: prefix.hdr.gz/prefix.img.gz\n"
            "  -zn1 ==> write a NIFTI-1 single file: prefix.nii.gz\n"
            "  -zna ==> write a NIFTI-1 ASCII+binary file: prefix.nia.gz\n"
            "\n"
            "     The default is '-n1'.\n"
            "\n"
            "  -d level    : specify a debug level (0-3)\n"
            "  -nifti_hist : display the nifti library modification history\n"
            "  -nifti_ver  : display the nifti library version number\n"
            "\n"
            " If prefix is not given, then the header info from infile\n"
            " file is printed to stdout.\n"
            "\n"
            " Please note that the '.nia' format is NOT part of the\n"
            " NIFTI-1 specification, but is provided mostly for ease\n"
            " of visualization (e.g., you can edit a .nia file and\n"
            " change some header fields, then rewrite it as .nii)\n"
           ) ;
     printf("\nsizeof(nifti_1_header)=%u\n",(unsigned int)sizeof(nifti_1_header)) ;
     exit(0) ;
   }

   usegzip = 0;
   outmode = 1;
   while ( (iarg < argc) && (argv[iarg][0] == '-') ) {
      if      ( ! strncmp(argv[iarg], "-a2",  3 ) )  outmode = 0;
      else if ( ! strncmp(argv[iarg], "-n1",  3 ) )  outmode = 1;
      else if ( ! strncmp(argv[iarg], "-n2",  3 ) )  outmode = 2;
      else if ( ! strncmp(argv[iarg], "-na",  3 ) )  outmode = 3;
      else if ( ! strncmp(argv[iarg], "-za2", 4 ) ){ outmode = 0; usegzip = 1; }
      else if ( ! strncmp(argv[iarg], "-zn1", 4 ) ){ outmode = 1; usegzip = 1; }
      else if ( ! strncmp(argv[iarg], "-zn2", 4 ) ){ outmode = 2; usegzip = 1; }
      else if ( ! strncmp(argv[iarg], "-zna", 4 ) ){ outmode = 3; usegzip = 1; }
      else if ( ! strncmp(argv[iarg], "-nifti_hist", 8 ) ) {
         nifti_disp_lib_hist();
         exit(0);
      }
      else if ( ! strncmp(argv[iarg], "-nifti_ver", 8 ) ) {
         nifti_disp_lib_version();
         exit(0);
      }
      else if ( ! strncmp(argv[iarg], "-d", 2 ) )  {
         iarg++;
         if( iarg >= argc ){
            fprintf(stderr,"** ERROR: missing debug level\n"); exit(1);
         }
         debug = atoi(argv[iarg]);
         nifti_set_debug_level(debug);
      }
      else {
         fprintf(stderr,"** ERROR: invalid option '%s'\n", argv[iarg]);
         exit(1);
      }

      iarg++;
   }

   if( iarg >= argc ){
     fprintf(stderr,"** ERROR: no input file on command line!?\n"); exit(1);
   }

   nim = nifti_image_read( argv[iarg++] , 1 ) ;
   if( nim == NULL ){ fprintf(stderr,"** image read failure\n"); exit(1) ; }

   /* if no output prefix, dump the image to the screen */
   if( iarg >= argc ){ nifti_image_infodump(nim); exit(0); }

   if( debug > 1 ){
      if( nim->fname )
         fprintf(stderr,"nift1_test: free header filename '%s'\n",nim->fname);
      if( nim->iname )
         fprintf(stderr,"nift1_test: free image filename '%s'\n",nim->iname);
   }

   nim->nifti_type = outmode ;
   if( nim->fname != NULL ) free(nim->fname) ;
   if( nim->iname != NULL ) free(nim->iname) ;

   ll = strlen(argv[iarg]) ;
   tmpstr = nifti_makebasename(argv[iarg]);
   nim->fname = (char *)calloc(1,ll+8) ; strcpy(nim->fname,tmpstr) ;
   nim->iname = (char *)calloc(1,ll+8) ; strcpy(nim->iname,tmpstr) ;
   free(tmpstr);
   if( nim->nifti_type == 1 ){
     strcat(nim->fname,".nii") ;
     strcat(nim->iname,".nii") ;
   } else if ( nim->nifti_type == 3 ){
     strcat(nim->fname,".nia") ;
     strcat(nim->iname,".nia") ;
   } else {
     strcat(nim->fname,".hdr") ;
     strcat(nim->iname,".img") ;
   }
   if (usegzip) {
     strcat(nim->fname,".gz");
     strcat(nim->iname,".gz");
   }
   if (debug > 0)
   {
      fprintf(stderr,"+d writing nifti output to '%s'", nim->fname);
      if ( nim->nifti_type == 2 ) fprintf(stderr," and '%s'", nim->iname);
      fputc('\n', stderr);
   }

   nifti_image_write( nim ) ;
   exit(0) ;
}
