1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef clitkWriteDicomSeriesGenericFilter_txx
19 #define clitkWriteDicomSeriesGenericFilter_txx
21 /* =================================================
22 * @file clitkWriteDicomSeriesGenericFilter.txx
28 ===================================================*/
35 //-----------------------------------------------------------
37 //-----------------------------------------------------------
38 template<class args_info_type>
39 WriteDicomSeriesGenericFilter<args_info_type>::WriteDicomSeriesGenericFilter()
46 //-----------------------------------------------------------
48 //-----------------------------------------------------------
49 template<class args_info_type>
50 void WriteDicomSeriesGenericFilter<args_info_type>::Update()
52 // Read the Dimension and PixelType
54 std::string PixelType;
55 ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
59 if(Dimension==2) UpdateWithDim<2>(PixelType);
60 else if(Dimension==3) UpdateWithDim<3>(PixelType);
61 // else if (Dimension==4)UpdateWithDim<4>(PixelType);
64 std::cout<<"Error, Only for 2 or 3 Dimensions!!!"<<std::endl ;
69 //-------------------------------------------------------------------
70 // Update with the number of dimensions
71 //-------------------------------------------------------------------
72 template<class args_info_type>
73 template<unsigned int Dimension>
75 WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
77 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
79 if(PixelType == "short"){
80 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
81 UpdateWithDimAndPixelType<Dimension, signed short>();
83 // else if(PixelType == "unsigned_short"){
84 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
85 // UpdateWithDimAndPixelType<Dimension, unsigned short>();
88 else if (PixelType == "unsigned_char"){
89 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
90 UpdateWithDimAndPixelType<Dimension, unsigned char>();
93 // else if (PixelType == "char"){
94 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
95 // UpdateWithDimAndPixelType<Dimension, signed char>();
98 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
99 UpdateWithDimAndPixelType<Dimension, float>();
104 //-------------------------------------------------------------------
105 // Update with the number of dimensions and the pixeltype
106 //-------------------------------------------------------------------
107 template<class args_info_type>
108 template <unsigned int Dimension, class PixelType>
110 WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
114 typedef itk::Image<PixelType, Dimension> InputImageType;
115 typedef itk::Image<PixelType, 2> OutputImageType;
117 // Read the input (volumetric)
118 typedef itk::ImageFileReader<InputImageType> InputReaderType;
119 typename InputReaderType::Pointer volumeReader = InputReaderType::New();
120 volumeReader->SetFileName( m_InputFileName);
121 volumeReader->Update();
122 typename InputImageType::Pointer input= volumeReader->GetOutput();
124 // Read the dicom directory
125 typedef itk::ImageSeriesReader< InputImageType > ReaderType;
126 typedef itk::GDCMImageIO ImageIOType;
127 typedef itk::GDCMSeriesFileNames NamesGeneratorType;
129 ImageIOType::Pointer gdcmIO = ImageIOType::New();
130 NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
131 namesGenerator->SetInputDirectory( m_ArgsInfo.inputDir_arg );
132 typename ReaderType::FileNamesContainer filenames = namesGenerator->GetInputFileNames();
134 // Output the dicom files
135 unsigned int numberOfFilenames = filenames.size();
138 std::cout << numberOfFilenames <<" were read in the directory "<<m_ArgsInfo.inputDir_arg<<"..."<<std::endl<<std::endl;
139 for(unsigned int fni = 0; fni<numberOfFilenames; fni++)
141 std::cout << "filename # " << fni << " = ";
142 std::cout << filenames[fni] << std::endl;
147 typename ReaderType::Pointer reader = ReaderType::New();
148 reader->SetImageIO( gdcmIO );
149 reader->SetFileNames( filenames );
154 catch (itk::ExceptionObject &excp)
156 std::cerr << "Error: Exception thrown while writing the DICOM series!!" << std::endl;
157 std::cerr << excp << std::endl;
161 // Modify the meta dictionary
162 typedef itk::MetaDataDictionary DictionaryType;
163 const std::vector<DictionaryType*>* dictionary = reader->GetMetaDataDictionaryArray();
166 unsigned int numberOfKeysGiven=0;
167 if(m_ArgsInfo.midP_flag && m_ArgsInfo.key_given)
168 std::cerr<<"Error: both keys and midP option are given"<<std::endl;
169 else if (m_ArgsInfo.midP_flag)
172 numberOfKeysGiven=m_ArgsInfo.key_given;
174 for (unsigned int i = 0; i < numberOfKeysGiven; i++)
176 std::string entryId(m_ArgsInfo.key_arg[i] );
177 std::string value( m_ArgsInfo.tag_arg[i] );
178 for(unsigned int fni = 0; fni<numberOfFilenames; fni++)
179 itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), entryId, value );
182 // Output directory and filenames
183 itksys::SystemTools::MakeDirectory( m_ArgsInfo.outputDir_arg ); // create if it doesn't exist
184 typedef itk::ImageSeriesWriter<InputImageType, OutputImageType > SeriesWriterType;
185 typename SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
187 seriesWriter->SetInput( volumeReader->GetOutput() );
188 seriesWriter->SetImageIO( gdcmIO );
189 namesGenerator->SetOutputDirectory( m_ArgsInfo.outputDir_arg );
190 seriesWriter->SetFileNames( namesGenerator->GetOutputFileNames() );
191 //seriesWriter->SetMetaDataDictionaryArray( dictionary );
192 seriesWriter->SetMetaDataDictionaryArray( dictionary );
197 seriesWriter->Update();
199 catch( itk::ExceptionObject & excp )
201 std::cerr << "Error: Exception thrown while writing the series!!" << std::endl;
202 std::cerr << excp << std::endl;
210 #endif //#define clitkWriteDicomSeriesGenericFilter_txx