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);
63 std::cout<<"Error, Only for 2 or 3 Dimensions!!!"<<std::endl ;
68 //-------------------------------------------------------------------
69 // Update with the number of dimensions
70 //-------------------------------------------------------------------
71 template<class args_info_type>
72 template<unsigned int Dimension>
74 WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
76 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
78 if(PixelType == "short") {
79 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
80 UpdateWithDimAndPixelType<Dimension, signed short>();
82 // else if(PixelType == "unsigned_short"){
83 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
84 // UpdateWithDimAndPixelType<Dimension, unsigned short>();
87 else if (PixelType == "unsigned_char") {
88 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
89 UpdateWithDimAndPixelType<Dimension, unsigned char>();
92 // else if (PixelType == "char"){
93 // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
94 // UpdateWithDimAndPixelType<Dimension, signed char>();
97 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
98 UpdateWithDimAndPixelType<Dimension, float>();
103 //-------------------------------------------------------------------
104 // Update with the number of dimensions and the pixeltype
105 //-------------------------------------------------------------------
106 template<class args_info_type>
107 template <unsigned int Dimension, class PixelType>
109 WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
113 typedef itk::Image<PixelType, Dimension> InputImageType;
114 typedef itk::Image<PixelType, 2> OutputImageType;
116 // Read the input (volumetric)
117 typedef itk::ImageFileReader<InputImageType> InputReaderType;
118 typename InputReaderType::Pointer volumeReader = InputReaderType::New();
119 volumeReader->SetFileName( m_InputFileName);
120 volumeReader->Update();
121 typename InputImageType::Pointer input= volumeReader->GetOutput();
123 // Read the dicom directory
124 typedef itk::ImageSeriesReader< InputImageType > ReaderType;
125 typedef itk::GDCMImageIO ImageIOType;
126 typedef itk::GDCMSeriesFileNames NamesGeneratorType;
128 ImageIOType::Pointer gdcmIO = ImageIOType::New();
129 NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
130 namesGenerator->SetInputDirectory( m_ArgsInfo.inputDir_arg );
131 typename ReaderType::FileNamesContainer filenames = namesGenerator->GetInputFileNames();
133 // Output the dicom files
134 unsigned int numberOfFilenames = filenames.size();
136 std::cout << numberOfFilenames <<" were read in the directory "<<m_ArgsInfo.inputDir_arg<<"..."<<std::endl<<std::endl;
137 for(unsigned int fni = 0; fni<numberOfFilenames; fni++) {
138 std::cout << "filename # " << fni << " = ";
139 std::cout << filenames[fni] << std::endl;
144 typename ReaderType::Pointer reader = ReaderType::New();
145 reader->SetImageIO( gdcmIO );
146 reader->SetFileNames( filenames );
149 } catch (itk::ExceptionObject &excp) {
150 std::cerr << "Error: Exception thrown while writing the DICOM series!!" << std::endl;
151 std::cerr << excp << std::endl;
155 // Modify the meta dictionary
156 typedef itk::MetaDataDictionary DictionaryType;
157 const std::vector<DictionaryType*>* dictionary = reader->GetMetaDataDictionaryArray();
160 unsigned int numberOfKeysGiven=0;
161 if(m_ArgsInfo.midP_flag && m_ArgsInfo.key_given)
162 std::cerr<<"Error: both keys and midP option are given"<<std::endl;
163 else if (m_ArgsInfo.midP_flag)
166 numberOfKeysGiven=m_ArgsInfo.key_given;
168 for (unsigned int i = 0; i < numberOfKeysGiven; i++) {
169 std::string entryId(m_ArgsInfo.key_arg[i] );
170 std::string value( m_ArgsInfo.tag_arg[i] );
171 for(unsigned int fni = 0; fni<numberOfFilenames; fni++)
172 itk::EncapsulateMetaData<std::string>( *((*dictionary)[fni]), entryId, value );
175 // Output directory and filenames
176 itksys::SystemTools::MakeDirectory( m_ArgsInfo.outputDir_arg ); // create if it doesn't exist
177 typedef itk::ImageSeriesWriter<InputImageType, OutputImageType > SeriesWriterType;
178 typename SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
180 seriesWriter->SetInput( volumeReader->GetOutput() );
181 seriesWriter->SetImageIO( gdcmIO );
182 namesGenerator->SetOutputDirectory( m_ArgsInfo.outputDir_arg );
183 seriesWriter->SetFileNames( namesGenerator->GetOutputFileNames() );
184 //seriesWriter->SetMetaDataDictionaryArray( dictionary );
185 seriesWriter->SetMetaDataDictionaryArray( dictionary );
189 seriesWriter->Update();
190 } catch( itk::ExceptionObject & excp ) {
191 std::cerr << "Error: Exception thrown while writing the series!!" << std::endl;
192 std::cerr << excp << std::endl;
200 #endif //#define clitkWriteDicomSeriesGenericFilter_txx