diff OrthancFramework/Sources/DicomFormat/DicomMap.cpp @ 5044:6fed78e13233

Refactored DicomMap to handle sequences when needed
author Alain Mazy <am@osimis.io>
date Tue, 28 Jun 2022 17:45:09 +0200
parents 1c08cd68250a
children ea9e2680da6f
line wrap: on
line diff
--- a/OrthancFramework/Sources/DicomFormat/DicomMap.cpp	Mon Jun 27 15:22:19 2022 +0200
+++ b/OrthancFramework/Sources/DicomFormat/DicomMap.cpp	Tue Jun 28 17:45:09 2022 +0200
@@ -359,6 +359,11 @@
     SetValueInternal(group, element, new DicomValue(str, isBinary));
   }
 
+  void DicomMap::SetValue(const DicomTag& tag, const Json::Value& value)
+  {
+    SetValueInternal(tag.GetGroup(), tag.GetElement(), new DicomValue(value));
+  }
+
   bool DicomMap::HasTag(uint16_t group, uint16_t element) const
   {
     return HasTag(DicomTag(group, element));
@@ -1328,7 +1333,7 @@
   }
 
   
-  void DicomMap::FromDicomAsJson(const Json::Value& dicomAsJson, bool append)
+  void DicomMap::FromDicomAsJson(const Json::Value& dicomAsJson, bool append, bool parseSequences)
   {
     if (dicomAsJson.type() != Json::objectValue)
     {
@@ -1371,6 +1376,18 @@
           SetValue(tag, value["Value"].asString(), false /* not binary */);
         }
       }
+      else if (value["Type"] == "Sequence" && parseSequences)
+      {
+        if (value["Value"].type() != Json::arrayValue)
+        {
+          printf("%s", dicomAsJson.toStyledString().c_str());
+          throw OrthancException(ErrorCode_CorruptedFile);
+        }
+        else
+        {
+          SetValue(tag, value["Value"]);
+        }
+      }
     }
   }
 
@@ -1435,21 +1452,18 @@
     return true;
   }
 
-#if ORTHANC_ENABLE_DCMTK == 1
-  void DicomMap::ExtractSequences(std::set<DicomTag>& sequences, const std::set<DicomTag>& tags)
+  void DicomMap::ExtractSequences(DicomMap& result) const
   {
-    sequences.clear();
+    result.Clear();
 
-    for (std::set<DicomTag>::const_iterator it = tags.begin(); it != tags.end(); ++it)
+    for (Content::const_iterator it = content_.begin(); it != content_.end(); ++it)
     {
-      ValueRepresentation vr = FromDcmtkBridge::LookupValueRepresentation(*it);
-      if (vr == ValueRepresentation_Sequence)
+      if (it->second->IsSequence())
       {
-        sequences.insert(*it);
+        result.SetValue(it->first, it->second->GetSequenceContent());
       }
     }
   }
-#endif
 
   void DicomMap::Serialize(Json::Value& target) const
   {
@@ -1673,6 +1687,27 @@
   }
 
 
+  void DicomMap::RemoveSequences()
+  {
+    Content kept;
+
+    for (Content::iterator it = content_.begin(); it != content_.end(); ++it)
+    {
+      assert(it->second != NULL);
+
+      if (!it->second->IsSequence())
+      {
+        kept[it->first] = it->second;
+      }
+      else
+      {
+        delete it->second;
+      }
+    }
+
+    content_ = kept;
+  }
+
   void DicomMap::DumpMainDicomTags(Json::Value& target,
                                    ResourceType level) const
   {