Skip to content
Snippets Groups Projects
fslchfiletype.cc 1.83 KiB
//     fslchfiletype.cc  Conversion program for different image types
//     Matthew Webster, FMRIB Image Analysis Group
//     Copyright (C) 2008-2019 University of Oxford
//     CCOPYRIGHT

#include "newimage/newimageall.h"
#include "newimage/fmribmain.h"
#include <cstdlib>

using namespace NEWIMAGE;


int printUsage(const string& progname)
{
  const vector<pair<string,int> > formats=NEWIMAGE::fileFormats::allFormats();
  cout << "Usage: fslchfiletype[_exe] <filetype> <filename> [filename2]" << endl << endl;
  cout << "Changes the file type of the image file [ or copies to new file ]." << endl;
  cout << "Valid values for filetype are:" << endl;
  for ( std::vector<pair<string,int> >::const_iterator it=formats.begin(); it != formats.end(); it++ )
    cout << "\t" << it->first << endl;
  return 1;
}


template <class T>
int fmrib_main(int argc, char *argv[])
{
  volume<T> input;
  read_volume4D(input,string(argv[2]));
  int format(-1);
  const vector<pair<string,int> > formats=NEWIMAGE::fileFormats::allFormats();
  for ( std::vector<pair<string,int> >::const_iterator it=formats.begin(); it != formats.end(); it++ )
    if ( string(argv[1]) == it->first ) format = it->second;
  if ( format == -1 )
    return printUsage(string(argv[0]));
  string outputFile( argc == 4 ? string(argv[3]) : string(argv[2]) );
  save_volume_dtype(input,outputFile,dtype(string(argv[2])),format);
  return 0;
}


int main(int argc,char *argv[])
{
  if (argc < 3 || argc > 4)
    return printUsage(string(argv[0]));

  string inputFile(argv[2]);
  string outputFile( argc == 4 ? string(argv[3]) : string(argv[2]) );

  if (dtype(inputFile)==DT_COMPLEX)
  {
    volume4D<float> real, imaginary;
    read_complexvolume(real, imaginary, inputFile);
    save_complexvolume(real, imaginary, outputFile);
    return 0;
  }
  call_fmrib_main(dtype(inputFile),argc,argv);
  return 0;
}