]> Creatis software - clitk.git/blob - tools/clitkMirrorPadImageGenericFilter.txx
Be sure to have the correct origin in clitkImage2DicomDose output
[clitk.git] / tools / clitkMirrorPadImageGenericFilter.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 clitkMirrorPadImageGenericFilter_txx
19 #define clitkMirrorPadImageGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkMirrorPadImageGenericFilter.txx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34   //-------------------------------------------------------------------
35   // Update with the number of dimensions
36   //-------------------------------------------------------------------
37   template<unsigned int Dimension>
38   void 
39   MirrorPadImageGenericFilter::UpdateWithDim(std::string PixelType)
40   {
41     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
42
43     if(PixelType == "short"){  
44       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
45       UpdateWithDimAndPixelType<Dimension, signed short>(); 
46     }
47     //    else if(PixelType == "unsigned_short"){  
48     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
49     //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
50     //     }
51     
52     else if (PixelType == "unsigned_char"){ 
53       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
54       UpdateWithDimAndPixelType<Dimension, unsigned char>();
55     }
56     
57     //     else if (PixelType == "char"){ 
58     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
59     //       UpdateWithDimAndPixelType<Dimension, signed char>();
60     //     }
61     else {
62       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
63       UpdateWithDimAndPixelType<Dimension, float>();
64     }
65   }
66
67
68   //-------------------------------------------------------------------
69   // Update with the number of dimensions and the pixeltype
70   //-------------------------------------------------------------------
71   template <unsigned int Dimension, class  PixelType> 
72   void 
73   MirrorPadImageGenericFilter::UpdateWithDimAndPixelType()
74   {
75
76     // ImageTypes
77     typedef itk::Image<PixelType, Dimension> InputImageType;
78     typedef itk::Image<PixelType, Dimension> OutputImageType;
79     
80     // Read the input
81     typedef itk::ImageFileReader<InputImageType> InputReaderType;
82     typename InputReaderType::Pointer reader = InputReaderType::New();
83     reader->SetFileName( m_InputFileName);
84     reader->Update();
85     typename InputImageType::Pointer input= reader->GetOutput();
86
87     // Filter
88     typedef itk::MirrorPadImageFilter<InputImageType, OutputImageType> MirrorPadImageFilterType;
89     typename MirrorPadImageFilterType::Pointer mirrorPadImageFilter=MirrorPadImageFilterType::New();
90     mirrorPadImageFilter->SetInput(input);
91
92     //Get the padding size
93     typename InputImageType::SizeType padSize;
94
95     // Mirror the entire image
96     if (m_ArgsInfo.mirror_given)
97       {
98         typename InputImageType::SizeType inputSize=input->GetLargestPossibleRegion().GetSize();
99         padSize.Fill(0);
100         padSize[m_ArgsInfo.mirror_arg]=inputSize[m_ArgsInfo.mirror_arg];
101         if (m_Verbose) std::cout<<"Mirroring the entire image along axis "<< m_ArgsInfo.mirror_arg<<"..."<<std::endl;
102       }
103
104     // Pad with a given size
105     else
106       {
107         if(m_ArgsInfo.pad_given)
108           for(unsigned int i=0; i<Dimension; i++)
109             padSize[i]=m_ArgsInfo.pad_arg[i];
110         if (m_Verbose) std::cout<<"Mirror padding with size "<< padSize<<"..."<<std::endl;
111       }
112
113     // Lower or upper?
114     if (m_ArgsInfo.lower_given)
115       mirrorPadImageFilter->SetPadLowerBound(padSize);
116     
117     else
118       mirrorPadImageFilter->SetPadUpperBound(padSize);
119   
120     mirrorPadImageFilter->Update();
121     typename  OutputImageType::Pointer output = mirrorPadImageFilter->GetOutput();
122
123     // Origin?
124     typename OutputImageType::PointType origin;
125     origin.Fill(itk::NumericTraits<double>::Zero);
126     if (m_ArgsInfo.origin_flag)
127       {
128         output->Update();
129         output->SetOrigin(origin);
130         if (m_Verbose) std::cout<<"Setting origin to  "<< output->GetOrigin()<<"..."<<std::endl;
131       }
132
133     // Output
134     typedef itk::ImageFileWriter<OutputImageType> WriterType;
135     typename WriterType::Pointer writer = WriterType::New();
136     writer->SetFileName(m_ArgsInfo.output_arg);
137     writer->SetInput(output);
138     writer->Update();
139   }
140
141
142 }//end clitk
143  
144 #endif //#define clitkMirrorPadImageGenericFilter_txx