From 6c67067c05036ea098b8de9f6b7860b68bce7568 Mon Sep 17 00:00:00 2001 From: "DESKTOP-AU32L56\\Purushothaman" Date: Fri, 17 Oct 2025 13:28:34 +0530 Subject: [PATCH 1/2] code crashes when DICOM with 12 bits allocated is processed --- .../gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx b/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx index 18208127cda..187681c38f4 100644 --- a/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx +++ b/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx @@ -117,6 +117,12 @@ bool RAWCodec::DecodeBytes(const char* inBytes, size_t inBufferLength, this->GetPixelFormat() == PixelFormat::INT12 ) { size_t len = str.size() * 16 / 12; + if (inOutBufferLength != len) + { + gdcmDebugMacro("inOutBufferLength = " << inOutBufferLength + << ", inBufferLength = " << inBufferLength << ", len = " << len) + return false; + } char * copy = new char[len]; bool b = Unpacker12Bits::Unpack(copy, str.data(), str.size() ); if (!b) From 71dd7f838add202434845389be4d9c4b6b0097df Mon Sep 17 00:00:00 2001 From: "DESKTOP-AU32L56\\Purushothaman" Date: Wed, 22 Oct 2025 12:51:45 +0530 Subject: [PATCH 2/2] Support for 12 bit DICOM files --- .../MediaStorageAndFileFormat/gdcmBitmap.cxx | 11 ++++ .../gdcmRAWCodec.cxx | 59 ++++++++----------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmBitmap.cxx b/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmBitmap.cxx index abba18bbddf..fb051011904 100644 --- a/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmBitmap.cxx +++ b/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmBitmap.cxx @@ -375,6 +375,17 @@ bool Bitmap::TryRAWCodec(char *buffer, bool &lossyflag) const Bitmap *i = (Bitmap*)this; i->SetPixelFormat( codec.GetPixelFormat() ); } +#else + const PixelFormat pf = GetPixelFormat(); // PixelFormat::UINT8; + const PixelFormat & cpf = codec.GetPixelFormat(); + if( cpf.GetBitsAllocated() == 16 && pf.GetBitsAllocated() == 12 ) + { + Bitmap *i = const_cast(this); + gdcmWarningMacro( "Mismatch Bits Allocated. correcting." ); + i->GetPixelFormat().SetBitsAllocated( cpf.GetBitsAllocated() ); + i->GetPixelFormat().SetBitsStored( pf.GetBitsStored() ); + i->GetPixelFormat().SetHighBit( pf.GetHighBit() ); + } #endif unsigned long check; // = outbv->GetLength(); // FIXME diff --git a/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx b/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx index 187681c38f4..65bd0d4421a 100644 --- a/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx +++ b/Modules/ThirdParty/GDCM/src/gdcm/Source/MediaStorageAndFileFormat/gdcmRAWCodec.cxx @@ -116,42 +116,35 @@ bool RAWCodec::DecodeBytes(const char* inBytes, size_t inBufferLength, if( this->GetPixelFormat() == PixelFormat::UINT12 || this->GetPixelFormat() == PixelFormat::INT12 ) { - size_t len = str.size() * 16 / 12; - if (inOutBufferLength != len) - { - gdcmDebugMacro("inOutBufferLength = " << inOutBufferLength - << ", inBufferLength = " << inBufferLength << ", len = " << len) - return false; - } - char * copy = new char[len]; - bool b = Unpacker12Bits::Unpack(copy, str.data(), str.size() ); - if (!b) - { - delete[] copy; - return false; - } - assert (len == inOutBufferLength); - gdcm_assert(inOutBufferLength == len); - memcpy(outBytes, copy, len); - - delete[] copy; - this->GetPixelFormat().SetBitsAllocated( 16 ); + const size_t len = str.size() * 16 / 12; + if(len == inOutBufferLength) { + char * copy = new char[len]; + bool b = Unpacker12Bits::Unpack(copy, str.data(), str.size() ); + if (!b) + { + delete[] copy; + return false; + } + gdcm_assert(inOutBufferLength == len); + memcpy(outBytes, copy, len); + + delete[] copy; + return r; } + } + + // DermaColorLossLess.dcm + //assert (check == inOutBufferLength || check == inOutBufferLength + 1); + // problem with: SIEMENS_GBS_III-16-ACR_NEMA_1.acr + size_t len = str.size(); + if( inOutBufferLength <= len ) + memcpy(outBytes, str.c_str(), inOutBufferLength); else - { - // DermaColorLossLess.dcm - //assert (check == inOutBufferLength || check == inOutBufferLength + 1); - // problem with: SIEMENS_GBS_III-16-ACR_NEMA_1.acr - size_t len = str.size(); - if( inOutBufferLength <= len ) - memcpy(outBytes, str.c_str(), inOutBufferLength); - else - { - gdcmWarningMacro( "Requesting too much data. Truncating result" ); - memcpy(outBytes, str.c_str(), len); - } - } + { + gdcmWarningMacro( "Requesting too much data. Truncating result" ); + memcpy(outBytes, str.c_str(), len); + } return r; }