From 72f5364eb052738319e25095501d6dbaf59f0393 Mon Sep 17 00:00:00 2001
From: Mark Jenkinson <mark@fmrib.ox.ac.uk>
Date: Tue, 4 Nov 2008 12:38:33 +0000
Subject: [PATCH] Added memory efficient read_binary_matrix routines

---
 miscmaths.cc | 39 ++++++++++++++++++++++++++-------------
 miscmaths.h  |  2 ++
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/miscmaths.cc b/miscmaths.cc
index eab3b18..5e9cf87 100644
--- a/miscmaths.cc
+++ b/miscmaths.cc
@@ -191,21 +191,35 @@ namespace MISCMATHS {
 
   ReturnMatrix read_binary_matrix(const string& filename)
   {
-    Matrix mat;
-    if ( filename.size()<1 ) return mat;
+    Matrix mres;
+    read_binary_matrix(mres,filename);
+    mres.Release();
+    return mres;
+  }
+
+
+  int read_binary_matrix(Matrix& mres, const string& filename)
+  {
+    if ( filename.size()<1 ) return 1;
     ifstream fs(filename.c_str(), ios::in | ios::binary);
     if (!fs) { 
       cerr << "Could not open matrix file " << filename << endl;
-      return mat;
+      return 2;
     }
-    mat = read_binary_matrix(fs);
+    read_binary_matrix(mres,fs);
     fs.close();
-    mat.Release();
-    return mat;
+    return 0;
   }
 
-
   ReturnMatrix read_binary_matrix(ifstream& fs)
+  {
+    Matrix mres;
+    read_binary_matrix(mres,fs);
+    mres.Release();
+    return mres;
+  }
+
+  int read_binary_matrix(Matrix& mres, ifstream& fs)
   {
     bool swapbytes = false;
     unsigned int testval;
@@ -216,9 +230,7 @@ namespace MISCMATHS {
       Swap_Nbytes(1,sizeof(testval),&testval);
       if (testval!=BINFLAG) { 
 	cerr << "Unrecognised binary matrix file format" << endl;
-	Matrix mres;
-	mres.Release();
-	return mres;
+	return 2;
       }
     }
 
@@ -235,7 +247,9 @@ namespace MISCMATHS {
 
     // set up and read matrix (rows fast, cols slow)
     double val;
-    Matrix mres(nx,ny);
+    if ( (((unsigned int) mres.Ncols())!=ny) || (((unsigned int) mres.Nrows())<nx) ) {
+      mres.ReSize(nx,ny);
+    }
     for (unsigned int y=1; y<=ny; y++) {
       for (unsigned int x=1; x<=nx; x++) {
 	fs.read((char*)&val,sizeof(val));
@@ -244,8 +258,7 @@ namespace MISCMATHS {
       }
     }
     
-    mres.Release();
-    return mres;
+    return 0;
   }
 
 
diff --git a/miscmaths.h b/miscmaths.h
index 48de2bf..197ae44 100644
--- a/miscmaths.h
+++ b/miscmaths.h
@@ -55,6 +55,7 @@ namespace MISCMATHS {
   ReturnMatrix read_ascii_matrix(int nrows, int ncols, const string& filename);
   ReturnMatrix read_ascii_matrix(const string& filename);
   ReturnMatrix read_vest(string p_fname);
+  int read_binary_matrix(Matrix& mres, const string& filename);
   ReturnMatrix read_binary_matrix(const string& filename);
   ReturnMatrix read_matrix(const string& filename);
 
@@ -71,6 +72,7 @@ namespace MISCMATHS {
   ReturnMatrix read_ascii_matrix(ifstream& fs, int nrows, int ncols);
   ReturnMatrix read_ascii_matrix(int nrows, int ncols, ifstream& fs);
   ReturnMatrix read_ascii_matrix(ifstream& fs);
+  int read_binary_matrix(Matrix& mres, ifstream& fs);
   ReturnMatrix read_binary_matrix(ifstream& fs);
   int write_ascii_matrix(const Matrix& mat, ofstream& fs, int precision=-1);
   int write_ascii_matrix(ofstream& fs, const Matrix& mat, int precision=-1);
-- 
GitLab