]> Creatis software - clitk.git/blob - tools/clitkGateSimulation2DicomGenericFilter.txx
f49bebcee94966dfe57ffb3588eee40882e77818
[clitk.git] / tools / clitkGateSimulation2DicomGenericFilter.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://www.centreleonberard.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 clitkGateSimulation2DicomGenericFilter_txx
19 #define clitkGateSimulation2DicomGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkGateSimulation2DicomGenericFilter.txx
23  * @author
24  * @date
25  *
26  * @brief
27  *
28  ===================================================*/
29
30 // clitk
31 #include "clitkResampleImageWithOptionsFilter.h"
32 #if GDCM_MAJOR_VERSION >= 2
33 #include "gdcmUIDGenerator.h"
34 #else
35 #include "gdcmFile.h"
36 #include "gdcmUtil.h"
37 #endif
38
39
40 namespace clitk
41 {
42
43
44 //-----------------------------------------------------------
45 // Constructor
46 //-----------------------------------------------------------
47 template<class args_info_type>
48 GateSimulation2DicomGenericFilter<args_info_type>::GateSimulation2DicomGenericFilter()
49 {
50   m_Verbose=false;
51   m_InputFileName="";
52 }
53
54
55 //-----------------------------------------------------------
56 // Update
57 //-----------------------------------------------------------
58 template<class args_info_type>
59 void GateSimulation2DicomGenericFilter<args_info_type>::Update()
60 {
61   // Read the Dimension and PixelType
62   int Dimension;
63   std::string PixelType;
64   ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
65
66
67   // Call UpdateWithDim
68   if(Dimension==2) UpdateWithDim<2>(PixelType);
69   else if(Dimension==3) UpdateWithDim<3>(PixelType);
70   // else if (Dimension==4)UpdateWithDim<4>(PixelType);
71   else {
72     std::cout<<"Error, Only for 2 or 3  Dimensions!!!"<<std::endl ;
73     return;
74   }
75 }
76
77 //-------------------------------------------------------------------
78 // Update with the number of dimensions
79 //-------------------------------------------------------------------
80 template<class args_info_type>
81 template<unsigned int Dimension>
82 void
83 GateSimulation2DicomGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
84 {
85   if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
86
87   if(PixelType == "short") {
88     if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
89     UpdateWithDimAndPixelType<Dimension, signed short>();
90   }
91   else if(PixelType == "unsigned_short"){
92     if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
93     UpdateWithDimAndPixelType<Dimension, unsigned short>();
94   }
95
96   else if (PixelType == "unsigned_char") {
97     if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
98     UpdateWithDimAndPixelType<Dimension, unsigned char>();
99   }
100
101   //     else if (PixelType == "char"){
102   //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
103   //       UpdateWithDimAndPixelType<Dimension, signed char>();
104   //     }
105   else if (PixelType == "double") {
106     if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and double..." << std::endl;
107     UpdateWithDimAndPixelType<Dimension, double>();
108   }
109   else {
110     if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
111     UpdateWithDimAndPixelType<Dimension, float>();
112   }
113 }
114
115 //-------------------------------------------------------------------
116 // Update with the number of dimensions and the pixeltype read from
117 // the dicom files. The MHD files may be resampled to match the
118 // dicom spacing (and number of slices). Rounding errors in resampling
119 // are handled by removing files when generating the output dicom
120 // series.
121 //-------------------------------------------------------------------
122 template<class args_info_type>
123 template <unsigned int Dimension, class  PixelType>
124 void
125 GateSimulation2DicomGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
126 {
127
128   // ImageTypes
129   typedef itk::Image<PixelType, Dimension> InputImageType;
130   typedef itk::Image<PixelType, Dimension> OutputImageType;
131
132   // Read the dicom directory
133   typedef itk::ImageFileReader< InputImageType >     ReaderType;
134   typedef itk::GDCMImageIO ImageIOType;
135   typedef itk::GDCMSeriesFileNames NamesGeneratorType;
136
137   ImageIOType::Pointer gdcmIO = ImageIOType::New();
138   std::string filename_out = m_ArgsInfo.outputFilename_arg;
139
140   // Output the dicom files
141   unsigned int numberOfFilenames =  1;
142   if (m_Verbose) {
143     std::cout << numberOfFilenames <<" were read in the directory "<<m_ArgsInfo.inputModelFilename_arg<<"..."<<std::endl<<std::endl;
144   }
145
146   // Read the series
147   typename ReaderType::Pointer reader = ReaderType::New();
148 #if GDCM_MAJOR_VERSION >= 2
149   gdcmIO->LoadPrivateTagsOn();
150   gdcmIO->KeepOriginalUIDOn();
151 #endif
152   reader->SetImageIO( gdcmIO );
153   reader->SetFileName( m_ArgsInfo.inputModelFilename_arg );
154   try {
155     reader->Update();
156   } catch (itk::ExceptionObject &excp) {
157     std::cerr << "Error: Exception thrown while reading the DICOM series!!" << std::endl;
158     std::cerr << excp << std::endl;
159   }
160
161   // Read the input (MHD file)
162   typedef typename InputImageType::RegionType RegionType;
163   typedef typename RegionType::SizeType SizeType;
164   typedef itk::ImageFileReader<InputImageType> InputReaderType;
165   typename InputReaderType::Pointer volumeReader = InputReaderType::New();
166   volumeReader->SetFileName( m_InputFileName);
167   volumeReader->Update();
168   
169   typename InputImageType::Pointer input = volumeReader->GetOutput();
170   if (input->GetLargestPossibleRegion().GetSize() != reader->GetOutput()->GetLargestPossibleRegion().GetSize()) {
171         
172     // resampling is carried out on the fly if resolution or size between 
173     // the input mhd and input dicom series is different
174     
175     // Filter
176     typedef clitk::ResampleImageWithOptionsFilter<InputImageType, InputImageType> ResampleImageFilterType;
177     typename ResampleImageFilterType::Pointer filter = ResampleImageFilterType::New();
178     filter->SetInput(input);
179     filter->SetVerboseOptions(m_Verbose);
180     filter->SetGaussianFilteringEnabled(false);
181     filter->SetDefaultPixelValue(0);
182
183     const SizeType& input_size = input->GetLargestPossibleRegion().GetSize();
184     SizeType output_size;
185     for (unsigned int i = 0; i < Dimension; i++)
186       output_size[i] = input_size[i];
187     filter->SetOutputSize(output_size);
188     if (m_Verbose) {
189         std::cout << "Warning: The image size differs between the MHD file and the input dicom series. Performing resampling with default options using mhd size as reference (for advanced options, use clitkResampleImage)." << std::endl;
190         std::cout << "MHD -> " << input->GetLargestPossibleRegion().GetSize() << std::endl;
191         std::cout << "dicom -> " << reader->GetOutput()->GetLargestPossibleRegion().GetSize() << std::endl;
192     }
193
194     try {
195       filter->Update();
196       input = filter->GetOutput();
197     } catch( itk::ExceptionObject & excp ) {
198     std::cerr << "Error: Exception thrown while resampling!!" << std::endl;
199     std::cerr << excp << std::endl;
200     }
201   }
202
203 #if GDCM_MAJOR_VERSION < 2
204   gdcmIO->SetKeepOriginalUID(true);
205 #endif
206
207
208   //Read the dicom file to find the energy, the head & the steps (TODO: do it on the mhd filetext)
209
210
211
212 /*
213   // update output dicom keys/tags
214   //std::string seriesUIDkey = "0020|000e";
215   for (unsigned int i = 0; i < numberOfKeysGiven; i++) {
216     std::string entryId(m_ArgsInfo.key_arg[i]  );
217     std::string value( m_ArgsInfo.tag_arg[i] );
218
219     itk::EncapsulateMetaData<std::string>(reader->GetMetaDataDictionary(), entryId, value );
220   }
221 */
222   // Output directory and filenames
223   //itksys::SystemTools::MakeDirectory( m_ArgsInfo.outputFilename_arg ); // create if it doesn't exist
224   typedef itk::ImageFileWriter<OutputImageType>  SeriesWriterType;
225   typename SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
226
227   seriesWriter->SetInput( input );
228   seriesWriter->SetImageIO( gdcmIO );
229   
230   seriesWriter->SetFileName( filename_out );
231   seriesWriter->SetMetaDataDictionary(reader->GetMetaDataDictionary());
232
233   // Write
234   try {
235     if (m_ArgsInfo.verbose_flag)
236       std::cout << seriesWriter << std::endl;
237     seriesWriter->Update();
238   } catch( itk::ExceptionObject & excp ) {
239     std::cerr << "Error: Exception thrown while writing the series!!" << std::endl;
240     std::cerr << excp << std::endl;
241   }
242
243 }
244
245 }//end clitk
246
247 #endif //#define clitkGateSimulation2DicomGenericFilter_txx