From 14b631b0009e07f36623ec878345b2a3b6c39774 Mon Sep 17 00:00:00 2001
From: Matthew Webster <mwebster@fmrib.ox.ac.uk>
Date: Wed, 11 Feb 2009 15:10:08 +0000
Subject: [PATCH] removed globals

---
 fslstats.cc | 67 +++++++++++++++++++----------------------------------
 1 file changed, 24 insertions(+), 43 deletions(-)

diff --git a/fslstats.cc b/fslstats.cc
index 58f7c77..0ee67f5 100644
--- a/fslstats.cc
+++ b/fslstats.cc
@@ -2,7 +2,7 @@
 
     Mark Jenkinson and Matthew Webster, FMRIB Image Analysis Group
 
-    Copyright (C) 2003-2007 University of Oxford  */
+    Copyright (C) 2003-2009 University of Oxford  */
 
 /*  CCOPYRIGHT  */
 
@@ -11,13 +11,7 @@
 #include "newimage/costfns.h"
 #include "utils/fsl_isfinite.h"
 
-
 using namespace NEWIMAGE;
-using namespace MISCMATHS;
-
-bool masks_used=false;
-bool lthr_used=false;
-bool uthr_used=false;
 
 void print_usage(const string& progname) {
   cout << "Usage: fslstats <input> [options]" << endl << endl;
@@ -49,7 +43,6 @@ void print_usage(const string& progname) {
   cout << "Note - thresholds are not inclusive ie lthresh<allowed<uthresh" << endl;
 }
 
-
 // Some specialised nonzero functions just for speedup
 //  (it avoids generating masks when not absolutely necessary)
 
@@ -119,29 +112,18 @@ double nonzerostddev(const volume4D<float>& vol)
   return std::sqrt(var);
 }
 
-int generate_masks(volume4D<float> &mask, volume4D<float> &masknz, const volume4D<float> &vin,float& lthr, float& uthr) 
+int generateNonZeroMask(const volume4D<float> &mask, volume4D<float> &masknz, const volume4D<float> &input)
 {
-  if (!lthr_used) { lthr=vin.min()-1; }
-  if (!uthr_used) { uthr=vin.max()+1; }
-  mask = binarise(vin,lthr,uthr,exclusive);
-  if (mask.tsize()!=1) {
-    masknz.reinitialize(mask.xsize(),mask.ysize(),mask.zsize(),mask.tsize());
-    for (int t=mask.mint(); t<=mask.maxt(); t++) 
-      masknz[t]=((binarise(vin[t],0.0f, 0.0f)-1.0f)*-1.0f*mask[t]);
-  }
-  else masknz = (binarise(vin,0.0f, 0.0f)-1.0f)*-1.0f*mask[0];
+  masknz.reinitialize(mask.xsize(),mask.ysize(),mask.zsize(),input.tsize());
+  for (int t=input.mint(); t<=input.maxt(); t++) 
+    masknz[t]=((binarise(input[t],0.0f, 0.0f)-1.0f)*-1.0f*mask[t % mask.tsize()]);
   return 0;
 }
 
-int generate_masks(const volume4D<float> &mask, volume4D<float> &masknz, const volume4D<float> &vin)
+int generate_masks(volume4D<float> &mask, volume4D<float> &masknz, const volume4D<float> &input,float& lthr, float& uthr) 
 {
-  if (mask.tsize()!=1) {
-    masknz.reinitialize(mask.xsize(),mask.ysize(),mask.zsize(),mask.tsize());
-    for (int t=mask.mint(); t<=mask.maxt(); t++) 
-      masknz[t]=((binarise(vin[t],0.0f, 0.0f)-1.0f)*-1.0f*mask[t]);
-  }
-  else masknz = (binarise(vin,0.0f, 0.0f)-1.0f)*-1.0f*mask[0];
-  return 0;
+  mask = binarise(input,lthr,uthr,exclusive);
+  return generateNonZeroMask(mask,masknz,input);
 }
 
 int fmrib_main_float(int argc, char* argv[]) 
@@ -155,7 +137,8 @@ int fmrib_main_float(int argc, char* argv[])
   volume4D<float> vol, mask, masknz;
   read_volume4D(vol,argv[1]);
 
-  float lthr=0, uthr=0;  // these initial values are not used
+  float lthr(vol.min()-1);
+  float uthr=(vol.max()+1);    
 
   int narg=2;
   string sarg;
@@ -170,10 +153,10 @@ int fmrib_main_float(int argc, char* argv[])
               if (!isfinite((double)vol(x,y,z,t)))
 	        vol(x,y,z,t)=0;
     } else if (sarg=="-m") {
-      if (masks_used) cout <<  vol.mean(mask) << " ";
+      if (mask.nvoxels()>0) cout <<  vol.mean(mask) << " ";
       else cout << vol.mean() << " ";
     } else if (sarg=="-M") {
-      if (masks_used) cout << vol.mean(masknz) << " ";
+      if (masknz.nvoxels()>0) cout << vol.mean(masknz) << " ";
       else {
 	double nzmean=0;
 	nzmean = nonzeromean(vol);
@@ -182,7 +165,7 @@ int fmrib_main_float(int argc, char* argv[])
     } else if (sarg=="-X") {
       ColumnVector coord(4);
       coord(4)=1.0;
-      if (masks_used) {
+      if (mask.nvoxels()>0) {
 	coord(1) = vol.mincoordx(mask);
 	coord(2) = vol.mincoordy(mask);
 	coord(3) = vol.mincoordz(mask);
@@ -197,7 +180,7 @@ int fmrib_main_float(int argc, char* argv[])
     } else if (sarg=="-x") { 
       ColumnVector coord(4);
       coord(4)=1.0;
-      if (masks_used) {
+      if (mask.nvoxels()>0) {
 	coord(1) = vol.maxcoordx(mask);
 	coord(2) = vol.maxcoordy(mask);
 	coord(3) = vol.maxcoordz(mask);
@@ -305,7 +288,7 @@ int fmrib_main_float(int argc, char* argv[])
       }
       
       mask.binarise(0.5);
-      generate_masks(mask,masknz,vol);
+      generateNonZeroMask(mask,masknz,vol);
         if (mask.tsize()!=1) vol*=mask; 
 	else vol*=mask[0];
     } else if (sarg=="-l") {
@@ -316,7 +299,6 @@ int fmrib_main_float(int argc, char* argv[])
 	cerr << "Must specify an argument to -l" << endl;
 	exit(2);
       }
-      lthr_used=true;
       generate_masks(mask,masknz,vol,lthr,uthr);
       vol*=mask;
     } else if (sarg=="-u") {
@@ -327,13 +309,12 @@ int fmrib_main_float(int argc, char* argv[])
 	cerr << "Must specify an argument to -u" << endl;
 	exit(2);
       }
-      uthr_used=true;
       generate_masks(mask,masknz,vol,lthr,uthr);
       vol*=mask;
     } else if (sarg=="-a") {
       vol = abs(vol);
     } else if (sarg=="-v") {
-      if (masks_used) {
+      if (mask.nvoxels()>0) {
 	cout << (long int) mask.sum() << " " 
 	     << mask.sum() * vol.xdim() * vol.ydim() * vol.zdim() << " ";
       } else {
@@ -341,7 +322,7 @@ int fmrib_main_float(int argc, char* argv[])
 	     << vol.nvoxels() * vol.tsize() * vol.xdim() * vol.ydim() * vol.zdim() << " ";
       }
     } else if (sarg=="-V") {
-      if (masks_used) {
+      if (masknz.nvoxels()>0) {
 	cout << (long int) masknz.sum() << " " 
 	     << masknz.sum() * vol.xdim() * vol.ydim() * vol.zdim() << " ";
       } else {
@@ -354,19 +335,19 @@ int fmrib_main_float(int argc, char* argv[])
 	// hidden debug option!
       cout << vol.sum() << " ";
     } else if (sarg=="-s") {
-	if (masks_used) cout << vol.stddev(mask) << " ";
+	if (mask.nvoxels()>0) cout << vol.stddev(mask) << " ";
 	else cout << vol.stddev() << " ";
     } else if (sarg=="-S") {
-      if (masks_used) {
+      if (masknz.nvoxels()>0) {
 	cout << vol.stddev(masknz) << " ";
       } else {
 	cout << nonzerostddev(vol) << " ";
       }
     } else if (sarg=="-r") {
-        if (masks_used) cout << vol.robustmin(mask) << " " << vol.robustmax(mask) << " ";
+      if (mask.nvoxels()>0) cout << vol.robustmin(mask) << " " << vol.robustmax(mask) << " ";
         else cout << vol.robustmin() << " " << vol.robustmax() << " ";
     } else if (sarg=="-R") {
-	if (masks_used) cout << vol.min(mask) << " " << vol.max(mask) << " ";
+	if (mask.nvoxels()>0) cout << vol.min(mask) << " " << vol.max(mask) << " ";
 	else cout << vol.min() << " " << vol.max() << " ";
     } else if (sarg=="-c") {
 	ColumnVector cog(4);
@@ -395,7 +376,7 @@ int fmrib_main_float(int argc, char* argv[])
     	cerr << "Percentile must be between 0 and 100" << endl;
     	exit(1);
       }
-      if (masks_used) cout << vol.percentile((float) n/100.0, mask) << " ";
+      if (mask.nvoxels()>0) cout << vol.percentile((float) n/100.0, mask) << " ";
       else cout << vol.percentile((float) n/100.0) << " ";
     } else if (sarg=="-P") { 
       float n;
@@ -429,7 +410,7 @@ int fmrib_main_float(int argc, char* argv[])
     	cerr << "Must specify at least 1 bin" << endl;
     	exit(1);
       }
-      if (masks_used) {
+      if (mask.nvoxels()>0) {
 	cout << vol.histogram(nbins,vol.min(),vol.max(),mask) << " ";
       } else {
 	cout << vol.histogram(nbins,vol.min(),vol.max()) << " ";
@@ -464,7 +445,7 @@ int fmrib_main_float(int argc, char* argv[])
 	cerr << "Must specify the histogram maximum intensity" << endl;
 	exit(2);
       }
-      if (masks_used) {
+      if (mask.nvoxels()>0) {
 	cout << vol.histogram(nbins,min,max,mask) << " ";
       } else {
 	cout << vol.histogram(nbins,min,max) << " ";
-- 
GitLab