From 085f8f85f35aef127ea5911f255e4182b7398d3b Mon Sep 17 00:00:00 2001
From: Matthew Webster <mwebster@fmrib.ox.ac.uk>
Date: Tue, 13 Nov 2007 10:40:57 +0000
Subject: [PATCH] improved -k mem requirements for 3D mask

---
 fslstats.cc | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/fslstats.cc b/fslstats.cc
index 1fa4540..1056bb4 100644
--- a/fslstats.cc
+++ b/fslstats.cc
@@ -130,13 +130,15 @@ int generate_masks(volume4D<float> &mask, volume4D<float> &masknz, const volume4
   if (!lthr_used) { lthr=vin.min()-1; }
   if (!uthr_used) { uthr=vin.max()+1; }
   mask = binarise(vin,lthr,uthr,exclusive);
-  masknz = mask * (1.0f - binarise(vin,0.0f, 0.0f));
+  if (mask.tsize()!=1) masknz = (binarise(vin,0.0f, 0.0f)-1.0f)*-1.0f*mask;
+  else masknz = (binarise(vin,0.0f, 0.0f)-1.0f)*mask[0];
   return 0;
 }
 
 int generate_masks(const volume4D<float> &mask, volume4D<float> &masknz, const volume4D<float> &vin)
 {
-  masknz = mask * (1.0f - binarise(vin,0.0f, 0.0f));
+  if (mask.tsize()!=1) masknz = (binarise(vin,0.0f, 0.0f)-1.0f)*-1.0f*mask;
+  else masknz = (binarise(vin,0.0f, 0.0f)-1.0f)*mask[0];
   return 0;
 }
 
@@ -273,7 +275,7 @@ int fmrib_main_float(int argc, char* argv[])
 	cerr << "Mask and image must be the same size" << endl;
 	exit(3);
       }
-      if ( mask.tsize() != vol.tsize() ) {
+      if ( mask.tsize() != vol.tsize() && mask.tsize() != 1) {
 	// copy the last max volume until the correct size is reached
 	while (mask.tsize() < vol.tsize() ) {
    	  mask.addvolume(mask[mask.maxt()]);
@@ -290,7 +292,8 @@ int fmrib_main_float(int argc, char* argv[])
         mask.binarise(1);
       }
       generate_masks(mask,masknz,vin);
-      vol = vin * mask;
+        if (mask.tsize()!=1) vol=vin*mask; 
+	else vol=vin*mask[0];
     } else if (sarg=="-l") {
       narg++;
       if (narg<argc) {
-- 
GitLab