Mercurial > hg > orthanc
changeset 6821:ee0a1ce93912 streaming
created TranscoderDataSource
| author | Sebastien Jodogne <s.jodogne@gmail.com> |
|---|---|
| date | Tue, 19 May 2026 21:59:55 +0200 |
| parents | f300221d97d6 |
| children | 5eac7f32c814 |
| files | OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp OrthancFramework/Sources/DataSource/TranscoderDataSource.h |
| diffstat | 3 files changed, 341 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue May 19 20:41:04 2026 +0200 +++ b/OrthancFramework/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue May 19 21:59:55 2026 +0200 @@ -681,6 +681,7 @@ 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()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp Tue May 19 21:59:55 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 Tue May 19 21:59:55 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); + }; +}
