From 4938cf7bbf7932b62a73f4c537f9e5c471a5c897 Mon Sep 17 00:00:00 2001
From: Mark Jenkinson <mark@fmrib.ox.ac.uk>
Date: Fri, 5 Aug 2005 11:16:17 +0000
Subject: [PATCH] Hopefully fixed bugs in avwstats++, except -r and -R when -l
 and -u are used - but this is an old, existing bug

---
 fslstats.cc | 66 ++++++++++++++++++++++++-----------------------------
 1 file changed, 30 insertions(+), 36 deletions(-)

diff --git a/fslstats.cc b/fslstats.cc
index 7202436..25901f9 100644
--- a/fslstats.cc
+++ b/fslstats.cc
@@ -11,7 +11,6 @@
 
 #include "miscmaths/miscmaths.h"
 #include "newimage/newimageall.h"
-#include "newimage/fmribmain.h"
 #include "newimage/costfns.h"
 
 using namespace NEWIMAGE;
@@ -53,8 +52,7 @@ void print_usage(const string& progname) {
 // Some specialised nonzero functions just for speedup
 //  (it avoids generating masks when not absolutely necessary)
 
-template <class T>
-long int nonzerocount(const volume4D<T>& vol)
+long int nonzerocount(const volume4D<float>& vol)
 {
   long int totn=0;
   for (int t=vol.mint(); t<=vol.maxt(); t++) {
@@ -71,8 +69,7 @@ long int nonzerocount(const volume4D<T>& vol)
   return totn;
 }
 
-template <class T>
-double nonzeromean(const volume4D<T>& vol)
+double nonzeromean(const volume4D<float>& vol)
 {
   double totv=0.0;
   long int totn=0;
@@ -95,8 +92,7 @@ double nonzeromean(const volume4D<T>& vol)
   return meanval;
 }
 
-template <class T>
-double nonzerostddev(const volume4D<T>& vol)
+double nonzerostddev(const volume4D<float>& vol)
 {
   double totv=0.0, totvv=0.0;
   long int totn=0;
@@ -105,7 +101,7 @@ double nonzerostddev(const volume4D<T>& vol)
       for (int y=vol.miny(); y<=vol.maxy(); y++) {
 	for (int x=vol.minx(); x<=vol.maxx(); x++) {
 	  if (vol(x,y,z,t)!=0.0) {
-	    T v=vol(x,y,z,t);
+	    float v=vol(x,y,z,t);
 	    totvv+=(double) v*v;
 	    totv+=(double) v;
 	    totn++;
@@ -122,27 +118,24 @@ double nonzerostddev(const volume4D<T>& vol)
   return std::sqrt(var);
 }
 
-template <class T>
-int generate_masks(volume4D<T> &mask, volume4D<T> &masknz, const volume4D<T> &vin,
-		   T& lthr, T& uthr) 
+int generate_masks(volume4D<float> &mask, volume4D<float> &masknz, const volume4D<float> &vin,
+		   float& lthr, float& uthr) 
 {
   if (!lthr_used) { lthr=vin.min()-1; }
   if (!uthr_used) { uthr=vin.max()+1; }
   mask = binarise(vin,lthr,uthr,exclusive);
-  masknz = mask * ((T) 1 - binarise(vin,(T) 0.0, (T) 0.0));
+  masknz = mask * (1.0f - binarise(vin,0.0f, 0.0f));
   return 0;
 }
 
-template <class T>
-int generate_masks(const volume4D<T> &mask, volume4D<T> &masknz, const volume4D<T> &vin)
+int generate_masks(const volume4D<float> &mask, volume4D<float> &masknz, const volume4D<float> &vin)
 {
-  masknz = mask * ((T) 1 - binarise(vin,(T) 0.0, (T) 0.0));
+  masknz = mask * (1.0f - binarise(vin,0.0f, 0.0f));
   return 0;
 }
 
 
-template <class T>
-int fmrib_main(int argc, char* argv[]) 
+int fmrib_main_float(int argc, char* argv[]) 
 {
 
   cout.setf(ios::dec); 
@@ -150,10 +143,10 @@ int fmrib_main(int argc, char* argv[])
   cout.setf(ios::left, ios::adjustfield); 
   cout.precision(6);  
 
-  volume4D<T> vin, vol, mask, masknz;
+  volume4D<float> vin, vol, mask, masknz;
   read_volume4D(vol,argv[1]);
 
-  T lthr=0, uthr=0;  // these initial values are not used
+  float lthr=0, uthr=0;  // these initial values are not used
   if (masks_used) {
     vin = vol;
     generate_masks(mask,masknz,vin,lthr,uthr);
@@ -205,8 +198,8 @@ int fmrib_main(int argc, char* argv[])
 	for(int z=masknz.minz();z<=masknz.maxz();z++) {
 	  for(int y=masknz.miny();y<=masknz.maxy();y++) {
 	    for(int x=masknz.minx();x<=masknz.maxx();x++) {
-	      if (masknz(x,y,z,t)>(T) 0.5) {
-		// if (masknz(x,y,z)>(T) 0.5) {
+	      if (masknz(x,y,z,t)>0.5) {
+		// if (masknz(x,y,z)>0.5) {
 		if (x<xmin) xmin=x;
 		if (x>xmax) xmax=x;
 		if (y<ymin) ymin=y;
@@ -278,18 +271,18 @@ int fmrib_main(int argc, char* argv[])
 	masks_used=true;
 	vin = vol;
       }
-      T th=(T) 0.5;
+      float th= 0.5;
       if (th!=0) {
         mask.binarise(th);
       } else {
-        mask.binarise((T) 1);
+        mask.binarise(1);
       }
-      generate_masks<T>(mask,masknz,vin);
+      generate_masks(mask,masknz,vin);
       vol = vin * mask;
     } else if (sarg=="-l") {
       narg++;
       if (narg<argc) {
-        lthr = (T) atof(argv[narg]);
+        lthr = atof(argv[narg]);
       } else {
 	cerr << "Must specify an argument to -l" << endl;
 	exit(2);
@@ -299,12 +292,12 @@ int fmrib_main(int argc, char* argv[])
 	masks_used=true;
 	vin = vol;
       }
-      generate_masks<T>(mask,masknz,vin,lthr,uthr);
+      generate_masks(mask,masknz,vin,lthr,uthr);
       vol = vin * mask;
     } else if (sarg=="-u") {
       narg++;
       if (narg<argc) {
-        uthr = (T) atof(argv[narg]);
+        uthr = atof(argv[narg]);
       } else {
 	cerr << "Must specify an argument to -u" << endl;
 	exit(2);
@@ -314,7 +307,7 @@ int fmrib_main(int argc, char* argv[])
 	masks_used=true;
 	vin = vol;
       }
-      generate_masks<T>(mask,masknz,vin,lthr,uthr);
+      generate_masks(mask,masknz,vin,lthr,uthr);
       vol = vin * mask;
     } else if (sarg=="-a") {
       vol = abs(vin);
@@ -350,7 +343,14 @@ int fmrib_main(int argc, char* argv[])
       }
     } else if (sarg=="-r") {
       if (masks_used) {
-	cout << vol.robustmin(mask) << " " << vol.robustmax(mask) << " ";
+	float rmin=vol.robustmin(mask);
+	float rmax=vol.robustmax(mask);
+	if (rmin>rmax) { 
+	  float tmp = rmax;
+	  rmax = rmin;
+	  rmin = tmp;
+	}
+	cout << rmin << " " << rmax << " ";
       } else {
 	cout << vol.robustmin() << " " << vol.robustmax() << " ";
       }
@@ -433,13 +433,7 @@ int main(int argc,char *argv[])
     return 1; 
   }
 
-  int datatype = dtype(string(argv[1]));
+  return fmrib_main_float(argc,argv);
 
-  if (datatype==DT_UNSIGNED_CHAR) {
-	// avoid using char as all print outs become non-numeric!
-    return fmrib_main<short>(argc,argv);
-  } else {
-    return call_fmrib_main(datatype,argc,argv);
-  }
 }
 
-- 
GitLab