From 46437f42a717ad4d8f33ce193ccc85f241c00da7 Mon Sep 17 00:00:00 2001
From: Matthew Webster <mwebster@fmrib.ox.ac.uk>
Date: Fri, 19 Oct 2018 11:15:53 +0000
Subject: [PATCH] Fixes to more closely match previous version

---
 fslcreatehd.cc | 59 +++++++++++++++++++++++++-------------------------
 1 file changed, 30 insertions(+), 29 deletions(-)

diff --git a/fslcreatehd.cc b/fslcreatehd.cc
index 43dd194..ef4f97b 100755
--- a/fslcreatehd.cc
+++ b/fslcreatehd.cc
@@ -1,7 +1,7 @@
 //     fslcreatehd.cc - Copy certain parts of an AVW header
 //     Mark Jenkinson, Steve Smith and Matthew Webster, FMRIB Image Analysis Group
-//     Copyright (C) 2001-2005 University of Oxford  
-//     CCOPYRIGHT  
+//     Copyright (C) 2001-2005 University of Oxford
+//     CCOPYRIGHT
 
 #include "newimage/newimageall.h"
 #include <fstream>
@@ -9,12 +9,15 @@
 
 using namespace NEWIMAGE;
 
-void print_usage(const string& progname) 
+void print_usage(const string& progname)
 {
   cout << endl;
   cout << "Usage: fslcreatehd <xsize> <ysize> <zsize> <tsize> <xvoxsize> <yvoxsize> <zvoxsize> <tr> <xorigin> <yorigin> <zorigin> <datatype> <headername>" << endl;
   cout << "       fslcreatehd <nifti_xml_file> <headername>" << endl;
   cout << "  Datatype values: " << DT_UNSIGNED_CHAR << "=char, " << DT_SIGNED_SHORT  << "=short, " <<  DT_SIGNED_INT <<"=int, " << DT_FLOAT << "=float, " <<  DT_DOUBLE<< "=double" << endl;
+  cout << "  In the first form, a radiological image will be created, the origin being given as voxel co-ordindates," << endl;
+  cout << "  if the output file already exists, its data ( but not geometric information ) will be copied if it has" << endl;
+  cout << "  a matching number of elements."
   cout << "  In the second form, an XML-ish form of nifti header is read (as output by fslhd -x)" << endl;
   cout << "  Note that stdin is used if '-' is used in place of a filename" << endl;
 
@@ -30,10 +33,10 @@ int fslcreatehd_main(int argc, char *argv[])
   string filename;
   int fileread(1);
   bool existingImage(false);
-  
+
   if (argc==3) /* use the XML form of header specification */
     filename = string(argv[2]);
-  else 
+  else
     filename = string(argv[13]);
 
   /* check if file already exists and if so, read the image contents */
@@ -43,11 +46,11 @@ int fslcreatehd_main(int argc, char *argv[])
     header=reader.loadImage(filename,buffer,extensions);
     originalHeader=header;
   }
-  
+
 
   if (argc>3) {
     /* set uninteresting defaults */
-    if (!existingImage) 
+    if (!existingImage)
       header.datatype=atoi(argv[12]);
     header.dim[0]=4;
     for (int i=1;i<=4;i++)
@@ -57,40 +60,40 @@ int fslcreatehd_main(int argc, char *argv[])
 
 
     if ( atoi(argv[9]) != 0 || atoi(argv[10]) != 0 || atoi(argv[11]) != 0 ) {
-      header.sX[3]=atoi(argv[9]);
-      header.sY[3]=atoi(argv[10]);
-      header.sZ[3]=atoi(argv[11]);
-      header.sX[0]=atoi(argv[5]);
-      header.sY[1]=atoi(argv[6]);
-      header.sZ[2]=atoi(argv[7]);
+      header.sX[0]=-header.pixdim[1];
+      header.sY[1]=header.pixdim[2];
+      header.sZ[2]=header.pixdim[3];
+      header.sX[3]=-header.pixdim[1]*(header.dim[1]-1.0-atoi(argv[9]));
+      header.sY[3]=-header.pixdim[2]*atoi(argv[10]);
+      header.sZ[3]=-header.pixdim[3]*atoi(argv[11]);
     }
   } else {
       /* read XML form */
       char *newstr, *oldfname, *oldiname;
       ifstream inputfile;
       vector<string> settings;
-      
+
       if (strcmp(argv[1],"-")==0) {fileread=0;}
       newstr = (char *)calloc(10000,1);
       oldfname = (char *)calloc(10000,1);
       oldiname = (char *)calloc(10000,1);
-      if (fileread) 
+      if (fileread)
       {
-	inputfile.open (argv[1], ifstream::in | ifstream::binary);
+	      inputfile.open (argv[1], ifstream::in | ifstream::binary);
         if (!inputfile.is_open())
-        {      
+        {
 	      cerr << "Cannot open file " << argv[1] << endl;
 	      return EXIT_FAILURE;
 	}
       }
-       
-      do 
+
+      do
       {
-	  if (fileread) 
+	  if (fileread)
           {
 	    inputfile.getline(newstr,9999);  // maybe use > for delimiting character remove while increase size
-	  } 
-          else 
+	  }
+          else
           {
 	      if (fgets(newstr,9999,stdin)==NULL) break;
 	  }
@@ -99,7 +102,7 @@ int fslcreatehd_main(int argc, char *argv[])
 
       header=NiftiHeader(settings);
 
-      if (fileread) 
+      if (fileread)
 	inputfile.close();
   }
 
@@ -117,7 +120,7 @@ int fslcreatehd_main(int argc, char *argv[])
     fill(buffer,buffer+header.nElements(),0);
   }
 
-    int filetype=FslGetEnvOutputType(); 
+    int filetype=FslGetEnvOutputType();
     if(!existingImage) {
       filename=make_basename(filename)+outputExtension(filetype);
     }
@@ -132,12 +135,10 @@ int fslcreatehd_main(int argc, char *argv[])
 
 int main(int argc,char *argv[])
 {
-  if (argc != 14 && argc != 3) 
+  if (argc != 14 && argc != 3)
   {
     print_usage(string(argv[0]));
-    return 1; 
+    return 1;
   }
-  return fslcreatehd_main(argc,argv); 
+  return fslcreatehd_main(argc,argv);
 }
-
-
-- 
GitLab