+//-------------------------------------------------------------------
+// Update with the number of dimensions and the pixeltype read from
+// the dicom files. The MHD files may be resampled to match the
+// dicom spacing (and number of slices). Rounding errors in resampling
+// are handled by removing files when generating the output dicom
+// series.
+//-------------------------------------------------------------------
+template<class args_info_type>
+template <unsigned int Dimension, class PixelType>
+void
+WriteDicomSeriesGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
+{
+
+ // ImageTypes
+ typedef itk::Image<PixelType, Dimension> InputImageType;
+ typedef itk::Image<PixelType, 2> OutputImageType;
+
+ // Read the dicom directory
+ typedef itk::ImageSeriesReader< InputImageType > ReaderType;
+ typedef itk::GDCMImageIO ImageIOType;
+ typedef itk::GDCMSeriesFileNames NamesGeneratorType;
+
+ ImageIOType::Pointer gdcmIO = ImageIOType::New();
+ NamesGeneratorType::Pointer namesGenerator = NamesGeneratorType::New();
+ namesGenerator->SetInputDirectory( m_ArgsInfo.inputDir_arg );
+ namesGenerator->SetOutputDirectory( m_ArgsInfo.outputDir_arg );
+ typename ReaderType::FileNamesContainer filenames_in = namesGenerator->GetInputFileNames();
+ typename ReaderType::FileNamesContainer filenames_out = namesGenerator->GetOutputFileNames();
+
+ // Output the dicom files
+ unsigned int numberOfFilenames = filenames_in.size();
+ if (m_Verbose) {
+ std::cout << numberOfFilenames <<" were read in the directory "<<m_ArgsInfo.inputDir_arg<<"..."<<std::endl<<std::endl;
+ for(unsigned int fni = 0; fni<numberOfFilenames; fni++) {
+ std::cout << "filename # " << fni << " = ";
+ std::cout << filenames_in[fni] << std::endl;
+ }
+ }
+
+ // Read the series
+ typename ReaderType::Pointer reader = ReaderType::New();
+ reader->SetImageIO( gdcmIO );
+ reader->SetFileNames( filenames_in );
+ try {
+ reader->Update();
+ } catch (itk::ExceptionObject &excp) {
+ std::cerr << "Error: Exception thrown while reading the DICOM series!!" << std::endl;
+ std::cerr << excp << std::endl;
+ }
+
+ // Read the input (MHD file)
+ typedef typename InputImageType::RegionType RegionType;
+ typedef typename RegionType::SizeType SizeType;
+ typedef itk::ImageFileReader<InputImageType> InputReaderType;
+ typename InputReaderType::Pointer volumeReader = InputReaderType::New();
+ volumeReader->SetFileName( m_InputFileName);
+ volumeReader->Update();
+
+ typename InputImageType::Pointer input = volumeReader->GetOutput();
+ if (input->GetSpacing() != reader->GetOutput()->GetSpacing())
+ {
+ // resampling is carried out on the fly if resolution between
+ // the input mhd and input dicom series is different
+ if (m_Verbose)
+ std::cout << "Warning: The image spacing differs between the MHD file and the input dicom series. Performing resampling with default options (for advanced options, use clitkResampleImage)." << std::endl;
+
+ // Filter
+ typedef clitk::ResampleImageWithOptionsFilter<InputImageType, InputImageType> ResampleImageFilterType;
+ typename ResampleImageFilterType::Pointer filter = ResampleImageFilterType::New();
+ filter->SetInput(input);
+ filter->SetVerboseOptions(m_Verbose);
+ filter->SetGaussianFilteringEnabled(false);
+ filter->SetDefaultPixelValue(0);
+ filter->SetOutputSpacing(reader->GetOutput()->GetSpacing());
+ const SizeType& input_size = reader->GetOutput()->GetLargestPossibleRegion().GetSize();
+ SizeType output_size;
+ for (unsigned int i = 0; i < Dimension; i++)
+ output_size[i] = input_size[i];
+ filter->SetOutputSize(output_size);
+ filter->Update();
+ input = filter->GetOutput();
+ }