changeset 769:4ba8892870a2

improved interface for lookup tables
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 23 May 2019 20:37:19 +0200
parents 55411e7da2f7
children e6850c866469
files Framework/Scene2D/FloatTextureSceneLayer.h Framework/Scene2D/LookupTableTextureSceneLayer.cpp Framework/Scene2D/LookupTableTextureSceneLayer.h Samples/Sdl/Loader.cpp
diffstat 4 files changed, 31 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Scene2D/FloatTextureSceneLayer.h	Thu May 23 20:04:33 2019 +0200
+++ b/Framework/Scene2D/FloatTextureSceneLayer.h	Thu May 23 20:37:19 2019 +0200
@@ -33,7 +33,7 @@
     float            customWidth_;
 
   public:
-    // The pixel format must be "Float32"
+    // The pixel format must be convertible to "Float32"
     FloatTextureSceneLayer(const Orthanc::ImageAccessor& texture);
 
     void SetWindowing(ImageWindowing windowing);
--- a/Framework/Scene2D/LookupTableTextureSceneLayer.cpp	Thu May 23 20:04:33 2019 +0200
+++ b/Framework/Scene2D/LookupTableTextureSceneLayer.cpp	Thu May 23 20:37:19 2019 +0200
@@ -52,12 +52,12 @@
       SetTexture(t.release());
     }
 
-    SetLookupTableGrayscale(1);
+    SetLookupTableGrayscale();
     SetRange(0, 1);
   }
 
 
-  void LookupTableTextureSceneLayer::SetLookupTableGrayscale(float alpha)
+  void LookupTableTextureSceneLayer::SetLookupTableGrayscale()
   {
     std::vector<uint8_t> rgb(3 * 256);
 
@@ -68,18 +68,15 @@
       rgb[3 * i + 2] = i;
     }
 
-    SetLookupTableRgb(rgb, alpha);
+    SetLookupTableRgb(rgb);
   }  
 
 
-  void LookupTableTextureSceneLayer::SetLookupTableRgb(const std::vector<uint8_t>& lut,
-                                                       float alpha)
+  void LookupTableTextureSceneLayer::SetLookupTableRgb(const std::vector<uint8_t>& lut)
   {
-    if (lut.size() != 3 * 256 ||
-        alpha < 0 ||
-        alpha > 1)
+    if (lut.size() != 3 * 256)
     {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
     }
 
     lut_.resize(4 * 256);
@@ -98,14 +95,16 @@
       }
       else
       {
-        float r = static_cast<float>(lut[3 * i]) * alpha;
-        float g = static_cast<float>(lut[3 * i + 1]) * alpha;
-        float b = static_cast<float>(lut[3 * i + 2]) * alpha;
+        float a = static_cast<float>(i) / 255.0f;
+        
+        float r = static_cast<float>(lut[3 * i]) * a;
+        float g = static_cast<float>(lut[3 * i + 1]) * a;
+        float b = static_cast<float>(lut[3 * i + 2]) * a;
         
         lut_[4 * i] = static_cast<uint8_t>(std::floor(r));
         lut_[4 * i + 1] = static_cast<uint8_t>(std::floor(g));
         lut_[4 * i + 2] = static_cast<uint8_t>(std::floor(b));
-        lut_[4 * i + 3] = static_cast<uint8_t>(std::floor(alpha * 255.0f));
+        lut_[4 * i + 3] = static_cast<uint8_t>(std::floor(a * 255.0f));
       }
     }
 
@@ -113,25 +112,21 @@
   }
 
 
-  void LookupTableTextureSceneLayer::SetLookupTableRgb(const std::string& lut,
-                                                       float alpha)
-  {
-    std::vector<uint8_t> tmp;
-    StringToVector(tmp, lut);
-    SetLookupTableRgb(tmp, alpha);
-  }
-
-  
   void LookupTableTextureSceneLayer::SetLookupTable(const std::vector<uint8_t>& lut)
   {
-    if (lut.size() != 4 * 256)
+    if (lut.size() == 4 * 256)
+    {
+      lut_ = lut;
+      IncrementRevision();
+    }
+    else if (lut.size() == 3 * 256)
+    {
+      SetLookupTableRgb(lut);
+    }
+    else
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
     }
-
-    lut_ = lut;
-
-    IncrementRevision();
   }
 
 
--- a/Framework/Scene2D/LookupTableTextureSceneLayer.h	Thu May 23 20:04:33 2019 +0200
+++ b/Framework/Scene2D/LookupTableTextureSceneLayer.h	Thu May 23 20:37:19 2019 +0200
@@ -33,18 +33,16 @@
     float                 maxValue_;
     std::vector<uint8_t>  lut_;
 
+    void SetLookupTableRgb(const std::vector<uint8_t>& lut);
+
   public:
-    // The pixel format must be "Flot32"
+    // The pixel format must be convertible to Float32
     LookupTableTextureSceneLayer(const Orthanc::ImageAccessor& texture);
 
-    void SetLookupTableGrayscale(float alpha);
-    
-    void SetLookupTableRgb(const std::vector<uint8_t>& lut,
-                           float alpha);
+    void SetLookupTableGrayscale();
 
-    void SetLookupTableRgb(const std::string& lut,
-                           float alpha);
-
+    // The vector must contain either 3 * 256 values (RGB), or 4 * 256
+    // (RGBA). In the RGB case, an alpha channel will be automatically added.
     void SetLookupTable(const std::vector<uint8_t>& lut);
 
     void SetLookupTable(const std::string& lut);
@@ -64,6 +62,7 @@
       return maxValue_;
     }
 
+    // This returns a vector of 4 * 256 values between 0 and 255, in RGBA.
     const std::vector<uint8_t>& GetLookupTable() const
     {
       return lut_;
--- a/Samples/Sdl/Loader.cpp	Thu May 23 20:04:33 2019 +0200
+++ b/Samples/Sdl/Loader.cpp	Thu May 23 20:37:19 2019 +0200
@@ -192,7 +192,7 @@
           
           std::auto_ptr<LookupTableTextureSceneLayer> tmp(parameters.CreateLookupTableTexture(reader.GetAccessor()));
           tmp->FitRange();
-          tmp->SetLookupTableRgb(lut, 1);
+          tmp->SetLookupTable(lut);
           texture.reset(tmp.release());
         }