From e686453b100f091587cba592b65dc7a2a287fe9c Mon Sep 17 00:00:00 2001
From: Mathieu Faverge <mathieu.faverge@inria.fr>
Date: Wed, 30 Aug 2017 19:43:03 +0200
Subject: [PATCH] First patch to fix issue with colors out of the range [0.,1.]

---
 src/common/Tools.cpp       | 121 ++++++++++++++++++-------------------
 src/trace/values/Color.cpp |  44 +++++++++++---
 src/trace/values/Color.hpp |   2 +
 3 files changed, 98 insertions(+), 69 deletions(-)

diff --git a/src/common/Tools.cpp b/src/common/Tools.cpp
index eee37b3f..29006548 100644
--- a/src/common/Tools.cpp
+++ b/src/common/Tools.cpp
@@ -1,45 +1,45 @@
 /*
-** This file is part of the ViTE project.
-**
-** This software is governed by the CeCILL-A license under French law
-** and abiding by the rules of distribution of free software. You can
-** use, modify and/or redistribute the software under the terms of the
-** CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
-** URL: "http://www.cecill.info".
-** 
-** As a counterpart to the access to the source code and rights to copy,
-** modify and redistribute granted by the license, users are provided
-** only with a limited warranty and the software's author, the holder of
-** the economic rights, and the successive licensors have only limited
-** liability.
-** 
-** In this respect, the user's attention is drawn to the risks associated
-** with loading, using, modifying and/or developing or reproducing the
-** software by the user in light of its specific status of free software,
-** that may mean that it is complicated to manipulate, and that also
-** therefore means that it is reserved for developers and experienced
-** professionals having in-depth computer knowledge. Users are therefore
-** encouraged to load and test the software's suitability as regards
-** their requirements in conditions enabling the security of their
-** systems and/or data to be ensured and, more generally, to use and
-** operate it in the same conditions as regards security.
-** 
-** The fact that you are presently reading this means that you have had
-** knowledge of the CeCILL-A license and that you accept its terms.
-**
-**
-** ViTE developers are (for version 0.* to 1.0):
-**
-**        - COULOMB Kevin
-**        - FAVERGE Mathieu
-**        - JAZEIX Johnny
-**        - LAGRASSE Olivier
-**        - MARCOUEILLE Jule
-**        - NOISETTE Pascal
-**        - REDONDY Arthur
-**        - VUCHENER Clément 
-**
-*/
+ ** This file is part of the ViTE project.
+ **
+ ** This software is governed by the CeCILL-A license under French law
+ ** and abiding by the rules of distribution of free software. You can
+ ** use, modify and/or redistribute the software under the terms of the
+ ** CeCILL-A license as circulated by CEA, CNRS and INRIA at the following
+ ** URL: "http://www.cecill.info".
+ **
+ ** As a counterpart to the access to the source code and rights to copy,
+ ** modify and redistribute granted by the license, users are provided
+ ** only with a limited warranty and the software's author, the holder of
+ ** the economic rights, and the successive licensors have only limited
+ ** liability.
+ **
+ ** In this respect, the user's attention is drawn to the risks associated
+ ** with loading, using, modifying and/or developing or reproducing the
+ ** software by the user in light of its specific status of free software,
+ ** that may mean that it is complicated to manipulate, and that also
+ ** therefore means that it is reserved for developers and experienced
+ ** professionals having in-depth computer knowledge. Users are therefore
+ ** encouraged to load and test the software's suitability as regards
+ ** their requirements in conditions enabling the security of their
+ ** systems and/or data to be ensured and, more generally, to use and
+ ** operate it in the same conditions as regards security.
+ **
+ ** The fact that you are presently reading this means that you have had
+ ** knowledge of the CeCILL-A license and that you accept its terms.
+ **
+ **
+ ** ViTE developers are (for version 0.* to 1.0):
+ **
+ **        - COULOMB Kevin
+ **        - FAVERGE Mathieu
+ **        - JAZEIX Johnny
+ **        - LAGRASSE Olivier
+ **        - MARCOUEILLE Jule
+ **        - NOISETTE Pascal
+ **        - REDONDY Arthur
+ **        - VUCHENER Clément
+ **
+ */
 
 #include <cstdio>
 #include <iostream>
@@ -66,27 +66,27 @@ bool convert_to_double(const std::string &arg, double *val) {
     }
     else{ // We change the locale to test
         bool is_english_system_needed = false; // We have dot as separator of decimal and integer.
-        
+
         if(arg.find('.') != string::npos) {
             is_english_system_needed = true;
         }
 
         // We had dots initially, we need to have the english system
         if(is_english_system_needed) {
-	  if((setlocale(LC_NUMERIC, "C")           == NULL) && 
-	     (setlocale(LC_NUMERIC, "en_GB.UTF-8") == NULL)){
-	      vite_warning("The locale en_GB.UTF-8 is unavailable so the decimal pointed will not be printed");
+            if((setlocale(LC_NUMERIC, "C")           == NULL) &&
+               (setlocale(LC_NUMERIC, "en_GB.UTF-8") == NULL)){
+                vite_warning("The locale en_GB.UTF-8 is unavailable so the decimal pointed will not be printed");
             }
         }
         else { // It is comma separated
-            
-	  if ((setlocale(LC_NUMERIC, "fr_FR.UTF-8") == NULL) && 
-	      (setlocale(LC_NUMERIC, "French")      == NULL)){
-	      vite_warning("The locale fr_FR.UTF-8 is unavailable so the decimal with comma will not be printed");
+
+            if ((setlocale(LC_NUMERIC, "fr_FR.UTF-8") == NULL) &&
+                (setlocale(LC_NUMERIC, "French")      == NULL)){
+                vite_warning("The locale fr_FR.UTF-8 is unavailable so the decimal with comma will not be printed");
             }
-	    
+
         }
-        
+
         // Reads the value in the new locale
         sscanf(arg.c_str(), "%lf%n", val, &nb_read);
         return nb_read == arg.size();
@@ -99,17 +99,16 @@ double clockGet (void)
     // TODO
     return 0.;
 #elif (defined X_ARCHalpha_compaq_osf1) || (defined X_ARCHi686_mac)
-  struct rusage       data;
-  getrusage (RUSAGE_SELF, &data);
-  return (((double) data.ru_utime.tv_sec  + (double) data.ru_stime.tv_sec) +
-          ((double) data.ru_utime.tv_usec + (double) data.ru_stime.tv_usec) * 
-	  1.0e-6L);
+    struct rusage       data;
+    getrusage (RUSAGE_SELF, &data);
+    return (((double) data.ru_utime.tv_sec  + (double) data.ru_stime.tv_sec) +
+            ((double) data.ru_utime.tv_usec + (double) data.ru_stime.tv_usec) *
+            1.0e-6L);
 #elif defined _POSIX_TIMERS && defined CLOCK_REALTIME
-  struct timespec tp;
-  
-  clock_gettime (CLOCK_REALTIME, &tp);            /* Elapsed time */
+    struct timespec tp;
+
+    clock_gettime (CLOCK_REALTIME, &tp);            /* Elapsed time */
 
-  return ((double) tp.tv_sec + (double) tp.tv_nsec * (double)1.0e-9L);
+    return ((double) tp.tv_sec + (double) tp.tv_nsec * (double)1.0e-9L);
 #endif
 }
-
diff --git a/src/trace/values/Color.cpp b/src/trace/values/Color.cpp
index f4994df2..a7f82f04 100644
--- a/src/trace/values/Color.cpp
+++ b/src/trace/values/Color.cpp
@@ -42,6 +42,7 @@
 */
 
 #include <string>
+#include <iostream>
 #include <sstream>
 #include <iomanip> // For std::setprecision
 #include <cstdlib>
@@ -53,14 +54,35 @@
 
 using namespace std;
 
-Color::Color() : _r((rand()%256)/256),
-                 _g((rand()%256)/256),
-                 _b((rand()%256)/256) {
+double
+Color::check_value( double v, const string spectrum )
+{
+    if ( v < 0. ) {
+        cerr << "Invalid value for " << spectrum << " color " << v << " (must be between 0. and 1.)" << endl;
+        v = 0.;
+    }
+    /* Extra Test for StarPU bug in state color values */
+    if ( v > 1. ) {
+        v = v / 255.;
+    }
+    if ( v > 1. ) {
+        cerr << "Invalid value for " << spectrum << " color " << v << " (must be between 0. and 1.)" << endl;
+        v = 1.;
+    }
+    return v;
+}
+
+Color::Color() : _r((rand()%256)/255),
+                 _g((rand()%256)/255),
+                 _b((rand()%256)/255) {
     _is_correct = true;
 }
 
 
-Color::Color(double r, double g, double b) : _r(r), _g(g), _b(b) {
+Color::Color(double r, double g, double b) {
+    _r = Color::check_value( r, "red"   );
+    _g = Color::check_value( g, "green" );
+    _b = Color::check_value( b, "blue"  );
     _is_correct = true;
 }
 
@@ -72,7 +94,9 @@ Color::Color(const Color *c) : Value(*c), _r(c->_r), _g(c->_g), _b(c->_b) {
     _is_correct = c->_is_correct;
 }
 
-Color::Color(const std::string &in) {
+Color::Color(const std::string &in)
+{
+    double r, g, b;
     string separated_color[3];
     string temp = in;
 
@@ -82,9 +106,13 @@ Color::Color(const std::string &in) {
         temp = temp.substr(position_of_space+1);
     }
 
-    _is_correct = convert_to_double(separated_color[0], &_r) &&
-                  convert_to_double(separated_color[1], &_g) &&
-                  convert_to_double(separated_color[2], &_b);
+    _is_correct = convert_to_double(separated_color[0], &r) &&
+                  convert_to_double(separated_color[1], &g) &&
+                  convert_to_double(separated_color[2], &b);
+
+    _r = Color::check_value( r, "red"   );
+    _g = Color::check_value( g, "green" );
+    _b = Color::check_value( b, "blue"  );
 }
 
 std::string Color::to_string() const {
diff --git a/src/trace/values/Color.hpp b/src/trace/values/Color.hpp
index b9a8d97b..dc2e0d4e 100644
--- a/src/trace/values/Color.hpp
+++ b/src/trace/values/Color.hpp
@@ -63,6 +63,8 @@ private :
     /*! blue value */
     double _b;
 
+    static double check_value( double, const std::string );
+
 public:
     /*!
      * \brief Constructor
-- 
GitLab