diff Core/DicomFormat/DicomValue.cpp @ 3519:fc26659493b6

added support for COLUMNS/ROWS tags with invalid 800\0 value observed in some US images
author amazy
date Thu, 19 Sep 2019 13:10:10 +0200
parents 4e43e67f8ecf
children 94f4a18a79cc
line wrap: on
line diff
--- a/Core/DicomFormat/DicomValue.cpp	Thu Sep 05 13:11:35 2019 +0200
+++ b/Core/DicomFormat/DicomValue.cpp	Thu Sep 19 13:10:10 2019 +0200
@@ -94,6 +94,60 @@
   }
 #endif
 
+  // same as ParseValue but in case the value actually contains a sequence,
+  // it will return the first value
+  // this has been introduced to support invalid "width/height" DICOM tags in some US
+  // images where the width is stored as "800\0" !
+  template <typename T,
+            bool allowSigned>
+  static bool ParseFirstValue(T& result,
+                              const DicomValue& source)
+  {
+    if (source.IsBinary() ||
+        source.IsNull())
+    {
+      return false;
+    }
+
+    try
+    {
+      std::string value = Toolbox::StripSpaces(source.GetContent());
+      if (value.empty())
+      {
+        return false;
+      }
+
+      if (!allowSigned &&
+          value[0] == '-')
+      {
+        return false;
+      }
+
+      if (value.find("\\") == std::string::npos)
+      {
+        result = boost::lexical_cast<T>(value);
+        return true;
+      }
+      else
+      {
+        std::vector<std::string> tokens;
+        Toolbox::TokenizeString(tokens, value, '\\');
+
+        if (tokens.size() >= 1)
+        {
+          result = boost::lexical_cast<T>(tokens[0]);
+          return true;
+        }
+
+        return false;
+      }
+    }
+    catch (boost::bad_lexical_cast&)
+    {
+      return false;
+    }
+  }
+
 
   template <typename T,
             bool allowSigned>
@@ -177,6 +231,11 @@
     return ParseValue<double, true>(result, *this);
   }
 
+  bool DicomValue::ParseFirstUnsignedInteger(unsigned int& result) const
+  {
+    return ParseFirstValue<unsigned int, true>(result, *this);
+  }
+
   bool DicomValue::CopyToString(std::string& result,
                                 bool allowBinary) const
   {