changeset 2126:03b065778fc3

cleaning up
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 07 Nov 2016 13:47:21 +0100
parents b9bd52c72ba2
children bfa92c9328d7
files OrthancServer/DicomInstanceToStore.cpp OrthancServer/FromDcmtkBridge.cpp OrthancServer/FromDcmtkBridge.h OrthancServer/Internals/StoreScp.cpp OrthancServer/ParsedDicomFile.cpp UnitTestsSources/FromDcmtkTests.cpp
diffstat 6 files changed, 133 insertions(+), 124 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/DicomInstanceToStore.cpp	Mon Nov 07 12:38:09 2016 +0100
+++ b/OrthancServer/DicomInstanceToStore.cpp	Mon Nov 07 13:47:21 2016 +0100
@@ -111,12 +111,11 @@
     if (!json_.HasContent())
     {
       json_.Allocate();
-      FromDcmtkBridge::ToJson(json_.GetContent(), 
-                              *parsed_.GetContent().GetDcmtkObject().getDataset(),
-                              DicomToJsonFormat_Full, 
-                              DicomToJsonFlags_Default,
-                              ORTHANC_MAXIMUM_TAG_LENGTH,
-                              Configuration::GetDefaultEncoding());
+      FromDcmtkBridge::ExtractDicomAsJson(json_.GetContent(), 
+                                          *parsed_.GetContent().GetDcmtkObject().getDataset(),
+                                          DicomToJsonFormat_Full, 
+                                          DicomToJsonFlags_Default,
+                                          ORTHANC_MAXIMUM_TAG_LENGTH);
     }
   }
 
--- a/OrthancServer/FromDcmtkBridge.cpp	Mon Nov 07 12:38:09 2016 +0100
+++ b/OrthancServer/FromDcmtkBridge.cpp	Mon Nov 07 13:47:21 2016 +0100
@@ -741,20 +741,12 @@
   }                              
 
 
-  static void DatasetToJson(Json::Value& parent,
-                            DcmItem& item,
-                            DicomToJsonFormat format,
-                            DicomToJsonFlags flags,
-                            unsigned int maxStringLength,
-                            Encoding encoding);
-
-
-  void FromDcmtkBridge::ToJson(Json::Value& parent,
-                               DcmElement& element,
-                               DicomToJsonFormat format,
-                               DicomToJsonFlags flags,
-                               unsigned int maxStringLength,
-                               Encoding encoding)
+  void FromDcmtkBridge::ElementToJson(Json::Value& parent,
+                                      DcmElement& element,
+                                      DicomToJsonFormat format,
+                                      DicomToJsonFlags flags,
+                                      unsigned int maxStringLength,
+                                      Encoding encoding)
   {
     if (parent.type() == Json::nullValue)
     {
@@ -789,12 +781,12 @@
   }
 
 
-  static void DatasetToJson(Json::Value& parent,
-                            DcmItem& item,
-                            DicomToJsonFormat format,
-                            DicomToJsonFlags flags,
-                            unsigned int maxStringLength,
-                            Encoding encoding)
+  void FromDcmtkBridge::DatasetToJson(Json::Value& parent,
+                                      DcmItem& item,
+                                      DicomToJsonFormat format,
+                                      DicomToJsonFlags flags,
+                                      unsigned int maxStringLength,
+                                      Encoding encoding)
   {
     assert(parent.type() == Json::objectValue);
 
@@ -839,28 +831,29 @@
         }
       }
 
-      FromDcmtkBridge::ToJson(parent, *element, format, flags, maxStringLength, encoding);
+      FromDcmtkBridge::ElementToJson(parent, *element, format, flags, maxStringLength, encoding);
     }
   }
 
 
-  void FromDcmtkBridge::ToJson(Json::Value& target, 
-                               DcmDataset& dataset,
-                               DicomToJsonFormat format,
-                               DicomToJsonFlags flags,
-                               unsigned int maxStringLength,
-                               Encoding defaultEncoding)
+  void FromDcmtkBridge::ExtractDicomAsJson(Json::Value& target, 
+                                           DcmDataset& dataset,
+                                           DicomToJsonFormat format,
+                                           DicomToJsonFlags flags,
+                                           unsigned int maxStringLength)
   {
+    Encoding encoding = DetectEncoding(dataset, Configuration::GetDefaultEncoding());
+
     target = Json::objectValue;
-    DatasetToJson(target, dataset, format, flags, maxStringLength, DetectEncoding(dataset, defaultEncoding));
+    DatasetToJson(target, dataset, format, flags, maxStringLength, encoding);
   }
 
 
-  void FromDcmtkBridge::ToJson(Json::Value& target, 
-                               DcmMetaInfo& dataset,
-                               DicomToJsonFormat format,
-                               DicomToJsonFlags flags,
-                               unsigned int maxStringLength)
+  void FromDcmtkBridge::ExtractHeaderAsJson(Json::Value& target, 
+                                            DcmMetaInfo& dataset,
+                                            DicomToJsonFormat format,
+                                            DicomToJsonFlags flags,
+                                            unsigned int maxStringLength)
   {
     target = Json::objectValue;
     DatasetToJson(target, dataset, format, flags, maxStringLength, Encoding_Ascii);
--- a/OrthancServer/FromDcmtkBridge.h	Mon Nov 07 12:38:09 2016 +0100
+++ b/OrthancServer/FromDcmtkBridge.h	Mon Nov 07 13:47:21 2016 +0100
@@ -44,10 +44,19 @@
 #include <dcmtk/dcmdata/dcfilefo.h>
 #include <json/json.h>
 
+#if ORTHANC_BUILD_UNIT_TESTS == 1
+#  include <gtest/gtest_prod.h>
+#endif
+
+
 namespace Orthanc
 {
   class FromDcmtkBridge : public boost::noncopyable
   {
+#if ORTHANC_BUILD_UNIT_TESTS == 1
+    FRIEND_TEST(FromDcmtkBridge, FromJson);
+#endif
+
   private:
     FromDcmtkBridge();  // Pure static class
 
@@ -56,6 +65,20 @@
                                     unsigned int maxStringLength,
                                     Encoding defaultEncoding);
 
+    static void DatasetToJson(Json::Value& parent,
+                              DcmItem& item,
+                              DicomToJsonFormat format,
+                              DicomToJsonFlags flags,
+                              unsigned int maxStringLength,
+                              Encoding encoding);
+
+    static void ElementToJson(Json::Value& parent,
+                              DcmElement& element,
+                              DicomToJsonFormat format,
+                              DicomToJsonFlags flags,
+                              unsigned int maxStringLength,
+                              Encoding dicomEncoding);
+
   public:
     static void InitializeDictionary();
 
@@ -86,25 +109,17 @@
                                           unsigned int maxStringLength,
                                           Encoding encoding);
 
-    static void ToJson(Json::Value& parent,
-                       DcmElement& element,
-                       DicomToJsonFormat format,
-                       DicomToJsonFlags flags,
-                       unsigned int maxStringLength,
-                       Encoding dicomEncoding);
+    static void ExtractDicomAsJson(Json::Value& target, 
+                                   DcmDataset& dataset,
+                                   DicomToJsonFormat format,
+                                   DicomToJsonFlags flags,
+                                   unsigned int maxStringLength);
 
-    static void ToJson(Json::Value& target, 
-                       DcmDataset& dataset,
-                       DicomToJsonFormat format,
-                       DicomToJsonFlags flags,
-                       unsigned int maxStringLength,
-                       Encoding defaultEncoding);
-
-    static void ToJson(Json::Value& target, 
-                       DcmMetaInfo& header,
-                       DicomToJsonFormat format,
-                       DicomToJsonFlags flags,
-                       unsigned int maxStringLength);
+    static void ExtractHeaderAsJson(Json::Value& target, 
+                                    DcmMetaInfo& header,
+                                    DicomToJsonFormat format,
+                                    DicomToJsonFlags flags,
+                                    unsigned int maxStringLength);
 
     static std::string GetTagName(const DicomTag& tag,
                                   const std::string& privateCreator);
--- a/OrthancServer/Internals/StoreScp.cpp	Mon Nov 07 12:38:09 2016 +0100
+++ b/OrthancServer/Internals/StoreScp.cpp	Mon Nov 07 13:47:21 2016 +0100
@@ -169,11 +169,10 @@
           try
           {
             FromDcmtkBridge::ExtractDicomSummary(summary, **imageDataSet);
-            FromDcmtkBridge::ToJson(dicomJson, **imageDataSet,
-                                    DicomToJsonFormat_Full, 
-                                    DicomToJsonFlags_Default, 
-                                    ORTHANC_MAXIMUM_TAG_LENGTH,
-                                    Configuration::GetDefaultEncoding());
+            FromDcmtkBridge::ExtractDicomAsJson(dicomJson, **imageDataSet,
+                                                DicomToJsonFormat_Full, 
+                                                DicomToJsonFlags_Default, 
+                                                ORTHANC_MAXIMUM_TAG_LENGTH);
 
             if (!FromDcmtkBridge::SaveToMemoryBuffer(buffer, **imageDataSet))
             {
--- a/OrthancServer/ParsedDicomFile.cpp	Mon Nov 07 12:38:09 2016 +0100
+++ b/OrthancServer/ParsedDicomFile.cpp	Mon Nov 07 13:47:21 2016 +0100
@@ -1151,16 +1151,15 @@
                                DicomToJsonFlags flags,
                                unsigned int maxStringLength)
   {
-    FromDcmtkBridge::ToJson(target, *pimpl_->file_->getDataset(),
-                            format, flags, maxStringLength,
-                            Configuration::GetDefaultEncoding());
+    FromDcmtkBridge::ExtractDicomAsJson(target, *pimpl_->file_->getDataset(),
+                                        format, flags, maxStringLength);
   }
 
 
   void ParsedDicomFile::HeaderToJson(Json::Value& target, 
                                      DicomToJsonFormat format)
   {
-    FromDcmtkBridge::ToJson(target, *pimpl_->file_->getMetaInfo(), format, DicomToJsonFlags_None, 0);
+    FromDcmtkBridge::ExtractHeaderAsJson(target, *pimpl_->file_->getMetaInfo(), format, DicomToJsonFlags_None, 0);
   }
 
 
--- a/UnitTestsSources/FromDcmtkTests.cpp	Mon Nov 07 12:38:09 2016 +0100
+++ b/UnitTestsSources/FromDcmtkTests.cpp	Mon Nov 07 13:47:21 2016 +0100
@@ -380,81 +380,85 @@
 }
 
 
-TEST(FromDcmtkBridge, FromJson)
+namespace Orthanc
 {
-  std::auto_ptr<DcmElement> element;
-
-  {
-    Json::Value a;
-    a = "Hello";
-    element.reset(FromDcmtkBridge::FromJson(DICOM_TAG_PATIENT_NAME, a, false, Encoding_Utf8));
-
-    Json::Value b;
-    FromDcmtkBridge::ToJson(b, *element, DicomToJsonFormat_Short, DicomToJsonFlags_Default, 0, Encoding_Ascii);
-    ASSERT_EQ("Hello", b["0010,0010"].asString());
-  }
-
+  // Namespace for the "FRIEND_TEST()" directive in "FromDcmtkBridge" to apply:
+  // https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#private-class-members
+  TEST(FromDcmtkBridge, FromJson)
   {
-    Json::Value a;
-    a = "Hello";
-    // Cannot assign a string to a sequence
-    ASSERT_THROW(element.reset(FromDcmtkBridge::FromJson(REFERENCED_STUDY_SEQUENCE, a, false, Encoding_Utf8)), OrthancException);
-  }
-
-  {
-    Json::Value a = Json::arrayValue;
-    a.append("Hello");
-    // Cannot assign an array to a string
-    ASSERT_THROW(element.reset(FromDcmtkBridge::FromJson(DICOM_TAG_PATIENT_NAME, a, false, Encoding_Utf8)), OrthancException);
-  }
-
-  {
-    Json::Value a;
-    a = "data:application/octet-stream;base64,SGVsbG8=";  // echo -n "Hello" | base64
-    element.reset(FromDcmtkBridge::FromJson(DICOM_TAG_PATIENT_NAME, a, true, Encoding_Utf8));
-
-    Json::Value b;
-    FromDcmtkBridge::ToJson(b, *element, DicomToJsonFormat_Short, DicomToJsonFlags_Default, 0, Encoding_Ascii);
-    ASSERT_EQ("Hello", b["0010,0010"].asString());
-  }
-
-  {
-    Json::Value a = Json::arrayValue;
-    CreateSampleJson(a);
-    element.reset(FromDcmtkBridge::FromJson(REFERENCED_STUDY_SEQUENCE, a, true, Encoding_Utf8));
+    std::auto_ptr<DcmElement> element;
 
     {
+      Json::Value a;
+      a = "Hello";
+      element.reset(FromDcmtkBridge::FromJson(DICOM_TAG_PATIENT_NAME, a, false, Encoding_Utf8));
+
       Json::Value b;
-      FromDcmtkBridge::ToJson(b, *element, DicomToJsonFormat_Short, DicomToJsonFlags_Default, 0, Encoding_Ascii);
-      ASSERT_EQ(Json::arrayValue, b["0008,1110"].type());
-      ASSERT_EQ(2u, b["0008,1110"].size());
-      
-      Json::Value::ArrayIndex i = (b["0008,1110"][0]["0010,0010"].asString() == "Hello") ? 0 : 1;
+      FromDcmtkBridge::ElementToJson(b, *element, DicomToJsonFormat_Short, DicomToJsonFlags_Default, 0, Encoding_Ascii);
+      ASSERT_EQ("Hello", b["0010,0010"].asString());
+    }
 
-      ASSERT_EQ(3u, b["0008,1110"][i].size());
-      ASSERT_EQ(2u, b["0008,1110"][1 - i].size());
-      ASSERT_EQ(b["0008,1110"][i]["0010,0010"].asString(), "Hello");
-      ASSERT_EQ(b["0008,1110"][i]["0010,0020"].asString(), "World");
-      ASSERT_EQ(b["0008,1110"][i]["0008,1030"].asString(), "Toto");
-      ASSERT_EQ(b["0008,1110"][1 - i]["0010,0010"].asString(), "Hello2");
-      ASSERT_EQ(b["0008,1110"][1 - i]["0010,0020"].asString(), "World2");
+    {
+      Json::Value a;
+      a = "Hello";
+      // Cannot assign a string to a sequence
+      ASSERT_THROW(element.reset(FromDcmtkBridge::FromJson(REFERENCED_STUDY_SEQUENCE, a, false, Encoding_Utf8)), OrthancException);
+    }
+
+    {
+      Json::Value a = Json::arrayValue;
+      a.append("Hello");
+      // Cannot assign an array to a string
+      ASSERT_THROW(element.reset(FromDcmtkBridge::FromJson(DICOM_TAG_PATIENT_NAME, a, false, Encoding_Utf8)), OrthancException);
     }
 
     {
+      Json::Value a;
+      a = "data:application/octet-stream;base64,SGVsbG8=";  // echo -n "Hello" | base64
+      element.reset(FromDcmtkBridge::FromJson(DICOM_TAG_PATIENT_NAME, a, true, Encoding_Utf8));
+
       Json::Value b;
-      FromDcmtkBridge::ToJson(b, *element, DicomToJsonFormat_Full, DicomToJsonFlags_Default, 0, Encoding_Ascii);
+      FromDcmtkBridge::ElementToJson(b, *element, DicomToJsonFormat_Short, DicomToJsonFlags_Default, 0, Encoding_Ascii);
+      ASSERT_EQ("Hello", b["0010,0010"].asString());
+    }
+
+    {
+      Json::Value a = Json::arrayValue;
+      CreateSampleJson(a);
+      element.reset(FromDcmtkBridge::FromJson(REFERENCED_STUDY_SEQUENCE, a, true, Encoding_Utf8));
 
-      Json::Value c;
-      ServerToolbox::SimplifyTags(c, b, DicomToJsonFormat_Human);
+      {
+        Json::Value b;
+        FromDcmtkBridge::ElementToJson(b, *element, DicomToJsonFormat_Short, DicomToJsonFlags_Default, 0, Encoding_Ascii);
+        ASSERT_EQ(Json::arrayValue, b["0008,1110"].type());
+        ASSERT_EQ(2u, b["0008,1110"].size());
+      
+        Json::Value::ArrayIndex i = (b["0008,1110"][0]["0010,0010"].asString() == "Hello") ? 0 : 1;
 
-      a[1]["PatientName"] = "Hello2";  // To remove the Data URI Scheme encoding
-      ASSERT_EQ(0, c["ReferencedStudySequence"].compare(a));
+        ASSERT_EQ(3u, b["0008,1110"][i].size());
+        ASSERT_EQ(2u, b["0008,1110"][1 - i].size());
+        ASSERT_EQ(b["0008,1110"][i]["0010,0010"].asString(), "Hello");
+        ASSERT_EQ(b["0008,1110"][i]["0010,0020"].asString(), "World");
+        ASSERT_EQ(b["0008,1110"][i]["0008,1030"].asString(), "Toto");
+        ASSERT_EQ(b["0008,1110"][1 - i]["0010,0010"].asString(), "Hello2");
+        ASSERT_EQ(b["0008,1110"][1 - i]["0010,0020"].asString(), "World2");
+      }
+
+      {
+        Json::Value b;
+        FromDcmtkBridge::ElementToJson(b, *element, DicomToJsonFormat_Full, DicomToJsonFlags_Default, 0, Encoding_Ascii);
+
+        Json::Value c;
+        ServerToolbox::SimplifyTags(c, b, DicomToJsonFormat_Human);
+
+        a[1]["PatientName"] = "Hello2";  // To remove the Data URI Scheme encoding
+        ASSERT_EQ(0, c["ReferencedStudySequence"].compare(a));
+      }
     }
   }
 }
 
 
-
 TEST(ParsedDicomFile, InsertReplaceStrings)
 {
   ParsedDicomFile f(true);