Mercurial > hg > orthanc
changeset 6829:460330d630bc streaming
integration mainline->streaming
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Thu, 21 May 2026 08:59:49 +0200 |
| parents | bee76cdf2497 (diff) 13f0f6dda96d (current diff) |
| children | da03aa08b9aa |
| files | OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp OrthancServer/Sources/main.cpp |
| diffstat | 32 files changed, 1344 insertions(+), 687 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Thu May 21 08:54:11 2026 +0200 +++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Thu May 21 08:59:49 2026 +0200 @@ -681,6 +681,8 @@ if (ENABLE_DCMTK) list(APPEND ORTHANC_CORE_SOURCES_INTERNAL ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/DicomDataSource.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DataSource/TranscoderDataSource.cpp + ${CMAKE_CURRENT_LIST_DIR}/../../Sources/DicomParsing/TranscodingCallable.cpp ) endif()
--- a/OrthancFramework/Sources/DataSource/DataSourceReader.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceReader.cpp Thu May 21 08:59:49 2026 +0200 @@ -156,13 +156,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);
--- a/OrthancFramework/Sources/DataSource/DataSourceReader.h Thu May 21 08:54:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceReader.h Thu May 21 08:59:49 2026 +0200 @@ -46,22 +46,17 @@ 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);
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DicomDataSource.cpp Thu May 21 08:59:49 2026 +0200 @@ -35,34 +35,10 @@ namespace Orthanc { - static size_t ComputeValueSize(size_t size, - bool isKeepRawBuffer) - { - if (isKeepRawBuffer) - { - uint64_t s = 2 * static_cast<uint64_t>(size); - if (s != static_cast<uint64_t>(static_cast<size_t>(s))) - { - return std::numeric_limits<size_t>::max(); - } - else - { - return static_cast<size_t>(s); - } - } - else - { - return size; - } - } - - class DicomDataSource::BaseIdentifier : public IDataIdentifier { public: virtual StorageAreaDataSource::Range* ReadRange(DataSourceReader& reader) const = 0; - - virtual bool IsKeepRawBuffer() const = 0; }; @@ -70,15 +46,12 @@ { private: FileInfo attachment_; - bool keepRawBuffer_; bool checkMD5_; public: explicit WholeIdentifier(const FileInfo& attachment, - bool keepRawBuffer, bool checkMD5) : attachment_(attachment), - keepRawBuffer_(keepRawBuffer), checkMD5_(checkMD5) { } @@ -86,14 +59,13 @@ virtual bool GetCacheKey(std::string& key) const ORTHANC_OVERRIDE { key = (attachment_.GetUuid() + "|" + - boost::lexical_cast<std::string>(attachment_.GetContentType()) + "|" + - boost::lexical_cast<std::string>(keepRawBuffer_)); + boost::lexical_cast<std::string>(attachment_.GetContentType())); return true; } virtual bool EstimateValueSize(size_t& target) const ORTHANC_OVERRIDE { - target = ComputeValueSize(attachment_.GetUncompressedSize(), keepRawBuffer_); + target = attachment_.GetUncompressedSize(); return true; } @@ -101,11 +73,6 @@ { return StorageAreaDataSource::ReadAttachment(reader, attachment_, true /* uncompress */, checkMD5_); } - - virtual bool IsKeepRawBuffer() const ORTHANC_OVERRIDE - { - return keepRawBuffer_; - } }; @@ -143,11 +110,6 @@ { return StorageAreaDataSource::ReadBeginning(reader, attachment_, pixelDataOffset_); } - - virtual bool IsKeepRawBuffer() const ORTHANC_OVERRIDE - { - return false; - } }; @@ -156,15 +118,12 @@ private: std::unique_ptr<ParsedDicomFile> dicom_; size_t size_; - bool hasRawBuffer_; - std::string rawBuffer_; public: Value(ParsedDicomFile* dicom, size_t size) : dicom_(dicom), - size_(size), - hasRawBuffer_(false) + size_(size) { if (dicom == NULL) { @@ -177,46 +136,10 @@ return *dicom_; } - size_t GetRawBufferSize() const + size_t GetSize() const { return size_; } - - void SetRawBuffer(const void* data, - size_t size) - { - if (hasRawBuffer_) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - else if (size != size_) - { - throw OrthancException(ErrorCode_ParameterOutOfRange); - } - else - { - hasRawBuffer_ = true; - rawBuffer_.assign(reinterpret_cast<const char*>(data), size); - } - } - - bool HasRawBuffer() const - { - return hasRawBuffer_; - } - - const void* GetRawBufferData() const - { - if (hasRawBuffer_) - { - assert(rawBuffer_.size() == size_); - return rawBuffer_.empty() ? NULL : rawBuffer_.c_str(); - } - else - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - } }; @@ -240,30 +163,16 @@ { // Sanity check - bool ok = false; size_t estimatedSize; - if (obj.EstimateValueSize(estimatedSize)) - { - ok = (estimatedSize == ComputeValueSize(size, id.IsKeepRawBuffer())); - } - - if (!ok) + if (!obj.EstimateValueSize(estimatedSize) || + estimatedSize != size) { throw OrthancException(ErrorCode_InternalError); } } std::unique_ptr<Value> value(new Value(new ParsedDicomFile(range->GetData(), size), size)); - - if (id.IsKeepRawBuffer()) - { - value->SetRawBuffer(range->GetData(), size); - assert(GetValueSize(*value) == 2 * size); - } - else - { - assert(GetValueSize(*value) == size); - } + assert(GetValueSize(*value) == size); return value.release(); } @@ -272,7 +181,7 @@ size_t DicomDataSource::GetValueSize(const IDynamicObject& obj) const { const Value& value = dynamic_cast<const Value&>(obj); - return ComputeValueSize(value.GetRawBufferSize(), value.HasRawBuffer()); + return value.GetSize(); } @@ -286,36 +195,24 @@ } - ParsedDicomFile& DicomDataSource::Dicom::Lock::GetContent() const + ParsedDicomFile* DicomDataSource::Dicom::Clone() + { + Lock lock(*this); + return lock.GetContent().Clone(true); + } + + + const ParsedDicomFile& DicomDataSource::Dicom::Lock::GetContent() const { return dynamic_cast<const Value&>(*that_.value_).GetContent(); } - bool DicomDataSource::Dicom::Lock::HasRawBuffer() const - { - return dynamic_cast<const Value&>(*that_.value_).HasRawBuffer(); - } - - - const void* DicomDataSource::Dicom::Lock::GetRawBufferData() const - { - return dynamic_cast<const Value&>(*that_.value_).GetRawBufferData(); - } - - - size_t DicomDataSource::Dicom::Lock::GetRawBufferSize() const - { - return dynamic_cast<const Value&>(*that_.value_).GetRawBufferSize(); - } - - DicomDataSource::Dicom* DicomDataSource::ReadWhole(DataSourceReader& reader, const FileInfo& attachment, - bool keepRawBuffer, bool checkMD5) { - std::unique_ptr<IDataIdentifier> id(new WholeIdentifier(attachment, keepRawBuffer, checkMD5)); + std::unique_ptr<IDataIdentifier> id(new WholeIdentifier(attachment, checkMD5)); boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release()); return new Dicom(value); }
--- a/OrthancFramework/Sources/DataSource/DicomDataSource.h Thu May 21 08:54:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DicomDataSource.h Thu May 21 08:59:49 2026 +0200 @@ -72,6 +72,8 @@ public: explicit Dicom(const boost::shared_ptr<IDynamicObject>& value); + ParsedDicomFile* Clone(); + /** * Access to the DICOM value must be protected by a mutex, as it * could be shared by multiple threads if caching is enabled in @@ -90,19 +92,12 @@ { } - ParsedDicomFile& GetContent() const; - - bool HasRawBuffer() const; - - const void* GetRawBufferData() const; - - size_t GetRawBufferSize() const; + const ParsedDicomFile& GetContent() const; }; }; static Dicom* ReadWhole(DataSourceReader& reader, const FileInfo& attachment, - bool keepRawBuffer, // Must be set to "true" for transcoding bool checkMD5); static Dicom* ReadUntilPixelData(DataSourceReader& reader,
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp Thu May 21 08:59:49 2026 +0200 @@ -0,0 +1,242 @@ +/** + * 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 "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: + std::unique_ptr<ParsedDicomFile> dicom_; + size_t size_; + + public: + Value(ParsedDicomFile* dicom, + size_t size) : + dicom_(dicom), + size_(size) + { + if (dicom == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + ParsedDicomFile& GetContent() const + { + return *dicom_; + } + + size_t GetSize() const + { + return size_; + } + }; + + + class TranscoderDataSource::Identifier : public IDataIdentifier + { + private: + FileInfo attachment_; + DicomTransferSyntax targetSyntax_; + bool checkMD5_; + TranscodingSopInstanceUidMode mode_; + bool hasLossyQuality_; + unsigned int lossyQuality_; + + public: + Identifier(const FileInfo& attachment, + DicomTransferSyntax targetSyntax, + bool checkMD5) : + attachment_(attachment), + targetSyntax_(targetSyntax), + checkMD5_(checkMD5), + mode_(TranscodingSopInstanceUidMode_AllowNew), + hasLossyQuality_(false), + lossyQuality_(0) + { + } + + void SetMode(TranscodingSopInstanceUidMode mode) + { + mode_ = mode; + } + + 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_ && + !IsLosslessTransferSyntax(targetSyntax_)) + { + 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::ReadAttachment( + storageAreaReader, attachment_, true /* uncompress */, checkMD5_)); + + IDicomTranscoder::DicomImage source; + source.SetExternalBuffer(range->GetData(), range->GetSize()); + + 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) + { + const size_t size = target.GetBufferSize(); + return new Value(target.ReleaseAsParsedDicomFile(), size); + } + 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(const boost::shared_ptr<IDynamicObject>& value) : + value_(value) + { + if (!value) + { + throw OrthancException(ErrorCode_NullPointer); + } + } + + + const ParsedDicomFile& TranscoderDataSource::Transcoded::Lock::GetDicom() const + { + return dynamic_cast<const Value&>(*that_.value_).GetContent(); + } + + + TranscoderDataSource::Transcoded* TranscoderDataSource::Transcode(DataSourceReader& reader, + const FileInfo& attachment, + DicomTransferSyntax targetSyntax, + bool checkMD5) + { + std::unique_ptr<IDataIdentifier> id(new Identifier(attachment, targetSyntax, checkMD5)); + boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release()); + return new Transcoded(value); + } + + + TranscoderDataSource::Transcoded* TranscoderDataSource::Transcode(DataSourceReader& reader, + const FileInfo& attachment, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + unsigned int lossyQuality, + bool checkMD5) + { + std::unique_ptr<Identifier> id(new Identifier(attachment, targetSyntax, checkMD5)); + id->SetMode(mode); + id->SetLossyQuality(lossyQuality); + + boost::shared_ptr<IDynamicObject> value = reader.ReadSingle(id.release()); + return new Transcoded(value); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.h Thu May 21 08:59:49 2026 +0200 @@ -0,0 +1,98 @@ +/** + * 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 "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: + Mutex mutex_; + boost::shared_ptr<IDynamicObject> value_; + + public: + explicit Transcoded(const boost::shared_ptr<IDynamicObject>& value); + + /** + * Access to the DICOM value must be protected by a mutex, as it + * could be shared by multiple threads if caching is enabled in + * the DataSourceReader. + **/ + class Lock : public boost::noncopyable + { + private: + Transcoded& that_; + Mutex::ScopedLock lock_; + + public: + explicit Lock(Transcoded& that) : + that_(that), + lock_(that.mutex_) + { + } + + const ParsedDicomFile& GetDicom() const; + }; + }; + + static Transcoded* Transcode(DataSourceReader& reader, + const FileInfo& attachment, + DicomTransferSyntax targetSyntax, + bool checkMD5); + + static Transcoded* Transcode(DataSourceReader& reader, + const FileInfo& attachment, + DicomTransferSyntax targetSyntax, + TranscodingSopInstanceUidMode mode, + unsigned int lossyQuality, + bool checkMD5); + }; +}
--- a/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h Thu May 21 08:54:11 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/IDicomTranscoder.h Thu May 21 08:59:49 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_;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DicomParsing/TranscodingCallable.cpp Thu May 21 08:59:49 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 Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancFramework/Sources/HttpServer/HttpOutput.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancFramework/Sources/HttpServer/HttpOutput.h Thu May 21 08:59:49 2026 +0200 @@ -158,7 +158,7 @@ } void SendStatus(HttpStatus status, - const char* message, + const void* message, size_t messageSize); void SendStatus(HttpStatus status);
--- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Thu May 21 08:59:49 2026 +0200 @@ -65,6 +65,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" @@ -6081,7 +6082,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)
--- a/OrthancServer/Sources/OrthancGetRequestHandler.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/OrthancGetRequestHandler.cpp Thu May 21 08:59:49 2026 +0200 @@ -31,6 +31,7 @@ #include "ServerContext.h" #include "ServerJobs/DicomModalityStoreJob.h" #include "ServerJobs/ThreadedInstancesLoader.h" +#include "ServerTranscoder.h" #include <dcmtk/dcmdata/dcdeftag.h> #include <dcmtk/dcmdata/dcfilefo.h> @@ -331,7 +332,7 @@ 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;
--- a/OrthancServer/Sources/OrthancRestApi/OrthancRestAnonymizeModify.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestAnonymizeModify.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestResources.cpp Thu May 21 08:59:49 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" @@ -382,8 +384,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 +416,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 { @@ -520,8 +518,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 +633,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 +925,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 +1397,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 +1412,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 +1435,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 +1455,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 +1481,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 +1500,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 +1600,9 @@ } return; } - + + ServerContext& context = OrthancRestApi::GetContext(call); + std::string frameId = call.GetUriComponent("frame", "0"); unsigned int frame; @@ -1612,8 +1620,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 +2387,14 @@ if (hasRangeHeader) { - std::string fragment; - context.ReadAttachmentRange(fragment, info, range, uncompress); + std::unique_ptr<StorageAreaDataSource::Range> fragment( + StorageAreaDataSource::ReadRange( + context.GetStorageAreaReader(), info, range, uncompress, context.IsCheckMD5())); 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 +2404,11 @@ 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( + StorageAreaDataSource::ReadAttachment( + context.GetStorageAreaReader(), info, false /* don't uncompress */, context.IsCheckMD5())); + + call.GetOutput().AnswerBuffer(content->GetData(), content->GetSize(), MimeType_Binary); } } } @@ -2574,13 +2586,16 @@ 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( + StorageAreaDataSource::ReadAttachment( + context.GetStorageAreaReader(), info, false /* don't uncompress */, false /* check MD5 */)); + 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 +2605,16 @@ } 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( + StorageAreaDataSource::ReadAttachment( + context.GetStorageAreaReader(), info, true /* uncompress */, false /* check MD5 */)); + Toolbox::ComputeMD5(uncompressedMD5, uncompressed->GetData(), uncompressed->GetSize()); + } + + ok = (uncompressedMD5 == info.GetUncompressedMD5()); } } @@ -2827,11 +2849,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 +3820,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; + } } }
--- a/OrthancServer/Sources/ServerContext.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerContext.cpp Thu May 21 08:59:49 2026 +0200 @@ -25,11 +25,14 @@ #include "ServerContext.h" #include "../../OrthancFramework/Sources/Cache/SharedArchive.h" +#include "../../OrthancFramework/Sources/DataSource/DataSourceReader.h" +#include "../../OrthancFramework/Sources/DataSource/TranscoderDataSource.h" #include "../../OrthancFramework/Sources/DicomFormat/DicomElement.h" #include "../../OrthancFramework/Sources/DicomFormat/DicomStreamReader.h" #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" @@ -37,19 +40,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> @@ -358,19 +363,24 @@ void ServerContext::PublishCacheMetrics() { - 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())); + // TODO-Streaming + + //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())); } ServerContext::ServerContext(IDatabaseWrapper& database, IPluginStorageArea& area, + ServerTranscoder* transcoder /* takes ownership */, bool unitTesting, size_t maxCompletedJobs, bool readOnly) : @@ -379,7 +389,6 @@ compressionEnabled_(false), storeMD5_(true), largeDicomThrottler_(1), - dicomCache_(DICOM_CACHE_SIZE), mainLua_(*this), filterLua_(*this), luaListener_(*this), @@ -395,6 +404,8 @@ isExecuteLuaEnabled_(false), isRestApiWriteToFileSystemEnabled_(false), overwriteInstances_(OverwriteInstancesMode_Never), + transcoder_(transcoder), + transcodeDicomProtocol_(true), isIngestTranscoding_(false), ingestTranscodingOfUncompressed_(true), ingestTranscodingOfCompressed_(true), @@ -402,7 +413,8 @@ readOnly_(readOnly), patientLevelEnabled_(true), deidentifyLogs_(false), - serverStartTimeUtc_(boost::posix_time::second_clock::universal_time()) + serverStartTimeUtc_(boost::posix_time::second_clock::universal_time()), + checkMD5_(true) { try { @@ -531,6 +543,8 @@ SetAcceptedSopClasses(acceptedSopClasses, rejectedSopClasses); defaultDicomRetrieveMethod_ = StringToRetrieveMethod(lock.GetConfiguration().GetDicomDefaultRetrieveMethod()); + + checkMD5_ = lock.GetConfiguration().GetBooleanParameter("CheckMD5", true); // TODO-Streaming - Parameter } jobsEngine_.SetThreadSleep(unitTesting ? 20 : 200); @@ -545,6 +559,41 @@ #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_))); + storageAreaReader_->CreateCache(DICOM_CACHE_SIZE); // 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_->CreateCache(100 * 1024 * 1024); // 100 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_->CreateCache(100 * 1024 * 1024); // 100 MB - TODO-Streaming - Parameter + } } catch (OrthancException&) { @@ -637,7 +686,7 @@ FileContentType type, const std::string& customData) { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); + StorageAccessor accessor(area_, GetMetricsRegistry()); accessor.Remove(fileUuid, type, customData); } @@ -699,7 +748,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(); @@ -790,12 +839,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); @@ -1055,7 +1098,7 @@ IDicomTranscoder::DicomImage transcoded; - 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()); @@ -1091,8 +1134,17 @@ 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( + StorageAreaDataSource::ReadAttachment( + *storageAreaReader_, attachment, true /* uncompress */, checkMD5_)); + + 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); } @@ -1119,13 +1171,18 @@ 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( + StorageAreaDataSource::ReadAttachment( + *storageAreaReader_, attachment, true /* uncompress */, checkMD5_)); + + accessor.Write(modified, range->GetData(), range->GetSize(), attachmentType, compression, storeMD5_, NULL); + } try { @@ -1253,15 +1310,14 @@ if (LookupAttachment(attachment, FileContentType_DicomUntilPixelData, instanceAttachments)) { - std::string dicom; + std::unique_ptr<DicomDataSource::Dicom> dicom( + DicomDataSource::ReadWhole(*dicomReader_, attachment, checkMD5_)); { - 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 @@ -1302,17 +1358,15 @@ * this case cannot be used if "StorageCompression" option is * "true". **/ - - std::string dicom; - + + std::unique_ptr<DicomDataSource::Dicom> dicom( + DicomDataSource::ReadUntilPixelData(*dicomReader_, 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() && @@ -1325,15 +1379,12 @@ * "/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( + StorageAreaDataSource::ReadAttachment( + *storageAreaReader_, attachment, true /* uncompress */, checkMD5_)); - 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); @@ -1347,25 +1398,33 @@ * 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( + StorageAreaDataSource::ReadAttachment(*storageAreaReader_, attachment, true /* uncompress */, checkMD5_)); + + // We don't use the cache, as this would complexify the code for a rare edge case + ParsedDicomFile dicom(range->GetData(), range->GetSize()); + OrthancConfiguration::DefaultDicomDatasetToJson(result, dicom, ignoreTagLength); if (!hasPixelDataOffset) { /** - * 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)); @@ -1375,8 +1434,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 */); } } } @@ -1394,19 +1453,8 @@ 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 attachment; int64_t revision; @@ -1418,33 +1466,47 @@ } 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); - } + std::unique_ptr<StorageAreaDataSource::Range> range( + StorageAreaDataSource::ReadAttachment( + *storageAreaReader_, attachment, true /* uncompress */, checkMD5_)); - ReadAttachment(dicom, attachment, true /* uncompress */); + assert(range->GetSize() == attachment.GetUncompressedSize()); + range->Copy(dicom); } - void ServerContext::ReadDicom(std::string& dicom, - const std::string& instancePublicId) + FileInfo ServerContext::LookupDicomForInstance(const std::string& instancePublicId) { - std::string attachmentId; - ReadDicom(dicom, attachmentId, instancePublicId); + FileInfo attachment; + int64_t revision; + + 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); + } } - 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 StorageAreaDataSource::ReadAttachment(*storageAreaReader_, LookupDicomForInstance(instancePublicId), + true /* uncompress */, checkMD5_); } + + DicomDataSource::Dicom* ServerContext::ReadParsedDicom(const std::string& instancePublicId) + { + return DicomDataSource::ReadWhole(*dicomReader_, LookupDicomForInstance(instancePublicId), checkMD5_); + } + + void ServerContext::ReadDicomForHeader(std::string& dicom, const std::string& instancePublicId) { @@ -1461,10 +1523,12 @@ int64_t revision; // Ignored if (index_.LookupAttachment(attachment, revision, ResourceType_Instance, instancePublicId, FileContentType_DicomUntilPixelData)) { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); + std::unique_ptr<StorageAreaDataSource::Range> range( + StorageAreaDataSource::ReadAttachment( + *storageAreaReader_, attachment, true /* uncompress */, checkMD5_)); - accessor.Read(dicom, attachment); - assert(dicom.size() == attachment.GetUncompressedSize()); + assert(range->GetSize() == attachment.GetUncompressedSize()); + range->Copy(dicom); return true; } @@ -1491,11 +1555,12 @@ if (SerializationToolbox::ParseUnsignedInteger64(pixelDataOffset, s)) { - StorageAccessor accessor(area_, storageCache_, GetMetricsRegistry()); + std::unique_ptr<StorageAreaDataSource::Range> range( + StorageAreaDataSource::ReadBeginning(*storageAreaReader_, attachment, pixelDataOffset)); - accessor.ReadStartRange(dicom, attachment, pixelDataOffset); - assert(dicom.size() == pixelDataOffset); - + assert(range->GetSize() == pixelDataOffset); + range->Copy(dicom); + return true; // Success } else @@ -1508,112 +1573,6 @@ } - 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())); - } - else - { - accessor.reset(new StorageAccessor(area_, storageCache_, GetMetricsRegistry())); - } - - 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); - } - - - 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); - - // 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) - { - } - } - } - - - 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"); @@ -1636,7 +1595,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 @@ -1670,24 +1629,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()); } @@ -1918,36 +1865,47 @@ } - - - - 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, checkMD5_)); 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); + return GetTranscoder()->DecodeFrame(dicom.GetParsedDicomFile(), + dicom.GetBufferData(), + dicom.GetBufferSize(), + frameIndex); } @@ -1957,7 +1915,7 @@ unsigned int frameIndex) { std::unique_ptr<ParsedDicomFile> instance(new ParsedDicomFile(dicom, size)); - return GetTranscoder().DecodeFrame(*instance, dicom, size, frameIndex); + return GetTranscoder()->DecodeFrame(*instance, dicom, size, frameIndex); } @@ -2016,37 +1974,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 ***"; @@ -2235,47 +2162,28 @@ } - 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_); - void ServerContext::ResetTranscoder() - { - if (transcoder_.get() == NULL) - { - throw OrthancException(ErrorCode_BadSequenceOfCalls); - } - else - { - transcoder_.reset(NULL); - } + return transcoderThreadPool_->Submit(callable); } }
--- a/OrthancServer/Sources/ServerContext.h Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerContext.h Thu May 21 08:59:49 2026 +0200 @@ -28,13 +28,13 @@ #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" @@ -43,8 +43,11 @@ class DicomElement; class DicomStoreUserConnection; class OrthancPlugins; + class ServerTranscoder; class SharedArchive; class StorageCommitmentReports; + class ThreadPool; + class TranscodingCallable; /** @@ -192,13 +195,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 +247,7 @@ std::unique_ptr<StorageCommitmentReports> storageCommitmentReports_; - std::unique_ptr<ServerTranscoder> transcoder_; + boost::shared_ptr<ServerTranscoder> transcoder_; bool transcodeDicomProtocol_; bool isIngestTranscoding_; DicomTransferSyntax ingestTransferSyntax_; @@ -285,31 +286,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 + bool checkMD5_; + boost::shared_ptr<DataSourceReader> storageAreaReader_; + boost::shared_ptr<DataSourceReader> dicomReader_; + boost::shared_ptr<ThreadPool> transcoderThreadPool_; + boost::shared_ptr<DataSourceReader> transcoderReader_; - 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 +313,7 @@ void SetMaximumStorageCacheSize(size_t size) { - return storageCache_.SetMaximumSize(size); + // TODO-Streaming } void SetPatientLevelEnabled(bool enabled); @@ -395,25 +382,14 @@ 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); + void ReadDicom(std::string& dicom, + const std::string& instancePublicId); // TODO-Streaming : Remove - void ReadDicomInternal(std::string& dicom, - std::string& attachmentId, - const std::string& instancePublicId, - std::unique_ptr<Semaphore::Locker>& largeDicomLocker, - std::size_t largeDicomThreshold); + FileInfo LookupDicomForInstance(const std::string& instancePublicId); -public: - void ReadDicom(std::string& dicom, - const std::string& instancePublicId); + StorageAreaDataSource::Range* ReadRawDicom(const std::string& instancePublicId); - void ReadDicom(std::string& dicom, - std::string& attachmentId, - const std::string& instancePublicId); + DicomDataSource::Dicom* ReadParsedDicom(const std::string& instancePublicId); void ReadDicomForHeader(std::string& dicom, const std::string& instancePublicId); @@ -421,17 +397,6 @@ bool ReadDicomUntilPixelData(std::string& dicom, const std::string& instancePublicId); - // This method is for low-level operations on "/instances/.../attachments/..." - void ReadAttachment(std::string& result, - const FileInfo& attachment, - bool uncompressIfNeeded, - bool skipCache = false); - - void ReadAttachmentRange(std::string& result, - const FileInfo& attachment, - const StorageRange& range, - bool uncompressIfNeeded); - void SetStoreMD5ForAttachments(bool storeMD5); bool IsStoreMD5ForAttachments() const @@ -583,9 +548,11 @@ ImageAccessor* DecodeDicomFrame(const std::string& publicId, unsigned int frameIndex); + // TODO-Streaming : To remove (or adapt) ImageAccessor* DecodeDicomFrame(const DicomInstanceToStore& dicom, unsigned int frameIndex); + // TODO-Streaming : To remove ImageAccessor* DecodeDicomFrame(const void* dicom, size_t size, unsigned int frameIndex); @@ -598,12 +565,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 +596,23 @@ void PublishCacheMetrics(); - void SetTranscoder(ServerTranscoder* transcoder /* takes ownership */); + const boost::shared_ptr<ServerTranscoder>& GetTranscoder() const; // TODO-Streaming : REMOVE + + bool IsCheckMD5() const + { + return checkMD5_; + } - ServerTranscoder& GetTranscoder() const; + DataSourceReader& GetStorageAreaReader() const // TODO-Streaming : REMOVE? + { + return *storageAreaReader_; + } - void ResetTranscoder(); + DataSourceReader& GetDicomReader() const // TODO-Streaming : REMOVE? + { + return *dicomReader_; + } + + Future* SubmitTranscodingRequest(TranscodingCallable* callable); }; }
--- a/OrthancServer/Sources/ServerJobs/MergeStudyJob.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/MergeStudyJob.cpp Thu May 21 08:59:49 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/ModifyInstanceOperation.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/Operations/ModifyInstanceOperation.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/OrthancPeerStoreJob.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/ResourceModificationJob.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/SplitStudyJob.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/StorageCommitmentScpJob.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp Thu May 21 08:59:49 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,14 @@ try { boost::shared_ptr<std::string> dicomContent(new std::string()); - that->context_.ReadAttachment(*dicomContent, instanceToPreload->GetFileInfo(), true); + + { + std::unique_ptr<StorageAreaDataSource::Range> tmp( + StorageAreaDataSource::ReadAttachment( + that->context_.GetStorageAreaReader(), instanceToPreload->GetFileInfo(), + true /* uncompress */, that->context_.IsCheckMD5())); + tmp->Copy(*dicomContent); + } if (that->transcode_) { @@ -259,7 +268,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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerToolbox.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerTranscoder.cpp Thu May 21 08:59:49 2026 +0200 @@ -24,21 +24,90 @@ #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 "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,7 +136,153 @@ #endif - ImageAccessor* ServerTranscoder::DecodeFrame(const ParsedDicomFile& parsedDicom, + 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) + { +#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, + bool checkMD5) + { + std::unique_ptr<DicomDataSource::Dicom> dicom( + DicomDataSource::ReadWhole(*dicomReader, attachment, checkMD5)); + + 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, + bool checkMD5) + { + if (!HasPluginsDecoder()) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + std::unique_ptr<StorageAreaDataSource::Range> range( + StorageAreaDataSource::ReadAttachment(*storageAreaReader, attachment, true /* uncompress */, checkMD5)); + + return DecodeFrameUsingPluginsDecoder(range->GetData(), range->GetSize(), frameIndex); + } + } + + + ImageAccessor* ServerTranscoder::DecodeFrameUsingPluginsTranscoder(const boost::shared_ptr<DataSourceReader>& transcoderReader, + const FileInfo& attachment, + unsigned int frameIndex, + bool checkMD5) + { + if (!HasPluginsDecoder()) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else + { + std::unique_ptr<TranscoderDataSource::Transcoded> transcoded( + TranscoderDataSource::Transcode(*transcoderReader, attachment, DicomTransferSyntax_LittleEndianExplicit, checkMD5)); + + TranscoderDataSource::Transcoded::Lock lock(*transcoded); + + return DecodeFrameBuiltin(lock.GetDicom(), frameIndex); + } + } + + + ImageAccessor* ServerTranscoder::DecodeFrame(const ParsedDicomFile& dicom, const void* buffer, size_t size, unsigned int frameIndex) @@ -76,83 +291,124 @@ if (builtinDecoderTranscoderOrder_ == BuiltinDecoderTranscoderOrder_Before) { - // Use Orthanc's built-in decoder - - try + decoded.reset(DecodeFrameBuiltin(dicom, 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(buffer, size, 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(dicom, frameIndex)); if (decoded.get() != NULL) { return decoded.release(); } - else if (builtinDecoderTranscoderOrder_ == BuiltinDecoderTranscoderOrder_After) + } + + decoded.reset(DecodeFrameUsingPluginsTranscoder(buffer, size, 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, + bool checkMD5) + { + // 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, checkMD5)); + break; + + case WorkingSource_PluginsDecoder: + decoded.reset(DecodeFrameUsingPluginsDecoder(storageAreaReader, attachment, frameIndex, checkMD5)); + break; + + case WorkingSource_PluginsTranscoder: + decoded.reset(DecodeFrameUsingPluginsTranscoder(transcoderReader, attachment, frameIndex, checkMD5)); + 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, checkMD5)); + + 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, checkMD5)); + + if (decoded.get() != NULL) + { + pimpl_->SetWorkingSource(attachment.GetUuid(), WorkingSource_PluginsDecoder); + return decoded.release(); + } + } if (builtinDecoderTranscoderOrder_ == BuiltinDecoderTranscoderOrder_After) { - try + decoded.reset(DecodeFrameBuiltin(dicomReader, attachment, frameIndex, checkMD5)); + + 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, checkMD5)); - 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/ServerTranscoder.h Thu May 21 08:59:49 2026 +0200 @@ -24,12 +24,17 @@ #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; + #if ORTHANC_ENABLE_PLUGINS == 1 class OrthancPlugins; #endif @@ -37,13 +42,43 @@ 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); + + // TODO-Streaming : remove + 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, + bool checkMD5); + + ImageAccessor* DecodeFrameUsingPluginsDecoder(const boost::shared_ptr<DataSourceReader>& storageAreaReader, + const FileInfo& attachment, + unsigned int frameIndex, + bool checkMD5); + + ImageAccessor* DecodeFrameUsingPluginsTranscoder(const boost::shared_ptr<DataSourceReader>& transcoderReader, + const FileInfo& attachment, + unsigned int frameIndex, + bool checkMD5); + public: ServerTranscoder(unsigned int maxConcurrentDcmtkTranscoder); @@ -51,10 +86,21 @@ void SetPlugins(OrthancPlugins& plugins); #endif - ImageAccessor* DecodeFrame(const ParsedDicomFile& parsedDicom, + bool HasPluginsDecoder() const; + + bool HasPluginsTranscoder() const; + + ImageAccessor* DecodeFrame(const ParsedDicomFile& dicom, const void* buffer, // buffer that is the source of the ParsedDicomFile size_t size, - unsigned int frameIndex); + unsigned int frameIndex); // TODO-Streaming : Remove this flavor + + 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, + bool checkMD5); // This method can be used even if the global option // "TranscodeDicomProtocol" is set to "false"
--- a/OrthancServer/Sources/main.cpp Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/Sources/main.cpp Thu May 21 08:59:49 2026 +0200 @@ -53,6 +53,7 @@ #include "ServerJobs/DicomRetrieveScuBaseJob.h" #include "ServerJobs/StorageCommitmentScpJob.h" #include "ServerToolbox.h" +#include "ServerTranscoder.h" #include "StorageCommitmentReports.h" #include <boost/algorithm/string/predicate.hpp> @@ -1608,19 +1609,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 +1633,6 @@ context_.ResetPlugins(); } #endif - - context_.ResetTranscoder(); } }; } @@ -1709,8 +1703,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 +2180,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 +2232,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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/UnitTestsSources/ServerConfigTests.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/UnitTestsSources/ServerIndexTests.cpp Thu May 21 08:59:49 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 Thu May 21 08:54:11 2026 +0200 +++ b/OrthancServer/UnitTestsSources/ServerJobsTests.cpp Thu May 21 08:59:49 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(); } {
