# HG changeset patch # User Sebastien Jodogne # Date 1783155038 -7200 # Node ID e8e934fa22147a0f11ceaf23642713db4d4540bc # Parent 92590b48a7c1b793040a83f180bc124364fb81ba reduced coupling of FromDcmtkBridge::FillElementWithString() with DcmElement::putString() diff -r 92590b48a7c1 -r e8e934fa2214 OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp --- a/OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp Sat Jul 04 10:15:42 2026 +0200 +++ b/OrthancFramework/Sources/DicomParsing/FromDcmtkBridge.cpp Sat Jul 04 10:50:38 2026 +0200 @@ -406,7 +406,7 @@ }; - class AttributeElementFiller : public IElementFiller + class ValueRepresentationFiller_AT : public IElementFiller { public: bool FillSingleValue(DcmElement& element, @@ -434,6 +434,187 @@ }; + template + class ValueRepresentationFillerGeneric : public IElementFiller + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const DcmtkType& value) const = 0; + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const = 0; + + public: + bool FillSingleValue(DcmElement& element, + const std::string& value) const ORTHANC_OVERRIDE + { + return PutSingleValue(element, boost::lexical_cast(value)); + } + + bool FillMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + std::vector numeric; + numeric.resize(values.size()); + + for (size_t i = 0; i < values.size(); i++) + { + numeric[i] = boost::lexical_cast(values[i]); + } + + return PutMultipleValues(element, numeric); + } + }; + + + class ValueRepresentationFiller_SL : public ValueRepresentationFillerGeneric + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const Sint32& value) const ORTHANC_OVERRIDE + { + return element.putSint32(value).good(); + } + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + return element.putSint32Array(values.empty() ? NULL : &values[0], values.size()).good(); + } + }; + + + class ValueRepresentationFiller_SS : public ValueRepresentationFillerGeneric + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const Sint16& value) const ORTHANC_OVERRIDE + { + return element.putSint16(value).good(); + } + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + return element.putSint16Array(values.empty() ? NULL : &values[0], values.size()).good(); + } + }; + + +#if DCMTK_VERSION_NUMBER >= 365 + class ValueRepresentationFiller_SV : public ValueRepresentationFillerGeneric + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const Sint64& value) const ORTHANC_OVERRIDE + { + // The method "DcmElement::putSint64()" was only added in DCMTK 3.7.0, + // so we have to cast to support earlier versions of DCMTK + DcmSigned64bitVeryLong& e = dynamic_cast(element); + return e.putSint64(value).good(); + } + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + DcmSigned64bitVeryLong& e = dynamic_cast(element); + return e.putSint64Array(values.empty() ? NULL : &values[0], values.size()).good(); + } + }; +#endif + + + class ValueRepresentationFiller_UL : public ValueRepresentationFillerGeneric + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const Uint32& value) const ORTHANC_OVERRIDE + { + return element.putUint32(value).good(); + } + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + return element.putUint32Array(values.empty() ? NULL : &values[0], values.size()).good(); + } + }; + + + class ValueRepresentationFiller_US : public ValueRepresentationFillerGeneric + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const Uint16& value) const ORTHANC_OVERRIDE + { + return element.putUint16(value).good(); + } + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + return element.putUint16Array(values.empty() ? NULL : &values[0], values.size()).good(); + } + }; + + +#if DCMTK_VERSION_NUMBER >= 365 + class ValueRepresentationFiller_UV : public ValueRepresentationFillerGeneric + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const Uint64& value) const ORTHANC_OVERRIDE + { + // The method "DcmElement::putUint64()" was only added in DCMTK 3.7.0, + // so we have to cast to support earlier versions of DCMTK + DcmUnsigned64bitVeryLong& e = dynamic_cast(element); + return e.putUint64(value).good(); + } + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + DcmUnsigned64bitVeryLong& e = dynamic_cast(element); + return e.putUint64Array(values.empty() ? NULL : &values[0], values.size()).good(); + } + }; +#endif + + + class ValueRepresentationFiller_FL : public ValueRepresentationFillerGeneric + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const Float32& value) const ORTHANC_OVERRIDE + { + return element.putFloat32(value).good(); + } + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + return element.putFloat32Array(values.empty() ? NULL : &values[0], values.size()).good(); + } + }; + + + class ValueRepresentationFiller_FD : public ValueRepresentationFillerGeneric + { + protected: + virtual bool PutSingleValue(DcmElement& element, + const Float64& value) const ORTHANC_OVERRIDE + { + return element.putFloat64(value).good(); + } + + virtual bool PutMultipleValues(DcmElement& element, + const std::vector& values) const ORTHANC_OVERRIDE + { + return element.putFloat64Array(values.empty() ? NULL : &values[0], values.size()).good(); + } + }; + + class ILeafElementArrayReader : public boost::noncopyable { public: @@ -2577,64 +2758,25 @@ **/ case EVR_SL: // signed long - { - if (decoded->find('\\') != std::string::npos) - { - ok = element.putString(decoded->c_str()).good(); - } - else - { - ok = element.putSint32(boost::lexical_cast(*decoded)).good(); - } + ok = IElementFiller::Apply(element, ValueRepresentationFiller_SL(), *decoded); break; - } case EVR_SS: // signed short - { - if (decoded->find('\\') != std::string::npos) - { - ok = element.putString(decoded->c_str()).good(); - } - else - { - ok = element.putSint16(boost::lexical_cast(*decoded)).good(); - } + ok = IElementFiller::Apply(element, ValueRepresentationFiller_SS(), *decoded); break; - } #if DCMTK_VERSION_NUMBER >= 365 case EVR_SV: // signed very long (64-bit, new in Orthanc 1.12.12) - { - if (decoded->find('\\') != std::string::npos) - { - ok = element.putString(decoded->c_str()).good(); - } - else - { - // The method "DcmElement::putSint64()" was only added in DCMTK 3.7.0, - // so we have to cast to support earlier versions of DCMTK - DcmSigned64bitVeryLong& e = dynamic_cast(element); - ok = e.putSint64(boost::lexical_cast(*decoded)).good(); - } + ok = IElementFiller::Apply(element, ValueRepresentationFiller_SV(), *decoded); break; - } #endif case EVR_UL: // unsigned long #if DCMTK_VERSION_NUMBER >= 362 case EVR_OL: // other long (requires byte-swapping) #endif - { - if (decoded->find('\\') != std::string::npos) - { - ok = element.putString(decoded->c_str()).good(); - } - else - { - ok = element.putUint32(boost::lexical_cast(*decoded)).good(); - } + ok = IElementFiller::Apply(element, ValueRepresentationFiller_UL(), *decoded); break; - } case EVR_xs: // unsigned short, signed short or multiple values { @@ -2654,66 +2796,27 @@ } case EVR_US: // unsigned short - { - if (decoded->find('\\') != std::string::npos) - { - ok = element.putString(decoded->c_str()).good(); - } - else - { - ok = element.putUint16(boost::lexical_cast(*decoded)).good(); - } + ok = IElementFiller::Apply(element, ValueRepresentationFiller_US(), *decoded); break; - } #if DCMTK_VERSION_NUMBER >= 365 case EVR_UV: // unsigned very long (64-bit, new in Orthanc 1.12.12) case EVR_OV: // other very long (64-bit, new in Orthanc 1.12.12) - { - if (decoded->find('\\') != std::string::npos) - { - ok = element.putString(decoded->c_str()).good(); - } - else - { - // The method "DcmElement::putUint64()" was only added in DCMTK 3.7.0, - // so we have to cast to support earlier versions of DCMTK - DcmUnsigned64bitVeryLong& e = dynamic_cast(element); - ok = e.putUint64(boost::lexical_cast(*decoded)).good(); - } + ok = IElementFiller::Apply(element, ValueRepresentationFiller_UV(), *decoded); break; - } #endif case EVR_FL: // float single-precision case EVR_OF: // other float (requires byte swapping) - { - if (decoded->find('\\') != std::string::npos) - { - ok = element.putString(decoded->c_str()).good(); - } - else - { - ok = element.putFloat32(boost::lexical_cast(*decoded)).good(); - } + ok = IElementFiller::Apply(element, ValueRepresentationFiller_FL(), *decoded); break; - } case EVR_FD: // float double-precision #if DCMTK_VERSION_NUMBER >= 361 case EVR_OD: // other double (requires byte-swapping) #endif - { - if (decoded->find('\\') != std::string::npos) - { - ok = element.putString(decoded->c_str()).good(); - } - else - { - ok = element.putFloat64(boost::lexical_cast(*decoded)).good(); - } + ok = IElementFiller::Apply(element, ValueRepresentationFiller_FD(), *decoded); break; - } /** @@ -2721,11 +2824,9 @@ **/ case EVR_AT: // attribute tag, new in Orthanc 1.9.4 - { // Multiple values are supported since Orthanc 1.12.12 - ok = IElementFiller::Apply(element, AttributeElementFiller(), *decoded); + ok = IElementFiller::Apply(element, ValueRepresentationFiller_AT(), *decoded); break; - } /** diff -r 92590b48a7c1 -r e8e934fa2214 OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp --- a/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp Sat Jul 04 10:15:42 2026 +0200 +++ b/OrthancFramework/UnitTestsSources/FromDcmtkTests.cpp Sat Jul 04 10:50:38 2026 +0200 @@ -3905,8 +3905,6 @@ ASSERT_TRUE(f.GetDcmtkObject().getDataset()->insert(e.release()).good()); } - f.GetDcmtkObject().writeXML(std::cout); - std::string s; ASSERT_TRUE(f.GetTagValue(s, DicomTag(0x0072, 0x0026))); ASSERT_EQ("0072,0008", s);