From d7553ddc38f77d41741fc483ae78a341f45f9767 Mon Sep 17 00:00:00 2001
From: Matthew Webster <mwebster@fmrib.ox.ac.uk>
Date: Fri, 9 Jan 2015 15:03:25 +0000
Subject: [PATCH] Current Version Jan 15

---
 fslcpgeom.cc | 76 +++++++++++++++++-----------------------------------
 1 file changed, 24 insertions(+), 52 deletions(-)

diff --git a/fslcpgeom.cc b/fslcpgeom.cc
index ab481b1..9977a84 100755
--- a/fslcpgeom.cc
+++ b/fslcpgeom.cc
@@ -17,70 +17,42 @@ void print_usage(const string& progname)
 
 int avwcpgeom_main(int argc, char *argv[])
 {
-  FSLIO *src = NULL, *dest = NULL, *destcopy = NULL;
-  short x, y, z, v, copydim=1, t, scode, qcode, dt=-1;
-  float vx, vy, vz, tr;
-  int filetype;
-  mat44 smat, qmat;
-  void *buffer = NULL;
-  char desthdrname[10000];
-  size_t nbytes, nsrcbytes, ndestbytes;
-
-  if (argc>3)
-    copydim=0;
-    
-  src = FslOpen(argv[1], "r");
-
-  dest = FslOpen(argv[2], "r");
-  destcopy = (FSLIO *)calloc(sizeof(FSLIO),1);
-  FslCloneHeader(destcopy,dest);
-
-  if ((src == NULL) || (dest == NULL)) {
+  NiftiIO reader;
+  NiftiHeader headerSrc,headerDest;
+  string fullDestinationName(return_validimagefilename(string(argv[2])));
+  try {
+    headerSrc=reader.loadHeader(return_validimagefilename(string(argv[1])));
+    headerDest=reader.loadHeader(fullDestinationName);
+  } catch (...) { 
     perror("Error opening files");
     return EXIT_FAILURE;
   }
 
-  FslGetDim(src, &x, &y, &z, &v);
-  nsrcbytes = x * y * z * v * (FslGetDataType(dest, &t) / 8);
-  FslGetDim(dest, &x, &y, &z, &v); 
-  ndestbytes = x * y * z * v * (FslGetDataType(dest, &t) / 8);
-  if (nsrcbytes > ndestbytes) nbytes=nsrcbytes; else nbytes=ndestbytes;
-  if( (buffer = calloc(nbytes,1)) == NULL ) {
+  short copydim=1;
+  char *buffer = NULL;
+  size_t nbytes(max(headerSrc.nElements(),headerDest.nElements())*headerDest.bitsPerVoxel/8);
+
+  if( (buffer = (char*)calloc(nbytes,1)) == NULL ) {
     perror("Unable to allocate memory for copy");
     return EXIT_FAILURE;
   }
-  FslReadVolumes(dest, buffer, v);
 
+  if (argc>3)
+    copydim=0;
 
-  strcpy(desthdrname,dest->niftiptr->fname);
-  filetype = FslGetFileType(dest);
-  FslGetDataType(dest,&dt);
-  FslClose(dest);
-  dest = FslXOpen(desthdrname, "wb", filetype);
-  FslCloneHeader(dest,destcopy); 
+  vector<NiftiExtension> extensions;
+  reader.loadImage(fullDestinationName,buffer,extensions,false);
 
-  scode = FslGetStdXform(src,&smat);
-  FslSetStdXform(dest,scode,smat);
-  qcode = FslGetRigidXform(src,&qmat);
-  FslSetRigidXform(dest,qcode,qmat);
-  
-  FslGetVoxDim(src, &vx, &vy, &vz, &tr);
-  FslSetVoxDim(dest, vx, vy, vz, tr);
+  headerDest.sformCode = headerSrc.sformCode;
+  headerDest.setSForm(headerSrc.getSForm());
+  headerDest.qformCode = headerSrc.qformCode;
+  headerDest.setSForm(headerSrc.getQForm());
+  headerDest.pixdim=headerSrc.pixdim;
   
-  if (copydim) {
-    FslGetDim(src, &x, &y, &z, &v);
-    FslSetDim(dest, x, y, z, v);
-  }
-
-
-  /* Preserve the datatype - probably unneccesary now, but left for safety */ 
-  FslSetDataType(dest,dt);
-
- FslWriteHeader(dest);
- FslWriteVolumes(dest, buffer, v);
- FslClose(dest);
+  if (copydim)
+    headerDest.dim=headerSrc.dim;
 
- FslClose(src);
+  reader.saveImage(fullDestinationName,buffer,extensions,headerDest,FslIsCompressedFileType(fslFileType(fullDestinationName)));  
 
   return 0;
 }
-- 
GitLab