diff --git a/fslorient.cc b/fslorient.cc index 3e4166ba21d6fcf8ddfb5841f16be3a560cb2fd7..d66704afcc29410c186e2a21f69829ca55550f52 100644 --- a/fslorient.cc +++ b/fslorient.cc @@ -17,16 +17,30 @@ void print_usage() { cout << "Usage: " << progname << " <main option> <filename>" << endl; cout << endl; cout << " where the main option is one of:" << endl; - cout << " -forceradiological (makes radiological header)" << endl; - cout << " -forceneurological (makes neurological header - not Analyze)" << endl; - cout << " -swaporient (swaps radiological and neurological)" << endl; - cout << " -getorient (prints left-right orientation)" << endl; - cout << " -deleteorient (removes orient info from header - not Analyze)" << endl; + cout << " -getorient (prints FSL left-right orientation)" << endl; + cout << " -getsform (prints the 16 elements of the sform matrix)" << endl; + cout << " -getqform (prints the 16 elements of the qform matrix)" << endl; + cout << " -setsform <m11 m12 ... m44> (sets the 16 elements of the sform matrix)" << endl; + cout << " -setqform <m11 m12 ... m44> (sets the 16 elements of the qform matrix)" << endl; + cout << " -getsformcode (prints the sform integer code)" << endl; + cout << " -getqformcode (prints the qform integer code)" << endl; + cout << " -setsformcode <code> (sets sform integer code)" << endl; + cout << " -setqformcode <code> (sets qform integer code)" << endl; + cout << " -copysform2qform (sets the qform equal to the sform - code and matrix)" << endl; + cout << " -copyqform2sform (sets the sform equal to the qform - code and matrix)" << endl; + cout << " -deleteorient (removes orient info from header)" << endl; + cout << " -forceradiological (makes FSL radiological header)" << endl; + cout << " -forceneurological (makes FSL neurological header - not Analyze)" << endl; + cout << " -swaporient (swaps FSL radiological and FSL neurological)" << endl; cout << endl; - cout << " Note: The stored data order is never changed here - only the header info." << endl; + cout << " Note: ANALYZE files are NOT modified by any of the commands" << endl; + cout << " - they are for NIFTI files ONLY!" << endl; + cout << " For NIFTI: the stored data order is never changed here - only the header info." << endl; cout << " To change the data storage use avwswapdim." << endl; cout << endl; cout << " e.g. " << progname << " -forceradiological myimage" << endl; + cout << " " << progname << " -copysform2qform myimage" << endl; + cout << " " << progname << " -setsform -2 0 0 90 0 2 0 -126 0 0 2 -72 0 0 0 1 myimage" << endl; cout << endl; } @@ -42,51 +56,144 @@ void swaporient(volume4D<T>& invol) { invol.set_qform(invol.qform_code(),invol.qform_mat() * swapmat); } + +template <class T> +int getorient(const volume4D<T>& invol) +{ + if (invol.left_right_order()==FSL_RADIOLOGICAL) { + cout << "RADIOLOGICAL" << endl; + } else { + cout << "NEUROLOGICAL" << endl; + } + return 0; +} + + +template <class T> +int getorient(const string& filename) +{ + volume4D<T> invol; + read_volume4D(invol,filename); + return getorient(invol); +} + + +int showmat(const Matrix& mat) { + for (int a=1; a<=4; a++) { + for (int b=1; b<=4; b++) { + cout << mat(a,b) << " "; + } + } + cout << endl; + return 0; +} + + +int testminargs(int argnum, int argc) { + if (argc < (argnum + 1)) { + print_usage(); + exit(EXIT_FAILURE); + } + return 0; +} + + template <class T> int fmrib_main(int argc,char *argv[]) { + bool modified=false; int retval=0; - string option=argv[1], filename=argv[2]; + string option=argv[1], filename; volumeinfo volinfo; volume4D<T> invol; + + filename=argv[argc-1]; read_volume4D(invol,filename,volinfo); - if (option=="-forceradiological") { + if (option=="-getorient") { + getorient(invol); + } else if (option=="-getsformcode") { + cout << invol.sform_code() << endl; + } else if (option=="-getqformcode") { + cout << invol.qform_code() << endl; + } else if (option=="-getqform") { + showmat(invol.qform_mat()); + } else if (option=="-getsform") { + showmat(invol.sform_mat()); + } else if (option=="-setsformcode") { + testminargs(3,argc); + modified=true; + int code = (int) atof(argv[2]); + invol.set_sform(code,invol.sform_mat()); + } else if (option=="-setqformcode") { + testminargs(3,argc); + modified=true; + int code = (int) atof(argv[2]); + invol.set_qform(code,invol.qform_mat()); + } else if ( (option=="-setqform") || (option=="-setsform") ) { + testminargs(18,argc); + modified=true; + Matrix mat(4,4); + for (int a=1; a<=4; a++) { + for (int b=1; b<=4; b++) { + mat(a,b)=atof(argv[a*4+b-3]); + } + } + // override the last line + mat(4,1)=0; mat(4,2)=0; mat(4,3)=0; mat(4,4)=1; + if (option=="-setqform") { + invol.set_qform(invol.qform_code(),mat); + } + if (option=="-setsform") { + invol.set_sform(invol.sform_code(),mat); + } + } else if (option=="-copysform2qform") { + modified=true; + invol.set_qform(invol.sform_code(),invol.sform_mat()); + } else if (option=="-copyqform2sform") { + modified=true; + invol.set_sform(invol.qform_code(),invol.qform_mat()); + } else if (option=="-swaporient") { + modified=true; + swaporient(invol); + } else if (option=="-deleteorient") { + modified=true; + invol.set_sform(NIFTI_XFORM_UNKNOWN,invol.sform_mat()); + invol.set_qform(NIFTI_XFORM_UNKNOWN,invol.qform_mat()); + } else if (option=="-forceradiological") { + modified=true; if (invol.left_right_order()==FSL_NEUROLOGICAL) { swaporient(invol); } } else if (option=="-forceneurological") { + modified=true; if (invol.left_right_order()==FSL_RADIOLOGICAL) { swaporient(invol); } - } else if (option=="-swaporient") { - swaporient(invol); - } else if (option=="-getorient") { - if (invol.left_right_order()==FSL_RADIOLOGICAL) { - cout << "RADIOLOGICAL" << endl; + } else{ + // does the first arg exist as an image file? + if (fsl_imageexists(string(argv[1]))) { + getorient<T>(string(argv[1])); } else { - cout << "NEUROLOGICAL" << endl; + cerr << "Unrecognised option: " << option << endl; + print_usage(); + retval = -1; } - return 0; - } else if (option=="-deleteorient") { - invol.set_sform(NIFTI_XFORM_UNKNOWN,invol.sform_mat()); - invol.set_qform(NIFTI_XFORM_UNKNOWN,invol.qform_mat()); - } else{ - cerr << "Unrecognised option: " << option << endl; - print_usage(); - retval = -1; } - if (FslBaseFileType(FslGetFileType(&volinfo))!=FSL_TYPE_ANALYZE) { - FslSetOverrideOutputType(FslGetFileType(&volinfo)); - write_volume4D(invol,filename,volinfo); - FslSetOverrideOutputType(-1); // restore to default - } else { - cerr << "Cannot modify orientation for Analyze files" << endl; - cerr << " All Analyze files are treated as radiological" << endl; - cerr << " To change the data storage use avwswapdim" << endl; - retval=-1; + + if (modified) { + if (FslBaseFileType(FslGetFileType(&volinfo))!=FSL_TYPE_ANALYZE) { + FslSetOverrideOutputType(FslGetFileType(&volinfo)); + write_volume4D(invol,filename,volinfo); + FslSetOverrideOutputType(-1); // restore to default + } else { + cerr << "Cannot modify orientation for Analyze files" << endl; + cerr << " All Analyze files are treated as radiological" << endl; + cerr << " To change the data storage use avwswapdim" << endl; + retval=-1; + } } return retval; } @@ -94,12 +201,13 @@ int fmrib_main(int argc,char *argv[]) int main(int argc,char *argv[]) { - if (argc<3) { + if (argc<2) { print_usage(); return -1; } - string inname = argv[2]; + string inname; + inname = argv[argc-1]; // call the templated main return call_fmrib_main(dtype(inname),argc,argv); }