# HG changeset patch # User Alain Mazy # Date 1784279984 -7200 # Node ID 186b7ca8990d10a498a8c5e56034e52f0937a5ad # Parent 519b3bf8f0b84075b214dc4d0b5be1a62fa51d94# Parent a412fcd67ae6f008a6ea14d36219745b1498a04b merge diff -r 519b3bf8f0b8 -r 186b7ca8990d NEWS --- a/NEWS Fri Jul 17 11:18:45 2026 +0200 +++ b/NEWS Fri Jul 17 11:19:44 2026 +0200 @@ -4,7 +4,7 @@ General ------- -* Orthanc will now refuse to start if you have "AuthenticationEnabled" and "RemoteAccessAllowed" +* Orthanc will now refuse to start if you have "AuthenticationEnabled" and "RemoteAccessAllowed" set to true and have not defined any users in "RegisteredUsers". * New values for "OverwriteInstances" configuration. In previous versions, this configuration was a Boolean and it is now a string whose allowed values are "Never", "Always", @@ -32,7 +32,6 @@ - "HttpTimeout" was documented as 60 while its default value is 0 (no timeout). - "Name" was documented as "MyOrthanc" while its default value is "ORTHANC". - REST API -------- @@ -62,12 +61,15 @@ * Added OrthancPluginClearCurrentThreadName() to avoid storing many thread names when dynamically creating/killing threads in a plugin. * Added OD, OL, UC, UR, OV, SV, and UV values to the OrthancPluginValueRepresentation enumeration +* Added OrthancPluginDicomWebBinaryMode_ArrayOfValues to generate DICOMweb + representations with OW, OL, OV, OF, and OD value representations as array of values Maintenance ----------- * Added support for OV, SV, and UV value representations * Added support for multiple values in AT value representation +* Improved support of OW, OL, OV, OF, and OD value representations in DICOMweb JSON * Fix resubmit/pause/cancel and reloading of OrthancPeerStore and DicomModalityStore jobs (bug introduced in 1.12.10) * Fix OrthancPeerStore jobs that was using the loader threads only if transcoding diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Resources/CMake/DcmtkConfigurationStatic-3.7.0.cmake --- a/OrthancFramework/Resources/CMake/DcmtkConfigurationStatic-3.7.0.cmake Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Resources/CMake/DcmtkConfigurationStatic-3.7.0.cmake Fri Jul 17 11:19:44 2026 +0200 @@ -110,10 +110,19 @@ # C_CHAR_UNSIGNED *must* be set before calling "GenerateDCMTKConfigure.cmake" IF (CMAKE_CROSSCOMPILING) + message(STATUS "Cross-compiling to CMAKE_SYSTEM_PROCESSOR: ${CMAKE_SYSTEM_PROCESSOR}") if (CMAKE_COMPILER_IS_GNUCXX AND CMAKE_SYSTEM_NAME STREQUAL "Windows") # MinGW SET(C_CHAR_UNSIGNED 1 CACHE INTERNAL "Whether char is unsigned.") + elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux") + if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(mips|mips64|mipsel|mips64el)$") + message(STATUS "Target architecture is MIPS, the char type should be unsigned (rough guess)") + SET(C_CHAR_UNSIGNED 0 CACHE INTERNAL "") + else() + SET(C_CHAR_UNSIGNED 1 CACHE INTERNAL "") + endif() + elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten") # WebAssembly or asm.js # Check out "../WebAssembly/ArithmeticTests/" to regenerate the diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/DicomNetworking/DicomServer.cpp --- a/OrthancFramework/Sources/DicomNetworking/DicomServer.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/DicomNetworking/DicomServer.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -63,7 +63,7 @@ unsigned int maximumPduLength, bool useDicomTls) { - Logging::ScopedThreadNameSetter setter(server->name_); + Logging::ScopedCurrentThreadNameSetter setter(server->name_); CLOG(INFO, DICOM) << "DICOM server started"; while (server->continue_) diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.cpp --- a/OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -110,7 +110,7 @@ if (keyword != std::string(DcmTag_ERROR_TagName)) { node.append_attribute("keyword").set_value(keyword.c_str()); - } + } if (content.isMember(KEY_VALUE)) { @@ -409,6 +409,77 @@ #endif + template + static void ApplyBinaryModeToOtherTypes(Json::Value& node, + DicomWebJsonVisitor::BinaryMode mode, + const std::string& bulkDataUri, + const std::vector& values) + { + static const Endianness endianness = Toolbox::DetectEndianness(); + + switch (mode) + { + case DicomWebJsonVisitor::BinaryMode_Ignore: + break; + + case DicomWebJsonVisitor::BinaryMode_BulkDataUri: + if (!values.empty()) + { + node[KEY_BULK_DATA_URI] = bulkDataUri; + } + + break; + + case DicomWebJsonVisitor::BinaryMode_InlineBinary: + if (!values.empty()) + { + std::string raw; + raw.resize(values.size() * sizeof(MemoryType)); + + if (!raw.empty()) + { + MemoryType *p = reinterpret_cast(&raw[0]); + + for (size_t i = 0; i < values.size(); i++, p++) + { + *p = static_cast(values[i]); + } + + if (endianness == Endianness_Big) + { + Toolbox::SwapEndianness(raw, sizeof(MemoryType)); + } + } + + std::string base64; + Toolbox::EncodeBase64(base64, raw); + node[KEY_INLINE_BINARY] = base64; + } + + break; + + case DicomWebJsonVisitor::BinaryMode_ArrayOfValues: + if (!values.empty()) + { + node[KEY_VALUE] = Json::arrayValue; + Json::Value& a = node[KEY_VALUE]; + + for (size_t i = 0; i < values.size(); i++) + { + a.append(static_cast(values[i])); + } + } + + break; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + } + + ITagVisitor::Action DicomWebJsonVisitor::VisitNotSupported(const std::vector &parentTags, const std::vector &parentIndexes, @@ -490,6 +561,7 @@ node[KEY_BULK_DATA_URI] = bulkDataUri; break; + case BinaryMode_ArrayOfValues: case BinaryMode_InlineBinary: { std::string tmp(static_cast(data), size); @@ -519,6 +591,13 @@ ValueRepresentation vr, const std::vector& values) { + /** + * "If an attribute is present in DICOM but empty (i.e., Value + * Length is 0), it shall be preserved in the DICOM JSON attribute + * object containing no "Value", "BulkDataURI" or "InlineBinary"." + * https://dicom.nema.org/medical/dicom/current/output/chtml/part18/sect_f.2.5.html + **/ + assert((vr == ValueRepresentation_OtherWord && tag != DICOM_TAG_PIXEL_DATA) || vr == ValueRepresentation_OtherLong || vr == ValueRepresentation_OtherVeryLong || @@ -532,18 +611,61 @@ if (tag.GetElement() != 0x0000 && vr != ValueRepresentation_NotSupported) { - Json::Value& node = CreateNode(parentTags, parentIndexes, tag); - node[KEY_VR] = EnumerationToString(vr); - - if (!values.empty()) + if (vr == ValueRepresentation_OtherWord || + vr == ValueRepresentation_OtherLong || + vr == ValueRepresentation_OtherVeryLong) { - Json::Value content = Json::arrayValue; - for (size_t i = 0; i < values.size(); i++) + BinaryMode mode; + std::string bulkDataUri; + + if (formatter_ == NULL) { - content.append(FormatInteger(values[i])); + mode = BinaryMode_ArrayOfValues; // This was the default in Orthanc <= 1.12.11 + } + else + { + mode = formatter_->Format(bulkDataUri, parentTags, parentIndexes, tag, vr); } - node[KEY_VALUE] = content; + if (mode != BinaryMode_Ignore) + { + Json::Value& node = CreateNode(parentTags, parentIndexes, tag); + node[KEY_VR] = EnumerationToString(vr); + + switch (vr) + { + case ValueRepresentation_OtherWord: + ApplyBinaryModeToOtherTypes(node, mode, bulkDataUri, values); + break; + + case ValueRepresentation_OtherLong: + ApplyBinaryModeToOtherTypes(node, mode, bulkDataUri, values); + break; + + case ValueRepresentation_OtherVeryLong: + ApplyBinaryModeToOtherTypes(node, mode, bulkDataUri, values); + break; + + default: + throw OrthancException(ErrorCode_InternalError); + } + } + } + else + { + Json::Value& node = CreateNode(parentTags, parentIndexes, tag); + node[KEY_VR] = EnumerationToString(vr); + + if (!values.empty()) + { + Json::Value content = Json::arrayValue; + for (size_t i = 0; i < values.size(); i++) + { + content.append(FormatInteger(values[i])); + } + + node[KEY_VALUE] = content; + } } } @@ -557,6 +679,13 @@ ValueRepresentation vr, const std::vector& values) { + /** + * "If an attribute is present in DICOM but empty (i.e., Value + * Length is 0), it shall be preserved in the DICOM JSON attribute + * object containing no "Value", "BulkDataURI" or "InlineBinary"." + * https://dicom.nema.org/medical/dicom/current/output/chtml/part18/sect_f.2.5.html + **/ + assert(vr == ValueRepresentation_FloatingPointDouble || vr == ValueRepresentation_FloatingPointSingle || vr == ValueRepresentation_OtherDouble || @@ -565,18 +694,56 @@ if (tag.GetElement() != 0x0000 && vr != ValueRepresentation_NotSupported) { - Json::Value& node = CreateNode(parentTags, parentIndexes, tag); - node[KEY_VR] = EnumerationToString(vr); - - if (!values.empty()) + if (vr == ValueRepresentation_OtherDouble || + vr == ValueRepresentation_OtherFloat) { - Json::Value content = Json::arrayValue; - for (size_t i = 0; i < values.size(); i++) + BinaryMode mode; + std::string bulkDataUri; + + if (formatter_ == NULL) + { + mode = BinaryMode_ArrayOfValues; // This was the default in Orthanc <= 1.12.11 + } + else + { + mode = formatter_->Format(bulkDataUri, parentTags, parentIndexes, tag, vr); + } + + if (mode != BinaryMode_Ignore) { - content.append(FormatDouble(values[i])); + Json::Value& node = CreateNode(parentTags, parentIndexes, tag); + node[KEY_VR] = EnumerationToString(vr); + + switch (vr) + { + case ValueRepresentation_OtherDouble: + ApplyBinaryModeToOtherTypes(node, mode, bulkDataUri, values); + break; + + case ValueRepresentation_OtherFloat: + ApplyBinaryModeToOtherTypes(node, mode, bulkDataUri, values); + break; + + default: + throw OrthancException(ErrorCode_InternalError); + } } + } + else + { + Json::Value& node = CreateNode(parentTags, parentIndexes, tag); + node[KEY_VR] = EnumerationToString(vr); + + if (!values.empty()) + { + Json::Value content = Json::arrayValue; + for (size_t i = 0; i < values.size(); i++) + { + content.append(FormatDouble(values[i])); + } - node[KEY_VALUE] = content; + node[KEY_VALUE] = content; + } } } diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.h --- a/OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.h Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/DicomWebJsonVisitor.h Fri Jul 17 11:19:44 2026 +0200 @@ -39,11 +39,14 @@ class ORTHANC_PUBLIC DicomWebJsonVisitor : public ITagVisitor { public: + // This enumeration specifies how "other" value representations + // (OB, OW, OL, OV, OF, and OD) are encoded enum BinaryMode { BinaryMode_Ignore, BinaryMode_BulkDataUri, - BinaryMode_InlineBinary + BinaryMode_InlineBinary, + BinaryMode_ArrayOfValues // New in Orthanc 1.12.12 }; class IBinaryFormatter : public boost::noncopyable diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp --- a/OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -734,8 +734,7 @@ { DcmAttributeTag& e = dynamic_cast(element); - if (e.getUint16Array(content_).good() && - content_ != NULL) + if (e.getUint16Array(content_).good()) { if (element.getLength() % sizeof(Uint16) != 0) { @@ -743,6 +742,12 @@ } size_ = static_cast(element.getLength()) / sizeof(Uint16); + if (size_ != 0 && + content_ == NULL) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + if (size_ % 2 != 0) { throw OrthancException(ErrorCode_BadFileFormat); @@ -777,6 +782,8 @@ virtual void ReadValue(std::string& value, uint64_t index) const ORTHANC_OVERRIDE { + assert(valid_); + assert(index < size_); value = GetTag(index).Format(); } @@ -818,8 +825,7 @@ **/ DcmFloatingPointSingle& e = dynamic_cast(element); - if (e.getFloat32Array(content_).good() && - content_ != NULL) + if (e.getFloat32Array(content_).good()) { if (element.getLength() % sizeof(Float32) != 0) { @@ -827,6 +833,12 @@ } size_ = static_cast(element.getLength()) / sizeof(Float32); + if (size_ != 0 && + content_ == NULL) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + valid_ = true; } } @@ -846,6 +858,7 @@ uint64_t index) const ORTHANC_OVERRIDE { assert(valid_); + assert(index < size_); value = boost::lexical_cast(content_[index]); } @@ -890,8 +903,7 @@ **/ DcmFloatingPointDouble& e = dynamic_cast(element); - if (e.getFloat64Array(content_).good() && - content_ != NULL) + if (e.getFloat64Array(content_).good()) { if (element.getLength() % sizeof(Float64) != 0) { @@ -899,6 +911,12 @@ } size_ = static_cast(element.getLength()) / sizeof(Float64); + if (size_ != 0 && + content_ == NULL) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + valid_ = true; } } @@ -918,6 +936,7 @@ uint64_t index) const ORTHANC_OVERRIDE { assert(valid_); + assert(index < size_); value = boost::lexical_cast(content_[index]); } @@ -953,8 +972,7 @@ { DcmOtherByteOtherWord& e = dynamic_cast(element); - if (e.getUint16Array(content_).good() && - content_ != NULL) + if (e.getUint16Array(content_).good()) { if (element.getLength() % sizeof(Uint16) != 0) { @@ -962,6 +980,12 @@ } size_ = static_cast(element.getLength()) / sizeof(Uint16); + if (size_ != 0 && + content_ == NULL) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + valid_ = true; } } @@ -981,6 +1005,7 @@ uint64_t index) const ORTHANC_OVERRIDE { assert(valid_); + assert(index < size_); value = boost::lexical_cast(content_[index]); } @@ -1022,8 +1047,7 @@ **/ DcmUnsignedLong& e = dynamic_cast(element); - if (e.getUint32Array(content_).good() && - content_ != NULL) + if (e.getUint32Array(content_).good()) { if (element.getLength() % sizeof(Uint32) != 0) { @@ -1031,6 +1055,12 @@ } size_ = static_cast(element.getLength()) / sizeof(Uint32); + if (size_ != 0 && + content_ == NULL) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + valid_ = true; } } @@ -1050,6 +1080,7 @@ uint64_t index) const ORTHANC_OVERRIDE { assert(valid_); + assert(index < size_); value = boost::lexical_cast(content_[index]); } @@ -1086,8 +1117,7 @@ { DcmUnsigned64bitVeryLong& e = dynamic_cast(element); - if (e.getUint64Array(content_).good() && - content_ != NULL) + if (e.getUint64Array(content_).good()) { if (element.getLength() % sizeof(Uint64) != 0) { @@ -1095,6 +1125,12 @@ } size_ = static_cast(element.getLength()) / sizeof(Uint64); + if (size_ != 0 && + content_ == NULL) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + valid_ = true; } } @@ -1114,6 +1150,7 @@ uint64_t index) const ORTHANC_OVERRIDE { assert(valid_); + assert(index < size_); value = boost::lexical_cast(content_[index]); } diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/JobsEngine/JobsEngine.cpp --- a/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/JobsEngine/JobsEngine.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -109,7 +109,7 @@ void JobsEngine::RetryHandler(JobsEngine* engine) { - Logging::ScopedThreadNameSetter setter("JOBS-RETRY"); + Logging::ScopedCurrentThreadNameSetter setter("JOBS-RETRY"); assert(engine != NULL); @@ -125,7 +125,7 @@ size_t workerIndex) { assert(engine != NULL); - Logging::ScopedThreadNameSetter setter(std::string("JOBS-WORKER-") + boost::lexical_cast(workerIndex)); + Logging::ScopedCurrentThreadNameSetter setter(std::string("JOBS-WORKER-") + boost::lexical_cast(workerIndex)); CLOG(INFO, JOBS) << "Worker thread " << workerIndex << " has started"; while (engine->IsRunning()) diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/Logging.cpp --- a/OrthancFramework/Sources/Logging.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/Logging.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -46,6 +46,8 @@ { namespace Logging { + static bool logCallerThreadNameInContext = false; // add a "from THREADNAME" in the context everytime the context is copied from a thread to the other to help track the runnable/callable journey + static const uint32_t ALL_CATEGORIES_MASK = 0xffffffff; static uint32_t infoCategoriesMask_ = 0; @@ -537,10 +539,12 @@ #include "SystemToolbox.h" #include "Toolbox.h" +#include #include #include #include #include +#include namespace @@ -620,6 +624,324 @@ } +static const size_t THREAD_NAME_MAX_SIZE = 16; // Thread names are limited to 16 char + a space + + +static std::string FormatThreadName(const std::string& name) +{ + if (name.size() < THREAD_NAME_MAX_SIZE) + { + return std::string(THREAD_NAME_MAX_SIZE - name.size(), ' ') + name; + } + else if (name.size() == THREAD_NAME_MAX_SIZE) + { + return name; + } + else + { + return name.substr(THREAD_NAME_MAX_SIZE); + } +} + + +namespace +{ + class ThreadInformation : public boost::noncopyable + { + private: + bool hasName_; + std::string name_; + std::list context_; + + public: + ThreadInformation() : + hasName_(false) + { + } + + void SetThreadName(const std::string& name) + { +#if !defined(NDEBUG) + if (hasName_) + { + throw Orthanc::OrthancException( + Orthanc::ErrorCode_InternalError, "This thread already has a name or another thread is re-using " + "the same threadId and you have not called 'ClearCurrentThreadName()': " + name, + false /* don't log to avoid deadlock */); + } +#endif + + hasName_ = true; + name_ = FormatThreadName(name); + + assert(name_.size() == THREAD_NAME_MAX_SIZE); + } + + void ClearThreadName() + { + hasName_ = false; + name_.clear(); + } + + bool HasThreadName() const + { + return hasName_; + } + + std::string GetThreadName() const + { + if (hasName_) + { + return name_; + } + else + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + } + + void PopContext() + { + if (context_.empty()) + { +#if !defined(NDEBUG) + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "Cannot pop from an empty context", + false /* don't log to avoid deadlock */); +#endif + } + else + { + context_.pop_back(); + } + } + + void PushContext(const std::string& message) + { + context_.push_back(message); + } + + const std::list& GetContext() const + { + return context_; + } + }; + + + class ThreadsInformations : public boost::noncopyable + { + private: + typedef std::map Content; + + boost::shared_mutex mutex_; + Content content_; + + public: + ~ThreadsInformations() + { + Clear(); + } + + void Clear() + { + boost::unique_lock lock(mutex_); + + for (Content::iterator it = content_.begin(); it != content_.end(); ++it) + { + assert(it->second != NULL); + delete it->second; + } + + content_.clear(); + } + + + class CurrentThreadReader : public boost::noncopyable + { + private: + boost::shared_lock lock_; + boost::thread::id threadId_; + bool exists_; + const ThreadInformation* information_; + + public: + explicit CurrentThreadReader(ThreadsInformations& informations) : + lock_(informations.mutex_), + threadId_(boost::this_thread::get_id()), + exists_(false), + information_(NULL) + { + Content::const_iterator found = informations.content_.find(threadId_); + + if (found != informations.content_.end()) + { + assert(found->second != NULL); + + exists_ = true; + information_ = found->second; + } + } + + std::string GetThreadName() const + { + assert(!exists_ || information_ != NULL); + + if (exists_ && + information_->HasThreadName()) + { + return information_->GetThreadName(); + } + else + { + return FormatThreadName(boost::lexical_cast(threadId_)); + } + } + + bool HasThreadName() const + { + assert(!exists_ || information_ != NULL); + return (exists_ && + information_->HasThreadName()); + } + + void FormatContext(std::string& context) const + { + assert(!exists_ || information_ != NULL); + if (exists_ && + !information_->GetContext().empty()) + { + context = "| " + boost::algorithm::join(information_->GetContext(), std::string(" | ")) + " | "; + } + } + + void CopyContext(std::list& target) const + { + if (exists_) + { + assert(information_ != NULL); + target = information_->GetContext(); + } + else + { + target.clear(); + } + } + }; + + + class CurrentThreadWriter : public boost::noncopyable + { + private: + ThreadsInformations& informations_; + boost::unique_lock lock_; + boost::thread::id threadId_; + ThreadInformation* information_; + bool valid_; + + void Recycle() + { + if (!information_->HasThreadName() && + information_->GetContext().empty()) + { + Content::iterator found = informations_.content_.find(threadId_); + assert(found != informations_.content_.end()); + assert(found->second != NULL); + + delete found->second; + informations_.content_.erase(found); + + valid_ = false; + } + } + + public: + explicit CurrentThreadWriter(ThreadsInformations& informations) : + informations_(informations), + lock_(informations.mutex_), + threadId_(boost::this_thread::get_id()), + valid_(true) + { + Content::iterator found = informations.content_.find(threadId_); + + if (found == informations.content_.end()) + { + information_ = new ThreadInformation; + informations.content_[threadId_] = information_; + } + else + { + assert(found->second != NULL); + information_ = found->second; + } + } + + void SetThreadName(const std::string& name) + { + if (!valid_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + assert(information_ != NULL); + information_->SetThreadName(name); + +#if !defined(NDEBUG) + const std::string formattedName = information_->GetThreadName(); + + for (Content::const_iterator it = informations_.content_.begin(); it != informations_.content_.end(); ++it) + { + assert(it->second != NULL); + if (it->first != threadId_ && + it->second->HasThreadName() && + it->second->GetThreadName() == formattedName) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, + "Another thread already uses this thread name: " + name, + false /* don't log to avoid deadlock */); + } + } +#endif + } + + void PushContext(const std::string& context) + { + if (!valid_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + assert(information_ != NULL); + information_->PushContext(context); + } + + void ClearThreadName() + { + if (!valid_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + assert(information_ != NULL); + information_->ClearThreadName(); + + Recycle(); + } + + void PopContext() + { + if (!valid_) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + + assert(information_ != NULL); + information_->PopContext(); + + Recycle(); + } + }; + }; +} + static std::unique_ptr loggingStreamsContext_; static boost::mutex loggingStreamsMutex_; @@ -628,9 +950,9 @@ static std::string pluginName_; // this string can only be non-empty if running from a plugin static bool hasOrthancAdvancedLogging_ = false; // Whether the Orthanc runtime is >= 1.12.4 static bool hasClearThreadName_ = false; // Whether the Orthanc runtime is >= 1.12.12 -static boost::shared_mutex threadNamesMutex_; -static std::map threadNames_; +static ThreadsInformations threadsInformations_; static bool enableThreadNames_ = true; +static bool enableThreadContexts_ = true; static std::list loggingListeners_; static boost::shared_mutex loggingListenersMutex_; @@ -639,11 +961,16 @@ { namespace Logging { - void EnableThreadNames(bool enabled) + void SetThreadNamesEnabled(bool enabled) { enableThreadNames_ = enabled; } + void SetThreadContextsEnabled(bool enabled) + { + enableThreadContexts_ = enabled; + } + static void GetLogPath(boost::filesystem::path& log, boost::filesystem::path& link, const std::string& suffix, @@ -704,18 +1031,17 @@ void SetCurrentThreadName(const std::string& name) { - if (name.size() > 16) + if (name.size() > THREAD_NAME_MAX_SIZE) { - throw OrthancException(ErrorCode_InternalError, std::string("Thread name can not exceed 16 characters: ") + name); + throw OrthancException(ErrorCode_InternalError, "Thread name can not exceed " + + boost::lexical_cast(THREAD_NAME_MAX_SIZE) + ": " + name); } if (pluginContext_ == NULL) { - const boost::thread::id threadId = boost::this_thread::get_id(); - { - boost::unique_lock lock(threadNamesMutex_); - threadNames_[threadId] = name; + ThreadsInformations::CurrentThreadWriter writer(threadsInformations_); + writer.SetThreadName(name); } #if defined(__linux__) && !defined(NDEBUG) && !defined(__LSB_VERSION__) @@ -732,11 +1058,14 @@ bool HasCurrentThreadName() { - const boost::thread::id threadId = boost::this_thread::get_id(); - + if (pluginContext_ == NULL) { - boost::shared_lock lock(threadNamesMutex_); - return threadNames_.find(threadId) != threadNames_.end(); + ThreadsInformations::CurrentThreadReader reader(threadsInformations_); + return reader.HasThreadName(); + } + else + { + throw OrthancException(ErrorCode_NotImplemented); // Not available for plugins } } @@ -745,12 +1074,8 @@ { if (pluginContext_ == NULL) { - const boost::thread::id threadId = boost::this_thread::get_id(); - - { - boost::unique_lock lock(threadNamesMutex_); - threadNames_.erase(threadId); - } + ThreadsInformations::CurrentThreadWriter writer(threadsInformations_); + writer.ClearThreadName(); } else if (hasClearThreadName_) // only recent runtimes support it (from 1.12.12) { @@ -759,34 +1084,96 @@ } - static std::string GetCurrentThreadName() + std::string GetCurrentThreadName() { - const boost::thread::id threadId = boost::this_thread::get_id(); - + if (pluginContext_ == NULL) { - boost::shared_lock lock(threadNamesMutex_); - - std::map::const_iterator found = threadNames_.find(threadId); - if (found != threadNames_.end()) - { - return found->second; - } + ThreadsInformations::CurrentThreadReader reader(threadsInformations_); + return reader.GetThreadName(); } - - // "SetCurrentThreadName()" has not been invoked for this thread - return boost::lexical_cast(threadId); + else + { + throw OrthancException(ErrorCode_NotImplemented); // Not available for plugins + } } - ScopedThreadNameSetter::ScopedThreadNameSetter(const std::string& threadName) + static void PushCurrentThreadContext(const std::string& context) { - SetCurrentThreadName(threadName); + if (context.empty()) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + else + { + ThreadsInformations::CurrentThreadWriter writer(threadsInformations_); + writer.PushContext(context); + } + } + + + static void PopCurrentThreadContext() + { + ThreadsInformations::CurrentThreadWriter writer(threadsInformations_); + writer.PopContext(); } - ScopedThreadNameSetter::~ScopedThreadNameSetter() + struct ThreadContextMemento::PImpl : public boost::noncopyable + { + std::list context_; + std::string threadName_; + }; + + + ThreadContextMemento::ScopedSetter::ScopedSetter(const ThreadContextMemento& memento) : + count_(0) + { + if (logCallerThreadNameInContext && enableThreadNames_ && !memento.pimpl_->threadName_.empty()) + { + PushCurrentThreadContext("from " + memento.pimpl_->threadName_); + count_++; + } + + for (std::list::const_iterator it = memento.pimpl_->context_.begin(); it != memento.pimpl_->context_.end(); ++it) + { + PushCurrentThreadContext(*it); + count_++; + } + } + + + ThreadContextMemento::ScopedSetter::~ScopedSetter() { - ClearCurrentThreadName(); + for (size_t i = 0; i < count_; i++) + { + PopCurrentThreadContext(); + } + } + + + ThreadContextMemento::ThreadContextMemento() : + pimpl_(new PImpl) + { + ThreadsInformations::CurrentThreadReader reader(threadsInformations_); + reader.CopyContext(pimpl_->context_); + if (reader.HasThreadName()) + { + pimpl_->threadName_ = reader.GetThreadName(); + } + } + + + ThreadContextMemento::~ThreadContextMemento() + { + assert(pimpl_ != NULL); + delete pimpl_; + } + + + ThreadContextMemento* CreateCurrentThreadContextMemento() + { + return new ThreadContextMemento; } @@ -870,14 +1257,23 @@ static_cast(duration.seconds()), static_cast(duration.fractional_seconds())); - char threadName[20]; // thread names are limited to 16 char + a space - if (enableThreadNames_) + std::string threadName; + std::string context; + + if (enableThreadNames_ || + enableThreadContexts_) { - sprintf(threadName, "%16s ", GetCurrentThreadName().c_str()); - } - else - { - threadName[0] = '\0'; + ThreadsInformations::CurrentThreadReader reader(threadsInformations_); + + if (enableThreadNames_) + { + threadName = reader.GetThreadName() + " "; + } + + if (enableThreadContexts_) + { + reader.FormatContext(context); + } } std::string internalPluginName = ""; @@ -887,7 +1283,7 @@ } prefix = (std::string(date) + threadName + internalPluginName + path.filename().string() + ":" + - boost::lexical_cast(line) + "] "); + boost::lexical_cast(line) + "] " + context); if (level != LogLevel_ERROR && level != LogLevel_WARNING && @@ -989,6 +1385,18 @@ } + ScopedCurrentThreadContextSetter::ScopedCurrentThreadContextSetter(const std::string& context) + { + PushCurrentThreadContext(context); + } + + + ScopedCurrentThreadContextSetter::~ScopedCurrentThreadContextSetter() + { + PopCurrentThreadContext(); + } + + struct InternalLogger::PImpl { boost::mutex::scoped_lock lock_; @@ -1022,6 +1430,16 @@ // set to "/dev/null" return; } + else + { + if (enableThreadContexts_) + { + ThreadsInformations::CurrentThreadReader reader(threadsInformations_); + std::string prefix; + reader.FormatContext(prefix); + messageStream_ << prefix; + } + } } else { diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/Logging.h --- a/OrthancFramework/Sources/Logging.h Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/Logging.h Fri Jul 17 11:19:44 2026 +0200 @@ -28,6 +28,7 @@ #include "OrthancFramework.h" #include "Compatibility.h" +#include #include #include @@ -75,18 +76,37 @@ LogCategory_LUA = (1 << 6) }; - class ScopedThreadNameSetter + + class ThreadContextMemento : public boost::noncopyable + { + private: + struct PImpl; + PImpl* pimpl_; + + public: + ThreadContextMemento(); + + ~ThreadContextMemento(); + + class ScopedSetter : public boost::noncopyable + { + private: + size_t count_; + + public: + ScopedSetter(const ThreadContextMemento& memento); + + ~ScopedSetter(); + }; + }; + + + class ILoggingListener : public boost::noncopyable { public: - explicit ScopedThreadNameSetter(const std::string& threadName); - - ~ScopedThreadNameSetter(); - }; - - class ILoggingListener - { - public: - virtual ~ILoggingListener() {} + virtual ~ILoggingListener() + { + } virtual void HandleLog(LogLevel level, LogCategory category, @@ -96,6 +116,7 @@ const std::string& message) = 0; }; + ORTHANC_PUBLIC const char* EnumerationToString(LogLevel level); ORTHANC_PUBLIC LogLevel StringToLogLevel(const char* level); @@ -114,18 +135,24 @@ ORTHANC_PUBLIC void Flush(); + ORTHANC_PUBLIC void SetThreadNamesEnabled(bool enabled); + + ORTHANC_PUBLIC void SetThreadContextsEnabled(bool enabled); + ORTHANC_PUBLIC void SetCurrentThreadName(const std::string& name); ORTHANC_PUBLIC bool HasCurrentThreadName(); ORTHANC_PUBLIC void ClearCurrentThreadName(); + ORTHANC_PUBLIC std::string GetCurrentThreadName(); + + ORTHANC_PUBLIC ThreadContextMemento* CreateCurrentThreadContextMemento(); + ORTHANC_PUBLIC void AddLoggingListener(ILoggingListener* listener); ORTHANC_PUBLIC void ClearLoggingListeners(); - ORTHANC_PUBLIC void EnableThreadNames(bool enabled); - ORTHANC_PUBLIC void EnableInfoLevel(bool enabled); ORTHANC_PUBLIC void EnableTraceLevel(bool enabled); @@ -154,7 +181,32 @@ ORTHANC_PUBLIC void SetTargetFolder(const std::string& path); - struct ORTHANC_LOCAL NullStream : public std::ostream + + class ScopedCurrentThreadNameSetter : public boost::noncopyable + { + public: + explicit ScopedCurrentThreadNameSetter(const std::string& threadName) + { + SetCurrentThreadName(threadName); + } + + ~ScopedCurrentThreadNameSetter() + { + ClearCurrentThreadName(); + } + }; + + + class ScopedCurrentThreadContextSetter : public boost::noncopyable + { + public: + explicit ScopedCurrentThreadContextSetter(const std::string& context); + + ~ScopedCurrentThreadContextSetter(); + }; + + + struct ORTHANC_LOCAL NullStream : public std::ostream { NullStream() : std::ios(0), @@ -234,7 +286,6 @@ ORTHANC_ENABLE_LOGGING_STDIO == 1) // This is notably for WebAssembly -#include #include namespace Orthanc @@ -276,7 +327,6 @@ #if (ORTHANC_ENABLE_LOGGING == 1 && \ ORTHANC_ENABLE_LOGGING_STDIO == 0) -#include #include namespace Orthanc diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp --- a/OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/MultiThreading/RunnableWorkersPool.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -48,7 +48,7 @@ static void WorkerThread(Worker* that) { - Logging::ScopedThreadNameSetter setter(that->threadName_); + Logging::ScopedCurrentThreadNameSetter setter(that->threadName_); while (that->continue_) { diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/MultiThreading/ThreadPool.cpp --- a/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/MultiThreading/ThreadPool.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -209,7 +209,7 @@ void ThreadPool::WorkerLoop(const std::string& threadName) { - Logging::SetCurrentThreadName(threadName); + Logging::ScopedCurrentThreadNameSetter setter(threadName); while (true) { diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/Toolbox.cpp --- a/OrthancFramework/Sources/Toolbox.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/Toolbox.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -313,6 +313,8 @@ pimpl_->md5_.get_digest(digest); target.resize(32); + +# if BOOST_ENDIAN_LITTLE_BYTE sprintf(&target[0], "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", (digest[0] >> 0) & 0xFF, (digest[0] >> 8) & 0xFF, @@ -330,6 +332,25 @@ (digest[3] >> 8) & 0xFF, (digest[3] >> 16) & 0xFF, (digest[3] >> 24) & 0xFF); +# else + sprintf(&target[0], "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", + (digest[0] >> 24) & 0xFF, + (digest[0] >> 16) & 0xFF, + (digest[0] >> 8) & 0xFF, + (digest[0] >> 0) & 0xFF, + (digest[1] >> 24) & 0xFF, + (digest[1] >> 16) & 0xFF, + (digest[1] >> 8) & 0xFF, + (digest[1] >> 0) & 0xFF, + (digest[2] >> 24) & 0xFF, + (digest[2] >> 16) & 0xFF, + (digest[2] >> 8) & 0xFF, + (digest[2] >> 0) & 0xFF, + (digest[3] >> 24) & 0xFF, + (digest[3] >> 16) & 0xFF, + (digest[3] >> 8) & 0xFF, + (digest[3] >> 0) & 0xFF); +# endif # endif //BOOST_VERSION >= 108600 } @@ -3315,6 +3336,47 @@ throw OrthancException(ErrorCode_ParameterOutOfRange, "Not a valid version: " + std::string(version)); } } + + + void Toolbox::SwapEndianness(void* data, + size_t dataSize, + size_t itemSize) + { + if (itemSize == 0 || + dataSize % itemSize != 0) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + + if (itemSize != 1) + { + uint8_t* p = reinterpret_cast(data); + const size_t count = dataSize / itemSize; + + for (size_t i = 0; i < count; i++) + { + for (size_t b1 = 0; b1 < itemSize / 2; b1++) + { + size_t b2 = itemSize - 1 - b1; + uint8_t tmp = p[b1]; + p[b1] = p[b2]; + p[b2] = tmp; + } + + p += itemSize; + } + } + } + + + void Toolbox::SwapEndianness(std::string& buffer, + size_t itemSize) + { + if (!buffer.empty()) + { + SwapEndianness(&buffer[0], buffer.size(), itemSize); + } + } } diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/Sources/Toolbox.h --- a/OrthancFramework/Sources/Toolbox.h Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/Sources/Toolbox.h Fri Jul 17 11:19:44 2026 +0200 @@ -432,6 +432,13 @@ unsigned int major, unsigned int minor, unsigned int revision); + + static void SwapEndianness(void* data, + size_t dataSize, + size_t itemSize); + + static void SwapEndianness(std::string& buffer, + size_t itemSize); }; } diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/UnitTestsSources/DicomMapTests.cpp --- a/OrthancFramework/UnitTestsSources/DicomMapTests.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/DicomMapTests.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -1188,6 +1188,760 @@ } +// Mock formatter for testing OW/OL/OF/OD binary encoding modes +namespace +{ + class MockBinaryFormatter : public DicomWebJsonVisitor::IBinaryFormatter + { + private: + DicomWebJsonVisitor::BinaryMode mode_; + + public: + explicit MockBinaryFormatter(DicomWebJsonVisitor::BinaryMode mode) : + mode_(mode) + { + } + + virtual DicomWebJsonVisitor::BinaryMode Format(std::string& bulkDataUri, + const std::vector& parentTags, + const std::vector& parentIndexes, + const DicomTag& tag, + ValueRepresentation vr) ORTHANC_OVERRIDE + { + if (mode_ == DicomWebJsonVisitor::BinaryMode_BulkDataUri) + { + bulkDataUri = "http://bulk/" + tag.Format(); + } + else + { + bulkDataUri.clear(); + } + + return mode_; + } + }; +} + + +TEST(DicomWebJson, VisitIntegers) +{ + std::vector parentTags; + std::vector parentIndexes; + DicomTag tag(0x0066, 0x0041); // Long Triangle Point Index List (OL, but doesn't matter for this test) + + std::set vr; + vr.insert(ValueRepresentation_OtherWord); + vr.insert(ValueRepresentation_OtherLong); + +#if DCMTK_VERSION_NUMBER >= 365 + vr.insert(ValueRepresentation_OtherVeryLong); +#endif + + // Firstly, test with an empty array of values + + std::vector values; + + for (std::set::const_iterator it = vr.begin(); it != vr.end(); ++it) + { + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_Ignore); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_FALSE(result.isMember("00660041")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_BulkDataUri); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660041")); + ASSERT_EQ(EnumerationToString(*it), result["00660041"]["vr"].asString()); + ASSERT_FALSE(result["00660041"].isMember("BulkDataURI")); + ASSERT_FALSE(result["00660041"].isMember("Value")); + ASSERT_FALSE(result["00660041"].isMember("InlineBinary")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_ArrayOfValues); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660041")); + ASSERT_EQ(EnumerationToString(*it), result["00660041"]["vr"].asString()); + ASSERT_FALSE(result["00660041"].isMember("BulkDataURI")); + ASSERT_FALSE(result["00660041"].isMember("Value")); + ASSERT_FALSE(result["00660041"].isMember("InlineBinary")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_InlineBinary); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660041")); + ASSERT_EQ(EnumerationToString(*it), result["00660041"]["vr"].asString()); + ASSERT_FALSE(result["00660041"].isMember("InlineBinary")); + ASSERT_FALSE(result["00660041"].isMember("Value")); + ASSERT_FALSE(result["00660041"].isMember("BulkDataURI")); + } + } + + // Secondly, test with non-empty array of values + + values.push_back(100); + values.push_back(200); + values.push_back(300); + + for (std::set::const_iterator it = vr.begin(); it != vr.end(); ++it) + { + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_Ignore); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_FALSE(result.isMember("00660041")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_BulkDataUri); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660041")); + ASSERT_EQ(EnumerationToString(*it), result["00660041"]["vr"].asString()); + ASSERT_TRUE(result["00660041"].isMember("BulkDataURI")); + ASSERT_EQ("http://bulk/0066,0041", result["00660041"]["BulkDataURI"].asString()); + ASSERT_FALSE(result["00660041"].isMember("Value")); + ASSERT_FALSE(result["00660041"].isMember("InlineBinary")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_ArrayOfValues); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660041")); + ASSERT_EQ(EnumerationToString(*it), result["00660041"]["vr"].asString()); + ASSERT_FALSE(result["00660041"].isMember("BulkDataURI")); + ASSERT_TRUE(result["00660041"].isMember("Value")); + ASSERT_FALSE(result["00660041"].isMember("InlineBinary")); + ASSERT_EQ(3u, result["00660041"]["Value"].size()); + ASSERT_EQ(100, result["00660041"]["Value"][0].asInt()); + ASSERT_EQ(200, result["00660041"]["Value"][1].asInt()); + ASSERT_EQ(300, result["00660041"]["Value"][2].asInt()); + } + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_InlineBinary); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, ValueRepresentation_OtherWord, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660041")); + ASSERT_EQ("OW", result["00660041"]["vr"].asString()); + ASSERT_TRUE(result["00660041"].isMember("InlineBinary")); + ASSERT_FALSE(result["00660041"].isMember("Value")); + ASSERT_FALSE(result["00660041"].isMember("BulkDataURI")); + + /** + * "Other" transfer syntaxes must use Little Endian: + * https://dicom.nema.org/medical/dicom/current/output/html/part18.html#sect_F.2.7 + * https://dicom.nema.org/medical/dicom/current/output/html/part05.html#sect_7.3 + **/ + ASSERT_EQ("ZADIACwB", result["00660041"]["InlineBinary"].asString()); + + std::string decoded; + Toolbox::DecodeBase64(decoded, result["00660041"]["InlineBinary"].asString()); + ASSERT_EQ(3 * sizeof(uint16_t), decoded.size()); + + if (Toolbox::DetectEndianness() == Endianness_Big) + { + std::swap(decoded[0], decoded[1]); + std::swap(decoded[2], decoded[3]); + std::swap(decoded[4], decoded[5]); + } + + uint16_t f0, f1, f2; + memcpy(&f0, decoded.c_str(), sizeof(uint16_t)); + memcpy(&f1, decoded.c_str() + sizeof(uint16_t), sizeof(uint16_t)); + memcpy(&f2, decoded.c_str() + 2 * sizeof(uint16_t), sizeof(uint16_t)); + ASSERT_EQ(100, f0); + ASSERT_EQ(200, f1); + ASSERT_EQ(300, f2); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_InlineBinary); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, ValueRepresentation_OtherLong, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660041")); + ASSERT_EQ("OL", result["00660041"]["vr"].asString()); + ASSERT_TRUE(result["00660041"].isMember("InlineBinary")); + ASSERT_FALSE(result["00660041"].isMember("Value")); + ASSERT_FALSE(result["00660041"].isMember("BulkDataURI")); + + ASSERT_EQ("ZAAAAMgAAAAsAQAA", result["00660041"]["InlineBinary"].asString()); + + std::string decoded; + Toolbox::DecodeBase64(decoded, result["00660041"]["InlineBinary"].asString()); + ASSERT_EQ(3 * sizeof(uint32_t), decoded.size()); + + if (Toolbox::DetectEndianness() == Endianness_Big) + { + Toolbox::SwapEndianness(decoded, sizeof(uint32_t)); + } + + uint32_t f0, f1, f2; + memcpy(&f0, decoded.c_str(), sizeof(uint32_t)); + memcpy(&f1, decoded.c_str() + sizeof(uint32_t), sizeof(uint32_t)); + memcpy(&f2, decoded.c_str() + 2 * sizeof(uint32_t), sizeof(uint32_t)); + ASSERT_EQ(100, f0); + ASSERT_EQ(200, f1); + ASSERT_EQ(300, f2); + } + +#if DCMTK_VERSION_NUMBER >= 365 + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_InlineBinary); + visitor.SetFormatter(formatter); + visitor.VisitIntegers(parentTags, parentIndexes, tag, ValueRepresentation_OtherVeryLong, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660041")); + ASSERT_EQ("OV", result["00660041"]["vr"].asString()); + ASSERT_TRUE(result["00660041"].isMember("InlineBinary")); + ASSERT_FALSE(result["00660041"].isMember("Value")); + ASSERT_FALSE(result["00660041"].isMember("BulkDataURI")); + + ASSERT_EQ("ZAAAAAAAAADIAAAAAAAAACwBAAAAAAAA", result["00660041"]["InlineBinary"].asString()); + + std::string decoded; + Toolbox::DecodeBase64(decoded, result["00660041"]["InlineBinary"].asString()); + ASSERT_EQ(3 * sizeof(uint64_t), decoded.size()); + + if (Toolbox::DetectEndianness() == Endianness_Big) + { + Toolbox::SwapEndianness(decoded, sizeof(uint64_t)); + } + + uint64_t f0, f1, f2; + memcpy(&f0, decoded.c_str(), sizeof(uint64_t)); + memcpy(&f1, decoded.c_str() + sizeof(uint64_t), sizeof(uint64_t)); + memcpy(&f2, decoded.c_str() + 2 * sizeof(uint64_t), sizeof(uint64_t)); + ASSERT_EQ(100, f0); + ASSERT_EQ(200, f1); + ASSERT_EQ(300, f2); + } +#endif +} + + +TEST(DicomWebJson, VisitDoubles) +{ + std::vector parentTags; + std::vector parentIndexes; + DicomTag tag(0x0066, 0x0027); // Triangle Point Index List (OF, but doesn't matter for this test) + + std::set vr; + vr.insert(ValueRepresentation_OtherFloat); + vr.insert(ValueRepresentation_OtherDouble); + + // Firstly, test with an empty array of values + + std::vector values; + + for (std::set::const_iterator it = vr.begin(); it != vr.end(); ++it) + { + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_Ignore); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_FALSE(result.isMember("00660027")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_BulkDataUri); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660027")); + ASSERT_EQ(EnumerationToString(*it), result["00660027"]["vr"].asString()); + ASSERT_FALSE(result["00660027"].isMember("BulkDataURI")); + ASSERT_FALSE(result["00660027"].isMember("Value")); + ASSERT_FALSE(result["00660027"].isMember("InlineBinary")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_ArrayOfValues); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660027")); + ASSERT_EQ(EnumerationToString(*it), result["00660027"]["vr"].asString()); + ASSERT_FALSE(result["00660027"].isMember("BulkDataURI")); + ASSERT_FALSE(result["00660027"].isMember("Value")); + ASSERT_FALSE(result["00660027"].isMember("InlineBinary")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_InlineBinary); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660027")); + ASSERT_EQ(EnumerationToString(*it), result["00660027"]["vr"].asString()); + ASSERT_FALSE(result["00660027"].isMember("InlineBinary")); + ASSERT_FALSE(result["00660027"].isMember("Value")); + ASSERT_FALSE(result["00660027"].isMember("BulkDataURI")); + } + } + + // Secondly, test with non-empty array of values + + values.push_back(1.5); + values.push_back(2.5); + + for (std::set::const_iterator it = vr.begin(); it != vr.end(); ++it) + { + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_Ignore); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_FALSE(result.isMember("00660027")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_BulkDataUri); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660027")); + ASSERT_EQ(EnumerationToString(*it), result["00660027"]["vr"].asString()); + ASSERT_TRUE(result["00660027"].isMember("BulkDataURI")); + ASSERT_EQ("http://bulk/0066,0027", result["00660027"]["BulkDataURI"].asString()); + ASSERT_FALSE(result["00660027"].isMember("Value")); + ASSERT_FALSE(result["00660027"].isMember("InlineBinary")); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_ArrayOfValues); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, *it, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660027")); + ASSERT_EQ(EnumerationToString(*it), result["00660027"]["vr"].asString()); + ASSERT_FALSE(result["00660027"].isMember("BulkDataURI")); + ASSERT_TRUE(result["00660027"].isMember("Value")); + ASSERT_FALSE(result["00660027"].isMember("InlineBinary")); + ASSERT_EQ(2u, result["00660027"]["Value"].size()); + ASSERT_FLOAT_EQ(1.5f, result["00660027"]["Value"][0].asFloat()); + ASSERT_FLOAT_EQ(2.5f, result["00660027"]["Value"][1].asFloat()); + } + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_InlineBinary); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, ValueRepresentation_OtherFloat, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660027")); + ASSERT_EQ("OF", result["00660027"]["vr"].asString()); + ASSERT_TRUE(result["00660027"].isMember("InlineBinary")); + ASSERT_FALSE(result["00660027"].isMember("Value")); + ASSERT_FALSE(result["00660027"].isMember("BulkDataURI")); + + /** + * "Other" transfer syntaxes must use Little Endian: + * https://dicom.nema.org/medical/dicom/current/output/html/part18.html#sect_F.2.7 + * https://dicom.nema.org/medical/dicom/current/output/html/part05.html#sect_7.3 + **/ + ASSERT_EQ("AADAPwAAIEA=", result["00660027"]["InlineBinary"].asString()); + + std::string decoded; + Toolbox::DecodeBase64(decoded, result["00660027"]["InlineBinary"].asString()); + ASSERT_EQ(2 * sizeof(float), decoded.size()); + + if (Toolbox::DetectEndianness() == Endianness_Big) + { + Toolbox::SwapEndianness(decoded, sizeof(float)); + } + + float f0, f1; + memcpy(&f0, decoded.c_str(), sizeof(float)); + memcpy(&f1, decoded.c_str() + sizeof(float), sizeof(float)); + ASSERT_FLOAT_EQ(1.5f, f0); + ASSERT_FLOAT_EQ(2.5f, f1); + } + + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(DicomWebJsonVisitor::BinaryMode_InlineBinary); + visitor.SetFormatter(formatter); + visitor.VisitDoubles(parentTags, parentIndexes, tag, ValueRepresentation_OtherDouble, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660027")); + ASSERT_EQ("OD", result["00660027"]["vr"].asString()); + ASSERT_TRUE(result["00660027"].isMember("InlineBinary")); + ASSERT_FALSE(result["00660027"].isMember("Value")); + ASSERT_FALSE(result["00660027"].isMember("BulkDataURI")); + + ASSERT_EQ("AAAAAAAA+D8AAAAAAAAEQA==", result["00660027"]["InlineBinary"].asString()); + + std::string decoded; + Toolbox::DecodeBase64(decoded, result["00660027"]["InlineBinary"].asString()); + ASSERT_EQ(2 * sizeof(double), decoded.size()); + + if (Toolbox::DetectEndianness() == Endianness_Big) + { + Toolbox::SwapEndianness(decoded, sizeof(double)); + } + + double f0, f1; + memcpy(&f0, decoded.c_str(), sizeof(double)); + memcpy(&f1, decoded.c_str() + sizeof(double), sizeof(double)); + ASSERT_DOUBLE_EQ(1.5f, f0); + ASSERT_DOUBLE_EQ(2.5f, f1); + } +} + + +TEST(DicomWebJson, OnlyOtherAffectedByIntegersFormatter) +{ + std::vector parentTags; + std::vector parentIndexes; + DicomTag tag(0x0028, 0x0008); // NumberOfFrames (IS, but doesn't matter for this test) + + std::vector values; + values.push_back(10); + + std::set modes; + modes.insert(DicomWebJsonVisitor::BinaryMode_Ignore); + modes.insert(DicomWebJsonVisitor::BinaryMode_BulkDataUri); + modes.insert(DicomWebJsonVisitor::BinaryMode_ArrayOfValues); + modes.insert(DicomWebJsonVisitor::BinaryMode_InlineBinary); + + std::set vrs; + vrs.insert(ValueRepresentation_SignedLong); + vrs.insert(ValueRepresentation_SignedShort); + vrs.insert(ValueRepresentation_UnsignedLong); + vrs.insert(ValueRepresentation_UnsignedShort); + +#if DCMTK_VERSION_NUMBER >= 365 + vrs.insert(ValueRepresentation_SignedVeryLong); + vrs.insert(ValueRepresentation_UnsignedVeryLong); +#endif + + for (std::set::const_iterator vr = vrs.begin(); vr != vrs.end(); ++vr) + { + for (std::set::const_iterator mode = modes.begin(); mode != modes.end(); ++mode) + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(*mode); + visitor.SetFormatter(formatter); + + visitor.VisitIntegers(parentTags, parentIndexes, tag, *vr, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00280008")); + ASSERT_EQ(EnumerationToString(*vr), result["00280008"]["vr"].asString()); + ASSERT_TRUE(result["00280008"].isMember("Value")); + ASSERT_FALSE(result["00280008"].isMember("BulkDataURI")); + ASSERT_FALSE(result["00280008"].isMember("InlineBinary")); + ASSERT_EQ(1u, result["00280008"]["Value"].size()); + ASSERT_EQ(10, result["00280008"]["Value"][0].asInt()); + } + } +} + + +TEST(DicomWebJson, OnlyOtherAffectedByDoublesFormatter) +{ + std::vector parentTags; + std::vector parentIndexes; + DicomTag tag(0x0066, 0x0027); // Triangle Point Index List (OF, but doesn't matter for this test) + + std::vector values; + values.push_back(21.5); + + std::set modes; + modes.insert(DicomWebJsonVisitor::BinaryMode_Ignore); + modes.insert(DicomWebJsonVisitor::BinaryMode_BulkDataUri); + modes.insert(DicomWebJsonVisitor::BinaryMode_ArrayOfValues); + modes.insert(DicomWebJsonVisitor::BinaryMode_InlineBinary); + + std::set vrs; + vrs.insert(ValueRepresentation_FloatingPointSingle); + vrs.insert(ValueRepresentation_FloatingPointDouble); + + for (std::set::const_iterator vr = vrs.begin(); vr != vrs.end(); ++vr) + { + for (std::set::const_iterator mode = modes.begin(); mode != modes.end(); ++mode) + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(*mode); + visitor.SetFormatter(formatter); + + visitor.VisitDoubles(parentTags, parentIndexes, tag, *vr, values); + + const Json::Value& result = visitor.GetResult(); + ASSERT_TRUE(result.isMember("00660027")); + ASSERT_EQ(EnumerationToString(*vr), result["00660027"]["vr"].asString()); + ASSERT_TRUE(result["00660027"].isMember("Value")); + ASSERT_FALSE(result["00660027"].isMember("BulkDataURI")); + ASSERT_FALSE(result["00660027"].isMember("InlineBinary")); + ASSERT_EQ(1u, result["00660027"]["Value"].size()); + ASSERT_FLOAT_EQ(21.5, result["00660027"]["Value"][0].asFloat()); + } + } +} + + +TEST(DicomWebJson, BinaryModes) +{ + ParsedDicomFile dicom(false); + dicom.ReplacePlainString(DicomTag(0x0010, 0x9431), "43.5"); // FL + dicom.ReplacePlainString(DicomTag(0x0008, 0x1163), "44.5"); // FD + dicom.ReplacePlainString(DicomTag(0x0008, 0x1160), "45"); // IS + dicom.ReplacePlainString(DicomTag(0x0028, 0x1201), "57"); // OW + dicom.ReplacePlainString(DicomTag(0x7fe0, 0x0009), "3.5"); // OD (other double) + dicom.ReplacePlainString(DicomTag(0x0064, 0x0009), "2.5"); // OF (other float) + dicom.ReplacePlainString(DicomTag(0x0066, 0x0040), "46"); // OL (other long) + dicom.ReplacePlainString(DicomTag(0x0008, 0x1161), "128"); // UL + dicom.ReplacePlainString(DicomTag(0x0008, 0x0301), "17"); // US + dicom.ReplacePlainString(DicomTag(0x0002, 0x0102), "MyPrivateInformation"); // OB + + { + DicomWebJsonVisitor visitor; + dicom.Apply(visitor); + + const Json::Value& result = visitor.GetResult(); + + ASSERT_EQ("FL", result["00109431"]["vr"].asString()); + ASSERT_FLOAT_EQ(43.5f, result["00109431"]["Value"][0].asFloat()); + ASSERT_EQ("FD", result["00081163"]["vr"].asString()); + ASSERT_FLOAT_EQ(44.5f, result["00081163"]["Value"][0].asFloat()); + ASSERT_EQ("IS", result["00081160"]["vr"].asString()); + ASSERT_FLOAT_EQ(45.0f, result["00081160"]["Value"][0].asFloat()); + ASSERT_EQ("UL", result["00081161"]["vr"].asString()); + ASSERT_EQ(128u, result["00081161"]["Value"][0].asUInt()); + +#if DCMTK_VERSION_NUMBER >= 361 + ASSERT_EQ("US", result["00080301"]["vr"].asString()); + ASSERT_EQ(17u, result["00080301"]["Value"][0].asUInt()); + + ASSERT_EQ("OD", result["7FE00009"]["vr"].asString()); + ASSERT_FLOAT_EQ(3.5f, result["7FE00009"]["Value"][0].asFloat()); + +#if DCMTK_VERSION_NUMBER > 361 + ASSERT_EQ("OL", result["00660040"]["vr"].asString()); +#else + // DCMTK 3.6.1 confused "OL" with "UL", so it was handled as an array of values + ASSERT_EQ("UL", result["00660040"]["vr"].asString()); +#endif + + ASSERT_EQ(46, result["00660040"]["Value"][0].asInt()); +#endif + + ASSERT_EQ("OW", result["00281201"]["vr"].asString()); + ASSERT_EQ(57, result["00281201"]["Value"][0].asInt()); + + ASSERT_EQ("OF", result["00640009"]["vr"].asString()); + ASSERT_FLOAT_EQ(2.5f, result["00640009"]["Value"][0].asFloat()); + + ASSERT_EQ("OB", result["00020102"]["vr"].asString()); + + std::string decoded; + Toolbox::DecodeBase64(decoded, result["00020102"]["InlineBinary"].asString()); + ASSERT_EQ("MyPrivateInformation", decoded); + } + + std::set modes; + modes.insert(DicomWebJsonVisitor::BinaryMode_Ignore); + modes.insert(DicomWebJsonVisitor::BinaryMode_BulkDataUri); + modes.insert(DicomWebJsonVisitor::BinaryMode_ArrayOfValues); + modes.insert(DicomWebJsonVisitor::BinaryMode_InlineBinary); + + for (std::set::const_iterator mode = modes.begin(); mode != modes.end(); ++mode) + { + DicomWebJsonVisitor visitor; + MockBinaryFormatter formatter(*mode); + visitor.SetFormatter(formatter); + dicom.Apply(visitor); + + const Json::Value& result = visitor.GetResult(); + + ASSERT_EQ("FL", result["00109431"]["vr"].asString()); + ASSERT_FLOAT_EQ(43.5f, result["00109431"]["Value"][0].asFloat()); + ASSERT_EQ("FD", result["00081163"]["vr"].asString()); + ASSERT_FLOAT_EQ(44.5f, result["00081163"]["Value"][0].asFloat()); + ASSERT_EQ("IS", result["00081160"]["vr"].asString()); + ASSERT_FLOAT_EQ(45.0f, result["00081160"]["Value"][0].asFloat()); + ASSERT_EQ("UL", result["00081161"]["vr"].asString()); + ASSERT_EQ(128u, result["00081161"]["Value"][0].asUInt()); + +#if DCMTK_VERSION_NUMBER >= 361 + ASSERT_EQ("US", result["00080301"]["vr"].asString()); + ASSERT_EQ(17u, result["00080301"]["Value"][0].asUInt()); +#endif + + switch (*mode) + { + case DicomWebJsonVisitor::BinaryMode_Ignore: +#if DCMTK_VERSION_NUMBER >= 361 + ASSERT_FALSE(result.isMember("7FE00009")); +#endif + +#if DCMTK_VERSION_NUMBER > 361 + ASSERT_FALSE(result.isMember("00660040")); // DCMTK 3.6.1 considered OL as a non-other VR +#endif + + ASSERT_FALSE(result.isMember("00281201")); + ASSERT_FALSE(result.isMember("00640009")); + ASSERT_FALSE(result.isMember("00020102")); + break; + + case DicomWebJsonVisitor::BinaryMode_BulkDataUri: +#if DCMTK_VERSION_NUMBER >= 361 + ASSERT_EQ("OD", result["7FE00009"]["vr"].asString()); +#endif + +#if DCMTK_VERSION_NUMBER > 361 + ASSERT_EQ("OL", result["00660040"]["vr"].asString()); + ASSERT_EQ("http://bulk/0066,0040", result["00660040"]["BulkDataURI"].asString()); +#endif + + ASSERT_EQ("OW", result["00281201"]["vr"].asString()); + ASSERT_EQ("OF", result["00640009"]["vr"].asString()); + ASSERT_EQ("OB", result["00020102"]["vr"].asString()); + + ASSERT_FALSE(result["00281201"].isMember("Value")); + ASSERT_TRUE(result["00281201"].isMember("BulkDataURI")); + ASSERT_FALSE(result["00281201"].isMember("InlineBinary")); + + ASSERT_EQ("http://bulk/0028,1201", result["00281201"]["BulkDataURI"].asString()); + ASSERT_EQ("http://bulk/0064,0009", result["00640009"]["BulkDataURI"].asString()); + ASSERT_EQ("http://bulk/7fe0,0009", result["7FE00009"]["BulkDataURI"].asString()); + ASSERT_EQ("http://bulk/0002,0102", result["00020102"]["BulkDataURI"].asString()); + break; + + case DicomWebJsonVisitor::BinaryMode_ArrayOfValues: + { +#if DCMTK_VERSION_NUMBER >= 361 + ASSERT_EQ("OD", result["7FE00009"]["vr"].asString()); +#endif + +#if DCMTK_VERSION_NUMBER > 361 + ASSERT_EQ("OL", result["00660040"]["vr"].asString()); +#endif + + ASSERT_EQ("OW", result["00281201"]["vr"].asString()); + ASSERT_EQ("OF", result["00640009"]["vr"].asString()); + ASSERT_EQ("OB", result["00020102"]["vr"].asString()); + + ASSERT_TRUE(result["00281201"].isMember("Value")); + ASSERT_FALSE(result["00281201"].isMember("BulkDataURI")); + ASSERT_FALSE(result["00281201"].isMember("InlineBinary")); + + ASSERT_EQ(57, result["00281201"]["Value"][0].asInt()); + ASSERT_FLOAT_EQ(2.5f, result["00640009"]["Value"][0].asFloat()); + +#if DCMTK_VERSION_NUMBER >= 361 + ASSERT_FLOAT_EQ(3.5f, result["7FE00009"]["Value"][0].asFloat()); + ASSERT_EQ(46, result["00660040"]["Value"][0].asInt()); +#endif + + // Special case: OB, UN, and OW for PixelData are always inlined + std::string decoded; + Toolbox::DecodeBase64(decoded, result["00020102"]["InlineBinary"].asString()); + ASSERT_EQ("MyPrivateInformation", decoded); + + break; + } + + case DicomWebJsonVisitor::BinaryMode_InlineBinary: + { +#if DCMTK_VERSION_NUMBER >= 361 + ASSERT_EQ("OD", result["7FE00009"]["vr"].asString()); +#endif + +#if DCMTK_VERSION_NUMBER > 361 + ASSERT_EQ("OL", result["00660040"]["vr"].asString()); +#endif + + ASSERT_EQ("OW", result["00281201"]["vr"].asString()); + ASSERT_EQ("OF", result["00640009"]["vr"].asString()); + ASSERT_EQ("OB", result["00020102"]["vr"].asString()); + + ASSERT_FALSE(result["00281201"].isMember("Value")); + ASSERT_FALSE(result["00281201"].isMember("BulkDataURI")); + ASSERT_TRUE(result["00281201"].isMember("InlineBinary")); + + ASSERT_EQ("OQA=", result["00281201"]["InlineBinary"].asString()); + ASSERT_EQ("AAAgQA==", result["00640009"]["InlineBinary"].asString()); + +#if DCMTK_VERSION_NUMBER >= 361 + ASSERT_EQ("AAAAAAAADEA=", result["7FE00009"]["InlineBinary"].asString()); +#endif + +#if DCMTK_VERSION_NUMBER > 361 + ASSERT_EQ("LgAAAA==", result["00660040"]["InlineBinary"].asString()); +#endif + + std::string decoded; + Toolbox::DecodeBase64(decoded, result["00020102"]["InlineBinary"].asString()); + ASSERT_EQ("MyPrivateInformation", decoded); + + break; + } + + default: + break; + } + } +} + + #if ORTHANC_SANDBOXED != 1 #include "../Sources/SystemToolbox.h" diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp --- a/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -2051,7 +2051,7 @@ dicom.ReplacePlainString(DicomTag(0x0040, 0x0031), "UT"); #if DCMTK_VERSION_NUMBER >= 365 - dicom.ReplacePlainString(DicomTag(0x7056, 0x1000), "-17"); // OV + dicom.ReplacePlainString(DicomTag(0x7056, 0x1000), "17"); // OV dicom.ReplacePlainString(DicomTag(0x7056, 0x1001), "-18"); // SV dicom.ReplacePlainString(DicomTag(0x7056, 0x1002), "19"); // UV #endif @@ -2183,7 +2183,7 @@ #if DCMTK_VERSION_NUMBER >= 365 ASSERT_EQ("OV", visitor.GetResult() ["70561000"]["vr"].asString()); - ASSERT_EQ(-17, visitor.GetResult() ["70561000"]["Value"][0].asInt()); + ASSERT_EQ(17, visitor.GetResult() ["70561000"]["Value"][0].asInt()); ASSERT_EQ("SV", visitor.GetResult() ["70561001"]["vr"].asString()); ASSERT_EQ(-18, visitor.GetResult() ["70561001"]["Value"][0].asInt()); ASSERT_EQ("UV", visitor.GetResult() ["70561002"]["vr"].asString()); @@ -2254,7 +2254,7 @@ #endif #if DCMTK_VERSION_NUMBER >= 365 - ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x7056, 0x1000), true)); ASSERT_EQ("-17", s); // OV + ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x7056, 0x1000), true)); ASSERT_EQ("17", s); // OV ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x7056, 0x1001), true)); ASSERT_EQ("-18", s); // SV ASSERT_TRUE(m.LookupStringValue(s, DicomTag(0x7056, 0x1002), true)); ASSERT_EQ("19", s); // UV #endif diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/UnitTestsSources/LoggingTests.cpp --- a/OrthancFramework/UnitTestsSources/LoggingTests.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/LoggingTests.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -132,11 +132,24 @@ #if ORTHANC_ENABLE_LOGGING_STDIO == 0 +namespace +{ + class BasicTestRestore : public boost::noncopyable + { + public: + ~BasicTestRestore() + { + Orthanc::Logging::EnableTraceLevel(false); // Back to normal + } + }; +} + TEST(FuncStreamBuf, BasicTest) { LoggingMementoScope loggingConfiguration; Orthanc::Logging::EnableTraceLevel(true); + BasicTestRestore restore; typedef void(*LoggingFunctionFunc)(const char*); @@ -199,8 +212,6 @@ ASSERT_TRUE(ok); ASSERT_STREQ(payload.c_str(), text); } - - Orthanc::Logging::EnableTraceLevel(false); // Back to normal } #endif diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancFramework/UnitTestsSources/ToolboxTests.cpp --- a/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/ToolboxTests.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -499,3 +499,33 @@ ASSERT_EQ(8, sizeof(static_cast(16 * 1024 * 1024) * KILOBYTE)); ASSERT_EQ(8, sizeof(KILOBYTE * static_cast(16 * 1024 * 1024))); } + + +TEST(Toolbox, SwapEndianness) +{ + const std::string s = "abcdefgh"; + + { + std::string t = s; + Toolbox::SwapEndianness(t, 1); + ASSERT_EQ("abcdefgh", t); + } + + { + std::string t = s; + Toolbox::SwapEndianness(t, 2); + ASSERT_EQ("badcfehg", t); + } + + { + std::string t = s; + Toolbox::SwapEndianness(t, 4); + ASSERT_EQ("dcbahgfe", t); + } + + { + std::string t = s; + Toolbox::SwapEndianness(t, 8); + ASSERT_EQ("hgfedcba", t); + } +} diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Plugins/Engine/OrthancPlugins.cpp --- a/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Plugins/Engine/OrthancPlugins.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -1125,6 +1125,10 @@ that.currentMode_ = DicomWebJsonVisitor::BinaryMode_BulkDataUri; break; + case OrthancPluginDicomWebBinaryMode_ArrayOfValues: + that.currentMode_ = DicomWebJsonVisitor::BinaryMode_ArrayOfValues; + break; + default: throw OrthancException(ErrorCode_ParameterOutOfRange); } diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h --- a/OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Plugins/Include/orthanc/OrthancCPlugin.h Fri Jul 17 11:19:44 2026 +0200 @@ -126,7 +126,7 @@ #define ORTHANC_PLUGINS_MINIMAL_MAJOR_NUMBER 1 #define ORTHANC_PLUGINS_MINIMAL_MINOR_NUMBER 12 -#define ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER 10 +#define ORTHANC_PLUGINS_MINIMAL_REVISION_NUMBER 12 #if !defined(ORTHANC_PLUGINS_VERSION_IS_ABOVE) @@ -1070,13 +1070,15 @@ /** * The available modes to export a binary DICOM tag into a DICOMweb - * JSON or XML document. + * JSON or XML document. Binary DICOM tags correspond to the tags + * whose value representation is "other" (OB, OD, OF, OL, OV, or OW). **/ typedef enum ORTHANC_PLUGIN_SINCE_SDK("1.5.4") { OrthancPluginDicomWebBinaryMode_Ignore = 0, /*!< Don't include binary tags */ OrthancPluginDicomWebBinaryMode_InlineBinary = 1, /*!< Inline encoding using Base64 */ - OrthancPluginDicomWebBinaryMode_BulkDataUri = 2 /*!< Use a bulk data URI field */ + OrthancPluginDicomWebBinaryMode_BulkDataUri = 2, /*!< Use a bulk data URI field */ + OrthancPluginDicomWebBinaryMode_ArrayOfValues ORTHANC_PLUGIN_SINCE_SDK("1.12.12") = 3 /*!< Encode the values as an array (not available for OB, new in Orthanc 1.12.12) */ } OrthancPluginDicomWebBinaryMode; diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp --- a/OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Plugins/Samples/DelayedDeletion/Plugin.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -188,7 +188,7 @@ static void DeletionWorker() { - Orthanc::Logging::ScopedThreadNameSetter setter("DELETION"); + Orthanc::Logging::ScopedCurrentThreadNameSetter setter("DELETION"); static const unsigned int GRANULARITY = 100; // In milliseconds diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Sources/LuaScripting.cpp --- a/OrthancServer/Sources/LuaScripting.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Sources/LuaScripting.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -1023,7 +1023,7 @@ void LuaScripting::HeartBeatThread(LuaScripting* that) { - Logging::ScopedThreadNameSetter setter("LUA-HEARTBEAT"); + Logging::ScopedCurrentThreadNameSetter setter("LUA-HEARTBEAT"); static const unsigned int GRANULARITY = 100; // In milliseconds @@ -1061,7 +1061,7 @@ void LuaScripting::EventThread(LuaScripting* that) { - Logging::ScopedThreadNameSetter setter("LUA-EVENTS"); + Logging::ScopedCurrentThreadNameSetter setter("LUA-EVENTS"); for (;;) { diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp --- a/OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Sources/OrthancRestApi/OrthancRestSystem.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -172,17 +172,6 @@ result[ORTHANC_CONFIG_MAXIMUM_STORAGE_CACHE_SIZE] = lock.GetConfiguration().GetMaximumStorageCacheSize(); // New in Orthanc 1.12.12 result[ORTHANC_CONFIG_STORE_MD5_FOR_ATTACHMENTS] = lock.GetConfiguration().HasStoreMD5ForAttachments(); // New in Orthanc 1.12.12 result[ORTHANC_CONFIG_STORAGE_COMPRESSION] = lock.GetConfiguration().HasStorageCompression(); // New in Orthanc 1.11.0 - - DicomTransferSyntax ingestTransferSyntax; - if (context.LookupIngestTranscoding(ingestTransferSyntax)) - { - result[ORTHANC_CONFIG_INGEST_TRANSCODING] = GetTransferSyntaxUid(ingestTransferSyntax); // New in Orthanc 1.11.0 - } - else - { - result[ORTHANC_CONFIG_INGEST_TRANSCODING] = ""; - } - result[ORTHANC_CONFIG_DATABASE_SERVER_IDENTIFIER] = lock.GetConfiguration().GetDatabaseServerIdentifier(); result[ORTHANC_CONFIG_MAXIMUM_STORAGE_SIZE] = lock.GetConfiguration().GetMaximumStorageSize(); // New in Orthanc 1.11.3 result[ORTHANC_CONFIG_MAXIMUM_PATIENT_COUNT] = lock.GetConfiguration().GetMaximumPatientCount(); // New in Orthanc 1.12.4 @@ -190,6 +179,16 @@ result[ORTHANC_CONFIG_DICOM_DEFAULT_RETRIEVE_METHOD] = lock.GetConfiguration().GetDicomDefaultRetrieveMethod(); } + DicomTransferSyntax ingestTransferSyntax; + if (context.LookupIngestTranscoding(ingestTransferSyntax)) + { + result[ORTHANC_CONFIG_INGEST_TRANSCODING] = GetTransferSyntaxUid(ingestTransferSyntax); // New in Orthanc 1.11.0 + } + else + { + result[ORTHANC_CONFIG_INGEST_TRANSCODING] = ""; + } + result[ORTHANC_CONFIG_OVERWRITE_INSTANCES] = context.IsOverwriteInstances(); // New in Orthanc 1.11.0 result[OVERWRITE_INSTANCES_MODE] = EnumerationToString(context.GetOverwriteInstances()); // New in Orthanc 1.12.12 result[ORTHANC_CONFIG_PATIENT_LEVEL_ENABLED] = context.IsPatientLevelEnabled(); // New in Orthanc 1.12.11 diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Sources/OrthancWebDav.cpp --- a/OrthancServer/Sources/OrthancWebDav.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Sources/OrthancWebDav.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -1163,7 +1163,7 @@ void OrthancWebDav::UploadWorker(OrthancWebDav* that) { - Logging::ScopedThreadNameSetter setter("WEBDAV-UPLOAD"); + Logging::ScopedCurrentThreadNameSetter setter("WEBDAV-UPLOAD"); assert(that != NULL); diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Sources/ServerContext.cpp --- a/OrthancServer/Sources/ServerContext.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Sources/ServerContext.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -116,7 +116,7 @@ void ServerContext::MemoryTrimmingThread(ServerContext* that, unsigned int intervalInSeconds) { - Logging::ScopedThreadNameSetter setter("MEMORY-TRIM"); + Logging::ScopedCurrentThreadNameSetter setter("MEMORY-TRIM"); boost::posix_time::ptime lastExecution = boost::posix_time::second_clock::universal_time(); @@ -146,7 +146,7 @@ void ServerContext::ChangeThread(ServerContext* that, unsigned int sleepDelay) { - Logging::ScopedThreadNameSetter setter("CHANGES"); + Logging::ScopedCurrentThreadNameSetter setter("CHANGES"); while (!that->done_) { @@ -190,7 +190,7 @@ void ServerContext::JobEventsThread(ServerContext* that, unsigned int sleepDelay) { - Logging::ScopedThreadNameSetter setter("JOB-EVENTS"); + Logging::ScopedCurrentThreadNameSetter setter("JOB-EVENTS"); while (!that->done_) { @@ -234,7 +234,7 @@ void ServerContext::SaveJobsThread(ServerContext* that, unsigned int sleepDelay) { - Logging::ScopedThreadNameSetter setter("SAVE-JOBS"); + Logging::ScopedCurrentThreadNameSetter setter("SAVE-JOBS"); static const boost::posix_time::time_duration PERIODICITY = boost::posix_time::seconds(10); diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Sources/ServerIndex.cpp --- a/OrthancServer/Sources/ServerIndex.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Sources/ServerIndex.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -276,7 +276,7 @@ void ServerIndex::FlushThread(ServerIndex* that, unsigned int threadSleepGranularityMilliseconds) { - Logging::ScopedThreadNameSetter setter("DB-FLUSH"); + Logging::ScopedCurrentThreadNameSetter setter("DB-FLUSH"); // By default, wait for 10 seconds before flushing static const unsigned int SLEEP_SECONDS = 10; @@ -458,7 +458,7 @@ void ServerIndex::UnstableResourcesMonitorThread(ServerIndex* that, unsigned int threadSleepGranularityMilliseconds) { - Logging::ScopedThreadNameSetter setter("UNSTABLE-MON"); + Logging::ScopedCurrentThreadNameSetter setter("UNSTABLE-MON"); unsigned int stableAge = 0; diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp --- a/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/ThreadedInstancesLoader.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -155,7 +155,7 @@ void ThreadedInstancesLoader::PreloaderWorkerThread(ThreadedInstancesLoader* that) { - Logging::ScopedThreadNameSetter setter(::GetLoaderThreadName(that->nameForLogs4Char_)); + Logging::ScopedCurrentThreadNameSetter setter(::GetLoaderThreadName(that->nameForLogs4Char_)); LOG(INFO) << "Loader thread has started"; diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp --- a/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Sources/ServerJobs/ThreadedSetOfInstancesJob.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -256,7 +256,7 @@ void ThreadedSetOfInstancesJob::InstanceWorkerThread(ThreadedSetOfInstancesJob* that) { - Logging::ScopedThreadNameSetter setter(::GetInstanceWorkerThreadName()); + Logging::ScopedCurrentThreadNameSetter setter(::GetInstanceWorkerThreadName()); while (true) { diff -r 519b3bf8f0b8 -r 186b7ca8990d OrthancServer/Sources/main.cpp --- a/OrthancServer/Sources/main.cpp Fri Jul 17 11:18:45 2026 +0200 +++ b/OrthancServer/Sources/main.cpp Fri Jul 17 11:19:44 2026 +0200 @@ -2088,7 +2088,7 @@ } else if (argument == "--logs-no-thread") { - Logging::EnableThreadNames(false); + Logging::SetThreadNamesEnabled(false); } else if (argument == "--trace") {