changeset 102:21123729ac71 refactoring

simplification
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 30 Nov 2015 11:02:50 +0100
parents 2932473a9b19
children d5396fcd80bb
files Plugin/DecodedImageAdapter.cpp Plugin/DecodedImageAdapter.h Plugin/Plugin.cpp
diffstat 3 files changed, 61 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/Plugin/DecodedImageAdapter.cpp	Fri Nov 27 22:05:51 2015 +0100
+++ b/Plugin/DecodedImageAdapter.cpp	Mon Nov 30 11:02:50 2015 +0100
@@ -23,6 +23,7 @@
 #include "../Orthanc/Core/Images/ImageBuffer.h"
 #include "../Orthanc/Core/Images/ImageProcessing.h"
 #include "../Orthanc/Core/OrthancException.h"
+#include "../Orthanc/Core/Toolbox.h"
 #include "../Orthanc/Plugins/Samples/GdcmDecoder/OrthancImageWrapper.h"
 #include "../Orthanc/Resources/ThirdParty/base64/base64.h"
 #include "ViewerToolbox.h"
@@ -104,7 +105,7 @@
       throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
     }
 
-    std::auto_ptr<OrthancImageWrapper> image(decoderCache_.Decode(context_, dicom.c_str(), dicom.size(), frameIndex));
+    std::auto_ptr<OrthancImageWrapper> image(new OrthancImageWrapper(context_, OrthancPluginDecodeDicomImage(context_, dicom.c_str(), dicom.size(), frameIndex)));
 
     Json::Value json;
     if (GetCornerstoneMetadata(json, tags, *image))
@@ -135,9 +136,9 @@
   }
 
 
-  static bool GetTagValue(std::string& value,
-                          const Json::Value& tags,
-                          const std::string& tag)
+  static bool GetStringTag(std::string& value,
+                           const Json::Value& tags,
+                           const std::string& tag)
   {
     if (tags.type() == Json::objectValue &&
         tags.isMember(tag) &&
@@ -158,6 +159,26 @@
   }
                                  
 
+  static float GetFloatTag(const Json::Value& tags,
+                           const std::string& tag,
+                           float defaultValue)
+  {
+    std::string tmp;
+    if (GetStringTag(tmp, tags, tag))
+    {
+      try
+      {
+        return boost::lexical_cast<float>(tmp);
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+      }
+    }
+
+    return defaultValue;
+  }
+                                 
+
 
   bool DecodedImageAdapter::GetCornerstoneMetadata(Json::Value& result,
                                                    const Json::Value& tags,
@@ -209,34 +230,46 @@
         return false;
     }
 
-    result["slope"] = image.GetSlope();
-    result["intercept"] = image.GetIntercept();
+    float slope = GetFloatTag(tags, "0028,1053", 1.0f);
+    float intercept = GetFloatTag(tags, "0028,1052", 1.0f);
+
+    result["slope"] = slope;
+    result["intercept"] = intercept;
     result["rows"] = image.GetHeight();
     result["columns"] = image.GetWidth();
     result["height"] = image.GetHeight();
     result["width"] = image.GetWidth();
-    result["columnPixelSpacing"] = image.GetColumnPixelSpacing();
-    result["rowPixelSpacing"] = image.GetRowPixelSpacing();
 
-    result["windowCenter"] = windowCenter * image.GetSlope() + image.GetIntercept();
-    result["windowWidth"] = windowWidth * image.GetSlope();
-
-    try
+    bool ok = false;
+    std::string pixelSpacing;
+    if (GetStringTag(pixelSpacing, tags, "0028,0030"))
     {
-      std::string width, center;
-      if (GetTagValue(center, tags, "0028,1050" /*DICOM_TAG_WINDOW_CENTER*/) &&
-          GetTagValue(width, tags, "0028,1051" /*DICOM_TAG_WINDOW_WIDTH*/))
+      std::vector<std::string> tokens;
+      Orthanc::Toolbox::TokenizeString(tokens, pixelSpacing, '\\');
+
+      if (tokens.size() >= 2)
       {
-        float a = boost::lexical_cast<float>(width);
-        float b = boost::lexical_cast<float>(center);
-        result["windowWidth"] = a;
-        result["windowCenter"] = b;
+        try
+        {
+          result["columnPixelSpacing"] = boost::lexical_cast<float>(tokens[1]);
+          result["rowPixelSpacing"] = boost::lexical_cast<float>(tokens[0]);
+          ok = true;
+        }
+        catch (boost::bad_lexical_cast&)
+        {
+        }
       }
     }
-    catch (boost::bad_lexical_cast&)
+
+    if (!ok)
     {
+      result["columnPixelSpacing"] = 1.0f;
+      result["rowPixelSpacing"] = 1.0f;
     }
 
+    result["windowCenter"] = GetFloatTag(tags, "0028,1050", windowCenter * slope + intercept);
+    result["windowWidth"] = GetFloatTag(tags, "0028,1051", windowWidth * slope);
+
     return true;
   }
 
--- a/Plugin/DecodedImageAdapter.h	Fri Nov 27 22:05:51 2015 +0100
+++ b/Plugin/DecodedImageAdapter.h	Mon Nov 30 11:02:50 2015 +0100
@@ -26,7 +26,6 @@
 #include <stdint.h>
 #include <json/value.h>
 
-#include "../Orthanc/Plugins/Samples/GdcmDecoder/GdcmDecoderCache.h"
 #include "../Orthanc/Plugins/Samples/GdcmDecoder/OrthancImageWrapper.h"
 
 
@@ -60,7 +59,6 @@
                                 uint8_t quality /* between 0 and 100 */);
 
     OrthancPluginContext* context_;
-    GdcmDecoderCache  decoderCache_;
 
   public:
     DecodedImageAdapter(OrthancPluginContext* context) : context_(context)
@@ -69,10 +67,5 @@
 
     virtual bool Create(std::string& content,
                         const std::string& uri);  
-
-    GdcmDecoderCache& GetDecoderCache()
-    {
-      return decoderCache_;
-    }
   };
 }
--- a/Plugin/Plugin.cpp	Fri Nov 27 22:05:51 2015 +0100
+++ b/Plugin/Plugin.cpp	Mon Nov 30 11:02:50 2015 +0100
@@ -28,7 +28,7 @@
 #include "ViewerPrefetchPolicy.h"
 #include "DecodedImageAdapter.h"
 #include "SeriesInformationAdapter.h"
-#include "../Orthanc/Plugins/Samples/GdcmDecoder/GdcmImageDecoder.h"
+#include "../Orthanc/Plugins/Samples/GdcmDecoder/GdcmDecoderCache.h"
 #include "../Orthanc/Core/Toolbox.h"
 
 
@@ -64,7 +64,7 @@
   Orthanc::SharedMessageQueue  newInstances_;
   bool stop_;
   boost::thread newInstancesThread_;
-
+  OrthancPlugins::GdcmDecoderCache  decoder_;
 
   static void NewInstancesThread(CacheContext* cache)
   {
@@ -123,6 +123,11 @@
   {
     newInstances_.Enqueue(new DynamicString(instanceId));
   }
+
+  OrthancPlugins::GdcmDecoderCache&  GetDecoder()
+  {
+    return decoder_;
+  }
 };
 
 
@@ -333,8 +338,7 @@
     image.reset(new OrthancPlugins::OrthancImageWrapper(context_, decoder, frameIndex));
 #else
     using namespace OrthancPlugins;
-    ICacheFactory& factory = cache_->GetScheduler().GetFactory(CacheBundle_DecodedImage);
-    image.reset(dynamic_cast<DecodedImageAdapter&>(factory).GetDecoderCache().Decode(context_, dicom, size, frameIndex));
+    image.reset(cache_->GetDecoder().Decode(context_, dicom, size, frameIndex));
 #endif
 
     *target = image->Release();