changeset 6994:0d2fa25aa933

improved consistency of calls to ITagVisitor::VisitIntegers()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 06 Jul 2026 14:03:39 +0200
parents f4b6e9a6ab2a
children 28683da53c6b
files OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.cpp OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp OrthancFramework/Sources/DicomParsing/ITagVisitor.h OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp
diffstat 4 files changed, 182 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.cpp	Mon Jul 06 12:23:10 2026 +0200
+++ b/OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.cpp	Mon Jul 06 14:03:39 2026 +0200
@@ -452,12 +452,8 @@
                                    const void* data,
                                    size_t size)
   {
-    assert(vr == ValueRepresentation_OtherByte ||
-           vr == ValueRepresentation_OtherDouble ||
-           vr == ValueRepresentation_OtherFloat ||
-           vr == ValueRepresentation_OtherLong ||
-           vr == ValueRepresentation_OtherWord ||
-           vr == ValueRepresentation_OtherVeryLong ||
+    assert((vr == ValueRepresentation_OtherWord && tag == DICOM_TAG_PIXEL_DATA) ||
+           vr == ValueRepresentation_OtherByte ||
            vr == ValueRepresentation_Unknown);
 
     if (tag.GetElement() != 0x0000)
@@ -523,6 +519,16 @@
                                      ValueRepresentation vr,
                                      const std::vector<int64_t>& values)
   {
+    assert((vr == ValueRepresentation_OtherWord && tag != DICOM_TAG_PIXEL_DATA) ||
+           vr == ValueRepresentation_OtherLong ||
+           vr == ValueRepresentation_OtherVeryLong ||
+           vr == ValueRepresentation_SignedLong ||
+           vr == ValueRepresentation_SignedShort ||
+           vr == ValueRepresentation_SignedVeryLong ||
+           vr == ValueRepresentation_UnsignedLong ||
+           vr == ValueRepresentation_UnsignedShort ||
+           vr == ValueRepresentation_UnsignedVeryLong);
+
     if (tag.GetElement() != 0x0000 &&
         vr != ValueRepresentation_NotSupported)
     {
@@ -551,6 +557,11 @@
                                     ValueRepresentation vr,
                                     const std::vector<double>& values)
   {
+    assert(vr == ValueRepresentation_FloatingPointDouble ||
+           vr == ValueRepresentation_FloatingPointSingle ||
+           vr == ValueRepresentation_OtherDouble ||
+           vr == ValueRepresentation_OtherFloat);
+
     if (tag.GetElement() != 0x0000 &&
         vr != ValueRepresentation_NotSupported)
     {
--- a/OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp	Mon Jul 06 12:23:10 2026 +0200
+++ b/OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp	Mon Jul 06 14:03:39 2026 +0200
@@ -165,13 +165,24 @@
   }
 
 
+  static bool IsBinaryTag(DcmEVR evr,
+                          Uint16 group,
+                          Uint16 element)
+  {
+    return (evr == EVR_OB ||
+            (evr == EVR_OW &&
+             group == DCM_PixelData.getGroup() &&
+             element == DCM_PixelData.getElement()) ||
+            evr == EVR_UN ||
+            evr == EVR_ox);
+  }
+
+
   static bool IsBinaryTag(const DcmTag& key)
   {
+    // TODO - WARNING: This function doesn't inspect the registry of tags, don't use if possible
     return (key.isUnknownVR() ||
-            key.getEVR() == EVR_OB ||
-            key.getEVR() == EVR_OW ||
-            key.getEVR() == EVR_UN ||
-            key.getEVR() == EVR_ox);
+            IsBinaryTag(key.getEVR(), key.getGTag(), key.getETag()));
   }
 
 
@@ -541,6 +552,25 @@
     };
 
 
+    class ValueRepresentationFiller_OW : public ValueRepresentationFillerGeneric<Uint16>
+    {
+    protected:
+      virtual bool PutSingleValue(DcmElement& element,
+                                  const Uint16& value) const ORTHANC_OVERRIDE
+      {
+        // Contrary to US, the underlying type "DcmOtherByteOtherWord"
+        // doesn't implement "putUint16()" for a single value
+        return element.putUint16Array(&value, 1).good();
+      }
+
+      virtual bool PutMultipleValues(DcmElement& element,
+                                     const std::vector<Uint16>& values) const ORTHANC_OVERRIDE
+      {
+        return element.putUint16Array(values.empty() ? NULL : &values[0], values.size()).good();
+      }
+    };
+
+
     class ValueRepresentationFiller_US : public ValueRepresentationFillerGeneric<Uint16>
     {
     protected:
@@ -910,6 +940,68 @@
 #endif
 
 
+    class ValueRepresentationReader_OW : public ILeafElementArrayReader
+    {
+    private:
+      bool      valid_;
+      uint64_t  size_;
+      Uint16*   content_;
+
+    public:
+      ValueRepresentationReader_OW(DcmElement& element) :
+        valid_(false)
+      {
+        DcmOtherByteOtherWord& e = dynamic_cast<DcmOtherByteOtherWord&>(element);
+
+        if (e.getUint16Array(content_).good() &&
+            content_ != NULL)
+        {
+          if (element.getLength() % sizeof(Uint16) != 0)
+          {
+            throw OrthancException(ErrorCode_BadFileFormat);
+          }
+
+          size_ = static_cast<uint64_t>(element.getLength()) / sizeof(Uint16);
+          valid_ = true;
+        }
+      }
+
+      virtual bool IsValid() const ORTHANC_OVERRIDE
+      {
+        return valid_;
+      }
+
+      virtual uint64_t GetSize() const ORTHANC_OVERRIDE
+      {
+        assert(valid_);
+        return size_;
+      }
+
+      virtual void ReadValue(std::string& value,
+                             uint64_t index) const ORTHANC_OVERRIDE
+      {
+        assert(valid_);
+        value = boost::lexical_cast<std::string>(content_[index]);
+      }
+
+      void ReadArrayOfIntegers(std::vector<int64_t>& target) const
+      {
+        if (valid_)
+        {
+          target.resize(size_);
+          for (uint64_t i = 0; i < size_; i++)
+          {
+            target[i] = content_[i];
+          }
+        }
+        else
+        {
+          throw OrthancException(ErrorCode_BadFileFormat);
+        }
+      }
+    };
+
+
 #if DCMTK_VERSION_NUMBER >= 362
     class ValueRepresentationReader_OL : public ILeafElementArrayReader
     {
@@ -1443,7 +1535,7 @@
       {
         Uint8* data = NULL;
 
-        if (element.getUint8Array(data) == EC_Normal)
+        if (element.getUint8Array(data).good())
         {
           Uint32 length = element.getLength();
 
@@ -1530,11 +1622,11 @@
           {
             Uint8* data = NULL;
             Uint16* data16 = NULL;
-            if (element.getUint8Array(data) == EC_Normal)
+            if (element.getUint8Array(data).good())
             {
               return new DicomValue(reinterpret_cast<const char*>(data), element.getLength(), true);
             }
-            else if (element.getUint16Array(data16) == EC_Normal)
+            else if (element.getUint16Array(data16).good())
             {
               return new DicomValue(reinterpret_cast<const char*>(data16), element.getLength(), true);
             }
@@ -1937,7 +2029,7 @@
         }
       }
 
-      if (IsBinaryTag(element->getTag()))
+      if (IsBinaryTag(element->getTag()))   // TODO - Inspect the registry of tags
       {
         // This is a binary tag
         if ((tag == DICOM_TAG_PIXEL_DATA && !(flags & DicomToJsonFlags_IncludePixelData)) ||
@@ -2770,25 +2862,27 @@
       decoded = &binary;
     }
 
-    if (element.getTag().getEVR() == EVR_OW)
+    if (IsBinaryTag(element.getTag()))   // TODO - Inspect the registry of tags
     {
-      if (decoded->size() % sizeof(Uint16) != 0)
+      bool ok;
+
+      if (element.getTag().getEVR() == EVR_OW)
       {
-        throw OrthancException(ErrorCode_ParameterOutOfRange, "A tag with OW VR must have an even number of bytes");
-      }
-      else if (element.putUint16Array(reinterpret_cast<const Uint16*>(decoded->c_str()), decoded->size() / sizeof(Uint16)).good())
-      {
-        return;  // We're done
+        if (decoded->size() % sizeof(Uint16) != 0)
+        {
+          throw OrthancException(ErrorCode_ParameterOutOfRange, "A tag with OW VR must have an even number of bytes");
+        }
+        else
+        {
+          ok = element.putUint16Array(reinterpret_cast<const Uint16*>(decoded->c_str()), decoded->size() / sizeof(Uint16)).good();
+        }
       }
       else
       {
-        THROW_WITH_FILE_AND_LINE_INFO(ErrorCode_InternalError);
+        ok = element.putUint8Array(reinterpret_cast<const Uint8*>(decoded->c_str()), decoded->size()).good();
       }
-    }
-
-    if (IsBinaryTag(element.getTag()))
-    {
-      if (element.putUint8Array(reinterpret_cast<const Uint8*>(decoded->c_str()), decoded->size()).good())
+
+      if (ok)
       {
         return;  // We're done
       }
@@ -2811,7 +2905,6 @@
          **/
 
         case EVR_OB:  // other byte
-        case EVR_OW:  // other word
         case EVR_UN:  // unknown value representation
         case EVR_ox:  // OB or OW depending on context
           throw OrthancException(ErrorCode_InternalError);
@@ -2888,6 +2981,10 @@
           break;
         }
 
+        case EVR_OW:  // other word (was handled as a binary tag in Orthanc <= 1.12.11)
+          ok = IElementFiller::Apply(element, ValueRepresentationFiller_OW(), *decoded);
+          break;
+
         case EVR_US:  // unsigned short
           ok = IElementFiller::Apply(element, ValueRepresentationFiller_US(), *decoded);
           break;
@@ -3501,32 +3598,20 @@
      * Deal with binary data (including PixelData).
      **/
 
-    if (evr == EVR_OB ||  // other byte
-        evr == EVR_OW ||  // other word
-        evr == EVR_UN)    // unknown value representation
+    if (IsBinaryTag(evr, element.getTag().getGroup(), element.getTag().getElement()))
     {
       Uint16* data16 = NULL;
       Uint8* data = NULL;
 
       ITagVisitor::Action action;
       
-      if ((element.getTag() == DCM_PixelData ||  // (*) New in Orthanc 1.9.1
-           evr == EVR_OW) &&
-          element.getUint16Array(data16) == EC_Normal)
+      if (evr == EVR_OW &&
+          element.getUint16Array(data16).good())
       {
         action = visitor.VisitBinary(parentTags, parentIndexes, tag, vr, data16, element.getLength());
       }
-      else if (evr != EVR_OW &&
-               element.getUint8Array(data) == EC_Normal)
+      else if (element.getUint8Array(data).good())
       {
-        /**
-         * WARNING: The call to "getUint8Array()" crashes
-         * (segmentation fault) on big-endian architectures if applied
-         * to pixel data, during the call to "swapIfNecessary()" in
-         * "DcmPolymorphOBOW::getUint8Array()" (this method is not
-         * reimplemented in derived class "DcmPixelData"). However,
-         * "getUint16Array()" works correctly, hence (*).
-         **/
         action = visitor.VisitBinary(parentTags, parentIndexes, tag, vr, data, element.getLength());
       }
       else
@@ -3636,7 +3721,7 @@
         {
           Uint8* data = NULL;
           
-          if (element.getUint8Array(data) == EC_Normal)
+          if (element.getUint8Array(data).good())
           {
             const Uint32 length = element.getLength();
             Uint32 l = 0;
@@ -3791,6 +3876,20 @@
           break;
         }
 
+        case EVR_OW:  // other word - binary array of 16-bit unsigned integers (new in Orthanc 1.12.12)
+          if (tag == DICOM_TAG_PIXEL_DATA)
+          {
+            throw OrthancException(ErrorCode_InternalError);  // Should have been handled by "visitor.VisitBinary()"
+          }
+          else
+          {
+            ValueRepresentationReader_OW reader(element);
+            std::vector<int64_t> values;
+            reader.ReadArrayOfIntegers(values);
+            action = visitor.VisitIntegers(parentTags, parentIndexes, tag, vr, values);
+            break;
+          }
+
         case EVR_OF:  // other float - binary array of 32-bit floats (new in Orthanc 1.12.11)
         {
           ValueRepresentationReader_OF reader(element);
--- a/OrthancFramework/Sources/DicomParsing/ITagVisitor.h	Mon Jul 06 12:23:10 2026 +0200
+++ b/OrthancFramework/Sources/DicomParsing/ITagVisitor.h	Mon Jul 06 14:03:39 2026 +0200
@@ -58,7 +58,7 @@
                                  const DicomTag& tag,
                                  size_t countItems) = 0;
 
-    // SL, SS, UL, US - can return "Remove" or "None"
+    // SL, SS, UL, US, OW (if not pixel data), OL, OV, SV, UV - can return "Remove" or "None"
     virtual Action VisitIntegers(const std::vector<DicomTag>& parentTags,
                                  const std::vector<size_t>& parentIndexes,
                                  const DicomTag& tag,
@@ -78,7 +78,28 @@
                                    const DicomTag& tag,
                                    const std::vector<DicomTag>& values) = 0;
 
-    // OB, OL, OW, UN - can return "Remove" or "None"
+    /**
+     * "Binary" tags are represented as a raw byte buffer. This
+     * representation is used for:
+     *
+     * OB (Other Byte): always treated as an opaque byte stream, since
+     * its content may be non-numeric, compressed, or otherwise not
+     * meaningfully interpretable as a fixed-size element type.
+     *
+     * OW (Other Word): normally represents an array of 16-bit words
+     * and would map to a typed integer container (uint16_t), but it
+     * is treated as a raw memory buffer only in the specific case of
+     * PixelData, where the true bit depth/interpretation depends on
+     * other attributes (Bits Allocated, Bits Stored, Pixel
+     * Representation) rather than being fixed by the VR itself. Other
+     * OW elements should NOT use this representation.
+     *
+     * WARNING: In Orthanc <= 1.12.11, all OW values were considered
+     * forwarded to "VisitBinary()". Now, OW values that are not pixel
+     * data are forwarded to "VisitIntegers()".
+     **/
+
+    // OW (only if pixel data), OB, and UN - can return "Remove" or "None"
     virtual Action VisitBinary(const std::vector<DicomTag>& parentTags,
                                const std::vector<size_t>& parentIndexes,
                                const DicomTag& tag,
@@ -97,5 +118,5 @@
     // empty sequence element - can return "Remove" or "None"
     virtual Action VisitEmptyElement(const std::vector<DicomTag>& parentTags,
                                      const std::vector<size_t>& parentIndexes) = 0;
-    };
+  };
 }
--- a/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp	Mon Jul 06 12:23:10 2026 +0200
+++ b/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp	Mon Jul 06 14:03:39 2026 +0200
@@ -2035,7 +2035,7 @@
   dicom.ReplacePlainString(DicomTag(0x0064, 0x0009), "2.71828");  // OF (other float)
   dicom.ReplacePlainString(DicomTag(0x0066, 0x0040), "46");  // OL (other long)
   ASSERT_THROW(dicom.ReplacePlainString(DicomTag(0x0028, 0x1201), "O"), OrthancException);
-  dicom.ReplacePlainString(DicomTag(0x0028, 0x1201), "OWOW");
+  dicom.ReplacePlainString(DicomTag(0x0028, 0x1201), "57");  // OW
   dicom.ReplacePlainString(DicomTag(0x0010, 0x0010), "PN");
   dicom.ReplacePlainString(DicomTag(0x0008, 0x0050), "SH");
   dicom.ReplacePlainString(DicomTag(0x0018, 0x6020), "-15");  // SL
@@ -2121,8 +2121,7 @@
 #endif
 
   ASSERT_EQ("OW", visitor.GetResult() ["00281201"]["vr"].asString());
-  Toolbox::DecodeBase64(s, visitor.GetResult() ["00281201"]["InlineBinary"].asString());
-  ASSERT_EQ("OWOW", s);
+  ASSERT_EQ(57, visitor.GetResult() ["00281201"]["Value"][0].asInt());
 
   ASSERT_EQ("PN", visitor.GetResult() ["00100010"]["vr"].asString());
   ASSERT_EQ("PN", visitor.GetResult() ["00100010"]["Value"][0]["Alphabetic"].asString());
@@ -2230,7 +2229,7 @@
 
     ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0064, 0x0009), true));
     ASSERT_FLOAT_EQ(2.71828f, boost::lexical_cast<float>(s));
-    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0028, 0x1201), true));   ASSERT_EQ("OWOW", s);
+    ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0028, 0x1201), false));  ASSERT_EQ("57", s);
     ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0010, 0x0010), false));  ASSERT_EQ("PN", s);
     ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0008, 0x0050), false));  ASSERT_EQ("SH", s);
     ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x0018, 0x6020), false));  ASSERT_EQ("-15", s);