Mercurial > hg > orthanc
changeset 6865:634a8082ce04 streaming
integration mainline->streaming
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Tue, 26 May 2026 15:52:35 +0200 |
| parents | f475ce43d92f (diff) 5c82dafd83c8 (current diff) |
| children | 4bd9dd9e3b9c |
| files | OrthancServer/Sources/ServerContext.cpp |
| diffstat | 64 files changed, 2987 insertions(+), 1118 deletions(-) [+] |
line wrap: on
line diff
--- a/.hgignore Tue May 26 15:48:55 2026 +0200 +++ b/.hgignore Tue May 26 15:52:35 2026 +0200 @@ -2,8 +2,11 @@ ThirdPartyDownloads/ CMakeLists.txt.user *.cpp.orig +*.cpp.rej *.h.orig +*.h.rej *.patch.orig +*.patch.rej .vs/ .vscode/ *~
--- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue May 26 15:52:35 2026 +0200 @@ -657,10 +657,12 @@ list(APPEND ORTHANC_CORE_SOURCES_INTERNAL ${CMAKE_CURRENT_LIST_DIR}/../../Sources/Cache/MemoryStringCache.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/Cache/SharedArchive.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/BaseDataIdentifier.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceAnswer.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceMemoryBudget.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceReader.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceRequest.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DataSourceSequentialReader.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/StorageAreaDataSource.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileBuffer.cpp ${CMAKE_CURRENT_LIST_DIR}/../../Sources/FileStorage/FilesystemStorage.cpp @@ -681,6 +683,9 @@ if (ENABLE_DCMTK) list(APPEND ORTHANC_CORE_SOURCES_INTERNAL ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DicomDataSource.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DicomSequentialReader.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/TranscoderDataSource.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DicomParsing/TranscodingCallable.cpp ) endif()
--- a/OrthancFramework/Sources/Cache/SharedObjectCache.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/Cache/SharedObjectCache.cpp Tue May 26 15:52:35 2026 +0200 @@ -189,4 +189,15 @@ assert(!lru_.Contains(id)); } + + + void SharedObjectCache::GetStatistics(size_t& capacity, + size_t& currentCount, + size_t& currentSize) + { + Mutex::ScopedLock lock(mutex_); + capacity = capacity_; + currentCount = lru_.GetSize(); + currentSize = currentSize_; + } }
--- a/OrthancFramework/Sources/Cache/SharedObjectCache.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/Cache/SharedObjectCache.h Tue May 26 15:52:35 2026 +0200 @@ -69,5 +69,9 @@ size_t size); void Invalidate(const std::string& id); + + void GetStatistics(size_t& capacity, + size_t& currentCount, + size_t& currentSize); }; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/BaseDataIdentifier.cpp Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,65 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#include "../PrecompiledHeaders.h" +#include "BaseDataIdentifier.h" + +#include "../OrthancException.h" + + +namespace Orthanc +{ + void BaseDataIdentifier::SetUserData(IDynamicObject* userData) + { + std::unique_ptr<IDynamicObject> protection(userData); + + if (userData == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + else + { + userData_.reset(protection.release()); + } + } + + + bool BaseDataIdentifier::HasUserData() const + { + return userData_.get() != NULL; + } + + + const IDynamicObject& BaseDataIdentifier::GetUserData() const + { + if (userData_.get() != NULL) + { + return *userData_; + } + else + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/BaseDataIdentifier.h Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,45 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "IDataIdentifier.h" +#include "../Compatibility.h" + + +namespace Orthanc +{ + class ORTHANC_PUBLIC BaseDataIdentifier : public IDataIdentifier + { + private: + std::unique_ptr<IDynamicObject> userData_; + + public: + void SetUserData(IDynamicObject* userData); + + virtual bool HasUserData() const ORTHANC_OVERRIDE; + + virtual const IDynamicObject& GetUserData() const ORTHANC_OVERRIDE; + }; +}
--- a/OrthancFramework/Sources/DataSource/DataSourceAnswer.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceAnswer.cpp Tue May 26 15:52:35 2026 +0200 @@ -75,10 +75,17 @@ const IDataIdentifier& DataSourceAnswer::Item::GetId() const { - assert(id_.get() != NULL); - return *id_; + if (id_.get() == NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return *id_; + } } + const boost::shared_ptr<IDynamicObject>& DataSourceAnswer::Item::GetValue() const { if (error_) @@ -96,6 +103,19 @@ } + IDataIdentifier* DataSourceAnswer::Item::ReleaseIdentifier() + { + if (id_.get() == NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return id_.release(); + } + } + + void DataSourceAnswer::EnqueueInternal(Item* item) { std::unique_ptr<Item> protection(item);
--- a/OrthancFramework/Sources/DataSource/DataSourceAnswer.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceAnswer.h Tue May 26 15:52:35 2026 +0200 @@ -75,6 +75,8 @@ const IDataIdentifier& GetId() const; const boost::shared_ptr<IDynamicObject>& GetValue() const; + + IDataIdentifier* ReleaseIdentifier(); }; private:
--- a/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.cpp Tue May 26 15:52:35 2026 +0200 @@ -72,5 +72,17 @@ boost::mutex::scoped_lock lock(mutex_); return currentMemory_; } + + + void DataSourceMemoryBudget::GetStatistics(uint64_t& maximumMemory, + uint64_t& currentMemory, + unsigned int& countReservations) + + { + boost::mutex::scoped_lock lock(mutex_); + maximumMemory = maximumMemory_; + currentMemory = currentMemory_; + countReservations = reservations_; + } } }
--- a/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceMemoryBudget.h Tue May 26 15:52:35 2026 +0200 @@ -79,6 +79,10 @@ that_.Release(memory_); } }; + + void GetStatistics(uint64_t& maximumMemory, + uint64_t& currentMemory, + unsigned int& countReservations); }; } }
--- a/OrthancFramework/Sources/DataSource/DataSourceReader.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceReader.cpp Tue May 26 15:52:35 2026 +0200 @@ -46,7 +46,7 @@ boost::shared_ptr<Internals::DataSourceMemoryBudget> budget_; public: - DataSourceRunnable(boost::shared_ptr<DataSourceAnswer>& answer, + DataSourceRunnable(const boost::shared_ptr<DataSourceAnswer>& answer, IDataSource& source, IDataIdentifier* id, boost::shared_ptr<SharedObjectCache>& cache, @@ -66,7 +66,17 @@ virtual void Run() ORTHANC_OVERRIDE { - // Phase 1: Do all the work WITHOUT holding a strong reference to "DataSourceAnswer". + // Phase 1: Make sure the target answer is still alive before doing unnecessary work. + { + boost::shared_ptr<DataSourceAnswer> lock = answer_.lock(); + if (!lock) + { + // The answer was abandonned, give up the request + return; + } + } + + // Phase 2: Do all the work WITHOUT holding a strong reference to "DataSourceAnswer". boost::shared_ptr<IDynamicObject> value; std::unique_ptr<OrthancException> error; @@ -119,7 +129,7 @@ error.reset(new OrthancException(ErrorCode_InternalError)); } - // Phase 2: Acquire budget WITHOUT holding a strong reference to "DataSourceAnswer". + // Phase 3: Acquire budget WITHOUT holding a strong reference to "DataSourceAnswer". // If the "DataSourceAnswer" has been dropped, "answer_.lock()" below will return NULL // and we will immediately "Release()" to revert this. if (!error) @@ -127,7 +137,7 @@ budget_->Acquire(size); // may block; "DataSourceAnswer" CAN be destroyed here } - // Phase 3: Only now take a strong reference to "DataSourceAnswer". + // Phase 4: Only now take a strong reference to "DataSourceAnswer". { boost::shared_ptr<DataSourceAnswer> lock = answer_.lock(); @@ -156,13 +166,13 @@ }; - DataSourceReader::DataSourceReader(IExecutorService* executor /* takes ownership */, + DataSourceReader::DataSourceReader(const boost::shared_ptr<IExecutorService>& executor /* takes ownership */, IDataSource* source /* takes ownership */) : executor_(executor), source_(source), budget_(new Internals::DataSourceMemoryBudget(0)) { - if (executor == NULL || + if (executor.get() == NULL || source == NULL) { throw OrthancException(ErrorCode_NullPointer); @@ -185,7 +195,7 @@ } - void DataSourceReader::SetMaximumMemory(uint64_t maximumMemory) + void DataSourceReader::SetCapacity(uint64_t maximumMemory) { budget_ = boost::make_shared<Internals::DataSourceMemoryBudget>(maximumMemory); } @@ -207,7 +217,7 @@ } - boost::shared_ptr<IDynamicObject> DataSourceReader::ReadSingle(IDataIdentifier* id) + DataSourceAnswer::Item* DataSourceReader::ReadSingle(IDataIdentifier* id /* takes ownership */) { std::unique_ptr<IDataIdentifier> protection(id); @@ -217,8 +227,38 @@ boost::shared_ptr<DataSourceAnswer> answer(Submit(request.release())); std::unique_ptr<DataSourceAnswer::Item> item(answer->Dequeue()); + assert(item != NULL); assert(answer->Dequeue() == NULL); - return item->GetValue(); + return item.release(); + } + + + void DataSourceReader::GetStatistics(uint64_t& tasksMaximumMemory, + uint64_t& tasksCurrentMemory, + unsigned int& tasksReservations, + size_t& cacheCapacity, + size_t& cacheCurrentCount, + size_t& cacheCurrentSize) + { + { + boost::shared_ptr<Internals::DataSourceMemoryBudget> budgetCopy(budget_); + budgetCopy->GetStatistics(tasksMaximumMemory, tasksCurrentMemory, tasksReservations); + } + + { + boost::shared_ptr<SharedObjectCache> cacheCopy(cache_); + + if (cacheCopy) + { + cacheCopy->GetStatistics(cacheCapacity, cacheCurrentCount, cacheCurrentSize); + } + else + { + cacheCapacity = 0; + cacheCurrentCount = 0; + cacheCurrentSize = 0; + } + } } }
--- a/OrthancFramework/Sources/DataSource/DataSourceReader.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceReader.h Tue May 26 15:52:35 2026 +0200 @@ -46,33 +46,47 @@ private: class DataSourceRunnable; - std::unique_ptr<IExecutorService> executor_; + boost::shared_ptr<IExecutorService> executor_; std::unique_ptr<IDataSource> source_; boost::shared_ptr<SharedObjectCache> cache_; boost::shared_ptr<Internals::DataSourceMemoryBudget> budget_; public: - DataSourceReader(IExecutorService* executor /* takes ownership */, + DataSourceReader(const boost::shared_ptr<IExecutorService>& executor, IDataSource* source /* takes ownership */); ~DataSourceReader(); - IDataSource& GetSource() const - { - return *source_; - } - void CreateCache(size_t capacity); - void SetMaximumMemory(uint64_t maximumMemory); + /** + * Apply backpressure by limiting memory for pending read + * tasks. Further reads block until memory is released. + **/ + void SetCapacity(uint64_t maximumMemory); + /** + * Request the data source to load a set of items. The values will + * be read in parallel by a thread pool (cf. "executor_") and will + * be answered in no specific order through a message queue + * (cf. class "DataSourceAnswer"). The user data stored in + * "IDataIdentifier" can be used to identify items. If order is + * important, use the class "DataSourceSequentialReader" instead. + **/ boost::shared_ptr<DataSourceAnswer> Submit(DataSourceRequest* request /* takes ownership */); - boost::shared_ptr<IDynamicObject> ReadSingle(IDataIdentifier* id /* takes ownership */); + DataSourceAnswer::Item* ReadSingle(IDataIdentifier* id /* takes ownership */); void Stop() { executor_->Stop(); } + + void GetStatistics(uint64_t& tasksMaximumMemory, + uint64_t& tasksCurrentMemory, + unsigned int& tasksReservations, + size_t& cacheCapacity, + size_t& cacheCurrentCount, + size_t& cacheCurrentSize); }; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/DataSourceSequentialReader.cpp Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,291 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#include "../PrecompiledHeaders.h" +#include "DataSourceSequentialReader.h" + +#include "DataSourceReader.h" +#include "../OrthancException.h" + +namespace Orthanc +{ + DataSourceSequentialReader::Item::Item(IDataIdentifier* id, + IDynamicObject* value, + size_t estimatedSize) : + id_(id), + value_(value), + estimatedSize_(estimatedSize) + { + if (id == NULL || + value == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + + DataSourceSequentialReader::Item::Item(IDataIdentifier* id, + const OrthancException& error) : + id_(id), + error_(new OrthancException(error)), + estimatedSize_(0) + { + if (id == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + + const IDataIdentifier& DataSourceSequentialReader::Item::GetIdentifier() const + { + assert(id_.get() != NULL); + return *id_; + } + + + const IDynamicObject& DataSourceSequentialReader::Item::GetValue() const + { + if (value_.get() != NULL) + { + assert(error_.get() == NULL); + return *value_; + } + else if (error_.get() != NULL) + { + throw *error_; + } + else + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + } + + + IDynamicObject* DataSourceSequentialReader::Item::ReleaseValue() + { + if (value_.get() != NULL) + { + assert(error_.get() == NULL); + return value_.release(); + } + else if (error_.get() != NULL) + { + throw *error_; + } + else + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + } + + + class DataSourceSequentialReader::Callable : public ICallable + { + private: + boost::shared_ptr<DataSourceReader> reader_; + boost::shared_ptr<IValueDisconnector> disconnector_; + std::unique_ptr<IDataIdentifier> request_; + size_t estimatedSize_; + + public: + Callable(const boost::shared_ptr<DataSourceReader>& reader, + const boost::shared_ptr<IValueDisconnector>& disconnector, + IDataIdentifier* request, + size_t estimatedSize) : + reader_(reader), + disconnector_(disconnector), + request_(request), + estimatedSize_(estimatedSize) + { + if (reader.get() == NULL || + disconnector.get() == NULL || + request_ == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + virtual IDynamicObject* Call() ORTHANC_OVERRIDE + { + std::unique_ptr<DataSourceAnswer::Item> answer(reader_->ReadSingle(request_.release())); + + try + { + return new Item(answer->ReleaseIdentifier(), disconnector_->Apply(answer.release()), estimatedSize_); + } + catch (OrthancException& e) + { + return new Item(answer->ReleaseIdentifier(), e); + } + catch (...) + { + return new Item(answer->ReleaseIdentifier(), OrthancException(ErrorCode_InternalError)); + } + } + }; + + + void DataSourceSequentialReader::FillWindow() + { + while (!pendingRequests_.empty() && + runningRequests_.size() < windowSize_) + { + size_t estimatedSize; + if (pendingRequests_.front()->EstimateValueSize(estimatedSize)) + { + // Ensure that at least 1 callable is running even if too large for capacity + if (!runningRequests_.empty() && + windowCapacity_ != 0 && + runningSize_ + estimatedSize > windowCapacity_) + { + return; + } + } + else + { + estimatedSize = 0; + } + + std::unique_ptr<IDataIdentifier> front(pendingRequests_.front()); + pendingRequests_.pop_front(); + + runningSize_ += estimatedSize; + + std::unique_ptr<ICallable> callable(new Callable(reader_, disconnector_, front.release(), estimatedSize)); + runningRequests_.push_back(executor_->Submit(callable.release())); + } + } + + + DataSourceSequentialReader::DataSourceSequentialReader(const boost::shared_ptr<IExecutorService>& executor, + const boost::shared_ptr<DataSourceReader>& reader, + IValueDisconnector* disconnector /* takes ownership */, + unsigned int windowSize, + uint64_t windowCapacity) : + executor_(executor), + reader_(reader), + disconnector_(disconnector), + windowSize_(windowSize), + windowCapacity_(windowCapacity), + started_(false), + runningSize_(0) + { + if (executor.get() == NULL || + reader.get() == NULL || + disconnector == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + + if (windowSize == 0) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + } + + + DataSourceSequentialReader::~DataSourceSequentialReader() + { + for (PendingRequests::iterator it = pendingRequests_.begin(); it != pendingRequests_.end(); ++it) + { + assert(*it != NULL); + delete *it; + } + + for (RunningRequests::iterator it = runningRequests_.begin(); it != runningRequests_.end(); ++it) + { + assert(*it != NULL); + delete *it; + } + } + + + void DataSourceSequentialReader::Submit(IDataIdentifier* request /* takes ownership */) + { + std::unique_ptr<IDataIdentifier> protection(request); + + if (request == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + else if (started_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + pendingRequests_.push_back(protection.release()); + } + } + + + void DataSourceSequentialReader::Start() + { + if (started_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + started_ = true; + FillWindow(); + } + } + + + bool DataSourceSequentialReader::HasNext() const + { + if (!started_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return !runningRequests_.empty(); + } + } + + + DataSourceSequentialReader::Item* DataSourceSequentialReader::Next() + { + if (!started_ || + !HasNext()) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + std::unique_ptr<Future> future(runningRequests_.front()); + runningRequests_.pop_front(); + + std::unique_ptr<Item> item(dynamic_cast<Item*>(future->ReleaseResult())); + runningSize_ -= item->GetEstimatedSize(); + + FillWindow(); + + return item.release(); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/DataSourceSequentialReader.h Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,135 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "../MultiThreading/IExecutorService.h" +#include "DataSourceAnswer.h" + + +namespace Orthanc +{ + class DataSourceReader; + class OrthancException; + + /** + * This class retrieves items from a data source in a deterministic + * order while still using parallel execution through a thread pool. + * + * Internally, it uses a sliding-window approach. Since items may + * complete out of order, the class buffers them until earlier items + * become available. This additional buffering is necessary because + * applying backpressure (as done in "DataSourceReader::Submit()") + * could otherwise lead to deadlock. If item ordering is not + * required, prefer "DataSourceReader::Submit()", which supports + * backpressure and avoids the extra memory overhead. + * + * Note that this class is not thread safe, it should be invoked + * from a single thread. + **/ + class DataSourceSequentialReader : public boost::noncopyable + { + public: + class Item : public IDynamicObject + { + private: + std::unique_ptr<IDataIdentifier> id_; + std::unique_ptr<IDynamicObject> value_; + std::unique_ptr<OrthancException> error_; + size_t estimatedSize_; + + public: + Item(IDataIdentifier* id, + IDynamicObject* value, + size_t estimatedSize); + + Item(IDataIdentifier* id, + const OrthancException& error); + + const IDataIdentifier& GetIdentifier() const; + + const IDynamicObject& GetValue() const; + + size_t GetEstimatedSize() const + { + return estimatedSize_; + } + + IDynamicObject* ReleaseValue(); + }; + + + /** + * Extracts the value from an answer item so that it can be + * buffered independently and is no longer subject to + * backpressure. + **/ + class IValueDisconnector : public boost::noncopyable + { + public: + virtual ~IValueDisconnector() + { + } + + virtual IDynamicObject* Apply(DataSourceAnswer::Item* source) = 0; + }; + + + private: + class Callable; + + typedef std::list<IDataIdentifier*> PendingRequests; + typedef std::list<Future*> RunningRequests; + + boost::shared_ptr<IExecutorService> executor_; + boost::shared_ptr<DataSourceReader> reader_; + boost::shared_ptr<IValueDisconnector> disconnector_; + unsigned int windowSize_; + uint64_t windowCapacity_; + + bool started_; + PendingRequests pendingRequests_; + RunningRequests runningRequests_; + uint64_t runningSize_; + + void FillWindow(); + + public: + DataSourceSequentialReader(const boost::shared_ptr<IExecutorService>& executor, + const boost::shared_ptr<DataSourceReader>& reader, + IValueDisconnector* disconnector /* takes ownership */, + unsigned int windowSize /* number of elements in the sliding window */, + uint64_t windowCapacity /* if 0, memory usage is only controlled by the size of the sliding window */); + + ~DataSourceSequentialReader(); + + void Submit(IDataIdentifier* request /* takes ownership */); + + void Start(); + + bool HasNext() const; + + Item* Next(); + }; +}
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DicomDataSource.cpp Tue May 26 15:52:35 2026 +0200 @@ -27,6 +27,7 @@ #include "../DicomParsing/ParsedDicomFile.h" #include "../OrthancException.h" +#include "BaseDataIdentifier.h" #include "DataSourceReader.h" #include "StorageAreaDataSource.h" @@ -35,81 +36,46 @@ 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 + class DicomDataSource::Identifier : public BaseDataIdentifier { public: virtual StorageAreaDataSource::Range* ReadRange(DataSourceReader& reader) const = 0; - - virtual bool IsKeepRawBuffer() const = 0; }; - class DicomDataSource::WholeIdentifier : public DicomDataSource::BaseIdentifier + class DicomDataSource::WholeIdentifier : public DicomDataSource::Identifier { private: FileInfo attachment_; - bool keepRawBuffer_; - bool checkMD5_; public: - explicit WholeIdentifier(const FileInfo& attachment, - bool keepRawBuffer, - bool checkMD5) : - attachment_(attachment), - keepRawBuffer_(keepRawBuffer), - checkMD5_(checkMD5) + explicit WholeIdentifier(const FileInfo& attachment) : + attachment_(attachment) { } 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; } virtual StorageAreaDataSource::Range* ReadRange(DataSourceReader& reader) const ORTHANC_OVERRIDE { - return StorageAreaDataSource::ReadAttachment(reader, attachment_, true /* uncompress */, checkMD5_); - } - - virtual bool IsKeepRawBuffer() const ORTHANC_OVERRIDE - { - return keepRawBuffer_; + return StorageAreaDataSource::Execute( + reader, StorageAreaDataSource::CreateAttachmentRequest(attachment_, true /* uncompress */)); } }; - class DicomDataSource::BeginningIdentifier : public DicomDataSource::BaseIdentifier + class DicomDataSource::BeginningIdentifier : public DicomDataSource::Identifier { private: FileInfo attachment_; @@ -141,12 +107,7 @@ virtual StorageAreaDataSource::Range* ReadRange(DataSourceReader& reader) const ORTHANC_OVERRIDE { - return StorageAreaDataSource::ReadBeginning(reader, attachment_, pixelDataOffset_); - } - - virtual bool IsKeepRawBuffer() const ORTHANC_OVERRIDE - { - return false; + return StorageAreaDataSource::Execute(reader, StorageAreaDataSource::CreateBeginningRequest(attachment_, pixelDataOffset_)); } }; @@ -154,17 +115,15 @@ class DicomDataSource::Value : public IDynamicObject { private: - std::unique_ptr<ParsedDicomFile> dicom_; - size_t size_; - bool hasRawBuffer_; - std::string rawBuffer_; + Mutex mutex_; + std::unique_ptr<ParsedDicomFile> dicom_; + size_t size_; public: Value(ParsedDicomFile* dicom, size_t size) : dicom_(dicom), - size_(size), - hasRawBuffer_(false) + size_(size) { if (dicom == NULL) { @@ -172,50 +131,19 @@ } } - ParsedDicomFile& GetContent() const - { - return *dicom_; - } - - size_t GetRawBufferSize() const + size_t GetSize() const { return size_; } - void SetRawBuffer(const void* data, - size_t size) + Mutex::ScopedLock* Lock() { - 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); - } + return new Mutex::ScopedLock(mutex_); } - bool HasRawBuffer() const - { - return hasRawBuffer_; - } - - const void* GetRawBufferData() const + ParsedDicomFile& GetContent() const { - if (hasRawBuffer_) - { - assert(rawBuffer_.size() == size_); - return rawBuffer_.empty() ? NULL : rawBuffer_.c_str(); - } - else - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } + return *dicom_; } }; @@ -232,7 +160,7 @@ IDynamicObject* DicomDataSource::Load(const IDataIdentifier& obj) { - const BaseIdentifier& id = dynamic_cast<const BaseIdentifier&>(obj); + const Identifier& id = dynamic_cast<const Identifier&>(obj); std::unique_ptr<StorageAreaDataSource::Range> range(id.ReadRange(*storageAreaReader_)); @@ -240,30 +168,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,66 +186,77 @@ 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(); } - DicomDataSource::Dicom::Dicom(const boost::shared_ptr<IDynamicObject>& value) : - value_(value) + DicomDataSource::Dicom::Dicom(DataSourceAnswer::Item* item) : + item_(item) { - if (value.get() == NULL) + if (item == NULL) { throw OrthancException(ErrorCode_NullPointer); } } - ParsedDicomFile& DicomDataSource::Dicom::Lock::GetContent() const + ParsedDicomFile* DicomDataSource::Dicom::Clone() { - return dynamic_cast<const Value&>(*that_.value_).GetContent(); + Lock lock(*this); + return lock.GetContent().Clone(true); } - bool DicomDataSource::Dicom::Lock::HasRawBuffer() const + DicomDataSource::Dicom::Lock::Lock(Dicom& that): + that_(that) { - return dynamic_cast<const Value&>(*that_.value_).HasRawBuffer(); - } - - - const void* DicomDataSource::Dicom::Lock::GetRawBufferData() const - { - return dynamic_cast<const Value&>(*that_.value_).GetRawBufferData(); + Value& value = dynamic_cast<Value&>(*that_.item_->GetValue()); + lock_.reset(value.Lock()); } - size_t DicomDataSource::Dicom::Lock::GetRawBufferSize() const + ParsedDicomFile& DicomDataSource::Dicom::Lock::GetContent() const { - return dynamic_cast<const Value&>(*that_.value_).GetRawBufferSize(); + const Value& value = dynamic_cast<const Value&>(*that_.item_->GetValue()); + return value.GetContent(); } - DicomDataSource::Dicom* DicomDataSource::ReadWhole(DataSourceReader& reader, - const FileInfo& attachment, - bool keepRawBuffer, - bool checkMD5) + IDataIdentifier* DicomDataSource::CreateWholeRequest(const FileInfo& attachment) { - std::unique_ptr<IDataIdentifier> id(new WholeIdentifier(attachment, keepRawBuffer, checkMD5)); - boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release()); - return new Dicom(value); + return new WholeIdentifier(attachment); } - DicomDataSource::Dicom* DicomDataSource::ReadUntilPixelData(DataSourceReader& reader, - const FileInfo& attachment, + IDataIdentifier* DicomDataSource::CreateUntilPixelDataRequest(const FileInfo& attachment, uint64_t pixelDataOffset) { if (static_cast<uint64_t>(static_cast<size_t>(pixelDataOffset)) != pixelDataOffset) { throw OrthancException(ErrorCode_NotEnoughMemory); } + else if (attachment.GetCompressionType() != CompressionType_None) + { + throw OrthancException(ErrorCode_BadParameterType); + } + else + { + return new BeginningIdentifier(attachment, static_cast<size_t>(pixelDataOffset)); + } + } - std::unique_ptr<IDataIdentifier> id(new BeginningIdentifier(attachment, static_cast<size_t>(pixelDataOffset))); - boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release()); - return new Dicom(value); + + DicomDataSource::Dicom* DicomDataSource::Execute(DataSourceReader& reader, + IDataIdentifier* request) + { + if (request == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + else + { + std::unique_ptr<DataSourceAnswer::Item> item(reader.ReadSingle(request)); + return new Dicom(item.release()); + } } }
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DicomDataSource.h Tue May 26 15:52:35 2026 +0200 @@ -35,6 +35,7 @@ #include "../Compatibility.h" #include "../FileStorage/FileInfo.h" #include "../MultiThreading/Mutex.h" +#include "DataSourceAnswer.h" #include "IDataIdentifier.h" #include "IDataSource.h" @@ -49,7 +50,7 @@ class DicomDataSource : public IDataSource { private: - class BaseIdentifier; + class Identifier; class WholeIdentifier; class BeginningIdentifier; class Value; @@ -66,11 +67,12 @@ class Dicom : public boost::noncopyable { private: - Mutex mutex_; - boost::shared_ptr<IDynamicObject> value_; + std::unique_ptr<DataSourceAnswer::Item> item_; // Holding item puts backpressure on the data source public: - explicit Dicom(const boost::shared_ptr<IDynamicObject>& value); + Dicom(DataSourceAnswer::Item* item /* takes ownership */); + + ParsedDicomFile* Clone(); /** * Access to the DICOM value must be protected by a mutex, as it @@ -80,33 +82,22 @@ class Lock : public boost::noncopyable { private: - Dicom& that_; - Mutex::ScopedLock lock_; + Dicom& that_; + std::unique_ptr<Mutex::ScopedLock> lock_; public: - explicit Lock(Dicom& that) : - that_(that), - lock_(that.mutex_) - { - } + explicit Lock(Dicom& that); 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 IDataIdentifier* CreateWholeRequest(const FileInfo& attachment); - static Dicom* ReadUntilPixelData(DataSourceReader& reader, - const FileInfo& attachment, - uint64_t pixelDataOffset); + static IDataIdentifier* CreateUntilPixelDataRequest(const FileInfo& attachment, + uint64_t pixelDataOffset); + + static Dicom* Execute(DataSourceReader& reader, + IDataIdentifier* request /* takes ownership */); }; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.cpp Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,185 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#include "../PrecompiledHeaders.h" +#include "DicomSequentialReader.h" + +#include "../DicomParsing/ParsedDicomFile.h" +#include "../OrthancException.h" +#include "BaseDataIdentifier.h" +#include "DicomDataSource.h" +#include "TranscoderDataSource.h" + + +namespace Orthanc +{ + class DicomSequentialReader::Disconnector : public DataSourceSequentialReader::IValueDisconnector + { + private: + Type type_; + + public: + Disconnector(Type type) : + type_(type) + { + } + + virtual IDynamicObject* Apply(DataSourceAnswer::Item* source) + { + switch (type_) + { + case Type_Original: + { + std::unique_ptr<DicomDataSource::Dicom> dicom(new DicomDataSource::Dicom(source)); + return dicom->Clone(); + } + + case Type_Transcoded: + { + std::unique_ptr<TranscoderDataSource::Transcoded> dicom(new TranscoderDataSource::Transcoded(source)); + TranscoderDataSource::Transcoded::LockAsParsed lock(*dicom); + return lock.GetContent().Clone(true); + } + + default: + throw OrthancException(ErrorCode_InternalError); + } + } + }; + + + DicomSequentialReader::DicomSequentialReader(Type type, + const boost::shared_ptr<IExecutorService>& executor, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + bool hasLossyQuality, + unsigned int lossyQuality, + const boost::shared_ptr<DataSourceReader>& reader, + unsigned int windowSize, + uint64_t windowCapacity) : + type_(type), + targetSyntax_(targetSyntax), + mode_(mode), + hasLossyQuality_(hasLossyQuality), + lossyQuality_(lossyQuality), + reader_(executor, reader, new Disconnector(type), windowSize, windowCapacity) + { + } + + + void DicomSequentialReader::SubmitInternal(const FileInfo& attachment, + IDynamicObject* userData) + { + std::unique_ptr<IDynamicObject> protection(userData); + + std::unique_ptr<IDataIdentifier> id; + + switch (type_) + { + case Type_Original: + id.reset(DicomDataSource::CreateWholeRequest(attachment)); + break; + + case Type_Transcoded: + id.reset(TranscoderDataSource::CreateRequest(attachment, targetSyntax_, mode_, hasLossyQuality_, lossyQuality_)); + break; + + default: + throw OrthancException(ErrorCode_InternalError); + } + + if (protection.get() != NULL) + { + dynamic_cast<BaseDataIdentifier&>(*id).SetUserData(protection.release()); + } + + reader_.Submit(id.release()); + } + + + DicomSequentialReader* DicomSequentialReader::CreateForOriginal(const boost::shared_ptr<IExecutorService>& executor, + const boost::shared_ptr<DataSourceReader>& dicomReader, + unsigned int windowSize, + uint64_t windowCapacity) + { + return new DicomSequentialReader( + Type_Original, executor, + DicomTransferSyntax_LittleEndianImplicit, TranscodingSopInstanceUidMode_AllowNew, + false /* no lossy */, 0 /* unused lossy quality */, + dicomReader, windowSize, windowCapacity); + } + + + DicomSequentialReader* DicomSequentialReader::CreateForTranscoded(const boost::shared_ptr<IExecutorService>& executor, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + bool hasLossyQuality, + unsigned int lossyQuality, + const boost::shared_ptr<DataSourceReader>& transcoderReader, + unsigned int windowSize, + uint64_t windowCapacity) + { + return new DicomSequentialReader( + Type_Transcoded, executor, + targetSyntax, mode, hasLossyQuality, lossyQuality, + transcoderReader, windowSize, windowCapacity); + } + + + void DicomSequentialReader::Submit(const FileInfo& attachment) + { + SubmitInternal(attachment, NULL); + } + + + void DicomSequentialReader::Submit(const FileInfo& attachment, + IDynamicObject* userData) + { + if (userData == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + + SubmitInternal(attachment, userData); + } + + + void DicomSequentialReader::Start() + { + reader_.Start(); + } + + + bool DicomSequentialReader::HasNext() + { + return reader_.HasNext(); + } + + + ParsedDicomFile* DicomSequentialReader::Next() + { + std::unique_ptr<DataSourceSequentialReader::Item> item(reader_.Next()); + return dynamic_cast<ParsedDicomFile*>(item->ReleaseValue()); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/DicomSequentialReader.h Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,94 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "../Enumerations.h" +#include "../FileStorage/FileInfo.h" +#include "../MultiThreading/IExecutorService.h" +#include "DataSourceSequentialReader.h" + + +namespace Orthanc +{ + class ParsedDicomFile; + + class ORTHANC_PUBLIC DicomSequentialReader : public boost::noncopyable + { + private: + class Disconnector; + + enum Type + { + Type_Original, + Type_Transcoded + }; + + Type type_; + DicomTransferSyntax targetSyntax_; + TranscodingSopInstanceUidMode mode_; + bool hasLossyQuality_; + unsigned int lossyQuality_; + DataSourceSequentialReader reader_; + + DicomSequentialReader(Type type, + const boost::shared_ptr<IExecutorService>& executor, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + bool hasLossyQuality, + unsigned int lossyQuality, + const boost::shared_ptr<DataSourceReader>& reader, + unsigned int windowSize, + uint64_t windowCapacity); + + void SubmitInternal(const FileInfo& attachment, + IDynamicObject* userData); + + public: + static DicomSequentialReader* CreateForOriginal(const boost::shared_ptr<IExecutorService>& executor, + const boost::shared_ptr<DataSourceReader>& dicomReader, + unsigned int windowSize, + uint64_t windowCapacity); + + static DicomSequentialReader* CreateForTranscoded(const boost::shared_ptr<IExecutorService>& executor, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + bool hasLossyQuality, + unsigned int lossyQuality, + const boost::shared_ptr<DataSourceReader>& transcoderReader, + unsigned int windowSize, + uint64_t windowCapacity); + + void Submit(const FileInfo& attachment); + + void Submit(const FileInfo& attachment, + IDynamicObject* userData /* takes ownership */); + + void Start(); + + bool HasNext(); + + ParsedDicomFile* Next(); + }; +}
--- a/OrthancFramework/Sources/DataSource/IDataIdentifier.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/IDataIdentifier.h Tue May 26 15:52:35 2026 +0200 @@ -24,7 +24,7 @@ #pragma once -#include "../OrthancFramework.h" +#include "../IDynamicObject.h" #include <boost/noncopyable.hpp> @@ -42,5 +42,9 @@ virtual bool GetCacheKey(std::string& key) const = 0; virtual bool EstimateValueSize(size_t& target) const = 0; + + virtual bool HasUserData() const = 0; + + virtual const IDynamicObject& GetUserData() const = 0; }; }
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp Tue May 26 15:52:35 2026 +0200 @@ -28,6 +28,7 @@ #include "../OrthancException.h" #include "../StringMemoryBuffer.h" #include "../Toolbox.h" +#include "BaseDataIdentifier.h" #include "DataSourceReader.h" #if ORTHANC_ENABLE_ZLIB == 1 @@ -43,10 +44,13 @@ { private: std::unique_ptr<IMemoryBuffer> buffer_; + bool checkMD5_; public: - explicit Value(IMemoryBuffer* buffer) : - buffer_(buffer) + explicit Value(IMemoryBuffer* buffer, + bool checkMD5) : + buffer_(buffer), + checkMD5_(checkMD5) { if (buffer == NULL) { @@ -59,17 +63,35 @@ assert(buffer_.get() != NULL); return *buffer_; } + + bool IsCheckMD5() const + { + return checkMD5_; + } }; - class StorageAreaDataSource::Identifier : public IDataIdentifier + class StorageAreaDataSource::IPostProcessing : public boost::noncopyable + { + public: + virtual ~IPostProcessing() + { + } + + // This method can return NULL if no post-processing is required + virtual IMemoryBuffer* Apply(const Value& value) const = 0; + }; + + + class StorageAreaDataSource::Identifier : public BaseDataIdentifier { private: - std::string uuid_; - FileContentType type_; - uint64_t start_; - uint64_t end_; - std::string customData_; + std::string uuid_; + FileContentType type_; + uint64_t start_; + uint64_t end_; + std::string customData_; + std::unique_ptr<IPostProcessing> postProcessing_; public: Identifier(const std::string& uuid, @@ -95,9 +117,9 @@ } } - Value* Read(IPluginStorageArea& area) const + IMemoryBuffer* Read(IPluginStorageArea& area) const { - return new Value(area.ReadRange(uuid_, type_, start_, end_, customData_)); + return area.ReadRange(uuid_, type_, start_, end_, customData_); } virtual bool GetCacheKey(std::string& key) const ORTHANC_OVERRIDE @@ -116,6 +138,133 @@ assert(start_ <= end_); return end_ - start_; } + + void SetPostProcessing(IPostProcessing* postProcessing) + { + std::unique_ptr<IPostProcessing> protection(postProcessing); + + if (postProcessing == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + else if (postProcessing_.get() != NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + postProcessing_.reset(protection.release()); + } + } + + bool HasPostProcessing() const + { + return postProcessing_.get() != NULL; + } + + const IPostProcessing& GetPostProcessing() const + { + if (postProcessing_.get() == NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return *postProcessing_; + } + } + }; + + + class StorageAreaDataSource::AttachmentPostProcessing : public StorageAreaDataSource::IPostProcessing + { + private: + FileInfo attachment_; + bool uncompress_; + + public: + AttachmentPostProcessing(const FileInfo& attachment, + bool uncompress) : + attachment_(attachment), + uncompress_(uncompress) + { + if (attachment.GetCompressionType() == CompressionType_None) + { + if (attachment.GetCompressedMD5() != attachment.GetUncompressedMD5() || + attachment.GetCompressedSize() != attachment.GetUncompressedSize()) + { + throw OrthancException(ErrorCode_CorruptedFile); + } + } + } + + const FileInfo& GetAttachment() const + { + return attachment_; + } + + virtual IMemoryBuffer* Apply(const Value& value) const ORTHANC_OVERRIDE + { + if (value.IsCheckMD5()) + { + std::string md5; + Toolbox::ComputeMD5(md5, value.GetBuffer().GetData(), value.GetBuffer().GetSize()); + + if (md5 != attachment_.GetCompressedMD5()) + { + throw OrthancException(ErrorCode_CorruptedFile); + } + } + + if (uncompress_) + { + switch (attachment_.GetCompressionType()) + { + case CompressionType_None: + if (value.IsCheckMD5() && + attachment_.GetCompressedMD5() != attachment_.GetUncompressedMD5()) + { + throw OrthancException(ErrorCode_CorruptedFile); + } + else + { + return NULL; + } + + case CompressionType_ZlibWithSize: + { +#if ORTHANC_ENABLE_ZLIB == 1 + ZlibCompressor zlib; + + std::string content; + zlib.Uncompress(content, value.GetBuffer().GetData(), value.GetBuffer().GetSize()); + + if (value.IsCheckMD5()) + { + std::string md5; + Toolbox::ComputeMD5(md5, content); + + if (md5 != attachment_.GetUncompressedMD5()) + { + throw OrthancException(ErrorCode_CorruptedFile); + } + } + + return StringMemoryBuffer::CreateFromSwap(content); +#else + throw OrthancException(ErrorCode_InternalError, "Support for zlib is disabled, cannot uncompress attachment"); +#endif + } + + default: + throw OrthancException(ErrorCode_NotImplemented); + } + } + else + { + return NULL; + } + } }; @@ -125,146 +274,78 @@ return tmp.GetBuffer().GetSize(); } + IDynamicObject* StorageAreaDataSource::Load(const IDataIdentifier& identifier) { const Identifier& id = dynamic_cast<const Identifier&>(identifier); - return id.Read(area_); + return new Value(id.Read(area_), checkMD5_); } - const StorageAreaDataSource::Value& StorageAreaDataSource::Range::GetValue() const + const IMemoryBuffer& StorageAreaDataSource::Range::GetBuffer() const { - assert(value_.get() != NULL); - return dynamic_cast<const StorageAreaDataSource::Value&>(*value_); + if (item_.get() != NULL) + { + assert(buffer_.get() == NULL); + return dynamic_cast<const Value&>(*item_->GetValue()).GetBuffer(); + } + else if (buffer_.get() != NULL) + { + assert(item_.get() == NULL); + return *buffer_; + } + else + { + throw OrthancException(ErrorCode_InternalError); + } } - StorageAreaDataSource::Range::Range(const boost::shared_ptr<IDynamicObject>& value) : - value_(value) + StorageAreaDataSource::Range::Range(IMemoryBuffer* buffer /* takes ownership */) : + buffer_(buffer) { - if (value_.get() == NULL) + if (buffer == NULL) { throw OrthancException(ErrorCode_NullPointer); } } - const void* StorageAreaDataSource::Range::GetData() const - { - return GetValue().GetBuffer().GetData(); - } - - - size_t StorageAreaDataSource::Range::GetSize() const - { - return GetValue().GetBuffer().GetSize(); - } - - - void StorageAreaDataSource::Range::Copy(std::string& to) const - { - const IMemoryBuffer& buffer = GetValue().GetBuffer(); - to.assign(reinterpret_cast<const char*>(buffer.GetData()), buffer.GetSize()); - } - - - StorageAreaDataSource::Range* StorageAreaDataSource::Range::CreateFromSwap(std::string& content) + StorageAreaDataSource::Range::Range(DataSourceAnswer::Item* item) : + item_(item) { - boost::shared_ptr<Value> value(new Value(StringMemoryBuffer::CreateFromSwap(content))); - return new Range(value); - } - - - StorageAreaDataSource::Range* StorageAreaDataSource::ReadRange(DataSourceReader& reader, - const std::string& uuid, - FileContentType type, - uint64_t start, - uint64_t end, - const std::string& 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) - { - if (attachment.GetCompressionType() == CompressionType_None) + if (item == NULL) { - if (attachment.GetCompressedMD5() != attachment.GetUncompressedMD5() || - attachment.GetCompressedSize() != attachment.GetUncompressedSize()) - { - throw OrthancException(ErrorCode_CorruptedFile); - } - } - - 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: - { -#if ORTHANC_ENABLE_ZLIB == 1 - 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); - } - } - - return Range::CreateFromSwap(content); -#else - throw OrthancException(ErrorCode_InternalError, "Support for zlib is disabled, cannot uncompress attachment"); -#endif - } - - default: - throw OrthancException(ErrorCode_NotImplemented); - } - } - else - { - return range.release(); + throw OrthancException(ErrorCode_NullPointer); } } - StorageAreaDataSource::Range* StorageAreaDataSource::ReadBeginning(DataSourceReader& reader, - const FileInfo& attachment, - uint64_t untilPosition) + IDataIdentifier* StorageAreaDataSource::CreateRangeRequest(const std::string& uuid, + FileContentType type, + uint64_t start, + uint64_t end, + const std::string& pluginCustomData) + { + return new Identifier(uuid, type, start, end, pluginCustomData); + } + + + IDataIdentifier* StorageAreaDataSource::CreateAttachmentRequest(const FileInfo& attachment, + bool uncompress) + { + std::unique_ptr<Identifier> id(new Identifier(attachment.GetUuid(), + attachment.GetContentType(), + 0, attachment.GetCompressedSize(), + attachment.GetCustomData())); + id->SetPostProcessing(new AttachmentPostProcessing(attachment, uncompress)); + + return id.release(); + } + + + IDataIdentifier* StorageAreaDataSource::CreateBeginningRequest(const FileInfo& attachment, + uint64_t untilPosition) { if (attachment.GetCompressionType() != CompressionType_None) { @@ -282,16 +363,15 @@ throw OrthancException(ErrorCode_ParameterOutOfRange); } - return ReadRange(reader, attachment.GetUuid(), attachment.GetContentType(), - 0, untilPosition, attachment.GetCustomData()); + return CreateRangeRequest(attachment.GetUuid(), attachment.GetContentType(), + 0, untilPosition, attachment.GetCustomData()); } StorageAreaDataSource::Range* StorageAreaDataSource::ReadRange(DataSourceReader& reader, const FileInfo& attachment, const StorageRange& range, - bool uncompress, - bool checkMD5) + bool uncompress) { // This mimics "StorageAccessor::ReadRange()" @@ -299,13 +379,13 @@ attachment.GetCompressionType() != CompressionType_None) { // An uncompression is needed in this case - std::unique_ptr<StorageAreaDataSource::Range> uncompressed( - ReadAttachment(reader, attachment, true /* uncompress */, checkMD5)); + std::unique_ptr<Range> uncompressed( + Execute(reader, CreateAttachmentRequest(attachment, true /* uncompress */))); std::string content; range.Extract(content, uncompressed->GetData(), uncompressed->GetSize()); - return Range::CreateFromSwap(content); + return new Range(StringMemoryBuffer::CreateFromSwap(content)); } else { @@ -321,8 +401,47 @@ } else { - return ReadRange(reader, attachment.GetUuid(), attachment.GetContentType(), - start, endExclusive, attachment.GetCustomData()); + return Execute(reader, CreateRangeRequest(attachment.GetUuid(), attachment.GetContentType(), + start, endExclusive, attachment.GetCustomData())); + } + } + } + + + StorageAreaDataSource::Range* StorageAreaDataSource::Execute(DataSourceReader& reader, + IDataIdentifier* request /* takes ownership */) + { + if (request == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + else + { + std::unique_ptr<DataSourceAnswer::Item> item(reader.ReadSingle(request)); + + const Identifier& id = dynamic_cast<const Identifier&>(item->GetId()); + + std::unique_ptr<Range> range; + + if (id.HasPostProcessing()) + { + const Value& value = dynamic_cast<const Value&>(*item->GetValue()); + std::unique_ptr<IMemoryBuffer> postProcessed(id.GetPostProcessing().Apply(value)); + + if (postProcessed.get() != NULL) + { + range.reset(new Range(postProcessed.release())); + } + } + + if (range.get() == NULL) // No postprocessing was applied + { + return new Range(item.release()); // Keep backpressure on "item" + } + else + { + // In this case, the backporessure on the source "item" is released + return range.release(); } } }
--- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h Tue May 26 15:52:35 2026 +0200 @@ -32,6 +32,8 @@ #include "../FileStorage/FileInfo.h" #include "../FileStorage/IStorageArea.h" #include "../FileStorage/StorageRange.h" +#include "../IMemoryBuffer.h" +#include "DataSourceAnswer.h" #include "IDataSource.h" #include <boost/shared_ptr.hpp> @@ -46,13 +48,18 @@ private: class Value; class Identifier; + class IPostProcessing; + class AttachmentPostProcessing; private: IPluginStorageArea& area_; + bool checkMD5_; public: - explicit StorageAreaDataSource(IPluginStorageArea& area) : - area_(area) + explicit StorageAreaDataSource(IPluginStorageArea& area, + bool checkMD5) : + area_(area), + checkMD5_(checkMD5) { } @@ -63,42 +70,50 @@ class Range : public boost::noncopyable { private: - boost::shared_ptr<IDynamicObject> value_; + std::unique_ptr<DataSourceAnswer::Item> item_; // Holding item puts backpressure on the data source (can be NULL) + std::unique_ptr<IMemoryBuffer> buffer_; // To be used if "item_ == NULL" - const Value& GetValue() const; + const IMemoryBuffer& GetBuffer() const; public: - explicit Range(const boost::shared_ptr<IDynamicObject>& value); + explicit Range(DataSourceAnswer::Item* item /* takes ownership */); + + explicit Range(IMemoryBuffer* buffer /* takes ownership */); - const void* GetData() const; + const void* GetData() const + { + return GetBuffer().GetData(); + } - size_t GetSize() const; + const size_t GetSize() const + { + return GetBuffer().GetSize(); + } - void Copy(std::string& to) const; - - static Range* CreateFromSwap(std::string& content); + void Copy(std::string& target) const + { + GetBuffer().CopyToString(target); + } }; - static Range* ReadRange(DataSourceReader& reader, - const std::string& uuid, - FileContentType type, - uint64_t start /* inclusive */, - uint64_t end /* exclusive */, - const std::string& customData); + static IDataIdentifier* CreateRangeRequest(const std::string& uuid, + FileContentType type, + uint64_t start /* inclusive */, + uint64_t end /* exclusive */, + const std::string& pluginCustomData); - static Range* ReadAttachment(DataSourceReader& reader, - const FileInfo& attachment, - bool uncompress, - bool checkMD5); + static IDataIdentifier* CreateAttachmentRequest(const FileInfo& attachment, + bool uncompress); - static Range* ReadBeginning(DataSourceReader& reader, - const FileInfo& attachment, - uint64_t untilPosition /* exclusive */); + static IDataIdentifier* CreateBeginningRequest(const FileInfo& attachment, + uint64_t untilPosition /* exclusive */); + + static Range* Execute(DataSourceReader& reader, + IDataIdentifier* request /* takes ownership */); static Range* ReadRange(DataSourceReader& reader, const FileInfo& attachment, const StorageRange& range, - bool uncompress, - bool checkMD5); + bool uncompress); }; }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,330 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + **/ + + +#include "../PrecompiledHeaders.h" +#include "TranscoderDataSource.h" + +#include "../DicomParsing/ParsedDicomFile.h" +#include "../OrthancException.h" +#include "BaseDataIdentifier.h" +#include "DataSourceReader.h" +#include "StorageAreaDataSource.h" + +#include <boost/lexical_cast.hpp> +#include <dcmtk/dcmdata/dcfilefo.h> + + +namespace Orthanc +{ + static bool IsLosslessTransferSyntax(DicomTransferSyntax syntax) + { + return (syntax == DicomTransferSyntax_LittleEndianImplicit || + syntax == DicomTransferSyntax_LittleEndianExplicit || + syntax == DicomTransferSyntax_DeflatedLittleEndianExplicit || + syntax == DicomTransferSyntax_BigEndianExplicit || + syntax == DicomTransferSyntax_JPEGLSLossless || + syntax == DicomTransferSyntax_JPEG2000LosslessOnly || + syntax == DicomTransferSyntax_JPEG2000MulticomponentLosslessOnly || + syntax == DicomTransferSyntax_RLELossless || + syntax == DicomTransferSyntax_JPEGXLLossless || + syntax == DicomTransferSyntax_HighThroughputJPEG2000LosslessOnly); + } + + + class TranscoderDataSource::Value : public IDynamicObject + { + private: + Mutex parsedMutex_; + std::unique_ptr<ParsedDicomFile> parsed_; + std::unique_ptr<std::string> raw_; + size_t size_; + + public: + Value(IDicomTranscoder::DicomImage* transcoded) + { + std::unique_ptr<IDicomTranscoder::DicomImage> protection(transcoded); + + if (transcoded == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + + if (transcoded->HasParsed() && + !transcoded->HasInternalBuffer()) + { + parsed_.reset(transcoded->ReleaseAsParsedDicomFile()); + + DcmFileFormat& f = parsed_->GetDcmtkObject(); + size_ = f.calcElementLength(f.getDataset()->getOriginalXfer(), EET_ExplicitLength); + } + else if (!transcoded->HasParsed() && + transcoded->HasInternalBuffer()) + { + raw_.reset(transcoded->ReleaseInternalBuffer()); + size_ = raw_->size(); + } + else + { + throw OrthancException(ErrorCode_InternalError); + } + } + + size_t GetSize() const + { + return size_; + } + + bool HasParsed() const + { + return parsed_.get() != NULL; + } + + bool HasRawBuffer() const + { + return raw_.get() != NULL; + } + + Mutex::ScopedLock* LockParsed() + { + return new Mutex::ScopedLock(parsedMutex_); + } + + ParsedDicomFile& GetParsed() const + { + if (parsed_.get() == NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return *parsed_; + } + } + + const std::string& GetRawBuffer() const + { + if (raw_.get() == NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return *raw_; + } + } + }; + + + class TranscoderDataSource::Identifier : public BaseDataIdentifier + { + private: + FileInfo attachment_; + DicomTransferSyntax targetSyntax_; + TranscodingSopInstanceUidMode mode_; + bool hasLossyQuality_; + unsigned int lossyQuality_; + + public: + Identifier(const FileInfo& attachment, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode) : + attachment_(attachment), + targetSyntax_(targetSyntax), + mode_(mode), + hasLossyQuality_(false), + lossyQuality_(0) + { + } + + void SetLossyQuality(unsigned int quality) + { + hasLossyQuality_ = true; + lossyQuality_ = quality; + } + + virtual bool GetCacheKey(std::string& key) const ORTHANC_OVERRIDE + { + key = (attachment_.GetUuid() + "|" + + GetTransferSyntaxUid(targetSyntax_) + "|" + + boost::lexical_cast<std::string>(mode_)); + + if (hasLossyQuality_) + { + key += "|" + boost::lexical_cast<std::string>(lossyQuality_); + } + + return true; + } + + virtual bool EstimateValueSize(size_t& target) const ORTHANC_OVERRIDE + { + target = attachment_.GetUncompressedSize(); + return true; + } + + Value* Load(IDicomTranscoder& transcoder, + DataSourceReader& storageAreaReader) const + { + std::unique_ptr<StorageAreaDataSource::Range> range( + StorageAreaDataSource::Execute( + storageAreaReader, StorageAreaDataSource::CreateAttachmentRequest(attachment_, true /* uncompress */))); + + IDicomTranscoder::DicomImage source; + source.SetExternalBuffer(range->GetData(), range->GetSize()); + + std::unique_ptr<IDicomTranscoder::DicomImage> target; + + std::set<DicomTransferSyntax> allowedSyntaxes; + allowedSyntaxes.insert(targetSyntax_); + + bool success; + if (hasLossyQuality_) + { + success = transcoder.Transcode(*target, source, allowedSyntaxes, mode_, lossyQuality_); + } + else + { + success = transcoder.Transcode(*target, source, allowedSyntaxes, mode_); + } + + if (success) + { + return new Value(target.release()); + } + else + { + throw OrthancException(ErrorCode_NotImplemented); + } + } + }; + + + TranscoderDataSource::TranscoderDataSource(const boost::shared_ptr<IDicomTranscoder>& transcoder, + const boost::shared_ptr<DataSourceReader>& storageAreaReader) : + transcoder_(transcoder), + storageAreaReader_(storageAreaReader) + { + if (!transcoder_ || + !storageAreaReader_) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + + IDynamicObject* TranscoderDataSource::Load(const IDataIdentifier& id) + { + return dynamic_cast<const Identifier&>(id).Load(*transcoder_, *storageAreaReader_); + } + + + size_t TranscoderDataSource::GetValueSize(const IDynamicObject& value) const + { + return dynamic_cast<const Value&>(value).GetSize(); + } + + + TranscoderDataSource::Transcoded::Transcoded(DataSourceAnswer::Item* item) : + item_(item) + { + if (!item) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + + TranscoderDataSource::Transcoded::LockAsParsed::LockAsParsed(Transcoded& that) + { + Value& value = dynamic_cast<Value&>(*that.item_->GetValue()); + + if (value.HasParsed()) + { + lock_.reset(value.LockParsed()); + content_ = &value.GetParsed(); + } + else + { + assert(value.HasRawBuffer()); + parsed_.reset(new ParsedDicomFile(value.GetRawBuffer())); + + content_ = parsed_.get(); + } + + assert(content_ != NULL); + } + + + TranscoderDataSource::Transcoded::LockAsBuffer::LockAsBuffer(Transcoded& that) + { + Value& value = dynamic_cast<Value&>(*that.item_->GetValue()); + + if (value.HasParsed()) + { + std::unique_ptr<Mutex::ScopedLock> lock(value.LockParsed()); + serialized_.reset(new std::string); + value.GetParsed().SaveToMemoryBuffer(*serialized_); + content_ = serialized_.get(); + } + else + { + assert(value.HasRawBuffer()); + content_ = &value.GetRawBuffer(); + } + + assert(content_ != NULL); + } + + + IDataIdentifier* TranscoderDataSource::CreateRequest(const FileInfo& attachment, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + bool hasLossyQuality, + unsigned int lossyQuality) + { + std::unique_ptr<Identifier> id(new Identifier(attachment, targetSyntax, mode)); + + if (hasLossyQuality && + !IsLosslessTransferSyntax(targetSyntax)) + { + id->SetLossyQuality(lossyQuality); + } + + return id.release(); + } + + + TranscoderDataSource::Transcoded* TranscoderDataSource::Execute(DataSourceReader& reader, + IDataIdentifier* request) + { + if (request == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + else + { + std::unique_ptr<DataSourceAnswer::Item> item(reader.ReadSingle(request)); + return new Transcoded(item.release()); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.h Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,116 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "../DicomParsing/IDicomTranscoder.h" +#include "../FileStorage/FileInfo.h" +#include "../MultiThreading/Mutex.h" +#include "DataSourceAnswer.h" +#include "IDataSource.h" + +#include <boost/shared_ptr.hpp> + + +namespace Orthanc +{ + class DataSourceReader; + + class TranscoderDataSource : public IDataSource + { + private: + class Value; + class Identifier; + + boost::shared_ptr<IDicomTranscoder> transcoder_; + boost::shared_ptr<DataSourceReader> storageAreaReader_; + + public: + TranscoderDataSource(const boost::shared_ptr<IDicomTranscoder>& transcoder, + const boost::shared_ptr<DataSourceReader>& storageAreaReader); + + virtual IDynamicObject* Load(const IDataIdentifier& id) ORTHANC_OVERRIDE; + + virtual size_t GetValueSize(const IDynamicObject& value) const ORTHANC_OVERRIDE; + + class Transcoded : public boost::noncopyable + { + private: + std::unique_ptr<DataSourceAnswer::Item> item_; // Holding item puts backpressure on the data source + + public: + explicit Transcoded(DataSourceAnswer::Item* item /* takes ownership */); + + class LockAsParsed : public boost::noncopyable + { + private: + /** + * Access to the transcoded DICOM must be protected by a + * mutex, as it could be shared by multiple threads if caching + * is enabled in the DataSourceReader. + **/ + + std::unique_ptr<Mutex::ScopedLock> lock_; + std::unique_ptr<ParsedDicomFile> parsed_; + const ParsedDicomFile* content_; + + public: + explicit LockAsParsed(Transcoded& that); + + const ParsedDicomFile& GetContent() const + { + return *content_; + } + }; + + class LockAsBuffer : public boost::noncopyable + { + private: + /** + * Access to the transcoded DICOM must only be protected in + * the constructor, if serialization to the buffer is required. + **/ + + std::unique_ptr<std::string> serialized_; + const std::string* content_; + + public: + explicit LockAsBuffer(Transcoded& that); + + const std::string& GetContent() const + { + return *content_; + } + }; + }; + + static IDataIdentifier* CreateRequest(const FileInfo& attachment, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + bool hasLossyQuality, + unsigned int lossyQuality); + + static Transcoded* Execute(DataSourceReader& reader, + IDataIdentifier* request /* takes ownership */); + }; +}
--- a/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.cpp Tue May 26 15:52:35 2026 +0200 @@ -426,4 +426,17 @@ return buffer_->size(); } } + + + std::string* IDicomTranscoder::DicomImage::ReleaseInternalBuffer() + { + if (buffer_.get() == NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + return buffer_.release(); + } + } }
--- a/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h Tue May 26 15:52:35 2026 +0200 @@ -24,8 +24,13 @@ #pragma once +#if ORTHANC_ENABLE_DCMTK != 1 +# error The macro ORTHANC_ENABLE_DCMTK must be set to 1 +#endif + #include "../Compatibility.h" #include "../Enumerations.h" +#include "../IDynamicObject.h" #include <boost/noncopyable.hpp> #include <set> @@ -43,7 +48,7 @@ class ORTHANC_PUBLIC IDicomTranscoder : public boost::noncopyable { public: - class ORTHANC_PUBLIC DicomImage : public boost::noncopyable + class ORTHANC_PUBLIC DicomImage : public IDynamicObject { private: std::unique_ptr<DcmFileFormat> parsed_; @@ -86,6 +91,18 @@ const void* GetBufferData(); size_t GetBufferSize(); + + bool HasParsed() const + { + return parsed_.get() != NULL; + } + + bool HasInternalBuffer() const + { + return buffer_.get() != NULL; + } + + std::string* ReleaseInternalBuffer(); };
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DicomParsing/TranscodingCallable.cpp Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,101 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#include "../PrecompiledHeaders.h" +#include "TranscodingCallable.h" + +#include "../OrthancException.h" + +#include <dcmtk/dcmdata/dcfilefo.h> + + +namespace Orthanc +{ + TranscodingCallable::TranscodingCallable() : + mode_(TranscodingSopInstanceUidMode_AllowNew), + hasLossyQuality_(false), + lossyQuality_(0) + { + } + + + void TranscodingCallable::SetTranscoder(const boost::shared_ptr<IDicomTranscoder>& transcoder) + { + if (transcoder_.get() == NULL) + { + transcoder_ = transcoder; + } + else + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + } + + + void TranscodingCallable::SetLossyQuality(unsigned int quality) + { + hasLossyQuality_ = true; + lossyQuality_ = quality; + } + + + IDynamicObject* TranscodingCallable::Call() + { + if (transcoder_.get() == NULL) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + + std::unique_ptr<IDicomTranscoder::DicomImage> transcoded(new IDicomTranscoder::DicomImage); + bool success; + + if (hasLossyQuality_) + { + success = transcoder_->Transcode(*transcoded, source_, allowedSyntaxes_, mode_, lossyQuality_); + } + else + { + success = transcoder_->Transcode(*transcoded, source_, allowedSyntaxes_, mode_); + } + + if (success) + { + return transcoded.release(); + } + else + { + if (allowedSyntaxes_.size() == 1) + { + DicomTransferSyntax syntax = *allowedSyntaxes_.begin(); + throw OrthancException(ErrorCode_InternalError, + "Cannot transcode to transfer syntax: " + + std::string(GetTransferSyntaxUid(syntax))); + } + else + { + throw OrthancException(ErrorCode_NotImplemented, "Cannot transcode DICOM image"); + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DicomParsing/TranscodingCallable.h Tue May 26 15:52:35 2026 +0200 @@ -0,0 +1,75 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2023 Osimis S.A., Belgium + * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium + * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/>. + **/ + + +#pragma once + +#include "IDicomTranscoder.h" +#include "../MultiThreading/ICallable.h" + +#include <boost/shared_ptr.hpp> + + +namespace Orthanc +{ + class TranscodingCallable : public ICallable + { + private: + boost::shared_ptr<IDicomTranscoder> transcoder_; + IDicomTranscoder::DicomImage source_; + std::set<DicomTransferSyntax> allowedSyntaxes_; + TranscodingSopInstanceUidMode mode_; + bool hasLossyQuality_; + unsigned int lossyQuality_; + + public: + // By default, allow new SOP instance UID + TranscodingCallable(); + + void AcquireParsed(ParsedDicomFile& parsed /* will be invalidated */) + { + source_.AcquireParsed(parsed); + } + + void AcquireBuffer(std::string& buffer /* will be swapped */) + { + source_.AcquireBuffer(buffer); + } + + void SetTranscoder(const boost::shared_ptr<IDicomTranscoder>& transcoder); + + void SetMode(TranscodingSopInstanceUidMode mode) + { + mode_ = mode; + } + + void AddTransferSyntax(DicomTransferSyntax syntax) + { + allowedSyntaxes_.insert(syntax); + } + + void SetLossyQuality(unsigned int quality); + + virtual IDynamicObject* Call() ORTHANC_OVERRIDE; + }; +}
--- a/OrthancFramework/Sources/HttpServer/HttpOutput.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/HttpServer/HttpOutput.cpp Tue May 26 15:52:35 2026 +0200 @@ -377,7 +377,7 @@ void HttpOutput::SendStatus(HttpStatus status, - const char* message, + const void* message, size_t messageSize) { if (status == HttpStatus_301_MovedPermanently ||
--- a/OrthancFramework/Sources/HttpServer/HttpOutput.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/HttpServer/HttpOutput.h Tue May 26 15:52:35 2026 +0200 @@ -158,7 +158,7 @@ } void SendStatus(HttpStatus status, - const char* message, + const void* message, size_t messageSize); void SendStatus(HttpStatus status);
--- a/OrthancFramework/Sources/IMemoryBuffer.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/IMemoryBuffer.h Tue May 26 15:52:35 2026 +0200 @@ -40,6 +40,8 @@ { } + virtual void CopyToString(std::string& target) const = 0; + // The content of the memory buffer will emptied after this call virtual void MoveToString(std::string& target) = 0;
--- a/OrthancFramework/Sources/MallocMemoryBuffer.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/MallocMemoryBuffer.cpp Tue May 26 15:52:35 2026 +0200 @@ -102,6 +102,17 @@ } + void MallocMemoryBuffer::CopyToString(std::string& target) const + { + target.resize(size_); + + if (size_ != 0) + { + memcpy(&target[0], buffer_, size_); + } + } + + void MallocMemoryBuffer::MoveToString(std::string& target) { target.resize(size_);
--- a/OrthancFramework/Sources/MallocMemoryBuffer.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/MallocMemoryBuffer.h Tue May 26 15:52:35 2026 +0200 @@ -53,6 +53,8 @@ uint64_t size, FreeFunction freeFunction); + virtual void CopyToString(std::string& target) const ORTHANC_OVERRIDE; + virtual void MoveToString(std::string& target) ORTHANC_OVERRIDE; virtual const void* GetData() const ORTHANC_OVERRIDE
--- a/OrthancFramework/Sources/MultiThreading/Mutex.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/MultiThreading/Mutex.h Tue May 26 15:52:35 2026 +0200 @@ -47,6 +47,10 @@ explicit ScopedLock(Mutex& mutex) { } + + void Unlock() + { + } }; }; @@ -68,6 +72,11 @@ lock_(mutex.mutex_) { } + + void Unlock() + { + lock_.unlock(); + } }; }; #endif
--- a/OrthancFramework/Sources/StringMemoryBuffer.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/StringMemoryBuffer.cpp Tue May 26 15:52:35 2026 +0200 @@ -28,6 +28,12 @@ namespace Orthanc { + void StringMemoryBuffer::CopyToString(std::string& target) const + { + target = buffer_; + } + + void StringMemoryBuffer::MoveToString(std::string& target) { buffer_.swap(target);
--- a/OrthancFramework/Sources/StringMemoryBuffer.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancFramework/Sources/StringMemoryBuffer.h Tue May 26 15:52:35 2026 +0200 @@ -50,6 +50,8 @@ buffer_.swap(buffer); } + virtual void CopyToString(std::string& target) const ORTHANC_OVERRIDE; + virtual void MoveToString(std::string& target) ORTHANC_OVERRIDE; virtual const void* GetData() const ORTHANC_OVERRIDE
--- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Tue May 26 15:52:35 2026 +0200 @@ -35,6 +35,8 @@ #include "../../../OrthancFramework/Sources/Compression/GzipCompressor.h" #include "../../../OrthancFramework/Sources/Compression/ZlibCompressor.h" +#include "../../../OrthancFramework/Sources/DataSource/DicomDataSource.h" +#include "../../../OrthancFramework/Sources/DataSource/StorageAreaDataSource.h" #include "../../../OrthancFramework/Sources/DicomFormat/DicomArray.h" #include "../../../OrthancFramework/Sources/DicomNetworking/DicomConnectionInfo.h" #include "../../../OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.h" @@ -65,6 +67,7 @@ #include "../../Sources/Search/HierarchicalMatcher.h" #include "../../Sources/ServerContext.h" #include "../../Sources/ServerToolbox.h" +#include "../../Sources/ServerTranscoder.h" #include "OrthancPluginDatabase.h" #include "OrthancPluginDatabaseV3.h" #include "OrthancPluginDatabaseV4.h" @@ -3542,14 +3545,14 @@ const _OrthancPluginGetDicomForInstance& p = *reinterpret_cast<const _OrthancPluginGetDicomForInstance*>(parameters); - std::string dicom; + std::unique_ptr<StorageAreaDataSource::Range> raw; { PImpl::ServerContextReference lock(*pimpl_); - lock.GetContext().ReadDicom(dicom, p.instanceId); - } - - CopyToMemoryBuffer(p.target, dicom); + raw.reset(lock.GetContext().ReadRawDicom(p.instanceId)); + } + + CopyToMemoryBuffer(p.target, raw->GetData(), raw->GetSize()); } static void ThrowOnHttpError(HttpStatus httpStatus) @@ -4036,7 +4039,7 @@ std::unique_ptr<ImageAccessor> decoded; { PImpl::ServerContextReference lock(*pimpl_); - decoded.reset(lock.GetContext().DecodeDicomFrame(instance, p.frameIndex)); + decoded.reset(lock.GetContext().GetTranscoder()->DecodeFrame(instance, p.frameIndex)); } *(p.targetImage) = ReturnImage(decoded); @@ -4150,8 +4153,14 @@ case OrthancPluginImageFormat_Dicom: { - PImpl::ServerContextReference lock(*pimpl_); - image.reset(lock.GetContext().DecodeDicomFrame(p.data, p.size, 0)); + std::unique_ptr<DicomInstanceToStore> instance(DicomInstanceToStore::CreateFromBuffer(p.data, p.size)); + + { + PImpl::ServerContextReference lock(*pimpl_); + image.reset(lock.GetContext().GetTranscoder()->DecodeFrame( + *instance, 0 /* frame index is not available in _OrthancPluginUncompressImage */)); + } + break; } @@ -4696,14 +4705,11 @@ throw OrthancException(ErrorCode_NullPointer); } - std::string content; - { PImpl::ServerContextReference lock(*pimpl_); - lock.GetContext().ReadDicom(content, p.instanceId); - } - - dicom.reset(new ParsedDicomFile(content)); + std::unique_ptr<DicomDataSource::Dicom> loaded(lock.GetContext().ReadParsedDicom(p.instanceId)); + dicom.reset(loaded->Clone()); + } } Json::Value json; @@ -4828,8 +4834,13 @@ case _OrthancPluginService_DecodeDicomImage: { - PImpl::ServerContextReference lock(*pimpl_); - result.reset(lock.GetContext().DecodeDicomFrame(p.constBuffer, p.bufferSize, p.frameIndex)); + std::unique_ptr<DicomInstanceToStore> instance(DicomInstanceToStore::CreateFromBuffer(p.constBuffer, p.bufferSize)); + + { + PImpl::ServerContextReference lock(*pimpl_); + result.reset(lock.GetContext().GetTranscoder()->DecodeFrame(*instance, p.frameIndex)); + } + break; } @@ -5124,14 +5135,14 @@ { case OrthancPluginLoadDicomInstanceMode_WholeDicom: { - std::string buffer; + std::unique_ptr<StorageAreaDataSource::Range> raw; { PImpl::ServerContextReference lock(*pimpl_); - lock.GetContext().ReadDicom(buffer, params.instanceId); - } - - target.reset(new DicomInstanceFromBuffer(buffer)); + raw.reset(lock.GetContext().ReadRawDicom(params.instanceId)); + } + + target.reset(new DicomInstanceFromBuffer(raw->GetData(), raw->GetSize())); break; } @@ -5141,17 +5152,9 @@ std::unique_ptr<ParsedDicomFile> parsed; { - std::string buffer; - - { - PImpl::ServerContextReference lock(*pimpl_); - if (!lock.GetContext().ReadDicomUntilPixelData(buffer, params.instanceId)) - { - lock.GetContext().ReadDicom(buffer, params.instanceId); - } - } - - parsed.reset(new ParsedDicomFile(buffer)); + PImpl::ServerContextReference lock(*pimpl_); + std::unique_ptr<DicomDataSource::Dicom> loaded(lock.GetContext().ReadParsedDicom(params.instanceId)); + parsed.reset(loaded->Clone()); } parsed->RemoveFromPixelData(); @@ -6081,7 +6084,7 @@ { PImpl::ServerContextReference lock(*pimpl_); - success = lock.GetContext().GetTranscoder().Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew); + success = lock.GetContext().GetTranscoder()->Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew); } if (success) @@ -6889,7 +6892,7 @@ ImageAccessor* OrthancPlugins::Decode(const void* dicom, size_t size, - unsigned int frame) + unsigned int frame) const { boost::shared_lock<boost::shared_mutex> lock(pimpl_->decoderTranscoderMutex_);
--- a/OrthancServer/Plugins/Engine/OrthancPlugins.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.h Tue May 26 15:52:35 2026 +0200 @@ -53,7 +53,6 @@ #include "../../../OrthancFramework/Sources/JobsEngine/IJob.h" #include "../../../OrthancFramework/Sources/MallocMemoryBuffer.h" #include "../../Sources/Database/IDatabaseWrapper.h" -#include "../../Sources/IDicomImageDecoder.h" #include "../../Sources/IServerListener.h" #include "../../Sources/ServerJobs/IStorageCommitmentFactory.h" #include "PluginMemoryBuffer64.h" @@ -73,7 +72,6 @@ public IPluginServiceProvider, public IServerListener, public IWorklistRequestHandlerFactory, - public IDicomImageDecoder, public IFindRequestHandlerFactory, public IMoveRequestHandlerFactory, public IStorageCommitmentFactory, @@ -394,9 +392,9 @@ bool HasCustomTranscoder(); - virtual ImageAccessor* Decode(const void* dicom, - size_t size, - unsigned int frame) ORTHANC_OVERRIDE; + ImageAccessor* Decode(const void* dicom, + size_t size, + unsigned int frame) const; bool IsAllowed(HttpMethod method, const char* uri,
--- a/OrthancServer/Plugins/Engine/PluginMemoryBuffer32.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Plugins/Engine/PluginMemoryBuffer32.cpp Tue May 26 15:52:35 2026 +0200 @@ -61,6 +61,19 @@ } + void PluginMemoryBuffer32::CopyToString(std::string& target) const + { + SanityCheck(); + + target.resize(buffer_.size); + + if (buffer_.size != 0) + { + memcpy(&target[0], buffer_.data, buffer_.size); + } + } + + void PluginMemoryBuffer32::MoveToString(std::string& target) { SanityCheck();
--- a/OrthancServer/Plugins/Engine/PluginMemoryBuffer32.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Plugins/Engine/PluginMemoryBuffer32.h Tue May 26 15:52:35 2026 +0200 @@ -49,6 +49,8 @@ Clear(); } + virtual void CopyToString(std::string& target) const ORTHANC_OVERRIDE; + virtual void MoveToString(std::string& target) ORTHANC_OVERRIDE; virtual const void* GetData() const ORTHANC_OVERRIDE;
--- a/OrthancServer/Plugins/Engine/PluginMemoryBuffer64.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Plugins/Engine/PluginMemoryBuffer64.cpp Tue May 26 15:52:35 2026 +0200 @@ -58,6 +58,19 @@ } + void PluginMemoryBuffer64::CopyToString(std::string& target) const + { + SanityCheck(); + + target.resize(buffer_.size); + + if (buffer_.size != 0) + { + memcpy(&target[0], buffer_.data, buffer_.size); + } + } + + void PluginMemoryBuffer64::MoveToString(std::string& target) { SanityCheck();
--- a/OrthancServer/Plugins/Engine/PluginMemoryBuffer64.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Plugins/Engine/PluginMemoryBuffer64.h Tue May 26 15:52:35 2026 +0200 @@ -49,6 +49,8 @@ Clear(); } + virtual void CopyToString(std::string& target) const ORTHANC_OVERRIDE; + virtual void MoveToString(std::string& target) ORTHANC_OVERRIDE; virtual const void* GetData() const ORTHANC_OVERRIDE;
--- a/OrthancServer/Sources/IDicomImageDecoder.h Tue May 26 15:48:55 2026 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -/** - * Orthanc - A Lightweight, RESTful DICOM Store - * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics - * Department, University Hospital of Liege, Belgium - * Copyright (C) 2017-2023 Osimis S.A., Belgium - * Copyright (C) 2024-2026 Orthanc Team SRL, Belgium - * Copyright (C) 2021-2026 Sebastien Jodogne, ICTEAM UCLouvain, Belgium - * - * This program is free software: you can redistribute it and/or - * modify it under the terms of the GNU General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - **/ - - -#pragma once - -#include "../../OrthancFramework/Sources/Images/ImageAccessor.h" - -#include <boost/noncopyable.hpp> - -namespace Orthanc -{ - class IDicomImageDecoder : public boost::noncopyable - { - public: - virtual ~IDicomImageDecoder() - { - } - - virtual ImageAccessor* Decode(const void* dicom, - size_t size, - unsigned int frame) = 0; - }; -}
--- a/OrthancServer/Sources/OrthancGetRequestHandler.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/OrthancGetRequestHandler.cpp Tue May 26 15:52:35 2026 +0200 @@ -30,7 +30,7 @@ #include "OrthancConfiguration.h" #include "ServerContext.h" #include "ServerJobs/DicomModalityStoreJob.h" -#include "ServerJobs/ThreadedInstancesLoader.h" +#include "ServerTranscoder.h" #include <dcmtk/dcmdata/dcdeftag.h> #include <dcmtk/dcmdata/dcfilefo.h> @@ -65,45 +65,40 @@ throw OrthancException(ErrorCode_ParameterOutOfRange); } - if (instancesLoader_.get() == NULL) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - - const std::string& id = instancesIds_[position_++]; - - std::string dicom; - instancesLoader_->WaitDicomInstance(dicom, id); - - if (dicom.empty()) - { - throw OrthancException(ErrorCode_BadFileFormat); - } - - std::unique_ptr<DcmFileFormat> parsed( - FromDcmtkBridge::LoadFromMemoryBuffer(dicom.c_str(), dicom.size())); - - if (parsed.get() == NULL || - parsed->getDataset() == NULL) + if (instancesLoader_.get() == NULL || + !instancesLoader_->HasNext()) { throw OrthancException(ErrorCode_InternalError); } - - DcmDataset& dataset = *parsed->getDataset(); - - OFString a, b; - if (!dataset.findAndGetOFString(DCM_SOPClassUID, a).good() || - !dataset.findAndGetOFString(DCM_SOPInstanceUID, b).good()) + { - throw OrthancException(ErrorCode_NoSopClassOrInstance, - "Unable to determine the SOP class/instance for C-STORE with AET " + - originatorAet_); - } + /** + * It is OK to modify the content of "dicom", as + * "DicomSequentialReader" returns copies of the cached objects + * (cf. "DataSourceSequentialReader::IValueDisconnector"). (*) + **/ + std::unique_ptr<ParsedDicomFile> dicom(instancesLoader_->Next()); - std::string sopClassUid(a.c_str()); - std::string sopInstanceUid(b.c_str()); + if (dicom.get() == NULL || + dicom->GetDcmtkObject().getDataset() == NULL) + { + throw OrthancException(ErrorCode_InternalError); + } - return PerformGetSubOp(assoc, sopClassUid, sopInstanceUid, parsed.release()); + OFString a, b; + if (!dicom->GetDcmtkObject().getDataset()->findAndGetOFString(DCM_SOPClassUID, a).good() || + !dicom->GetDcmtkObject().getDataset()->findAndGetOFString(DCM_SOPInstanceUID, b).good()) + { + throw OrthancException(ErrorCode_NoSopClassOrInstance, + "Unable to determine the SOP class/instance for C-STORE with AET " + + originatorAet_); + } + + std::string sopClassUid(a.c_str()); + std::string sopInstanceUid(b.c_str()); + + return PerformGetSubOp(assoc, sopClassUid, sopInstanceUid, *dicom); + } } @@ -233,13 +228,10 @@ bool OrthancGetRequestHandler::PerformGetSubOp(T_ASC_Association* assoc, const std::string& sopClassUid, const std::string& sopInstanceUid, - DcmFileFormat* dicomRaw) + ParsedDicomFile& dicom) { - assert(dicomRaw != NULL); - std::unique_ptr<DcmFileFormat> dicom(dicomRaw); - DicomTransferSyntax sourceSyntax; - if (!FromDcmtkBridge::LookupOrthancTransferSyntax(sourceSyntax, *dicom)) + if (!FromDcmtkBridge::LookupOrthancTransferSyntax(sourceSyntax, dicom.GetDcmtkObject())) { failedCount_++; AddFailedUIDInstance(sopInstanceUid); @@ -316,7 +308,7 @@ // No transcoding is required DcmDataset *stDetailTmp = NULL; cond = DIMSE_storeUser( - assoc, presId, &req, NULL /* imageFileName */, dicom->getDataset(), + assoc, presId, &req, NULL /* imageFileName */, dicom.GetDcmtkObject().getDataset(), ProgressCallback, NULL /* callbackData */, (timeout_ > 0 ? DIMSE_NONBLOCKING : DIMSE_BLOCKING), static_cast<int>(timeout_), &rsp, &stDetailTmp, &cancelParameters); @@ -326,12 +318,19 @@ { // Transcoding to the selected uncompressed transfer syntax IDicomTranscoder::DicomImage source, transcoded; - source.AcquireParsed(dicom.release()); + source.AcquireParsed(dicom); // This invalidates the "dicom" object, cf. (*) + + /** + * TODO-Streaming : It doesn't seem possible to uniformly trancode upfront + * using "DicomSequentialReader::CreateForTranscoded()", as the + * selected transfer syntax is not constant, but varies upon + * each individual instance given the presentation context. + **/ std::set<DicomTransferSyntax> ts; ts.insert(selectedSyntax); - if (context_.GetTranscoder().Transcode(transcoded, source, ts, TranscodingSopInstanceUidMode_AllowNew)) + if (context_.GetTranscoder()->Transcode(transcoded, source, ts, TranscodingSopInstanceUidMode_AllowNew)) { // Transcoding has succeeded DcmDataset *stDetailTmp = NULL; @@ -550,7 +549,6 @@ localAet_ = context_.GetDefaultLocalApplicationEntityTitle(); position_ = 0; originatorAet_ = originatorAet; - unsigned int loaderThreads = 1; { OrthancConfiguration::ReaderLock lock; @@ -574,11 +572,8 @@ throw OrthancException(ErrorCode_InexistentItem, "C-GET: Rejecting SCU request from unknown modality with AET: " + originatorAet); } - - loaderThreads = lock.GetConfiguration().GetLoaderThreads(); } - instancesLoader_.reset(new ThreadedInstancesLoader(context_, loaderThreads, false, DicomTransferSyntax_BigEndianExplicit /* dummy unused value */, 0, "CGET")); std::vector<FileInfo> filesInfo; for (std::list<std::string>::const_iterator @@ -590,11 +585,16 @@ context_.GetOrderedChildInstances(instancesIds_, filesInfo, *resourceId, level); } + + instancesLoader_.reset(context_.CreateDicomSequentialReader()); + for (size_t i = 0; i < instancesIds_.size(); ++i) { - instancesLoader_->PreloadDicomInstance(instancesIds_[i], filesInfo[i]); + instancesLoader_->Submit(filesInfo[i]); } + instancesLoader_->Start(); + failedUIDs_.clear(); completedCount_ = 0;
--- a/OrthancServer/Sources/OrthancGetRequestHandler.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/OrthancGetRequestHandler.h Tue May 26 15:52:35 2026 +0200 @@ -23,19 +23,16 @@ #pragma once #include "../../OrthancFramework/Sources/Compatibility.h" // For ORTHANC_OVERRIDE +#include "../../OrthancFramework/Sources/DataSource/DicomSequentialReader.h" #include "../../OrthancFramework/Sources/DicomNetworking/IGetRequestHandler.h" #include "../../OrthancFramework/Sources/DicomNetworking/RemoteModalityParameters.h" -#include <dcmtk/dcmnet/dimse.h> - #include <list> -class DcmFileFormat; - namespace Orthanc { + class ParsedDicomFile; class ServerContext; - class ThreadedInstancesLoader; class OrthancGetRequestHandler : public IGetRequestHandler { @@ -45,7 +42,7 @@ std::vector<std::string> instancesIds_; size_t position_; std::string originatorAet_; - std::unique_ptr<ThreadedInstancesLoader> instancesLoader_; + std::unique_ptr<DicomSequentialReader> instancesLoader_; unsigned int completedCount_; unsigned int warningCount_; @@ -63,7 +60,7 @@ bool PerformGetSubOp(T_ASC_Association *assoc, const std::string& sopClassUid, const std::string& sopInstanceUid, - DcmFileFormat* datasetRaw); + ParsedDicomFile& dicom); void AddFailedUIDInstance(const std::string& sopInstance);
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestAnonymizeModify.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestAnonymizeModify.cpp Tue May 26 15:52:35 2026 +0200 @@ -25,6 +25,7 @@ #include "OrthancRestApi.h" #include "../../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h" +#include "../../../OrthancFramework/Sources/DicomParsing/TranscodingCallable.h" #include "../../../OrthancFramework/Sources/Logging.h" #include "../../../OrthancFramework/Sources/SerializationToolbox.h" #include "../DicomInstanceToStore.h" @@ -37,6 +38,7 @@ #include <boost/lexical_cast.hpp> #include <boost/algorithm/string/predicate.hpp> + #define INFO_SUBSEQUENCES \ "Starting with Orthanc 1.9.4, paths to subsequences can be provided using the "\ "same syntax as the `dcmodify` command-line tool (wildcards are supported as well)." @@ -217,33 +219,24 @@ std::unique_ptr<ParsedDicomFile> modified; { - ServerContext::DicomCacheLocker locker(context, id); - modified.reset(locker.GetDicom().Clone(true)); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(id)); + modified.reset(dicom->Clone()); } modification.Apply(modified); if (transcode) { - IDicomTranscoder::DicomImage source; - source.AcquireParsed(*modified); // "modified" is invalid below this point - - IDicomTranscoder::DicomImage transcoded; + std::unique_ptr<TranscodingCallable> callable(new TranscodingCallable); + callable->AcquireParsed(*modified); // "modified" is invalid below this point + callable->AddTransferSyntax(targetSyntax); + callable->SetLossyQuality(lossyQuality); - std::set<DicomTransferSyntax> s; - s.insert(targetSyntax); - - if (context.GetTranscoder().Transcode(transcoded, source, s, TranscodingSopInstanceUidMode_AllowNew, lossyQuality)) - { - call.GetOutput().AnswerBuffer(transcoded.GetBufferData(), - transcoded.GetBufferSize(), MimeType_Dicom); - } - else - { - throw OrthancException(ErrorCode_InternalError, - "Cannot transcode to transfer syntax: " + - std::string(GetTransferSyntaxUid(targetSyntax))); - } + std::unique_ptr<Future> future(context.SubmitTranscodingRequest(callable.release())); + + std::unique_ptr<IDicomTranscoder::DicomImage> transcoded(dynamic_cast<IDicomTranscoder::DicomImage*>(future->ReleaseResult())); + call.GetOutput().AnswerBuffer(transcoded->GetBufferData(), + transcoded->GetBufferSize(), MimeType_Dicom); } else {
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp Tue May 26 15:52:35 2026 +0200 @@ -29,10 +29,12 @@ #include "../../../OrthancFramework/Sources/CompatibilityMath.h" #include "../../../OrthancFramework/Sources/Compression/GzipCompressor.h" #include "../../../OrthancFramework/Sources/Constants.h" +#include "../../../OrthancFramework/Sources/DataSource/StorageAreaDataSource.h" #include "../../../OrthancFramework/Sources/DicomFormat/DicomImageInformation.h" #include "../../../OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.h" #include "../../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h" #include "../../../OrthancFramework/Sources/DicomParsing/Internals/DicomImageDecoder.h" +#include "../../../OrthancFramework/Sources/DicomParsing/TranscodingCallable.h" #include "../../../OrthancFramework/Sources/HttpServer/HttpContentNegociation.h" #include "../../../OrthancFramework/Sources/Images/Image.h" #include "../../../OrthancFramework/Sources/Images/ImageProcessing.h" @@ -51,7 +53,6 @@ // This "include" is mandatory for Release builds using Linux Standard Base #include <boost/shared_ptr.hpp> -#include "../../../OrthancFramework/Sources/FileStorage/StorageAccessor.h" /** * This semaphore is used to limit the number of concurrent HTTP @@ -382,8 +383,9 @@ DicomWebJsonVisitor visitor; { - ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), publicId); - locker.GetDicom().Apply(visitor); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(publicId)); + DicomDataSource::Dicom::Lock lock(*dicom); + lock.GetContent().Apply(visitor); } if (mime == MimeType_DicomWebJson) @@ -413,36 +415,31 @@ { unsigned int lossyQuality; unsigned int defaultLossyQuality; + { OrthancConfiguration::ReaderLock lock; defaultLossyQuality = lock.GetConfiguration().GetDicomLossyTranscodingQuality(); } + lossyQuality = call.GetUnsignedInteger32Argument(GET_LOSSY_QUALITY, defaultLossyQuality); - std::string source; - std::string attachmentId; - std::string transcoded; - context.ReadDicom(source, attachmentId, publicId); - - if (lossyQuality != defaultLossyQuality) // we can't use the cache if the lossy quality is not the default one + std::unique_ptr<TranscodingCallable> callable(new TranscodingCallable); + { - IDicomTranscoder::DicomImage targetImage; - IDicomTranscoder::DicomImage sourceImage; - sourceImage.SetExternalBuffer(source); - std::set<DicomTransferSyntax> allowedSyntaxes; - allowedSyntaxes.insert(GetTransferSyntax(call.GetArgument(GET_TRANSCODE, ""))); - - if (context.GetTranscoder().Transcode(targetImage, sourceImage, allowedSyntaxes, TranscodingSopInstanceUidMode_AllowNew, lossyQuality)) - { - call.GetOutput().SetContentFilename(filename.c_str()); - call.GetOutput().AnswerBuffer(targetImage.GetBufferData(), targetImage.GetBufferSize(), MimeType_Dicom); - } + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(publicId)); + std::unique_ptr<ParsedDicomFile> clone(dicom->Clone()); + callable->AcquireParsed(*clone); // "clone" is invalid below this point } - else if (context.TranscodeWithCache(transcoded, source, publicId, attachmentId, GetTransferSyntax(call.GetArgument(GET_TRANSCODE, "")))) - { - call.GetOutput().SetContentFilename(filename.c_str()); - call.GetOutput().AnswerBuffer(transcoded, MimeType_Dicom); - } + + callable->AddTransferSyntax(GetTransferSyntax(call.GetArgument(GET_TRANSCODE, ""))); + callable->SetLossyQuality(lossyQuality); + + std::unique_ptr<Future> future(context.SubmitTranscodingRequest(callable.release())); + std::unique_ptr<IDicomTranscoder::DicomImage> transcoded(dynamic_cast<IDicomTranscoder::DicomImage*>(future->ReleaseResult())); + + call.GetOutput().SetContentFilename(filename.c_str()); + call.GetOutput().AnswerBuffer(transcoded->GetBufferData(), + transcoded->GetBufferSize(), MimeType_Dicom); } else { @@ -489,12 +486,14 @@ std::string publicId = call.GetUriComponent("id", ""); - std::string dicom; - context.ReadDicom(dicom, publicId); - std::string target; call.BodyToString(target); - SystemToolbox::WriteFile(dicom, SystemToolbox::PathFromUtf8(target)); + + { + std::unique_ptr<StorageAreaDataSource::Range> raw(context.ReadRawDicom(publicId)); + SystemToolbox::WriteFile(raw->GetData(), raw->GetSize(), + SystemToolbox::PathFromUtf8(target), false /* don't automatically call fsync */); + } call.GetOutput().AnswerBuffer("{}", MimeType_Json); } @@ -520,8 +519,9 @@ Json::Value answer; { - ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), publicId); - locker.GetDicom().DatasetToJson(answer, format, flags, + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(publicId)); + DicomDataSource::Dicom::Lock lock(*dicom); + lock.GetContent().DatasetToJson(answer, format, flags, ORTHANC_MAXIMUM_TAG_LENGTH, ignoreTagLength); } @@ -634,13 +634,16 @@ return; } + ServerContext& context = OrthancRestApi::GetContext(call); + std::string publicId = call.GetUriComponent("id", ""); unsigned int numberOfFrames; { - ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), publicId); - numberOfFrames = locker.GetDicom().GetFramesCount(); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(publicId)); + DicomDataSource::Dicom::Lock lock(*dicom); + numberOfFrames = lock.GetContent().GetFramesCount(); } Json::Value result = Json::arrayValue; @@ -923,9 +926,11 @@ * Retrieve a summary of the DICOM tags, which is * necessary to deal with MONOCHROME1 photometric * interpretation, and with windowing parameters. - **/ - ServerContext::DicomCacheLocker locker(context, publicId); - handler.Handle(call, decoded, &locker.GetDicom(), frame); + **/ + + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(publicId)); + DicomDataSource::Dicom::Lock lock(*dicom); + handler.Handle(call, decoded, &lock.GetContent(), frame); } else { @@ -1393,6 +1398,8 @@ } else { + ServerContext& context = OrthancRestApi::GetContext(call); + const std::string instanceId = call.GetUriComponent("id", ""); const bool compress = call.GetBooleanArgument("compress", false); const bool rescale = call.GetBooleanArgument("rescale", true); @@ -1406,10 +1413,9 @@ NumpyVisitor visitor(0 /* no depth, 2D frame */, rescale); { - Semaphore::Locker throttling(throttlingSemaphore_); - ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), instanceId); - - visitor.WriteFrame(locker.GetDicom(), frame); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(instanceId)); + DicomDataSource::Dicom::Lock lock(*dicom); + visitor.WriteFrame(lock.GetContent(), frame); } visitor.Answer(call.GetOutput(), compress); @@ -1430,15 +1436,17 @@ } else { + ServerContext& context = OrthancRestApi::GetContext(call); + const std::string instanceId = call.GetUriComponent("id", ""); const bool compress = call.GetBooleanArgument("compress", false); const bool rescale = call.GetBooleanArgument("rescale", true); { - Semaphore::Locker throttling(throttlingSemaphore_); - ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), instanceId); - - const unsigned int depth = locker.GetDicom().GetFramesCount(); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(instanceId)); + DicomDataSource::Dicom::Lock lock(*dicom); + + const unsigned int depth = lock.GetContent().GetFramesCount(); if (depth == 0) { throw OrthancException(ErrorCode_BadFileFormat, "Empty DICOM instance"); @@ -1448,7 +1456,7 @@ for (unsigned int frame = 0; frame < depth; frame++) { - visitor.WriteFrame(locker.GetDicom(), frame); + visitor.WriteFrame(lock.GetContent(), frame); } visitor.Answer(call.GetOutput(), compress); @@ -1474,8 +1482,6 @@ const bool compress = call.GetBooleanArgument("compress", false); const bool rescale = call.GetBooleanArgument("rescale", true); - Semaphore::Locker throttling(throttlingSemaphore_); - ServerIndex& index = OrthancRestApi::GetIndex(call); SliceOrdering ordering(index, seriesId); @@ -1495,11 +1501,12 @@ unsigned int framesCount = ordering.GetFramesCount(i); { - ServerContext::DicomCacheLocker locker(context, instanceId); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(instanceId)); + DicomDataSource::Dicom::Lock lock(*dicom); for (unsigned int frame = 0; frame < framesCount; frame++) { - visitor.WriteFrame(locker.GetDicom(), frame); + visitor.WriteFrame(lock.GetContent(), frame); } } } @@ -1594,7 +1601,9 @@ } return; } - + + ServerContext& context = OrthancRestApi::GetContext(call); + std::string frameId = call.GetUriComponent("frame", "0"); unsigned int frame; @@ -1612,8 +1621,9 @@ MimeType mime; { - ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), publicId); - locker.GetDicom().GetRawFrame(raw, mime, frame); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(publicId)); + DicomDataSource::Dicom::Lock lock(*dicom); + lock.GetContent().GetRawFrame(raw, mime, frame); } if (GzipCompression) @@ -2378,13 +2388,12 @@ if (hasRangeHeader) { - std::string fragment; - context.ReadAttachmentRange(fragment, info, range, uncompress); + std::unique_ptr<StorageAreaDataSource::Range> fragment(context.ReadAttachment(info, range, uncompress)); uint64_t fullSize = (uncompress ? info.GetUncompressedSize() : info.GetCompressedSize()); call.GetOutput().GetLowLevelOutput().SetContentType(MimeType_Binary); call.GetOutput().GetLowLevelOutput().AddHeader("Content-Range", range.FormatHttpContentRange(fullSize)); - call.GetOutput().GetLowLevelOutput().SendStatus(HttpStatus_206_PartialContent, fragment); + call.GetOutput().GetLowLevelOutput().SendStatus(HttpStatus_206_PartialContent, fragment->GetData(), fragment->GetSize()); } else if (uncompress || info.GetCompressionType() == CompressionType_None) @@ -2394,9 +2403,8 @@ else { // Access to the raw attachment (which is compressed) - std::string content; - context.ReadAttachment(content, info, false /* don't uncompress */, true /* skip cache */); - call.GetOutput().AnswerBuffer(content, MimeType_Binary); + std::unique_ptr<StorageAreaDataSource::Range> content(context.ReadAttachment(info, false /* don't uncompress */)); + call.GetOutput().AnswerBuffer(content->GetData(), content->GetSize(), MimeType_Binary); } } } @@ -2574,13 +2582,14 @@ bool ok = false; // First check whether the compressed data is correctly stored in the disk - std::string data; - context.ReadAttachment(data, info, false, true /* skipCache when you absolutely need the compressed data */); - - std::string actualMD5; - Toolbox::ComputeMD5(actualMD5, data); + std::string compressedMD5; + + { + std::unique_ptr<StorageAreaDataSource::Range> compressed(context.ReadAttachment(info, false /* don't uncompress */)); + Toolbox::ComputeMD5(compressedMD5, compressed->GetData(), compressed->GetSize()); + } - if (actualMD5 == info.GetCompressedMD5()) + if (compressedMD5 == info.GetCompressedMD5()) { // The compressed data is OK. If a compression algorithm was // applied to it, now check the MD5 of the uncompressed data. @@ -2590,9 +2599,14 @@ } else { - context.ReadAttachment(data, info, true, true /* skipCache when you absolutely need the compressed data */); - Toolbox::ComputeMD5(actualMD5, data); - ok = (actualMD5 == info.GetUncompressedMD5()); + std::string uncompressedMD5; + + { + std::unique_ptr<StorageAreaDataSource::Range> uncompressed(context.ReadAttachment(info, true /* uncompress */)); + Toolbox::ComputeMD5(uncompressedMD5, uncompressed->GetData(), uncompressed->GetSize()); + } + + ok = (uncompressedMD5 == info.GetUncompressedMD5()); } } @@ -2827,11 +2841,15 @@ return; } + ServerContext& context = OrthancRestApi::GetContext(call); + std::string id = call.GetUriComponent("id", ""); - ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), id); - - locker.GetDicom().SendPathValue(call.GetOutput(), call.GetTrailingUri()); + { + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(id)); + DicomDataSource::Dicom::Lock lock(*dicom); + lock.GetContent().SendPathValue(call.GetOutput(), call.GetTrailingUri()); + } } @@ -3794,14 +3812,20 @@ return; } + ServerContext& context = OrthancRestApi::GetContext(call); + const std::string id = call.GetUriComponent("id", ""); std::string pdf; - ServerContext::DicomCacheLocker locker(OrthancRestApi::GetContext(call), id); - - if (locker.GetDicom().ExtractPdf(pdf)) + { - call.GetOutput().AnswerBuffer(pdf, MimeType_Pdf); - return; + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(id)); + DicomDataSource::Dicom::Lock lock(*dicom); + + if (lock.GetContent().ExtractPdf(pdf)) + { + call.GetOutput().AnswerBuffer(pdf, MimeType_Pdf); + return; + } } } @@ -3861,16 +3885,16 @@ std::string publicId = call.GetUriComponent("id", ""); - std::string dicomContent; - context.ReadDicomForHeader(dicomContent, publicId); - - // TODO Consider using "DicomMap::ParseDicomMetaInformation()" to - // speed up things here - - ParsedDicomFile dicom(dicomContent); - Json::Value header; - OrthancConfiguration::DefaultDicomHeaderToJson(header, dicom); + + { + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadDicomUntilPixelData(publicId)); + DicomDataSource::Dicom::Lock lock(*dicom); + + // TODO Consider using "DicomMap::ParseDicomMetaInformation()" to + // speed up things here + OrthancConfiguration::DefaultDicomHeaderToJson(header, lock.GetContent()); + } AnswerDicomAsJson(call, header, OrthancRestApi::GetDicomFormat(call, DicomToJsonFormat_Full)); }
--- a/OrthancServer/Sources/OrthancWebDav.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/OrthancWebDav.cpp Tue May 26 15:52:35 2026 +0200 @@ -226,7 +226,11 @@ time_ = GetNow(); } - context_.ReadDicom(target_, resource.GetIdentifier()); + { + std::unique_ptr<StorageAreaDataSource::Range> raw(context_.ReadRawDicom(resource.GetIdentifier())); + raw->Copy(target_); + } + success_ = true; } } @@ -482,7 +486,12 @@ try { mime = MimeType_Dicom; - context_.ReadDicom(content, instanceId); + + { + std::unique_ptr<StorageAreaDataSource::Range> raw(context_.ReadRawDicom(instanceId)); + raw->Copy(content); + } + LookupTime(time, context_, instanceId, ResourceType_Instance, MetadataType_Instance_ReceptionDate); return true; }
--- a/OrthancServer/Sources/ServerContext.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerContext.cpp Tue May 26 15:52:35 2026 +0200 @@ -25,12 +25,17 @@ #include "ServerContext.h" #include "../../OrthancFramework/Sources/Cache/SharedArchive.h" +#include "../../OrthancFramework/Sources/Constants.h" +#include "../../OrthancFramework/Sources/DataSource/DataSourceReader.h" +#include "../../OrthancFramework/Sources/DataSource/DicomSequentialReader.h" +#include "../../OrthancFramework/Sources/DataSource/TranscoderDataSource.h" #include "../../OrthancFramework/Sources/DicomFormat/DicomElement.h" #include "../../OrthancFramework/Sources/DicomFormat/DicomImageInformation.h" #include "../../OrthancFramework/Sources/DicomFormat/DicomStreamReader.h" #include "../../OrthancFramework/Sources/DicomNetworking/DicomStoreUserConnection.h" #include "../../OrthancFramework/Sources/DicomParsing/DicomModification.h" #include "../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h" +#include "../../OrthancFramework/Sources/DicomParsing/TranscodingCallable.h" #include "../../OrthancFramework/Sources/FileStorage/StorageAccessor.h" #include "../../OrthancFramework/Sources/HttpServer/FilesystemHttpSender.h" #include "../../OrthancFramework/Sources/HttpServer/HttpStreamTranscoder.h" @@ -38,19 +43,21 @@ #include "../../OrthancFramework/Sources/Logging.h" #include "../../OrthancFramework/Sources/MallocMemoryBuffer.h" #include "../../OrthancFramework/Sources/MetricsRegistry.h" +#include "../../OrthancFramework/Sources/MultiThreading/ThreadPool.h" #include "../../OrthancFramework/Sources/SerializationToolbox.h" #include "../Plugins/Engine/OrthancPlugins.h" #include "DicomInstanceToStore.h" #include "OrthancConfiguration.h" #include "OrthancRestApi/OrthancRestApi.h" +#include "OutgoingDicomInstance.h" #include "ResourceFinder.h" #include "Search/DatabaseLookup.h" #include "ServerJobs/OrthancJobUnserializer.h" #include "ServerToolbox.h" +#include "ServerTranscoder.h" +#include "SimpleInstanceOrdering.h" #include "StorageCommitmentReports.h" -#include "OutgoingDicomInstance.h" -#include "SimpleInstanceOrdering.h" #include <dcmtk/dcmdata/dcfilefo.h> #include <dcmtk/dcmnet/dimse.h> @@ -63,8 +70,6 @@ # include <malloc.h> #endif -static size_t DICOM_CACHE_SIZE = static_cast<size_t>(128) * 1024 * 1024; // 128 MB - /** * IMPORTANT: We make the assumption that the same instance of @@ -359,19 +364,36 @@ void ServerContext::PublishCacheMetrics() { + uint64_t tasksMaximumMemory; + uint64_t tasksCurrentMemory; + unsigned int tasksReservations; + size_t cacheCapacity; + size_t cacheCurrentCount; + size_t cacheCurrentSize; + + storageAreaReader_->GetStatistics(tasksMaximumMemory, tasksCurrentMemory, tasksReservations, + cacheCapacity, cacheCurrentCount, cacheCurrentSize); + metricsRegistry_->SetFloatValue("orthanc_storage_cache_size_mb", + static_cast<float>(cacheCurrentSize) / static_cast<float>(MEGABYTE)); + metricsRegistry_->SetIntegerValue("orthanc_storage_cache_count", cacheCurrentCount); + + dicomReader_->GetStatistics(tasksMaximumMemory, tasksCurrentMemory, tasksReservations, + cacheCapacity, cacheCurrentCount, cacheCurrentSize); metricsRegistry_->SetFloatValue("orthanc_dicom_cache_size_mb", - static_cast<float>(dicomCache_.GetCurrentSize()) / static_cast<float>(1024 * 1024)); - metricsRegistry_->SetIntegerValue("orthanc_dicom_cache_count", - static_cast<int64_t>(dicomCache_.GetNumberOfItems())); - metricsRegistry_->SetFloatValue("orthanc_storage_cache_size_mb", - static_cast<float>(storageCache_.GetCurrentSize()) / static_cast<float>(1024 * 1024)); - metricsRegistry_->SetIntegerValue("orthanc_storage_cache_count", - static_cast<int64_t>(storageCache_.GetNumberOfItems())); + static_cast<float>(cacheCurrentSize) / static_cast<float>(MEGABYTE)); + metricsRegistry_->SetIntegerValue("orthanc_dicom_cache_count", cacheCurrentCount); + + transcoderReader_->GetStatistics(tasksMaximumMemory, tasksCurrentMemory, tasksReservations, + cacheCapacity, cacheCurrentCount, cacheCurrentSize); + metricsRegistry_->SetFloatValue("orthanc_transcoder_cache_size_mb", + static_cast<float>(cacheCurrentSize) / static_cast<float>(MEGABYTE)); + metricsRegistry_->SetIntegerValue("orthanc_transcoder_cache_count", cacheCurrentCount); } ServerContext::ServerContext(IDatabaseWrapper& database, IPluginStorageArea& area, + ServerTranscoder* transcoder /* takes ownership */, bool unitTesting, size_t maxCompletedJobs, bool readOnly) : @@ -380,7 +402,6 @@ compressionEnabled_(false), storeMD5_(true), largeDicomThrottler_(1), - dicomCache_(DICOM_CACHE_SIZE), mainLua_(*this), filterLua_(*this), luaListener_(*this), @@ -396,6 +417,8 @@ isExecuteLuaEnabled_(false), isRestApiWriteToFileSystemEnabled_(false), overwriteInstances_(OverwriteInstancesMode_Never), + transcoder_(transcoder), + transcodeDicomProtocol_(true), isIngestTranscoding_(false), ingestTranscodingOfUncompressed_(true), ingestTranscodingOfCompressed_(true), @@ -407,6 +430,8 @@ { try { + bool checkMD5 = true; + { OrthancConfiguration::ReaderLock lock; @@ -532,6 +557,8 @@ SetAcceptedSopClasses(acceptedSopClasses, rejectedSopClasses); defaultDicomRetrieveMethod_ = StringToRetrieveMethod(lock.GetConfiguration().GetDicomDefaultRetrieveMethod()); + + checkMD5 = lock.GetConfiguration().GetBooleanParameter("CheckMD5", true); // TODO-Streaming - Parameter } jobsEngine_.SetThreadSleep(unitTesting ? 20 : 200); @@ -546,6 +573,57 @@ #else LOG(INFO) << "Your platform does not support malloc_trim(), not starting the memory trimming thread"; #endif + + // For streaming + { + boost::shared_ptr<ThreadPool> pool(new ThreadPool); + pool->SetCountThreads(4); // TODO-Streaming - Parameter + pool->SetLoggingThreadName("STORAGE"); + pool->SetDequeueTimeout(100); + pool->Start(); + + storageAreaReader_.reset(new DataSourceReader(pool, new StorageAreaDataSource(area_, checkMD5))); + storageAreaReader_->SetCapacity(1); // Put highest pressure + //storageAreaReader_->SetCapacity(1 * GIGABYTE); // 1 GB - TODO-Streaming - Parameter + storageAreaReader_->CreateCache(256 * MEGABYTE); // 256 MB - TODO-Streaming - Parameter + } + + { + boost::shared_ptr<ThreadPool> pool(new ThreadPool); + pool->SetCountThreads(2); // TODO-Streaming - Parameter + pool->SetLoggingThreadName("DICOM"); + pool->SetDequeueTimeout(100); + pool->Start(); + + dicomReader_.reset(new DataSourceReader(pool, new DicomDataSource(storageAreaReader_))); + dicomReader_->SetCapacity(1); // Put highest pressure + //dicomReader_->SetCapacity(1 * GIGABYTE); // 1 GB - TODO-Streaming - Parameter + dicomReader_->CreateCache(32 * MEGABYTE); // 256 MB - TODO-Streaming - Parameter + } + + if (transcoder_.get() != NULL) + { + transcoderThreadPool_.reset(new ThreadPool); + transcoderThreadPool_->SetCountThreads(2); // TODO-Streaming - Parameter + transcoderThreadPool_->SetLoggingThreadName("TRANSCODER"); + transcoderThreadPool_->SetDequeueTimeout(100); + transcoderThreadPool_->Start(); + + transcoderReader_.reset(new DataSourceReader(transcoderThreadPool_, new TranscoderDataSource(transcoder_, storageAreaReader_))); + transcoderReader_->SetCapacity(1); // Put highest pressure + //transcoderReader_->SetCapacity(1 * GIGABYTE); // 1 GB - TODO-Streaming - Parameter + transcoderReader_->CreateCache(256 * MEGABYTE); // 256 MB - TODO-Streaming - Parameter + } + + { + std::unique_ptr<ThreadPool> pool(new ThreadPool); + pool->SetCountThreads(16); // TODO-Streaming - Parameter + pool->SetLoggingThreadName("READER"); + pool->SetDequeueTimeout(100); + pool->Start(); + + sequentialReaderThreadPool_.reset(pool.release()); + } } catch (OrthancException&) { @@ -638,7 +716,7 @@ FileContentType type, const std::string& customData) { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); + StorageAccessor accessor(area_, GetMetricsRegistry()); accessor.Remove(fileUuid, type, customData); } @@ -700,7 +778,7 @@ try { MetricsRegistry::Timer timer(GetMetricsRegistry(), "orthanc_store_dicom_duration_ms"); - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); + StorageAccessor accessor(area_, GetMetricsRegistry()); DicomInstanceHasher hasher(summary); resultPublicId = hasher.HashInstance(); @@ -791,12 +869,6 @@ return result; } - // Remove the file from the DicomCache - // If this is a new instance, this is a no-op - // If overwriteMode == OverwriteInstancesMode_IfChanged and the file has not changed, we won't reach this point - // If overwriteMode == OverwriteInstancesMode_Always, then, let's always invalidate even if it is meaningful only if the file has changed (note: there is a TODO about cache and MD5) - dicomCache_.Invalidate(resultPublicId); - // TODO Should we use "gzip" instead? CompressionType compression = (compressionEnabled_ ? CompressionType_ZlibWithSize : CompressionType_None); @@ -1074,7 +1146,7 @@ imageInfo.ThrowIfInvalidFrameSize(); } - if (GetTranscoder().Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew /* allow new SOP instance UID */)) + if (GetTranscoder()->Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew /* allow new SOP instance UID */)) { std::unique_ptr<ParsedDicomFile> tmp(transcoded.ReleaseAsParsedDicomFile()); @@ -1110,8 +1182,15 @@ const FileInfo& attachment, const std::string& filename) { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); - accessor.AnswerFile(output, attachment, GetFileContentMime(attachment.GetContentType()), filename); + std::unique_ptr<StorageAreaDataSource::Range> range(ReadAttachment(attachment, true /* uncompress */)); + + BufferHttpSender sender; + sender.SetBuffer(range->GetData(), range->GetSize()); + sender.SetContentType(GetFileContentMime(attachment.GetContentType())); + sender.SetContentFilename(filename.c_str()); + + HttpStreamTranscoder transcoder(sender, CompressionType_None); + output.AnswerStream(transcoder); } @@ -1138,13 +1217,15 @@ return; } - std::string content; - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); - accessor.Read(content, attachment); + StorageAccessor accessor(area_, GetMetricsRegistry()); FileInfo modified; - accessor.Write(modified, content.empty() ? NULL : content.c_str(), content.size(), attachmentType, compression, storeMD5_, NULL); + + { + std::unique_ptr<StorageAreaDataSource::Range> range(ReadAttachment(attachment, true /* uncompress */)); + accessor.Write(modified, range->GetData(), range->GetSize(), attachmentType, compression, storeMD5_, NULL); + } try { @@ -1272,15 +1353,14 @@ if (LookupAttachment(attachment, FileContentType_DicomUntilPixelData, instanceAttachments)) { - std::string dicom; + std::unique_ptr<DicomDataSource::Dicom> dicom( + DicomDataSource::Execute(*dicomReader_, DicomDataSource::CreateWholeRequest(attachment))); { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); - accessor.Read(dicom, attachment); + DicomDataSource::Dicom::Lock lock(*dicom); + OrthancConfiguration::DefaultDicomDatasetToJson(result, lock.GetContent(), ignoreTagLength); } - ParsedDicomFile parsed(dicom); - OrthancConfiguration::DefaultDicomDatasetToJson(result, parsed, ignoreTagLength); InjectEmptyPixelData(result); } else @@ -1321,17 +1401,15 @@ * this case cannot be used if "StorageCompression" option is * "true". **/ - - std::string dicom; - + + std::unique_ptr<DicomDataSource::Dicom> dicom( + DicomDataSource::Execute(*dicomReader_, DicomDataSource::CreateUntilPixelDataRequest(attachment, pixelDataOffset))); + { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); - accessor.ReadStartRange(dicom, attachment, pixelDataOffset); + DicomDataSource::Dicom::Lock lock(*dicom); + OrthancConfiguration::DefaultDicomDatasetToJson(result, lock.GetContent(), ignoreTagLength); } - assert(dicom.size() == pixelDataOffset); - ParsedDicomFile parsed(dicom); - OrthancConfiguration::DefaultDicomDatasetToJson(result, parsed, ignoreTagLength); InjectEmptyPixelData(result); } else if (ignoreTagLength.empty() && @@ -1344,15 +1422,10 @@ * "/tools/invalidate-tags" or to one flavors of * "/.../.../reconstruct" will disable this case. **/ - - std::string dicomAsJson; - { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); - accessor.Read(dicomAsJson, attachment); - } + std::unique_ptr<StorageAreaDataSource::Range> dicomAsJson(ReadAttachment(attachment, true /* uncompress */)); - if (!Toolbox::ReadJson(result, dicomAsJson)) + if (!Toolbox::ReadJson(result, dicomAsJson->GetData(), dicomAsJson->GetSize())) { throw OrthancException(ErrorCode_CorruptedFile, "Corrupted DICOM-as-JSON attachment of instance: " + instancePublicId); @@ -1366,25 +1439,32 @@ * file from the storage area. **/ - std::string dicom; - ReadDicom(dicom, instancePublicId); + if (!LookupAttachment(attachment, FileContentType_Dicom, instanceAttachments)) + { + throw OrthancException(ErrorCode_UnknownResource); + } - ParsedDicomFile parsed(dicom); - OrthancConfiguration::DefaultDicomDatasetToJson(result, parsed, ignoreTagLength); + std::unique_ptr<StorageAreaDataSource::Range> range(ReadAttachment(attachment, true /* uncompress */)); + + // 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) { /** - * The pixel data offset was never computed for this + * (*) 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. **/ + ValueRepresentation pixelDataVR; - if (DicomStreamReader::LookupPixelDataOffset(pixelDataOffset, pixelDataVR, dicom) && - pixelDataOffset < dicom.size()) + if (DicomStreamReader::LookupPixelDataOffset( + pixelDataOffset, pixelDataVR, range->GetData(), range->GetSize()) && + pixelDataOffset < range->GetSize()) { index_.OverwriteMetadata(instancePublicId, MetadataType_Instance_PixelDataOffset, boost::lexical_cast<std::string>(pixelDataOffset)); @@ -1394,8 +1474,8 @@ { int64_t newRevision; AddAttachment(newRevision, instancePublicId, ResourceType_Instance, FileContentType_DicomUntilPixelData, - dicom.empty() ? NULL: dicom.c_str(), pixelDataOffset, - false /* no old revision */, -1 /* dummy revision */, "" /* dummy MD5 */); + range->GetData(), pixelDataOffset, + false /* no old revision */, -1 /* dummy revision */, "" /* dummy MD5 */); } } } @@ -1412,227 +1492,91 @@ } - void ServerContext::ReadDicom(std::string& dicom, - std::string& attachmentId, - const std::string& instancePublicId) - { - std::unique_ptr<Semaphore::Locker> dummyLargeDicomLocker; // only the DicomCacheLocker uses a real largeDicomLocker_ - ReadDicomInternal(dicom, attachmentId, instancePublicId, dummyLargeDicomLocker, 0); - } - - void ServerContext::ReadDicomInternal(std::string& dicom, - std::string& attachmentId, - const std::string& instancePublicId, - std::unique_ptr<Semaphore::Locker>& largeDicomLocker, - std::size_t largeDicomThreshold) + 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); } + } - assert(attachment.GetContentType() == FileContentType_Dicom); - attachmentId = attachment.GetUuid(); - if (attachment.GetUncompressedSize() < largeDicomThreshold) // release ASAP (before the read) if we don't plan to hold the lock (https://discourse.orthanc-server.org/t/patch-release-large-dicom-semaphore-lock-early-for-better-performance/6440) - { - largeDicomLocker.reset(NULL); - } - - ReadAttachment(dicom, attachment, true /* uncompress */); + StorageAreaDataSource::Range* ServerContext::ReadAttachment(const FileInfo& attachment, + bool uncompress) + { + return StorageAreaDataSource::Execute( + *storageAreaReader_, StorageAreaDataSource::CreateAttachmentRequest(attachment, uncompress)); } - void ServerContext::ReadDicom(std::string& dicom, - const std::string& instancePublicId) + StorageAreaDataSource::Range* ServerContext::ReadAttachment(const FileInfo& attachment, + const StorageRange& range, + bool uncompress) { - std::string attachmentId; - ReadDicom(dicom, attachmentId, instancePublicId); + return StorageAreaDataSource::ReadRange(*storageAreaReader_, attachment, range, uncompress); } - void ServerContext::ReadDicomInternal(std::string& dicom, - const std::string& instancePublicId, - std::unique_ptr<Semaphore::Locker>& largeDicomLocker, - std::size_t largeDicomThreshold) + + StorageAreaDataSource::Range* ServerContext::ReadRawDicom(const std::string& instancePublicId) { - std::string attachmentId; - ReadDicomInternal(dicom, attachmentId, instancePublicId, largeDicomLocker, largeDicomThreshold); + return ReadAttachment(LookupDicomForInstance(instancePublicId), true /* uncompress */); } - void ServerContext::ReadDicomForHeader(std::string& dicom, - const std::string& instancePublicId) + + DicomDataSource::Dicom* ServerContext::ReadParsedDicom(const std::string& instancePublicId) { - if (!ReadDicomUntilPixelData(dicom, instancePublicId)) - { - ReadDicom(dicom, instancePublicId); - } + return DicomDataSource::Execute(*dicomReader_, DicomDataSource::CreateWholeRequest(LookupDicomForInstance(instancePublicId))); } - bool ServerContext::ReadDicomUntilPixelData(std::string& dicom, - const std::string& instancePublicId) + + DicomDataSource::Dicom* ServerContext::ReadDicomUntilPixelData(const std::string& instancePublicId) { FileInfo attachment; int64_t revision; // Ignored - if (index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_DicomUntilPixelData)) - { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); - accessor.Read(dicom, attachment); - assert(dicom.size() == attachment.GetUncompressedSize()); - - return true; - } - - if (!area_.HasEfficientReadRange()) - { - return false; - } - - if (!index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_Dicom)) - { - throw OrthancException(ErrorCode_InternalError, - "Unable to read the DICOM file of instance " + instancePublicId); - } - - std::string s; - - if (attachment.GetCompressionType() == CompressionType_None && - index_.LookupMetadata(s, instancePublicId, ResourceType_Instance, - MetadataType_Instance_PixelDataOffset) && - !s.empty()) + if (index_.LookupAttachment(attachment, revision, ResourceType_Instance, + instancePublicId, FileContentType_DicomUntilPixelData)) { - uint64_t pixelDataOffset = 0; - - if (SerializationToolbox::ParseUnsignedInteger64(pixelDataOffset, s)) - { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); - - accessor.ReadStartRange(dicom, attachment, pixelDataOffset); - assert(dicom.size() == pixelDataOffset); - - return true; // Success - } - else - { - LOG(ERROR) << "Metadata \"PixelDataOffset\" is corrupted for instance: " << instancePublicId; - } - } - - return false; - } - - - void ServerContext::ReadAttachment(std::string& result, - const FileInfo& attachment, - bool uncompressIfNeeded, - bool skipCache) - { - std::unique_ptr<StorageAccessor> accessor; - - if (skipCache) - { - accessor.reset(new StorageAccessor(area_, GetMetricsRegistry())); + return DicomDataSource::Execute(*dicomReader_, DicomDataSource::CreateWholeRequest(attachment)); } else { - accessor.reset(new StorageAccessor(area_, storageCache_, GetMetricsRegistry())); - } + attachment = LookupDicomForInstance(instancePublicId); + + std::string metadata; - if (uncompressIfNeeded) - { - accessor->Read(result, attachment); - } - else - { - // Do not uncompress the content of the storage area, return the - // raw data - accessor->ReadRaw(result, attachment); - } - } - - void ServerContext::ReadAttachmentRange(std::string &result, - const FileInfo &attachment, - const StorageRange &range, - bool uncompressIfNeeded) - { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); - accessor.ReadRange(result, attachment, range, uncompressIfNeeded); - } - + if (attachment.GetCompressionType() == CompressionType_None && + index_.LookupMetadata(metadata, instancePublicId, ResourceType_Instance, + MetadataType_Instance_PixelDataOffset) && + !metadata.empty()) + { + uint64_t pixelDataOffset = 0; - ServerContext::DicomCacheLocker::DicomCacheLocker(ServerContext& context, - const std::string& instancePublicId) : - context_(context), - instancePublicId_(instancePublicId) - { - accessor_.reset(new ParsedDicomCache::Accessor(context_.dicomCache_, instancePublicId)); - - if (!accessor_->IsValid()) - { - accessor_.reset(NULL); + if (SerializationToolbox::ParseUnsignedInteger64(pixelDataOffset, metadata)) + { + return DicomDataSource::Execute(*dicomReader_, DicomDataSource::CreateUntilPixelDataRequest(attachment, pixelDataOffset)); + } + else + { + LOG(ERROR) << "Metadata \"PixelDataOffset\" is corrupted for instance: " << instancePublicId; + } + } - // Throttle to avoid loading several large DICOM files simultaneously (since the ParsedDicomCache is 128MB, loading multiple 50MB files would throw them out directly after loading) - largeDicomLocker_.reset(new Semaphore::Locker(context.largeDicomThrottler_)); - - // Release the throttle if loading "small" DICOM files (under - // 50MB, which is an arbitrary value) - context_.ReadDicomInternal(buffer_, instancePublicId_, largeDicomLocker_, static_cast<size_t>(50) * 1024 * 1024); - - dicom_.reset(new ParsedDicomFile(buffer_)); - dicomSize_ = buffer_.size(); - } - - assert(accessor_.get() != NULL || - dicom_.get() != NULL); - } - - - ServerContext::DicomCacheLocker::~DicomCacheLocker() - { - if (dicom_.get() != NULL) - { - try - { - context_.dicomCache_.Acquire(instancePublicId_, dicom_.release(), dicomSize_); - } - catch (OrthancException&) // NOLINT(bugprone-empty-catch) - { - } + // Fallback: The pixel data offset is not present or cannot be used, the whole DICOM file must be read + return DicomDataSource::Execute(*dicomReader_, DicomDataSource::CreateWholeRequest(attachment)); } } - ParsedDicomFile& ServerContext::DicomCacheLocker::GetDicom() const - { - if (dicom_.get() != NULL) - { - return *dicom_; - } - else - { - assert(accessor_.get() != NULL); - return accessor_->GetDicom(); - } - } - - const std::string& ServerContext::DicomCacheLocker::GetBuffer() - { - if (buffer_.size() > 0) - { - return buffer_; - } - else - { - context_.ReadDicom(buffer_, instancePublicId_); - return buffer_; - } - } - void ServerContext::SetStoreMD5ForAttachments(bool storeMD5) { LOG(INFO) << "Storing MD5 for attachments: " << (storeMD5 ? "yes" : "no"); @@ -1655,7 +1599,7 @@ // TODO Should we use "gzip" instead? CompressionType compression = (compressionEnabled_ ? CompressionType_ZlibWithSize : CompressionType_None); - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); + StorageAccessor accessor(area_, GetMetricsRegistry()); assert(attachmentType != FileContentType_Dicom && attachmentType != FileContentType_DicomUntilPixelData); // this method can not be used to store instances @@ -1689,24 +1633,12 @@ const std::string& uuid, ResourceType expectedType) { - if (expectedType == ResourceType_Instance) - { - // remove the file from the DicomCache - dicomCache_.Invalidate(uuid); - } - return index_.DeleteResource(remainingAncestor, uuid, expectedType); } void ServerContext::SignalChange(const ServerIndexChange& change) { - if (change.GetResourceType() == ResourceType_Instance && - change.GetChangeType() == ChangeType_Deleted) - { - dicomCache_.Invalidate(change.GetPublicId()); - } - pendingChanges_.Enqueue(change.Clone()); } @@ -1937,49 +1869,40 @@ } - - - - ImageAccessor* ServerContext::DecodeDicomFrame(const std::string& publicId, + ImageAccessor* ServerContext::DecodeDicomFrame(const std::string& instancePublicId, unsigned int frameIndex) { - ServerContext::DicomCacheLocker locker(*this, publicId); - std::unique_ptr<ImageAccessor> decoded(GetTranscoder().DecodeFrame(locker.GetDicom(), locker.GetBuffer().c_str(), locker.GetBuffer().size(), frameIndex)); + FileInfo attachment; + int64_t revision; + + if (!index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_Dicom)) + { + throw OrthancException(ErrorCode_InternalError, + "Unable to read attachment " + EnumerationToString(FileContentType_Dicom) + + " of instance " + instancePublicId); + } + + std::unique_ptr<ImageAccessor> decoded; + + decoded.reset(GetTranscoder()->DecodeFrame(dicomReader_, storageAreaReader_, transcoderReader_, attachment, frameIndex)); if (decoded.get() == NULL) { OrthancConfiguration::ReaderLock configLock; if (configLock.GetConfiguration().IsWarningEnabled(Warnings_003_DecoderFailure)) { - LOG(WARNING) << "W003: Unable to decode frame " << frameIndex << " from instance " << publicId; + LOG(WARNING) << "W003: Unable to decode frame " << frameIndex << " from instance " << instancePublicId; } - return NULL; + + throw OrthancException(ErrorCode_NotImplemented); } - - return decoded.release(); + else + { + return decoded.release(); + } } - ImageAccessor* ServerContext::DecodeDicomFrame(const DicomInstanceToStore& dicom, - unsigned int frameIndex) - { - return GetTranscoder().DecodeFrame(dicom.GetParsedDicomFile(), - dicom.GetBufferData(), - dicom.GetBufferSize(), - frameIndex); - - } - - - ImageAccessor* ServerContext::DecodeDicomFrame(const void* dicom, - size_t size, - unsigned int frameIndex) - { - std::unique_ptr<ParsedDicomFile> instance(new ParsedDicomFile(dicom, size)); - return GetTranscoder().DecodeFrame(*instance, dicom, size, frameIndex); - } - - void ServerContext::PerformCStoreWithTranscoding(std::string& sopClassUid, std::string& sopInstanceUid, DicomStoreUserConnection& connection, @@ -2035,37 +1958,6 @@ } - bool ServerContext::TranscodeWithCache(std::string& target, - const std::string& source, - const std::string& sourceInstanceId, - const std::string& attachmentId, - DicomTransferSyntax targetSyntax) - { - StorageCache::Accessor cacheAccessor(storageCache_); - - if (!cacheAccessor.FetchTranscodedInstance(target, attachmentId, targetSyntax)) - { - IDicomTranscoder::DicomImage sourceDicom; - sourceDicom.SetExternalBuffer(source); - - IDicomTranscoder::DicomImage targetDicom; - std::set<DicomTransferSyntax> syntaxes; - syntaxes.insert(targetSyntax); - - if (GetTranscoder().Transcode(targetDicom, sourceDicom, syntaxes, TranscodingSopInstanceUidMode_AllowNew)) - { - cacheAccessor.AddTranscodedInstance(attachmentId, targetSyntax, reinterpret_cast<const char*>(targetDicom.GetBufferData()), targetDicom.GetBufferSize()); - target = std::string(reinterpret_cast<const char*>(targetDicom.GetBufferData()), targetDicom.GetBufferSize()); - return true; - } - - return false; - } - - return true; - } - - const std::string& ServerContext::GetDeidentifiedContent(const DicomElement &element) const { static const std::string redactedContent = "*** POTENTIAL PHI ***"; @@ -2254,47 +2146,49 @@ } - void ServerContext::SetTranscoder(ServerTranscoder* transcoder) + const boost::shared_ptr<ServerTranscoder>& ServerContext::GetTranscoder() const { - std::unique_ptr<ServerTranscoder> protection(transcoder); - - if (transcoder == NULL) + if (transcoder_.get() != NULL) { - throw OrthancException(ErrorCode_NullPointer); - } - else if (transcoder_.get() != NULL) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); + return transcoder_; } else { - transcoder_.reset(protection.release()); + throw OrthancException(ErrorCode_BadSequenceOfCalls, "No transcoder is available"); } } - ServerTranscoder& ServerContext::GetTranscoder() const + Future* ServerContext::SubmitTranscodingRequest(TranscodingCallable* callable) { - if (transcoder_.get() != NULL) + if (callable == NULL) { - return *transcoder_; + throw OrthancException(ErrorCode_NullPointer); } - else - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } + + callable->SetTranscoder(transcoder_); + + return transcoderThreadPool_->Submit(callable); } - void ServerContext::ResetTranscoder() + DicomSequentialReader* ServerContext::CreateDicomSequentialReader() { - if (transcoder_.get() == NULL) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - else - { - transcoder_.reset(NULL); - } + return DicomSequentialReader::CreateForOriginal( + sequentialReaderThreadPool_, dicomReader_, + 4 /* TODO-Streaming: Parameter */, + 0 /* TODO-Streaming: Parameter */); + } + + + DicomSequentialReader* ServerContext::CreateTranscodedSequentialReader(DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + bool hasLossyQuality, + unsigned int lossyQuality) + { + return DicomSequentialReader::CreateForTranscoded( + sequentialReaderThreadPool_, targetSyntax, mode, hasLossyQuality, lossyQuality, transcoderReader_, + 4 /* TODO-Streaming: Parameter */, + 0 /* TODO-Streaming: Parameter */); } }
--- a/OrthancServer/Sources/ServerContext.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerContext.h Tue May 26 15:52:35 2026 +0200 @@ -28,25 +28,30 @@ #include "OrthancHttpHandler.h" #include "ServerIndex.h" #include "ServerJobs/IStorageCommitmentFactory.h" -#include "ServerTranscoder.h" +#include "../../OrthancFramework/Sources/DataSource/DicomDataSource.h" +#include "../../OrthancFramework/Sources/DataSource/StorageAreaDataSource.h" #include "../../OrthancFramework/Sources/DicomParsing/DicomModification.h" -#include "../../OrthancFramework/Sources/DicomParsing/ParsedDicomCache.h" -#include "../../OrthancFramework/Sources/FileStorage/StorageAccessor.h" -#include "../../OrthancFramework/Sources/FileStorage/StorageCache.h" #include "../../OrthancFramework/Sources/JobsEngine/JobsEngine.h" +#include "../../OrthancFramework/Sources/MetricsRegistry.h" +#include "../../OrthancFramework/Sources/MultiThreading/Future.h" #include "../../OrthancFramework/Sources/MultiThreading/Semaphore.h" namespace Orthanc { class DicomElement; + class DicomSequentialReader; class DicomStoreUserConnection; + class IExecutorService; class OrthancPlugins; + class ServerTranscoder; class SharedArchive; class StorageCommitmentReports; + class ThreadPool; + class TranscodingCallable; - + /** * This class is responsible for maintaining the storage area on the * filesystem (including compression), as well as the index of the @@ -192,13 +197,11 @@ ServerIndex index_; IPluginStorageArea& area_; - StorageCache storageCache_; bool compressionEnabled_; bool storeMD5_; Semaphore largeDicomThrottler_; // New in Orthanc 1.9.0 (notably for very large DICOM files in WSI) - ParsedDicomCache dicomCache_; LuaScripting mainLua_; LuaScripting filterLua_; @@ -246,7 +249,7 @@ std::unique_ptr<StorageCommitmentReports> storageCommitmentReports_; - std::unique_ptr<ServerTranscoder> transcoder_; + boost::shared_ptr<ServerTranscoder> transcoder_; bool transcodeDicomProtocol_; bool isIngestTranscoding_; DicomTransferSyntax ingestTransferSyntax_; @@ -285,31 +288,17 @@ boost::posix_time::ptime serverStartTimeUtc_; - public: - class DicomCacheLocker : public boost::noncopyable - { - private: - ServerContext& context_; - std::string instancePublicId_; - std::unique_ptr<ParsedDicomCache::Accessor> accessor_; - std::unique_ptr<ParsedDicomFile> dicom_; - size_t dicomSize_; - std::unique_ptr<Semaphore::Locker> largeDicomLocker_; - std::string buffer_; + // For streaming + boost::shared_ptr<DataSourceReader> storageAreaReader_; + boost::shared_ptr<DataSourceReader> dicomReader_; + boost::shared_ptr<ThreadPool> transcoderThreadPool_; + boost::shared_ptr<DataSourceReader> transcoderReader_; + boost::shared_ptr<IExecutorService> sequentialReaderThreadPool_; - public: - DicomCacheLocker(ServerContext& context, - const std::string& instancePublicId); - - ~DicomCacheLocker(); - - ParsedDicomFile& GetDicom() const; - - const std::string& GetBuffer(); - }; - + public: ServerContext(IDatabaseWrapper& database, IPluginStorageArea& area, + ServerTranscoder* transcoder /* takes ownership */, bool unitTesting, size_t maxCompletedJobs, bool readOnly); @@ -326,7 +315,7 @@ void SetMaximumStorageCacheSize(size_t size) { - return storageCache_.SetMaximumSize(size); + // TODO-Streaming } void SetPatientLevelEnabled(bool enabled); @@ -371,7 +360,7 @@ StoreResult TranscodeAndStore(std::string& resultPublicId, DicomInstanceToStore* dicom, - bool isReconstruct = false); + bool isReconstruct = false); // TODO-Streaming: To check void AnswerAttachment(RestApiOutput& output, const FileInfo& fileInfo, @@ -395,42 +384,20 @@ void ReadDicomAsJson(Json::Value& result, const std::string& instancePublicId); // TODO-FIND: Can this be removed? -private: - void ReadDicomInternal(std::string& dicom, - const std::string& instancePublicId, - std::unique_ptr<Semaphore::Locker>& largeDicomLocker, - std::size_t largeDicomThreshold); + FileInfo LookupDicomForInstance(const std::string& instancePublicId); - void ReadDicomInternal(std::string& dicom, - std::string& attachmentId, - const std::string& instancePublicId, - std::unique_ptr<Semaphore::Locker>& largeDicomLocker, - std::size_t largeDicomThreshold); - -public: - void ReadDicom(std::string& dicom, - const std::string& instancePublicId); + StorageAreaDataSource::Range* ReadAttachment(const FileInfo& attachment, + bool uncompress); - void ReadDicom(std::string& dicom, - std::string& attachmentId, - const std::string& instancePublicId); - - void ReadDicomForHeader(std::string& dicom, - const std::string& instancePublicId); - - bool ReadDicomUntilPixelData(std::string& dicom, - const std::string& instancePublicId); + StorageAreaDataSource::Range* ReadAttachment(const FileInfo& attachment, + const StorageRange& range, + bool uncompress); - // This method is for low-level operations on "/instances/.../attachments/..." - void ReadAttachment(std::string& result, - const FileInfo& attachment, - bool uncompressIfNeeded, - bool skipCache = false); + StorageAreaDataSource::Range* ReadRawDicom(const std::string& instancePublicId); - void ReadAttachmentRange(std::string& result, - const FileInfo& attachment, - const StorageRange& range, - bool uncompressIfNeeded); + DicomDataSource::Dicom* ReadParsedDicom(const std::string& instancePublicId); + + DicomDataSource::Dicom* ReadDicomUntilPixelData(const std::string& instancePublicId); void SetStoreMD5ForAttachments(bool storeMD5); @@ -583,13 +550,6 @@ ImageAccessor* DecodeDicomFrame(const std::string& publicId, unsigned int frameIndex); - ImageAccessor* DecodeDicomFrame(const DicomInstanceToStore& dicom, - unsigned int frameIndex); - - ImageAccessor* DecodeDicomFrame(const void* dicom, - size_t size, - unsigned int frameIndex); - void PerformCStoreWithTranscoding(std::string& sopClassUid, std::string& sopInstanceUid, DicomStoreUserConnection& connection, @@ -598,12 +558,6 @@ const std::string& moveOriginatorAet, uint16_t moveOriginatorId); - virtual bool TranscodeWithCache(std::string& target, - const std::string& source, - const std::string& sourceInstanceId, - const std::string& attachmentId, // for the storage cache - DicomTransferSyntax targetSyntax); - bool IsTranscodeDicomProtocol() const { return transcodeDicomProtocol_; @@ -635,10 +589,15 @@ void PublishCacheMetrics(); - void SetTranscoder(ServerTranscoder* transcoder /* takes ownership */); + const boost::shared_ptr<ServerTranscoder>& GetTranscoder() const; + + Future* SubmitTranscodingRequest(TranscodingCallable* callable); - ServerTranscoder& GetTranscoder() const; + DicomSequentialReader* CreateDicomSequentialReader(); - void ResetTranscoder(); + DicomSequentialReader* CreateTranscodedSequentialReader(DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + bool hasLossyQuality, + unsigned int lossyQuality); }; }
--- a/OrthancServer/Sources/ServerJobs/MergeStudyJob.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/MergeStudyJob.cpp Tue May 26 15:52:35 2026 +0200 @@ -97,8 +97,8 @@ try { - ServerContext::DicomCacheLocker locker(GetContext(), instance); - modified.reset(locker.GetDicom().Clone(true)); + std::unique_ptr<DicomDataSource::Dicom> dicom(GetContext().ReadParsedDicom(instance)); + modified.reset(dicom->Clone()); } catch (OrthancException&) { @@ -201,8 +201,10 @@ DicomMap dicom; { - ServerContext::DicomCacheLocker locker(GetContext(), instances.front()); - OrthancConfiguration::DefaultExtractDicomSummary(dicom, locker.GetDicom()); + std::unique_ptr<DicomDataSource::Dicom> loadedDicom(GetContext().ReadParsedDicom(instances.front())); + DicomDataSource::Dicom::Lock lock(*loadedDicom); + + OrthancConfiguration::DefaultExtractDicomSummary(dicom, lock.GetContent()); } const std::set<DicomTag> moduleTags = removals_;
--- a/OrthancServer/Sources/ServerJobs/Operations/DicomInstanceOperationValue.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/Operations/DicomInstanceOperationValue.cpp Tue May 26 15:52:35 2026 +0200 @@ -30,6 +30,7 @@ { void DicomInstanceOperationValue::ReadDicom(std::string& dicom) const { - context_.ReadDicom(dicom, id_); + std::unique_ptr<StorageAreaDataSource::Range> raw(context_.ReadRawDicom(id_)); + raw->Copy(dicom); } }
--- a/OrthancServer/Sources/ServerJobs/Operations/ModifyInstanceOperation.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/Operations/ModifyInstanceOperation.cpp Tue May 26 15:52:35 2026 +0200 @@ -87,8 +87,8 @@ std::unique_ptr<ParsedDicomFile> modified; { - ServerContext::DicomCacheLocker lock(context_, instance.GetId()); - modified.reset(lock.GetDicom().Clone(true)); + std::unique_ptr<DicomDataSource::Dicom> dicom(context_.ReadParsedDicom(instance.GetId())); + modified.reset(dicom->Clone()); } try
--- a/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp Tue May 26 15:52:35 2026 +0200 @@ -29,6 +29,7 @@ #include "../../../OrthancFramework/Sources/Logging.h" #include "../../../OrthancFramework/Sources/SerializationToolbox.h" #include "../ServerContext.h" +#include "../ServerTranscoder.h" #include <dcmtk/dcmdata/dcfilefo.h> @@ -73,7 +74,7 @@ IDicomTranscoder::DicomImage source, transcoded; source.SetExternalBuffer(dicom); - if (context_.GetTranscoder().Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew)) + if (context_.GetTranscoder()->Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew)) { body.assign(reinterpret_cast<const char*>(transcoded.GetBufferData()), transcoded.GetBufferSize());
--- a/OrthancServer/Sources/ServerJobs/ResourceModificationJob.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/ResourceModificationJob.cpp Tue May 26 15:52:35 2026 +0200 @@ -24,10 +24,12 @@ #include "../PrecompiledHeadersServer.h" #include "ResourceModificationJob.h" +#include "../../../OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h" #include "../../../OrthancFramework/Sources/Logging.h" #include "../../../OrthancFramework/Sources/SerializationToolbox.h" #include "../DicomInstanceToStore.h" #include "../ServerContext.h" +#include "../ServerTranscoder.h" #include <dcmtk/dcmdata/dcfilefo.h> #include <dcmtk/dcmdata/dcdeftag.h> @@ -185,8 +187,10 @@ { for (std::set<std::string>::const_iterator it = instancesToReconstruct_.begin(); it != instancesToReconstruct_.end(); ++it) { - ServerContext::DicomCacheLocker locker(GetContext(), *it); - const ParsedDicomFile& modifiedDicom = locker.GetDicom(); + std::unique_ptr<DicomDataSource::Dicom> dicom(GetContext().ReadParsedDicom(*it)); + DicomDataSource::Dicom::Lock lock(*dicom); + + const ParsedDicomFile& modifiedDicom = lock.GetContent(); GetContext().GetIndex().ReconstructInstance(modifiedDicom, false, ResourceType_Instance /* dummy */); } @@ -219,8 +223,10 @@ try { - ServerContext::DicomCacheLocker locker(GetContext(), instance); - const ParsedDicomFile& original = locker.GetDicom(); + std::unique_ptr<DicomDataSource::Dicom> dicom(GetContext().ReadParsedDicom(instance)); + DicomDataSource::Dicom::Lock lock(*dicom); + + const ParsedDicomFile& original = lock.GetContent(); originalHasher.reset(new DicomInstanceHasher(original.GetHasher())); modified.reset(original.Clone(true)); @@ -262,7 +268,7 @@ source.AcquireParsed(*modified); // "modified" is invalid below this point IDicomTranscoder::DicomImage transcoded; - if (GetContext().GetTranscoder().Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew)) + if (GetContext().GetTranscoder()->Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew)) { modified.reset(transcoded.ReleaseAsParsedDicomFile());
--- a/OrthancServer/Sources/ServerJobs/SplitStudyJob.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/SplitStudyJob.cpp Tue May 26 15:52:35 2026 +0200 @@ -72,8 +72,8 @@ try { - ServerContext::DicomCacheLocker locker(GetContext(), instance); - modified.reset(locker.GetDicom().Clone(true)); + std::unique_ptr<DicomDataSource::Dicom> dicom(GetContext().ReadParsedDicom(instance)); + modified.reset(dicom->Clone()); } catch (OrthancException&) {
--- a/OrthancServer/Sources/ServerJobs/StorageCommitmentScpJob.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/StorageCommitmentScpJob.cpp Tue May 26 15:52:35 2026 +0200 @@ -290,9 +290,12 @@ // Make sure that the DICOM file can be re-read by DCMTK // from the file storage, and that the actual SOP // class/instance UIDs do match - ServerContext::DicomCacheLocker locker(context_, orthancId[0]); - if (locker.GetDicom().GetTagValue(a, DICOM_TAG_SOP_CLASS_UID) && - locker.GetDicom().GetTagValue(b, DICOM_TAG_SOP_INSTANCE_UID) && + + std::unique_ptr<DicomDataSource::Dicom> dicom(context_.ReadParsedDicom(orthancId[0])); + DicomDataSource::Dicom::Lock lock(*dicom); + + if (lock.GetContent().GetTagValue(a, DICOM_TAG_SOP_CLASS_UID) && + lock.GetContent().GetTagValue(b, DICOM_TAG_SOP_INSTANCE_UID) && b == sopInstanceUids_[index]) { if (a == sopClassUids_[index])
--- a/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp Tue May 26 15:52:35 2026 +0200 @@ -22,10 +22,12 @@ #include "ThreadedInstancesLoader.h" -#include "../ServerContext.h" +#include "../../../OrthancFramework/Sources/DataSource/StorageAreaDataSource.h" +#include "../../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h" +#include "../../../OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h" #include "../../../OrthancFramework/Sources/Logging.h" -#include "../../../OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h" -#include "../../../OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.h" +#include "../ServerContext.h" +#include "../ServerTranscoder.h" static boost::mutex loaderThreadsCounterMutex; static uint32_t loaderThreadsCounter = 0; @@ -176,7 +178,12 @@ try { boost::shared_ptr<std::string> dicomContent(new std::string()); - that->context_.ReadAttachment(*dicomContent, instanceToPreload->GetFileInfo(), true); + + { + std::unique_ptr<StorageAreaDataSource::Range> tmp( + that->context_.ReadAttachment(instanceToPreload->GetFileInfo(), true /* uncompress */)); + tmp->Copy(*dicomContent); + } if (that->transcode_) { @@ -259,7 +266,7 @@ IDicomTranscoder::DicomImage source, transcoded; source.SetExternalBuffer(sourceBuffer); - if (context_.GetTranscoder().Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew, lossyQuality_)) + if (context_.GetTranscoder()->Transcode(transcoded, source, syntaxes, TranscodingSopInstanceUidMode_AllowNew, lossyQuality_)) { transcodedBuffer.assign(reinterpret_cast<const char*>(transcoded.GetBufferData()), transcoded.GetBufferSize()); return true;
--- a/OrthancServer/Sources/ServerToolbox.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerToolbox.cpp Tue May 26 15:52:35 2026 +0200 @@ -294,26 +294,31 @@ if (limitToThisLevelDicomTags && instances.size() > 0) // in this case, we only need to rebuild one instance ! { - ServerContext::DicomCacheLocker locker(context, instances.front()); - context.GetIndex().ReconstructInstance(locker.GetDicom(), true, limitToLevel); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(instances.front())); + DicomDataSource::Dicom::Lock lock(*dicom); + + context.GetIndex().ReconstructInstance(lock.GetContent(), true, limitToLevel); } else { for (std::list<std::string>::const_iterator it = instances.begin(); it != instances.end(); ++it) { - ServerContext::DicomCacheLocker locker(context, *it); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(*it)); + DicomDataSource::Dicom::Lock lock(*dicom); // Delay the reconstruction of DICOM-as-JSON to its next access through "ServerContext" context.GetIndex().DeleteAttachment(*it, FileContentType_DicomAsJson, false /* no revision */, -1 /* dummy revision */, "" /* dummy MD5 */); - context.GetIndex().ReconstructInstance(locker.GetDicom(), false, ResourceType_Instance /* dummy */); + context.GetIndex().ReconstructInstance(lock.GetContent(), false, ResourceType_Instance /* dummy */); if (reconstructFiles) { + std::unique_ptr<ParsedDicomFile> clone(lock.GetContent().Clone(true)); + std::string resultPublicId; // ignored - std::unique_ptr<DicomInstanceToStore> dicomInstancetoStore(DicomInstanceToStore::CreateFromParsedDicomFile(locker.GetDicom())); + std::unique_ptr<DicomInstanceToStore> dicomInstancetoStore(DicomInstanceToStore::CreateFromParsedDicomFile(*clone)); // TODO: TranscodeAndStore and specifically ServerIndex::Store have been "poluted" by the isReconstruct parameter // we should very likely refactor it
--- a/OrthancServer/Sources/ServerTranscoder.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerTranscoder.cpp Tue May 26 15:52:35 2026 +0200 @@ -24,21 +24,91 @@ #include "PrecompiledHeadersServer.h" #include "ServerTranscoder.h" +#include "../../OrthancFramework/Sources/Cache/LeastRecentlyUsedIndex.h" +#include "../../OrthancFramework/Sources/DataSource/DicomDataSource.h" +#include "../../OrthancFramework/Sources/DataSource/StorageAreaDataSource.h" +#include "../../OrthancFramework/Sources/DataSource/TranscoderDataSource.h" #include "../../OrthancFramework/Sources/DicomParsing/DcmtkTranscoder.h" #include "../../OrthancFramework/Sources/Logging.h" #include "../../OrthancFramework/Sources/OrthancException.h" #include "../Plugins/Engine/OrthancPlugins.h" +#include "DicomInstanceToStore.h" #include "OrthancConfiguration.h" +#include <boost/thread.hpp> #include <dcmtk/dcmdata/dcfilefo.h> +static const size_t MAX_CACHE_SIZE = 10000; + namespace Orthanc { + namespace + { + enum WorkingSource + { + WorkingSource_Unknown, + WorkingSource_Builtin, + WorkingSource_PluginsDecoder, + WorkingSource_PluginsTranscoder + }; + } + + class ServerTranscoder::PImpl + { + private: + boost::mutex mutex_; + LeastRecentlyUsedIndex<std::string, WorkingSource> workingSources_; + + public: + PImpl() + { + } + + WorkingSource LookupWorkingSource(const std::string& attachmentId) + { + boost::mutex::scoped_lock lock(mutex_); + + WorkingSource source; + if (workingSources_.Contains(attachmentId, source)) + { + return source; + } + else + { + return WorkingSource_Unknown; + } + } + + void SetWorkingSource(const std::string& attachmentId, + WorkingSource source) + { + if (source == WorkingSource_Unknown) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + else + { + boost::mutex::scoped_lock lock(mutex_); + + assert(MAX_CACHE_SIZE > 0); + + if (workingSources_.GetSize() >= MAX_CACHE_SIZE && + !workingSources_.Contains(attachmentId)) + { + workingSources_.RemoveOldest(); + } + + workingSources_.AddOrMakeMostRecent(attachmentId, source); + } + } + }; + ServerTranscoder::ServerTranscoder(unsigned int maxConcurrentDcmtkTranscoder) : #if ORTHANC_ENABLE_PLUGINS == 1 plugins_(NULL), #endif + pimpl_(new PImpl), dcmtkTranscoder_(new DcmtkTranscoder(maxConcurrentDcmtkTranscoder)), builtinDecoderTranscoderOrder_(BuiltinDecoderTranscoderOrder_After) { @@ -67,92 +137,282 @@ #endif - ImageAccessor* ServerTranscoder::DecodeFrame(const ParsedDicomFile& parsedDicom, - const void* buffer, - size_t size, + bool ServerTranscoder::HasPluginsDecoder() const + { +#if ORTHANC_ENABLE_PLUGINS == 1 + return (plugins_ != NULL && + plugins_->HasCustomImageDecoder()); +#else + return false; +#endif + } + + + bool ServerTranscoder::HasPluginsTranscoder() const + { +#if ORTHANC_ENABLE_PLUGINS == 1 + return (plugins_ != NULL && + plugins_->HasCustomTranscoder()); +#else + return false; +#endif + } + + + ImageAccessor* ServerTranscoder::DecodeFrameBuiltin(const ParsedDicomFile& dicom, + unsigned int frameIndex) + { + // Use Orthanc's built-in decoder + + try + { + return dicom.DecodeFrame(frameIndex); + } + catch (const OrthancException&) // NOLINT(bugprone-empty-catch) + { + } + + return NULL; + } + + + ImageAccessor* ServerTranscoder::DecodeFrameUsingPluginsDecoder(const void* buffer, + size_t size, + unsigned int frameIndex) + { +#if ORTHANC_ENABLE_PLUGINS == 1 + if (HasPluginsDecoder()) + { + try + { + return plugins_->Decode(buffer, size, frameIndex); + } + catch (const OrthancException&) // NOLINT(bugprone-empty-catch) + { + } + } +#endif + + return NULL; + } + + + ImageAccessor* ServerTranscoder::DecodeFrameUsingPluginsTranscoder(const void* buffer, + size_t size, + unsigned int frameIndex) + { + /** + * Contrarily to "ServerTranscoder::Transcode()", this method + * doesn't use "dcmtkTranscoder_", as "DecodeFrameBuiltin()" would + * have succeeded in this case. + **/ + +#if ORTHANC_ENABLE_PLUGINS == 1 + if (HasPluginsTranscoder()) + { + DicomImage transcoded; + DicomImage source; + std::set<DicomTransferSyntax> allowedSyntaxes; + + source.SetExternalBuffer(buffer, size); + allowedSyntaxes.insert(DicomTransferSyntax_LittleEndianExplicit); + + if (plugins_->Transcode(transcoded, source, allowedSyntaxes, TranscodingSopInstanceUidMode_AllowNew)) + { + try + { + std::unique_ptr<ParsedDicomFile> dicom(transcoded.ReleaseAsParsedDicomFile()); + return DecodeFrameBuiltin(*dicom, frameIndex); + } + catch (const OrthancException&) // NOLINT(bugprone-empty-catch) + { + } + } + } +#endif + + return NULL; + } + + + ImageAccessor* ServerTranscoder::DecodeFrameBuiltin(const boost::shared_ptr<DataSourceReader>& dicomReader, + const FileInfo& attachment, + unsigned int frameIndex) + { + std::unique_ptr<DicomDataSource::Dicom> dicom( + DicomDataSource::Execute(*dicomReader, DicomDataSource::CreateWholeRequest(attachment))); + + DicomDataSource::Dicom::Lock lock(*dicom); + + return DecodeFrameBuiltin(lock.GetContent(), frameIndex); + } + + + ImageAccessor* ServerTranscoder::DecodeFrameUsingPluginsDecoder(const boost::shared_ptr<DataSourceReader>& storageAreaReader, + const FileInfo& attachment, + unsigned int frameIndex) + { + if (!HasPluginsDecoder()) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + std::unique_ptr<StorageAreaDataSource::Range> range( + StorageAreaDataSource::Execute( + *storageAreaReader, StorageAreaDataSource::CreateAttachmentRequest(attachment, true /* uncompress */))); + + return DecodeFrameUsingPluginsDecoder(range->GetData(), range->GetSize(), frameIndex); + } + } + + + ImageAccessor* ServerTranscoder::DecodeFrameUsingPluginsTranscoder(const boost::shared_ptr<DataSourceReader>& transcoderReader, + const FileInfo& attachment, + unsigned int frameIndex) + { + if (!HasPluginsDecoder()) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + std::unique_ptr<TranscoderDataSource::Transcoded> transcoded( + TranscoderDataSource::Execute(*transcoderReader, TranscoderDataSource::CreateRequest( + attachment, DicomTransferSyntax_LittleEndianExplicit, + TranscodingSopInstanceUidMode_AllowNew, false /* no lossy quality specified */, 0))); + + TranscoderDataSource::Transcoded::LockAsParsed lock(*transcoded); + + return DecodeFrameBuiltin(lock.GetContent(), frameIndex); + } + } + + + ImageAccessor* ServerTranscoder::DecodeFrame(const DicomInstanceToStore& image, unsigned int frameIndex) { std::unique_ptr<ImageAccessor> decoded; if (builtinDecoderTranscoderOrder_ == BuiltinDecoderTranscoderOrder_Before) { - // Use Orthanc's built-in decoder - - try + decoded.reset(DecodeFrameBuiltin(image.GetParsedDicomFile(), frameIndex)); + if (decoded.get() != NULL) { - decoded.reset(parsedDicom.DecodeFrame(frameIndex)); - if (decoded.get() != NULL) - { - return decoded.release(); - } - } - catch (const OrthancException&) // NOLINT(bugprone-empty-catch) - { // ignore, we'll try other alternatives + return decoded.release(); } } -#if ORTHANC_ENABLE_PLUGINS == 1 - if (plugins_ != NULL && - plugins_->HasCustomImageDecoder()) + decoded.reset(DecodeFrameUsingPluginsDecoder(image.GetBufferData(), image.GetBufferSize(), frameIndex)); + if (decoded.get() != NULL) { - try - { - decoded.reset(plugins_->Decode(buffer, size, frameIndex)); - } - catch (const OrthancException&) // NOLINT(bugprone-empty-catch) - { - } + return decoded.release(); + } + if (builtinDecoderTranscoderOrder_ == BuiltinDecoderTranscoderOrder_After) + { + decoded.reset(DecodeFrameBuiltin(image.GetParsedDicomFile(), frameIndex)); if (decoded.get() != NULL) { return decoded.release(); } - else if (builtinDecoderTranscoderOrder_ == BuiltinDecoderTranscoderOrder_After) + } + + decoded.reset(DecodeFrameUsingPluginsTranscoder(image.GetBufferData(), image.GetBufferSize(), frameIndex)); + if (decoded.get() != NULL) + { + return decoded.release(); + } + + return NULL; + } + + + ImageAccessor* ServerTranscoder::DecodeFrame(const boost::shared_ptr<DataSourceReader>& dicomReader, // For built-in decoding + const boost::shared_ptr<DataSourceReader>& storageAreaReader, // For plugin-based decoding + const boost::shared_ptr<DataSourceReader>& transcoderReader, // For transcoding-based decoding + const FileInfo& attachment, + unsigned int frameIndex) + { + // Step 1: Try with the last decoder that successfully handled this DICOM attachment + + const WorkingSource source = pimpl_->LookupWorkingSource(attachment.GetUuid()); + + std::unique_ptr<ImageAccessor> decoded; + + switch (source) + { + case WorkingSource_Unknown: + break; + + case WorkingSource_Builtin: + decoded.reset(DecodeFrameBuiltin(dicomReader, attachment, frameIndex)); + break; + + case WorkingSource_PluginsDecoder: + decoded.reset(DecodeFrameUsingPluginsDecoder(storageAreaReader, attachment, frameIndex)); + break; + + case WorkingSource_PluginsTranscoder: + decoded.reset(DecodeFrameUsingPluginsTranscoder(transcoderReader, attachment, frameIndex)); + break; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + + if (decoded.get() != NULL) + { + return decoded.release(); + } + + // Step 2: If this attachment is unknown, try the available decoders + + if (builtinDecoderTranscoderOrder_ == BuiltinDecoderTranscoderOrder_Before) + { + decoded.reset(DecodeFrameBuiltin(dicomReader, attachment, frameIndex)); + + if (decoded.get() != NULL) { - LOG(INFO) << "The installed image decoding plugins cannot handle an image, " - << "fallback to the built-in DCMTK decoder"; + pimpl_->SetWorkingSource(attachment.GetUuid(), WorkingSource_Builtin); + return decoded.release(); } } -#endif + + if (HasPluginsDecoder()) + { + decoded.reset(DecodeFrameUsingPluginsDecoder(storageAreaReader, attachment, frameIndex)); + + if (decoded.get() != NULL) + { + pimpl_->SetWorkingSource(attachment.GetUuid(), WorkingSource_PluginsDecoder); + return decoded.release(); + } + } if (builtinDecoderTranscoderOrder_ == BuiltinDecoderTranscoderOrder_After) { - try + decoded.reset(DecodeFrameBuiltin(dicomReader, attachment, frameIndex)); + + if (decoded.get() != NULL) { - decoded.reset(parsedDicom.DecodeFrame(frameIndex)); - if (decoded.get() != NULL) - { - return decoded.release(); - } - } - catch (OrthancException& e) - { - LOG(INFO) << "Failed to decode a DICOM frame: " << e.GetDetails(); + pimpl_->SetWorkingSource(attachment.GetUuid(), WorkingSource_Builtin); + return decoded.release(); } } -#if ORTHANC_ENABLE_PLUGINS == 1 - if (plugins_ != NULL && - plugins_->HasCustomTranscoder()) + if (HasPluginsTranscoder()) { - LOG(INFO) << "The plugins and built-in image decoders failed to decode a frame, " - << "trying to transcode the file to LittleEndianExplicit using the plugins."; - DicomImage explicitTemporaryImage; - DicomImage source; - std::set<DicomTransferSyntax> allowedSyntaxes; + decoded.reset(DecodeFrameUsingPluginsTranscoder(transcoderReader, attachment, frameIndex)); - source.SetExternalBuffer(buffer, size); - allowedSyntaxes.insert(DicomTransferSyntax_LittleEndianExplicit); - - if (Transcode(explicitTemporaryImage, source, allowedSyntaxes, TranscodingSopInstanceUidMode_AllowNew)) + if (decoded.get() != NULL) { - std::unique_ptr<ParsedDicomFile> file(explicitTemporaryImage.ReleaseAsParsedDicomFile()); - return file->DecodeFrame(frameIndex); + pimpl_->SetWorkingSource(attachment.GetUuid(), WorkingSource_PluginsTranscoder); + return decoded.release(); } } -#endif - return NULL; // TODO-Streaming - Throw exception here? + return NULL; }
--- a/OrthancServer/Sources/ServerTranscoder.h Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/ServerTranscoder.h Tue May 26 15:52:35 2026 +0200 @@ -24,12 +24,18 @@ #pragma once #include "../../OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h" +#include "../../OrthancFramework/Sources/FileStorage/FileInfo.h" #include "../../OrthancFramework/Sources/Images/ImageAccessor.h" #include "ServerEnumerations.h" +#include <boost/shared_ptr.hpp> + namespace Orthanc { + class DataSourceReader; + class DicomInstanceToStore; + #if ORTHANC_ENABLE_PLUGINS == 1 class OrthancPlugins; #endif @@ -37,13 +43,39 @@ class ServerTranscoder : public IDicomTranscoder { private: + class PImpl; + #if ORTHANC_ENABLE_PLUGINS == 1 OrthancPlugins* plugins_; #endif + boost::shared_ptr<PImpl> pimpl_; std::unique_ptr<IDicomTranscoder> dcmtkTranscoder_; BuiltinDecoderTranscoderOrder builtinDecoderTranscoderOrder_; + ImageAccessor* DecodeFrameBuiltin(const ParsedDicomFile& dicom, + unsigned int frameIndex); + + ImageAccessor* DecodeFrameUsingPluginsDecoder(const void* buffer, + size_t size, + unsigned int frameIndex); + + ImageAccessor* DecodeFrameUsingPluginsTranscoder(const void* buffer, + size_t size, + unsigned int frameIndex); + + ImageAccessor* DecodeFrameBuiltin(const boost::shared_ptr<DataSourceReader>& dicomReader, + const FileInfo& attachment, + unsigned int frameIndex); + + ImageAccessor* DecodeFrameUsingPluginsDecoder(const boost::shared_ptr<DataSourceReader>& storageAreaReader, + const FileInfo& attachment, + unsigned int frameIndex); + + ImageAccessor* DecodeFrameUsingPluginsTranscoder(const boost::shared_ptr<DataSourceReader>& transcoderReader, + const FileInfo& attachment, + unsigned int frameIndex); + public: ServerTranscoder(unsigned int maxConcurrentDcmtkTranscoder); @@ -51,9 +83,18 @@ void SetPlugins(OrthancPlugins& plugins); #endif - ImageAccessor* DecodeFrame(const ParsedDicomFile& parsedDicom, - const void* buffer, // buffer that is the source of the ParsedDicomFile - size_t size, + bool HasPluginsDecoder() const; + + bool HasPluginsTranscoder() const; + + // This version should only be used by plugins + ImageAccessor* DecodeFrame(const DicomInstanceToStore& image, + unsigned int frameIndex); + + ImageAccessor* DecodeFrame(const boost::shared_ptr<DataSourceReader>& dicomReader, // For built-in decoding + const boost::shared_ptr<DataSourceReader>& storageAreaReader, // For plugin-based decoding + const boost::shared_ptr<DataSourceReader>& transcoderReader, // For transcoding-based decoding + const FileInfo& attachment, unsigned int frameIndex); // This method can be used even if the global option
--- a/OrthancServer/Sources/main.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/Sources/main.cpp Tue May 26 15:52:35 2026 +0200 @@ -53,9 +53,11 @@ #include "ServerJobs/DicomRetrieveScuBaseJob.h" #include "ServerJobs/StorageCommitmentScpJob.h" #include "ServerToolbox.h" +#include "ServerTranscoder.h" #include "StorageCommitmentReports.h" #include <boost/algorithm/string/predicate.hpp> +#include <dcmtk/dcmnet/dimse.h> // For STATUS_STORE_Error_CannotUnderstand #if defined(_WIN32) || defined(__CYGWIN__) #include <windows.h> @@ -1608,19 +1610,14 @@ lock.GetConfiguration().SetServerIndex(context.GetIndex()); } - std::unique_ptr<ServerTranscoder> transcoder(new ServerTranscoder(maxDcmtkConcurrentTranscoders)); - #if ORTHANC_ENABLE_PLUGINS == 1 if (plugins_ != NULL) { plugins_->SetServerContext(context_); context_.SetPlugins(*plugins_); context_.GetIndex().SetMaxDatabaseRetries(plugins_->GetMaxDatabaseRetries()); - transcoder->SetPlugins(*plugins_); } #endif - - context.SetTranscoder(transcoder.release()); } ~ServerContextConfigurator() @@ -1637,8 +1634,6 @@ context_.ResetPlugins(); } #endif - - context_.ResetTranscoder(); } }; } @@ -1709,8 +1704,14 @@ DicomAssociationParameters::SetDefaultRemoteCertificateRequired( lock.GetConfiguration().GetBooleanParameter(KEY_DICOM_TLS_REMOTE_CERTIFICATE_REQUIRED, true)); } + + std::unique_ptr<ServerTranscoder> transcoder(new ServerTranscoder(maxDcmtkConcurrentTranscoders)); + +#if ORTHANC_ENABLE_PLUGINS == 1 + transcoder->SetPlugins(*plugins); +#endif - ServerContext context(database, storageArea, false /* not running unit tests */, maxCompletedJobs, readOnly); + ServerContext context(database, storageArea, transcoder.release(), false /* not running unit tests */, maxCompletedJobs, readOnly); { OrthancConfiguration::ReaderLock lock; @@ -2180,7 +2181,8 @@ SQLiteDatabaseWrapper inMemoryDatabase; inMemoryDatabase.Open(); PluginStorageAreaAdapter inMemoryStorage(new MemoryStorageArea); - ServerContext context(inMemoryDatabase, inMemoryStorage, true /* unit testing */, 0 /* max completed jobs */, false /* readonly */); + ServerContext context(inMemoryDatabase, inMemoryStorage, NULL /* no transcoder */, + true /* unit testing */, 0 /* max completed jobs */, false /* readonly */); OrthancRestApi restApi(context, false /* no Orthanc Explorer */); restApi.GenerateOpenApiDocumentation(openapi); context.Stop(); @@ -2231,7 +2233,8 @@ SQLiteDatabaseWrapper inMemoryDatabase; inMemoryDatabase.Open(); PluginStorageAreaAdapter inMemoryStorage(new MemoryStorageArea); - ServerContext context(inMemoryDatabase, inMemoryStorage, true /* unit testing */, 0 /* max completed jobs */, false /* readonly */); + ServerContext context(inMemoryDatabase, inMemoryStorage, NULL /* no transcoder */, + true /* unit testing */, 0 /* max completed jobs */, false /* readonly */); OrthancRestApi restApi(context, false /* no Orthanc Explorer */); restApi.GenerateReStructuredTextCheatSheet(cheatsheet, "https://orthanc.uclouvain.be/api/index.html"); context.Stop();
--- a/OrthancServer/UnitTestsSources/ServerConfigTests.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/UnitTestsSources/ServerConfigTests.cpp Tue May 26 15:52:35 2026 +0200 @@ -39,7 +39,7 @@ PluginStorageAreaAdapter storage(new MemoryStorageArea); SQLiteDatabaseWrapper db; // The SQLite DB is in memory db.Open(); - ServerContext context(db, storage, true /* running unit tests */, 10, false); + ServerContext context(db, storage, NULL /* no transcoder */, true /* running unit tests */, 10, false); { // default config -> all SOP Classes should be accepted std::set<std::string> s;
--- a/OrthancServer/UnitTestsSources/ServerIndexTests.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/UnitTestsSources/ServerIndexTests.cpp Tue May 26 15:52:35 2026 +0200 @@ -730,7 +730,7 @@ PluginStorageAreaAdapter storage(new FilesystemStorage(path)); SQLiteDatabaseWrapper db; // The SQLite DB is in memory db.Open(); - ServerContext context(db, storage, true /* running unit tests */, 10, false /* readonly */); + ServerContext context(db, storage, NULL /* no transcoder */, true /* running unit tests */, 10, false /* readonly */); context.SetupJobsEngine(true, false); ServerIndex& index = context.GetIndex(); @@ -812,7 +812,7 @@ PluginStorageAreaAdapter storage(new FilesystemStorage(path)); SQLiteDatabaseWrapper db; // The SQLite DB is in memory db.Open(); - ServerContext context(db, storage, true /* running unit tests */, 10, false /* readonly */); + ServerContext context(db, storage, NULL /* no transcoder */, true /* running unit tests */, 10, false /* readonly */); context.SetupJobsEngine(true, false); ServerIndex& index = context.GetIndex(); @@ -935,7 +935,7 @@ PluginStorageAreaAdapter storage(new MemoryStorageArea); SQLiteDatabaseWrapper db; // The SQLite DB is in memory db.Open(); - ServerContext context(db, storage, true /* running unit tests */, 10, false /* readonly */); + ServerContext context(db, storage, NULL /* no transcoder */, true /* running unit tests */, 10, false /* readonly */); context.SetupJobsEngine(true, false); context.SetCompressionEnabled(true); @@ -1002,9 +1002,11 @@ ASSERT_EQ("name", tmp["0010,0010"]["Value"].asString()); { - ServerContext::DicomCacheLocker locker(context, id); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(id)); + DicomDataSource::Dicom::Lock lock(*dicom); + std::string tmp; - locker.GetDicom().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME); + lock.GetContent().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME); ASSERT_EQ("name", tmp); } @@ -1057,9 +1059,11 @@ ASSERT_EQ("overwritten", tmp["0010,0010"]["Value"].asString()); { - ServerContext::DicomCacheLocker locker(context, id); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(id)); + DicomDataSource::Dicom::Lock lock(*dicom); + std::string tmp; - locker.GetDicom().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME); + lock.GetContent().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME); ASSERT_EQ("overwritten", tmp); } } @@ -1074,9 +1078,11 @@ ASSERT_EQ("name", tmp["0010,0010"]["Value"].asString()); { - ServerContext::DicomCacheLocker locker(context, id); + std::unique_ptr<DicomDataSource::Dicom> dicom(context.ReadParsedDicom(id)); + DicomDataSource::Dicom::Lock lock(*dicom); + std::string tmp; - locker.GetDicom().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME); + lock.GetContent().GetTagValue(tmp, DICOM_TAG_PATIENT_NAME); ASSERT_EQ("name", tmp); } } @@ -1100,7 +1106,7 @@ PluginStorageAreaAdapter storage(new MemoryStorageArea); SQLiteDatabaseWrapper db; // The SQLite DB is in memory db.Open(); - ServerContext context(db, storage, true /* running unit tests */, 10, false /* readonly */); + ServerContext context(db, storage, NULL /* no transcoder */, true /* running unit tests */, 10, false /* readonly */); context.SetupJobsEngine(true, false); context.SetCompressionEnabled(compression);
--- a/OrthancServer/UnitTestsSources/ServerJobsTests.cpp Tue May 26 15:48:55 2026 +0200 +++ b/OrthancServer/UnitTestsSources/ServerJobsTests.cpp Tue May 26 15:52:35 2026 +0200 @@ -566,7 +566,7 @@ storage_(new MemoryStorageArea) { db_.Open(); - context_.reset(new ServerContext(db_, storage_, true /* running unit tests */, 10, false /* readonly */)); + context_.reset(new ServerContext(db_, storage_, NULL /* no transcoder */, true /* running unit tests */, 10, false /* readonly */)); context_->SetupJobsEngine(true, false); } @@ -1021,9 +1021,11 @@ std::string study, series; { - ServerContext::DicomCacheLocker lock(GetContext(), instance); - study = lock.GetDicom().GetHasher().HashStudy(); - series = lock.GetDicom().GetHasher().HashSeries(); + std::unique_ptr<DicomDataSource::Dicom> dicom(GetContext().ReadParsedDicom(instance)); + DicomDataSource::Dicom::Lock lock(*dicom); + + study = lock.GetContent().GetHasher().HashStudy(); + series = lock.GetContent().GetHasher().HashSeries(); } {
