X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=tools%2FclitkWriteDicomSeriesGenericFilter.txx;h=a2bccc4aa0daa52fbef3b8a34cb115b3eb9cc20c;hb=d9c8c6e6c759655fc24d05d47b962d2ba574b95d;hp=9bc52dcfd8c2cd10c38b930cb0eada7033226691;hpb=6b3340701c3f6f4c87c1366b3090258c8df93deb;p=clitk.git diff --git a/tools/clitkWriteDicomSeriesGenericFilter.txx b/tools/clitkWriteDicomSeriesGenericFilter.txx index 9bc52dc..a2bccc4 100644 --- a/tools/clitkWriteDicomSeriesGenericFilter.txx +++ b/tools/clitkWriteDicomSeriesGenericFilter.txx @@ -29,6 +29,12 @@ // clitk #include "clitkResampleImageWithOptionsFilter.h" +#if GDCM_MAJOR_VERSION >= 2 +#include "gdcmUIDGenerator.h" +#else +#include "gdcmFile.h" +#include "gdcmUtil.h" +#endif namespace clitk @@ -82,10 +88,10 @@ WriteDicomSeriesGenericFilter::UpdateWithDim(std::string PixelTy if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl; UpdateWithDimAndPixelType(); } - // else if(PixelType == "unsigned_short"){ - // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl; - // UpdateWithDimAndPixelType(); - // } + else if(PixelType == "unsigned_short"){ + if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl; + UpdateWithDimAndPixelType(); + } else if (PixelType == "unsigned_char") { if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl; @@ -96,6 +102,10 @@ WriteDicomSeriesGenericFilter::UpdateWithDim(std::string PixelTy // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl; // UpdateWithDimAndPixelType(); // } + else if (PixelType == "double") { + if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and double..." << std::endl; + UpdateWithDimAndPixelType(); + } else { if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl; UpdateWithDimAndPixelType(); @@ -143,6 +153,12 @@ WriteDicomSeriesGenericFilter::UpdateWithDimAndPixelType() // Read the series typename ReaderType::Pointer reader = ReaderType::New(); + if (m_ArgsInfo.preserve_flag) { +#if GDCM_MAJOR_VERSION >= 2 + gdcmIO->LoadPrivateTagsOn(); + gdcmIO->KeepOriginalUIDOn(); +#endif + } reader->SetImageIO( gdcmIO ); reader->SetFileNames( filenames_in ); try { @@ -230,23 +246,160 @@ WriteDicomSeriesGenericFilter::UpdateWithDimAndPixelType() const std::vector* dictionary = reader->GetMetaDataDictionaryArray(); // Get keys - unsigned int numberOfKeysGiven=0; - if(m_ArgsInfo.midP_flag && m_ArgsInfo.key_given) - std::cerr<<"Error: both keys and midP option are given"<( *((*dictionary)[fni]), entryId, value ); + std::string entryId( m_ArgsInfo.key_arg[i] ); + if (m_ArgsInfo.verbose_flag) + DD(entryId); + + seriesUIDGiven |= (entryId == seriesUIDkey || entryId == frameOfReferenceUIDKey); + seriesNumberGiven |= (entryId == seriesNumberKey); + seriesDescriptionGiven |= (entryId == seriesDescriptionKey); + studyUIDGiven |= (entryId == studyUIDKey); + studyIDGiven |= (entryId == studyIDKey); + studyDescriptionGiven |= (entryId == studyDescriptionKey); + } + + // force the creation of a new series if a new study was specified + if (!studyUIDGiven && m_ArgsInfo.newStudyUID_flag) { + m_ArgsInfo.newSeriesUID_flag = true; +#if GDCM_MAJOR_VERSION >= 2 + gdcm::UIDGenerator suid; + studyUID = suid.Generate(); +#else + studyUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix()); +#endif + } + + if (!seriesUIDGiven && m_ArgsInfo.newSeriesUID_flag) { +#if GDCM_MAJOR_VERSION >= 2 + gdcm::UIDGenerator suid; + seriesUID = suid.Generate(); + gdcm::UIDGenerator fuid; + frameOfReferenceUID = fuid.Generate(); +#else + seriesUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix()); + frameOfReferenceUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix()); +#endif + } + + if (m_ArgsInfo.verbose_flag) { + DD(seriesUID); + DD(frameOfReferenceUID); + DD(studyUID); + } + + // check if file UIDs will be be preserved + bool useInputFileUID = true; + if (m_ArgsInfo.newSeriesUID_flag || m_ArgsInfo.newStudyUID_flag || seriesUIDGiven || studyUIDGiven) { + useInputFileUID = false; } + else { +#if GDCM_MAJOR_VERSION < 2 + gdcmIO->SetKeepOriginalUID(true); +#endif + namesGenerator->SetOutputDirectory( m_ArgsInfo.outputDir_arg ); + } + + filenames_out.resize(numberOfFilenames); + time_t t; + t = time(&t); + struct tm* instanceDateTimeTm = localtime(&t); + char datetime[16]; + strftime(datetime, 16, "%Y%m%d", instanceDateTimeTm); + std::ostringstream instanceDate; + instanceDate << datetime; + std::ostringstream instanceTime; + strftime(datetime, 16, "%H%M%S", instanceDateTimeTm); + instanceTime << datetime; + + // update output dicom keys/tags + for(unsigned int fni = 0; fni( *((*dictionary)[fni]), entryId, value ); + } + + // series UID + if (!seriesUIDGiven) { + if (m_ArgsInfo.newSeriesUID_flag) { + itk::EncapsulateMetaData( *((*dictionary)[fni]), seriesUIDkey, seriesUID ); + itk::EncapsulateMetaData( *((*dictionary)[fni]), frameOfReferenceUIDKey, frameOfReferenceUID ); + } + } + + // study UID + if (!studyUIDGiven) { + if (m_ArgsInfo.newStudyUID_flag) + itk::EncapsulateMetaData( *((*dictionary)[fni]), studyUIDKey, studyUID ); + } + + // study description + if (studyUIDGiven || m_ArgsInfo.newStudyUID_flag) { + if (!studyIDGiven) + itk::EncapsulateMetaData( *((*dictionary)[fni]), studyIDKey,itksys::SystemTools::GetFilenameName( m_ArgsInfo.outputDir_arg )); + if (!studyDescriptionGiven) + itk::EncapsulateMetaData( *((*dictionary)[fni]), studyDescriptionKey,itksys::SystemTools::GetFilenameName( m_ArgsInfo.outputDir_arg )); + + itk::EncapsulateMetaData( *((*dictionary)[fni]), "0008|0020", instanceDate.str() ); + itk::EncapsulateMetaData( *((*dictionary)[fni]), "0008|0030", instanceTime.str() ); + } + + // series description/number + if (seriesUIDGiven || m_ArgsInfo.newSeriesUID_flag) { + if (!seriesDescriptionGiven) + itk::EncapsulateMetaData( *((*dictionary)[fni]), seriesDescriptionKey, itksys::SystemTools::GetFilenameName(m_ArgsInfo.outputDir_arg) ); + if (!seriesNumberGiven) + itk::EncapsulateMetaData( *((*dictionary)[fni]), seriesNumberKey, itksys::SystemTools::GetFilenameName(m_ArgsInfo.outputDir_arg) ); + + itk::EncapsulateMetaData( *((*dictionary)[fni]), "0008|0012", instanceDate.str() ); + itk::EncapsulateMetaData( *((*dictionary)[fni]), "0008|0013", instanceTime.str() ); + } + // file UIDs are recreated for new studies or series + if (!useInputFileUID) + { + if (m_ArgsInfo.verbose_flag) + std::cout << "Recreating file UIDs" << std::endl; + + std::string fileUID; +#if GDCM_MAJOR_VERSION >= 2 + gdcm::UIDGenerator fid; + fileUID = fid.Generate(); +#else + fileUID = gdcm::Util::CreateUniqueUID( gdcmIO->GetUIDPrefix()); +#endif + itk::EncapsulateMetaData( *((*dictionary)[fni]), "0008|0018", fileUID ); + itk::EncapsulateMetaData( *((*dictionary)[fni]), "0002|0003", fileUID ); + + filenames_out[fni] = itksys::SystemTools::CollapseFullPath(fileUID.c_str(), m_ArgsInfo.outputDir_arg) + std::string(".dcm"); + } + } + // Output directory and filenames itksys::SystemTools::MakeDirectory( m_ArgsInfo.outputDir_arg ); // create if it doesn't exist typedef itk::ImageSeriesWriter SeriesWriterType; @@ -260,8 +413,9 @@ WriteDicomSeriesGenericFilter::UpdateWithDimAndPixelType() // Write try { + if (m_ArgsInfo.verbose_flag) + std::cout << seriesWriter << std::endl; seriesWriter->Update(); - std::cerr << "filter update" << std::endl; } catch( itk::ExceptionObject & excp ) { std::cerr << "Error: Exception thrown while writing the series!!" << std::endl; std::cerr << excp << std::endl;