changeset 6786:e10d890319c5

created DicomDataSource
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 14 May 2026 10:27:26 +0200
parents 967edd507e0e
children 0ab9e0f34268
files OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp OrthancFramework/Sources/DataSource/StorageAreaDataSource.h OrthancFramework/UnitTestsSources/JobsTests.cpp
diffstat 3 files changed, 213 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Wed May 13 17:26:47 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp	Thu May 14 10:27:26 2026 +0200
@@ -25,7 +25,10 @@
 #include "../PrecompiledHeaders.h"
 #include "StorageAreaDataSource.h"
 
+#include "../Compression/ZlibCompressor.h"
 #include "../OrthancException.h"
+#include "../StringMemoryBuffer.h"
+#include "../Toolbox.h"
 #include "DataSourceReader.h"
 
 #include <boost/lexical_cast.hpp>
@@ -163,8 +166,70 @@
                                                                  uint64_t end,
                                                                  const std::string& customData)
   {
-    std::unique_ptr<IDataIdentifier> id(new StorageAreaDataSource::Identifier(uuid, type, start, end, customData));
+    std::unique_ptr<IDataIdentifier> id(new Identifier(uuid, type, start, end, customData));
     boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release());
     return new Range(value);
   }
+
+
+  StorageAreaDataSource::Range* StorageAreaDataSource::ReadAttachment(DataSourceReader& reader,
+                                                                      const FileInfo& attachment,
+                                                                      bool uncompress,
+                                                                      bool checkMD5)
+  {
+    std::unique_ptr<StorageAreaDataSource::Range> range(ReadRange(reader,
+                                                                  attachment.GetUuid(),
+                                                                  attachment.GetContentType(),
+                                                                  0, attachment.GetCompressedSize(),
+                                                                  attachment.GetCustomData()));
+
+    if (checkMD5)
+    {
+      std::string md5;
+      Toolbox::ComputeMD5(md5, range->GetData(), range->GetSize());
+
+      if (md5 != attachment.GetCompressedMD5())
+      {
+        throw OrthancException(ErrorCode_CorruptedFile);
+      }
+    }
+
+    if (uncompress)
+    {
+      switch (attachment.GetCompressionType())
+      {
+        case CompressionType_None:
+          return range.release();
+
+        case CompressionType_ZlibWithSize:
+        {
+          ZlibCompressor zlib;
+
+          std::string content;
+          zlib.Uncompress(content, range->GetData(), range->GetSize());
+
+          if (checkMD5)
+          {
+            std::string md5;
+            Toolbox::ComputeMD5(md5, content);
+
+            if (md5 != attachment.GetUncompressedMD5())
+            {
+              throw OrthancException(ErrorCode_CorruptedFile);
+            }
+          }
+
+          boost::shared_ptr<Value> value(new Value(StringMemoryBuffer::CreateFromSwap(content)));
+          return new Range(value);
+        }
+
+        default:
+          throw OrthancException(ErrorCode_NotImplemented);
+      }
+    }
+    else
+    {
+      return range.release();
+    }
+  }
 }
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Wed May 13 17:26:47 2026 +0200
+++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h	Thu May 14 10:27:26 2026 +0200
@@ -25,6 +25,7 @@
 #pragma once
 
 #include "../Compatibility.h"
+#include "../FileStorage/FileInfo.h"
 #include "../FileStorage/IStorageArea.h"
 #include "IDataSource.h"
 
@@ -74,5 +75,10 @@
                             uint64_t start /* inclusive */,
                             uint64_t end /* exclusive */,
                             const std::string& customData);
+
+    static Range* ReadAttachment(DataSourceReader& reader,
+                                 const FileInfo& attachment,
+                                 bool uncompress,
+                                 bool checkMD5);
   };
 }
--- a/OrthancFramework/UnitTestsSources/JobsTests.cpp	Wed May 13 17:26:47 2026 +0200
+++ b/OrthancFramework/UnitTestsSources/JobsTests.cpp	Thu May 14 10:27:26 2026 +0200
@@ -1903,3 +1903,144 @@
 
   ASSERT_EQ(11u, counter_);
 }
+
+
+
+#include "../Sources/DataSource/DataSourceReader.h"
+#include "../Sources/DataSource/StorageAreaDataSource.h"
+#include "../Sources/FileStorage/FilesystemStorage.h"
+#include "../Sources/FileStorage/PluginStorageAreaAdapter.h"
+#include "../Sources/MultiThreading/ThreadPool.h"
+
+#include <dcmtk/dcmdata/dcfilefo.h>
+
+
+class DicomDataSource : public IDataSource
+{
+private:
+  boost::shared_ptr<DataSourceReader>  storageAreaReader_;
+  bool                                 checkMD5_;
+
+  class Identifier : public IDataIdentifier
+  {
+  private:
+    FileInfo  attachment_;
+
+  public:
+    Identifier(const FileInfo& attachment) :
+      attachment_(attachment)
+    {
+    }
+
+    const FileInfo& GetAttachment() const
+    {
+      return attachment_;
+    }
+
+    virtual bool GetCacheKey(std::string& key) const
+    {
+      // TODO - Support "read until pixel data"
+      key = (attachment_.GetUuid() + "|" +
+             boost::lexical_cast<std::string>(attachment_.GetContentType()));
+      return true;
+    }
+  };
+
+  class Value : public IDynamicObject
+  {
+  private:
+    std::unique_ptr<ParsedDicomFile>  dicom_;
+    size_t                            size_;
+
+  public:
+    Value(ParsedDicomFile* dicom,
+          size_t size) :
+      dicom_(dicom),
+      size_(size)
+    {
+      if (dicom == NULL)
+      {
+        throw OrthancException(ErrorCode_NullPointer);
+      }
+    }
+
+    ParsedDicomFile& GetDicom() const
+    {
+      return *dicom_;
+    }
+
+    size_t GetSize() const
+    {
+      return size_;
+    }
+  };
+
+public:
+  DicomDataSource(const boost::shared_ptr<DataSourceReader>& storageAreaReader) :
+    storageAreaReader_(storageAreaReader),
+    checkMD5_(false)
+  {
+    if (storageAreaReader == NULL)
+    {
+      throw OrthancException(ErrorCode_NullPointer);
+    }
+  }
+
+  void SetCheckMD5(bool check)
+  {
+    checkMD5_ = check;
+  }
+
+  virtual IDynamicObject* Load(const IDataIdentifier& obj) ORTHANC_OVERRIDE
+  {
+    const Identifier& id = dynamic_cast<const Identifier&>(obj);
+
+    std::unique_ptr<StorageAreaDataSource::Range> range(
+      StorageAreaDataSource::ReadAttachment(*storageAreaReader_, id.GetAttachment(), true /* uncompress */, true /* check MD5 */));
+
+    return new Value(new ParsedDicomFile(range->GetData(), range->GetSize()), id.GetAttachment().GetUncompressedSize());
+  }
+
+  virtual size_t GetValueSize(const IDynamicObject& obj) const ORTHANC_OVERRIDE
+  {
+    const Value& value = dynamic_cast<const Value&>(obj);
+    return value.GetSize();
+  }
+
+
+  // TODO - Add mutex and accessor
+  class Dicom : public boost::noncopyable
+  {
+  private:
+    boost::shared_ptr<IDynamicObject>  value_;
+
+  public:
+    Dicom(const boost::shared_ptr<IDynamicObject>& value) :
+      value_(value)
+    {
+      if (value.get() == NULL)
+      {
+        throw OrthancException(ErrorCode_NullPointer);
+      }
+    }
+
+    ParsedDicomFile& GetContent() const
+    {
+      return dynamic_cast<const Value&>(*value_).GetDicom();
+    }
+  };
+
+  static Dicom* ReadDicom(DataSourceReader& reader,
+                          const FileInfo& attachment)
+  {
+    if (attachment.GetCompressionType() == CompressionType_None &&
+        attachment.GetCompressedMD5() != attachment.GetUncompressedMD5())
+    {
+      throw OrthancException(ErrorCode_CorruptedFile);
+    }
+
+    std::unique_ptr<IDataIdentifier> id(new Identifier(attachment));
+    boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release());
+    return new Dicom(value);
+  }
+};