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://www.centreleonberard.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 clitkGateSimulation2DicomGenericFilter_txx
19 #define clitkGateSimulation2DicomGenericFilter_txx
21 /* =================================================
22 * @file clitkGateSimulation2DicomGenericFilter.txx
28 ===================================================*/
31 #include "clitkResampleImageWithOptionsFilter.h"
32 #if GDCM_MAJOR_VERSION >= 2
33 #include "gdcmUIDGenerator.h"
44 //-----------------------------------------------------------
46 //-----------------------------------------------------------
47 template<class args_info_type>
48 GateSimulation2DicomGenericFilter<args_info_type>::GateSimulation2DicomGenericFilter()
55 //-----------------------------------------------------------
57 //-----------------------------------------------------------
58 template<class args_info_type>
59 void GateSimulation2DicomGenericFilter<args_info_type>::Update()
61 // Read the Dimension and PixelType
63 std::string PixelType;
64 ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
68 if(Dimension==2) UpdateWithDim<2>(PixelType);
69 else if(Dimension==3) UpdateWithDim<3>(PixelType);
70 // else if (Dimension==4)UpdateWithDim<4>(PixelType);
72 std::cout<<"Error, Only for 2 or 3 Dimensions!!!"<<std::endl ;
77 //-------------------------------------------------------------------
78 // Update with the number of dimensions
79 //-------------------------------------------------------------------
80 template<class args_info_type>
81 template<unsigned int Dimension>
83 GateSimulation2DicomGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
85 if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
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>();
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>();
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>();
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>();
105 else if (PixelType == "double") {
106 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and double..." << std::endl;
107 UpdateWithDimAndPixelType<Dimension, double>();
110 if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
111 UpdateWithDimAndPixelType<Dimension, float>();
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
121 //-------------------------------------------------------------------
122 template<class args_info_type>
123 template <unsigned int Dimension, class PixelType>
125 GateSimulation2DicomGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
129 typedef itk::Image<PixelType, Dimension> InputImageType;
130 typedef itk::Image<PixelType, Dimension> OutputImageType;
132 // Read the dicom directory
133 typedef itk::ImageFileReader< InputImageType > ReaderType;
134 typedef itk::GDCMImageIO ImageIOType;
135 typedef itk::GDCMSeriesFileNames NamesGeneratorType;
137 ImageIOType::Pointer gdcmIO = ImageIOType::New();
138 std::string filename_out = m_ArgsInfo.outputFilename_arg;
140 // Output the dicom files
141 unsigned int numberOfFilenames = 1;
143 std::cout << numberOfFilenames <<" were read in the directory "<<m_ArgsInfo.inputModelFilename_arg<<"..."<<std::endl<<std::endl;
147 typename ReaderType::Pointer reader = ReaderType::New();
148 #if GDCM_MAJOR_VERSION >= 2
149 gdcmIO->LoadPrivateTagsOn();
150 gdcmIO->KeepOriginalUIDOn();
152 reader->SetImageIO( gdcmIO );
153 reader->SetFileName( m_ArgsInfo.inputModelFilename_arg );
156 } catch (itk::ExceptionObject &excp) {
157 std::cerr << "Error: Exception thrown while reading the DICOM series!!" << std::endl;
158 std::cerr << excp << std::endl;
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();
169 typename InputImageType::Pointer input = volumeReader->GetOutput();
170 if (input->GetLargestPossibleRegion().GetSize() != reader->GetOutput()->GetLargestPossibleRegion().GetSize()) {
172 // resampling is carried out on the fly if resolution or size between
173 // the input mhd and input dicom series is different
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);
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);
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;
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;
203 #if GDCM_MAJOR_VERSION < 2
204 gdcmIO->SetKeepOriginalUID(true);
208 //Read the dicom file to find the energy, the head & the steps (TODO: do it on the mhd filetext)
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] );
219 itk::EncapsulateMetaData<std::string>(reader->GetMetaDataDictionary(), entryId, value );
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();
227 seriesWriter->SetInput( input );
228 seriesWriter->SetImageIO( gdcmIO );
230 seriesWriter->SetFileName( filename_out );
231 seriesWriter->SetMetaDataDictionary(reader->GetMetaDataDictionary());
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;
247 #endif //#define clitkGateSimulation2DicomGenericFilter_txx