changeset 6825:adebd0adab8d streaming

simplification of DicomDataSource
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 21 May 2026 08:20:16 +0200
parents b9ecf4edf9c5
children bee76cdf2497
files OrthancFramework/Sources/DataSource/DicomDataSource.cpp OrthancFramework/Sources/DataSource/DicomDataSource.h OrthancServer/Sources/ServerContext.cpp OrthancServer/Sources/ServerContext.h OrthancServer/Sources/ServerTranscoder.cpp
diffstat 5 files changed, 64 insertions(+), 162 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.cpp	Thu May 21 07:57:26 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomDataSource.cpp	Thu May 21 08:20:16 2026 +0200
@@ -35,34 +35,10 @@
 
 namespace Orthanc
 {
-  static size_t ComputeValueSize(size_t size,
-                                 bool isKeepRawBuffer)
-  {
-    if (isKeepRawBuffer)
-    {
-      uint64_t s = 2 * static_cast<uint64_t>(size);
-      if (s != static_cast<uint64_t>(static_cast<size_t>(s)))
-      {
-        return std::numeric_limits<size_t>::max();
-      }
-      else
-      {
-        return static_cast<size_t>(s);
-      }
-    }
-    else
-    {
-      return size;
-    }
-  }
-
-
   class DicomDataSource::BaseIdentifier : public IDataIdentifier
   {
   public:
     virtual StorageAreaDataSource::Range* ReadRange(DataSourceReader& reader) const = 0;
-
-    virtual bool IsKeepRawBuffer() const = 0;
   };
 
 
@@ -70,15 +46,12 @@
   {
   private:
     FileInfo  attachment_;
-    bool      keepRawBuffer_;
     bool      checkMD5_;
 
   public:
     explicit WholeIdentifier(const FileInfo& attachment,
-                             bool keepRawBuffer,
                              bool checkMD5) :
       attachment_(attachment),
-      keepRawBuffer_(keepRawBuffer),
       checkMD5_(checkMD5)
     {
     }
@@ -86,14 +59,13 @@
     virtual bool GetCacheKey(std::string& key) const ORTHANC_OVERRIDE
     {
       key = (attachment_.GetUuid() + "|" +
-             boost::lexical_cast<std::string>(attachment_.GetContentType()) + "|" +
-             boost::lexical_cast<std::string>(keepRawBuffer_));
+             boost::lexical_cast<std::string>(attachment_.GetContentType()));
       return true;
     }
 
     virtual bool EstimateValueSize(size_t& target) const ORTHANC_OVERRIDE
     {
-      target = ComputeValueSize(attachment_.GetUncompressedSize(), keepRawBuffer_);
+      target = attachment_.GetUncompressedSize();
       return true;
     }
 
@@ -101,11 +73,6 @@
     {
       return StorageAreaDataSource::ReadAttachment(reader, attachment_, true /* uncompress */, checkMD5_);
     }
-
-    virtual bool IsKeepRawBuffer() const ORTHANC_OVERRIDE
-    {
-      return keepRawBuffer_;
-    }
   };
 
 
@@ -143,11 +110,6 @@
     {
       return StorageAreaDataSource::ReadBeginning(reader, attachment_, pixelDataOffset_);
     }
-
-    virtual bool IsKeepRawBuffer() const ORTHANC_OVERRIDE
-    {
-      return false;
-    }
   };
 
 
@@ -156,15 +118,12 @@
   private:
     std::unique_ptr<ParsedDicomFile>   dicom_;
     size_t                             size_;
-    bool                               hasRawBuffer_;
-    std::string                        rawBuffer_;
 
   public:
     Value(ParsedDicomFile* dicom,
           size_t size) :
       dicom_(dicom),
-      size_(size),
-      hasRawBuffer_(false)
+      size_(size)
     {
       if (dicom == NULL)
       {
@@ -177,46 +136,10 @@
       return *dicom_;
     }
 
-    size_t GetRawBufferSize() const
+    size_t GetSize() const
     {
       return size_;
     }
-
-    void SetRawBuffer(const void* data,
-                      size_t size)
-    {
-      if (hasRawBuffer_)
-      {
-        throw OrthancException(ErrorCode_BadSequenceOfCalls);
-      }
-      else if (size != size_)
-      {
-        throw OrthancException(ErrorCode_ParameterOutOfRange);
-      }
-      else
-      {
-        hasRawBuffer_ = true;
-        rawBuffer_.assign(reinterpret_cast<const char*>(data), size);
-      }
-    }
-
-    bool HasRawBuffer() const
-    {
-      return hasRawBuffer_;
-    }
-
-    const void* GetRawBufferData() const
-    {
-      if (hasRawBuffer_)
-      {
-        assert(rawBuffer_.size() == size_);
-        return rawBuffer_.empty() ? NULL : rawBuffer_.c_str();
-      }
-      else
-      {
-        throw OrthancException(ErrorCode_BadSequenceOfCalls);
-      }
-    }
   };
 
 
@@ -240,30 +163,16 @@
 
     {
       // Sanity check
-      bool ok = false;
       size_t estimatedSize;
-      if (obj.EstimateValueSize(estimatedSize))
-      {
-        ok = (estimatedSize == ComputeValueSize(size, id.IsKeepRawBuffer()));
-      }
-
-      if (!ok)
+      if (!obj.EstimateValueSize(estimatedSize) ||
+          estimatedSize != size)
       {
         throw OrthancException(ErrorCode_InternalError);
       }
     }
 
     std::unique_ptr<Value> value(new Value(new ParsedDicomFile(range->GetData(), size), size));
-
-    if (id.IsKeepRawBuffer())
-    {
-      value->SetRawBuffer(range->GetData(), size);
-      assert(GetValueSize(*value) == 2 * size);
-    }
-    else
-    {
-      assert(GetValueSize(*value) == size);
-    }
+    assert(GetValueSize(*value) == size);
 
     return value.release();
   }
@@ -272,7 +181,7 @@
   size_t DicomDataSource::GetValueSize(const IDynamicObject& obj) const
   {
     const Value& value = dynamic_cast<const Value&>(obj);
-    return ComputeValueSize(value.GetRawBufferSize(), value.HasRawBuffer());
+    return value.GetSize();
   }
 
 
@@ -299,30 +208,11 @@
   }
 
 
-  bool DicomDataSource::Dicom::Lock::HasRawBuffer() const
-  {
-    return dynamic_cast<const Value&>(*that_.value_).HasRawBuffer();
-  }
-
-
-  const void* DicomDataSource::Dicom::Lock::GetRawBufferData() const
-  {
-    return dynamic_cast<const Value&>(*that_.value_).GetRawBufferData();
-  }
-
-
-  size_t DicomDataSource::Dicom::Lock::GetRawBufferSize() const
-  {
-    return dynamic_cast<const Value&>(*that_.value_).GetRawBufferSize();
-  }
-
-
   DicomDataSource::Dicom* DicomDataSource::ReadWhole(DataSourceReader& reader,
                                                      const FileInfo& attachment,
-                                                     bool keepRawBuffer,
                                                      bool checkMD5)
   {
-    std::unique_ptr<IDataIdentifier> id(new WholeIdentifier(attachment, keepRawBuffer, checkMD5));
+    std::unique_ptr<IDataIdentifier> id(new WholeIdentifier(attachment, checkMD5));
     boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release());
     return new Dicom(value);
   }
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.h	Thu May 21 07:57:26 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/DicomDataSource.h	Thu May 21 08:20:16 2026 +0200
@@ -93,18 +93,11 @@
         }
 
         const ParsedDicomFile& GetContent() const;
-
-        bool HasRawBuffer() const;
-
-        const void* GetRawBufferData() const;
-
-        size_t GetRawBufferSize() const;
       };
     };
 
     static Dicom* ReadWhole(DataSourceReader& reader,
                             const FileInfo& attachment,
-                            bool keepRawBuffer,  // Must be set to "true" for transcoding
                             bool checkMD5);
 
     static Dicom* ReadUntilPixelData(DataSourceReader& reader,
--- a/OrthancServer/Sources/ServerContext.cpp	Thu May 21 07:57:26 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.cpp	Thu May 21 08:20:16 2026 +0200
@@ -25,7 +25,7 @@
 #include "ServerContext.h"
 
 #include "../../OrthancFramework/Sources/Cache/SharedArchive.h"
-#include "../../OrthancFramework/Sources/DataSource/StorageAreaDataSource.h"
+#include "../../OrthancFramework/Sources/DataSource/DataSourceReader.h"
 #include "../../OrthancFramework/Sources/DataSource/TranscoderDataSource.h"
 #include "../../OrthancFramework/Sources/DicomFormat/DicomElement.h"
 #include "../../OrthancFramework/Sources/DicomFormat/DicomStreamReader.h"
@@ -1311,7 +1311,7 @@
     if (LookupAttachment(attachment, FileContentType_DicomUntilPixelData, instanceAttachments))
     {
       std::unique_ptr<DicomDataSource::Dicom> dicom(
-        DicomDataSource::ReadWhole(*dicomReader_, attachment, false /* no need for raw buffer */, checkMD5_));
+        DicomDataSource::ReadWhole(*dicomReader_, attachment, checkMD5_));
 
       {
         DicomDataSource::Dicom::Lock lock(*dicom);
@@ -1403,40 +1403,39 @@
           throw OrthancException(ErrorCode_UnknownResource);
         }
 
-        std::unique_ptr<DicomDataSource::Dicom> dicom(
-          DicomDataSource::ReadWhole(*dicomReader_, attachment, !hasPixelDataOffset /* raw buffer is needed for (*) */, checkMD5_));
+        std::unique_ptr<StorageAreaDataSource::Range> range(
+          StorageAreaDataSource::ReadAttachment(*storageAreaReader_, attachment, true /* uncompress */, checkMD5_));
 
+        // We don't use the cache, as this would complexify the code for a rare edge case
+        ParsedDicomFile dicom(range->GetData(), range->GetSize());
+        OrthancConfiguration::DefaultDicomDatasetToJson(result, dicom, ignoreTagLength);
+
+        if (!hasPixelDataOffset)
         {
-          DicomDataSource::Dicom::Lock lock(*dicom);
-          OrthancConfiguration::DefaultDicomDatasetToJson(result, lock.GetContent(), ignoreTagLength);
+          /**
+           * (*) The pixel data offset was never computed for this
+           * instance, which indicates that it was created using
+           * Orthanc <= 1.9.0, or that calls to
+           * "LookupPixelDataOffset()" from earlier versions of
+           * Orthanc have failed. Try again this precomputation now
+           * for future calls.
+           **/
 
-          if (!hasPixelDataOffset)
+          ValueRepresentation pixelDataVR;
+          if (DicomStreamReader::LookupPixelDataOffset(
+                pixelDataOffset, pixelDataVR, range->GetData(), range->GetSize()) &&
+              pixelDataOffset < range->GetSize())
           {
-            /**
-             * (*) The pixel data offset was never computed for this
-             * instance, which indicates that it was created using
-             * Orthanc <= 1.9.0, or that calls to
-             * "LookupPixelDataOffset()" from earlier versions of
-             * Orthanc have failed. Try again this precomputation now
-             * for future calls.
-             **/
+            index_.OverwriteMetadata(instancePublicId, MetadataType_Instance_PixelDataOffset,
+                                     boost::lexical_cast<std::string>(pixelDataOffset));
 
-            ValueRepresentation pixelDataVR;
-            if (DicomStreamReader::LookupPixelDataOffset(
-                  pixelDataOffset, pixelDataVR, lock.GetRawBufferData(), lock.GetRawBufferSize()) &&
-                pixelDataOffset < lock.GetRawBufferSize())
+            if (!area_.HasEfficientReadRange() ||
+                compressionEnabled_)
             {
-              index_.OverwriteMetadata(instancePublicId, MetadataType_Instance_PixelDataOffset,
-                                       boost::lexical_cast<std::string>(pixelDataOffset));
-
-              if (!area_.HasEfficientReadRange() ||
-                  compressionEnabled_)
-              {
-                int64_t newRevision;
-                AddAttachment(newRevision, instancePublicId, ResourceType_Instance, FileContentType_DicomUntilPixelData,
-                              lock.GetRawBufferData(), pixelDataOffset,
-                              false /* no old revision */, -1 /* dummy revision */, "" /* dummy MD5 */);
-              }
+              int64_t newRevision;
+              AddAttachment(newRevision, instancePublicId, ResourceType_Instance, FileContentType_DicomUntilPixelData,
+                            range->GetData(), pixelDataOffset,
+                            false /* no old revision */, -1 /* dummy revision */, "" /* dummy MD5 */);
             }
           }
         }
@@ -1477,19 +1476,34 @@
   }
 
 
-  DicomDataSource::Dicom* ServerContext::ReadParsedDicom(const std::string& instancePublicId)
+  FileInfo ServerContext::LookupDicomForInstance(const std::string& instancePublicId)
   {
     FileInfo attachment;
     int64_t revision;
 
-    if (!index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_Dicom))
+    if (index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_Dicom))
+    {
+      return attachment;
+    }
+    else
     {
       throw OrthancException(ErrorCode_InternalError,
                              "Unable to read attachment " + EnumerationToString(FileContentType_Dicom) +
                              " of instance " + instancePublicId);
     }
+  }
 
-    return DicomDataSource::ReadWhole(*dicomReader_, attachment, false, checkMD5_);
+
+  StorageAreaDataSource::Range* ServerContext::ReadRawDicom(const std::string& instancePublicId)
+  {
+    return StorageAreaDataSource::ReadAttachment(*storageAreaReader_, LookupDicomForInstance(instancePublicId),
+                                                 true /* uncompress */, checkMD5_);
+  }
+
+
+  DicomDataSource::Dicom* ServerContext::ReadParsedDicom(const std::string& instancePublicId)
+  {
+    return DicomDataSource::ReadWhole(*dicomReader_, LookupDicomForInstance(instancePublicId), checkMD5_);
   }
 
 
--- a/OrthancServer/Sources/ServerContext.h	Thu May 21 07:57:26 2026 +0200
+++ b/OrthancServer/Sources/ServerContext.h	Thu May 21 08:20:16 2026 +0200
@@ -29,11 +29,12 @@
 #include "ServerIndex.h"
 #include "ServerJobs/IStorageCommitmentFactory.h"
 
-#include "../../OrthancFramework/Sources/DataSource/DataSourceReader.h"
 #include "../../OrthancFramework/Sources/DataSource/DicomDataSource.h"
+#include "../../OrthancFramework/Sources/DataSource/StorageAreaDataSource.h"
 #include "../../OrthancFramework/Sources/DicomParsing/DicomModification.h"
 #include "../../OrthancFramework/Sources/JobsEngine/JobsEngine.h"
 #include "../../OrthancFramework/Sources/MetricsRegistry.h"
+#include "../../OrthancFramework/Sources/MultiThreading/Future.h"
 #include "../../OrthancFramework/Sources/MultiThreading/Semaphore.h"
 
 
@@ -382,7 +383,11 @@
                          const std::string& instancePublicId);  // TODO-FIND: Can this be removed?
 
     void ReadDicom(std::string& dicom,
-                   const std::string& instancePublicId);
+                   const std::string& instancePublicId);  // TODO-Streaming : Remove
+
+    FileInfo LookupDicomForInstance(const std::string& instancePublicId);
+
+    StorageAreaDataSource::Range* ReadRawDicom(const std::string& instancePublicId);
 
     DicomDataSource::Dicom* ReadParsedDicom(const std::string& instancePublicId);
 
--- a/OrthancServer/Sources/ServerTranscoder.cpp	Thu May 21 07:57:26 2026 +0200
+++ b/OrthancServer/Sources/ServerTranscoder.cpp	Thu May 21 08:20:16 2026 +0200
@@ -234,7 +234,7 @@
                                                       bool checkMD5)
   {
     std::unique_ptr<DicomDataSource::Dicom> dicom(
-      DicomDataSource::ReadWhole(*dicomReader, attachment, false, checkMD5));
+      DicomDataSource::ReadWhole(*dicomReader, attachment, checkMD5));
 
     DicomDataSource::Dicom::Lock lock(*dicom);