From 34f386c46af7fbaca60569608d6d16a7903c99c2 Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Fri, 3 May 2024 05:02:25 -0700 Subject: [PATCH 1/3] refactor code as function --- Applications/Cxx/gdcmconv.cxx | 160 ++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 75 deletions(-) diff --git a/Applications/Cxx/gdcmconv.cxx b/Applications/Cxx/gdcmconv.cxx index 3b018af48..a36e7b475 100644 --- a/Applications/Cxx/gdcmconv.cxx +++ b/Applications/Cxx/gdcmconv.cxx @@ -427,6 +427,89 @@ return true; } } // end namespace gdcm +namespace { + +int change_transfersyntax(const std::string &filename, const std::string &outfilename, int explicitts, int implicit, int deflated, int raw, int changeprivatetags ) +{ + if( explicitts && implicit ) return 1; // guard + if( explicitts && deflated ) return 1; // guard + if( implicit && deflated ) return 1; // guard + gdcm::Reader reader; + reader.SetFileName( filename.c_str() ); + if( !reader.Read() ) + { + std::cerr << "Could not read: " << filename << std::endl; + return 1; + } + gdcm::MediaStorage ms; + ms.SetFromFile( reader.GetFile() ); + if( ms == gdcm::MediaStorage::MediaStorageDirectoryStorage ) + { + std::cerr << "Sorry DICOMDIR is not supported" << std::endl; + return 1; + } + + gdcm::Writer writer; + writer.SetFileName( outfilename.c_str() ); + writer.SetFile( reader.GetFile() ); + gdcm::File & file = writer.GetFile(); + gdcm::FileMetaInformation &fmi = file.GetHeader(); + + const gdcm::TransferSyntax &orits = fmi.GetDataSetTransferSyntax(); + if( orits != gdcm::TransferSyntax::ExplicitVRLittleEndian + && orits != gdcm::TransferSyntax::ImplicitVRLittleEndian + && orits != gdcm::TransferSyntax::DeflatedExplicitVRLittleEndian ) + { + std::cerr << "Sorry input Transfer Syntax not supported for this conversion: " << orits << std::endl; + return 1; + } + + gdcm::TransferSyntax ts = gdcm::TransferSyntax::ImplicitVRLittleEndian; + if( explicitts ) + { + ts = gdcm::TransferSyntax::ExplicitVRLittleEndian; + } + else if( deflated ) + { + ts = gdcm::TransferSyntax::DeflatedExplicitVRLittleEndian; + } + std::string tsuid = gdcm::TransferSyntax::GetTSString( ts ); + if( tsuid.size() % 2 == 1 ) + { + tsuid.push_back( 0 ); // 0 padding + } + gdcm::DataElement de( gdcm::Tag(0x0002,0x0010) ); + de.SetByteValue( tsuid.data(), (uint32_t)tsuid.size() ); + de.SetVR( gdcm::Attribute<0x0002, 0x0010>::GetVR() ); + fmi.Clear(); + fmi.Replace( de ); + + fmi.SetDataSetTransferSyntax(ts); + + if( explicitts || deflated ) + { + gdcm::FileExplicitFilter fef; + fef.SetChangePrivateTags( (changeprivatetags > 0 ? true: false)); + fef.SetFile( reader.GetFile() ); + if( !fef.Change() ) + { + std::cerr << "Failed to change: " << filename << std::endl; + return 1; + } + } + + if( !writer.Write() ) + { + std::cerr << "Failed to write: " << outfilename << std::endl; + return 1; + } + + return 0; + +} + +} // end anonymous namespace + int main (int argc, char *argv[]) { int c; @@ -928,80 +1011,7 @@ int main (int argc, char *argv[]) // Handle here the general file (not required to be image) if ( !raw && (explicitts || implicit || deflated) ) { - if( explicitts && implicit ) return 1; // guard - if( explicitts && deflated ) return 1; // guard - if( implicit && deflated ) return 1; // guard - gdcm::Reader reader; - reader.SetFileName( filename.c_str() ); - if( !reader.Read() ) - { - std::cerr << "Could not read: " << filename << std::endl; - return 1; - } - gdcm::MediaStorage ms; - ms.SetFromFile( reader.GetFile() ); - if( ms == gdcm::MediaStorage::MediaStorageDirectoryStorage ) - { - std::cerr << "Sorry DICOMDIR is not supported" << std::endl; - return 1; - } - - gdcm::Writer writer; - writer.SetFileName( outfilename.c_str() ); - writer.SetFile( reader.GetFile() ); - gdcm::File & file = writer.GetFile(); - gdcm::FileMetaInformation &fmi = file.GetHeader(); - - const gdcm::TransferSyntax &orits = fmi.GetDataSetTransferSyntax(); - if( orits != gdcm::TransferSyntax::ExplicitVRLittleEndian - && orits != gdcm::TransferSyntax::ImplicitVRLittleEndian - && orits != gdcm::TransferSyntax::DeflatedExplicitVRLittleEndian ) - { - std::cerr << "Sorry input Transfer Syntax not supported for this conversion: " << orits << std::endl; - return 1; - } - - gdcm::TransferSyntax ts = gdcm::TransferSyntax::ImplicitVRLittleEndian; - if( explicitts ) - { - ts = gdcm::TransferSyntax::ExplicitVRLittleEndian; - } - else if( deflated ) - { - ts = gdcm::TransferSyntax::DeflatedExplicitVRLittleEndian; - } - std::string tsuid = gdcm::TransferSyntax::GetTSString( ts ); - if( tsuid.size() % 2 == 1 ) - { - tsuid.push_back( 0 ); // 0 padding - } - gdcm::DataElement de( gdcm::Tag(0x0002,0x0010) ); - de.SetByteValue( tsuid.data(), (uint32_t)tsuid.size() ); - de.SetVR( gdcm::Attribute<0x0002, 0x0010>::GetVR() ); - fmi.Clear(); - fmi.Replace( de ); - - fmi.SetDataSetTransferSyntax(ts); - - if( explicitts || deflated ) - { - gdcm::FileExplicitFilter fef; - fef.SetChangePrivateTags( (changeprivatetags > 0 ? true: false)); - fef.SetFile( reader.GetFile() ); - if( !fef.Change() ) - { - std::cerr << "Failed to change: " << filename << std::endl; - return 1; - } - } - - if( !writer.Write() ) - { - std::cerr << "Failed to write: " << outfilename << std::endl; - return 1; - } - - return 0; + return change_transfersyntax(filename, outfilename, raw, explicitts, implicit, deflated, changeprivatetags); } // split fragments @@ -1259,7 +1269,7 @@ int main (int argc, char *argv[]) } const gdcm::TransferSyntax &ts = image.GetTransferSyntax(); #ifdef GDCM_WORDS_BIGENDIAN - (void)ts; + (void)ts; change.SetTransferSyntax( gdcm::TransferSyntax::ExplicitVRBigEndian ); #else if( ts.IsExplicit() ) From 04fe74d13d7b29c06e37324119822033442350bb Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Fri, 3 May 2024 05:11:15 -0700 Subject: [PATCH 2/3] Allow --raw for DICOM/PDF --- Applications/Cxx/gdcmconv.cxx | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Applications/Cxx/gdcmconv.cxx b/Applications/Cxx/gdcmconv.cxx index a36e7b475..ae53a8486 100644 --- a/Applications/Cxx/gdcmconv.cxx +++ b/Applications/Cxx/gdcmconv.cxx @@ -1151,6 +1151,12 @@ int main (int argc, char *argv[]) reader.SetFileName( filename.c_str() ); if( !reader.Read() ) { + gdcm::MediaStorage ms; + ms.SetFromFile( reader.GetFile() ); + // handle bulk decompression '--raw' on a set of file, which may contains a PDF + if( raw && ms == gdcm::MediaStorage::EncapsulatedPDFStorage ) + return change_transfersyntax(filename, outfilename, raw, explicitts, implicit, deflated, changeprivatetags); + // else std::cerr << "Could not read (pixmap): " << filename << std::endl; return 1; } From 2eaae2091ed90566597c34886dd17d639c1ba8d5 Mon Sep 17 00:00:00 2001 From: Mathieu Malaterre Date: Fri, 3 May 2024 05:12:32 -0700 Subject: [PATCH 3/3] Release 3.0.24 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5b4d7dd89..38c65d116 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ endif() #---------------------------------------------------------------------------- project(GDCM - VERSION 3.0.23 + VERSION 3.0.24 LANGUAGES CXX C ) ## NOTE: the "DESCRIPTION" feature of project() was introduced in cmake 3.10.0