]> Creatis software - clitk.git/blob - tools/clitkWriteDicomSeriesGenericFilter.txx
Reformatted using new coding style
[clitk.git] / tools / clitkWriteDicomSeriesGenericFilter.txx
1 /*=========================================================================
2   Program:   vv                     http://www.creatis.insa-lyon.fr/rio/vv
3
4   Authors belong to:
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
8
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.
12
13   It is distributed under dual licence
14
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
20
21 /* =================================================
22  * @file   clitkWriteDicomSeriesGenericFilter.txx
23  * @author
24  * @date
25  *
26  * @brief
27  *
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34
35 //-----------------------------------------------------------
36 // Constructor
37 //-----------------------------------------------------------
38 template<class args_info_type>
39 WriteDicomSeriesGenericFilter<args_info_type>::WriteDicomSeriesGenericFilter()
40 {
41   m_Verbose=false;
42   m_InputFileName="";
43 }
44
45
46 //-----------------------------------------------------------
47 // Update
48 //-----------------------------------------------------------
49 template<class args_info_type>
50 void WriteDicomSeriesGenericFilter<args_info_type>::Update()
51 {
52   // Read the Dimension and PixelType
53   int Dimension;
54   std::string PixelType;
55   ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
56
57
58   // Call UpdateWithDim
59   if(Dimension==2) UpdateWithDim<2>(PixelType);
60   else if(Dimension==3) UpdateWithDim<3>(PixelType);
61   // else if (Dimension==4)UpdateWithDim<4>(PixelType);
62   else {
63     std::cout<<"Error, Only for 2 or 3  Dimensions!!!"<<std::endl ;
64     return;
65   }
66 }
67
68 //-------------------------------------------------------------------
69 // Update with the number of dimensions
70 //-------------------------------------------------------------------
71 template<class args_info_type>
72 template<unsigned int Dimension>
73 void
74 WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
75 {
76   if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
77
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>();
81   }
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>();
85   //     }
86
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>();
90   }
91
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>();
95   //     }
96   else {
97     if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
98     UpdateWithDimAndPixelType<Dimension, float>();
99   }
100 }
101
102
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>
108 void
109 WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
110 {
111
112   // ImageTypes
113   typedef itk::Image<PixelType, Dimension> InputImageType;
114   typedef itk::Image<PixelType, 2> OutputImageType;
115
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();
122
123   // Read the dicom directory
124   typedef itk::ImageSeriesReader< InputImageType >     ReaderType;
125   typedef itk::GDCMImageIO ImageIOType;
126   typedef itk::GDCMSeriesFileNames NamesGeneratorType;
127
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();
132
133   // Output the dicom files
134   unsigned int numberOfFilenames =  filenames.size();
135   if (m_Verbose) {
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;
140     }
141   }
142
143   // Read the series
144   typename ReaderType::Pointer reader = ReaderType::New();
145   reader->SetImageIO( gdcmIO );
146   reader->SetFileNames( filenames );
147   try {
148     reader->Update();
149   } catch (itk::ExceptionObject &excp) {
150     std::cerr << "Error: Exception thrown while writing the DICOM series!!" << std::endl;
151     std::cerr << excp << std::endl;
152   }
153
154
155   // Modify the meta dictionary
156   typedef itk::MetaDataDictionary   DictionaryType;
157   const std::vector<DictionaryType*>* dictionary = reader->GetMetaDataDictionaryArray();
158
159   // Get keys
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)
164     numberOfKeysGiven=1;
165   else
166     numberOfKeysGiven=m_ArgsInfo.key_given;
167
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 );
173   }
174
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();
179
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 );
186
187   // Write
188   try {
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;
193   }
194
195 }
196
197
198 }//end clitk
199
200 #endif //#define clitkWriteDicomSeriesGenericFilter_txx