]> Creatis software - clitk.git/blob - tools/clitkInvertVFGenericFilter.txx
added the new headers
[clitk.git] / tools / clitkInvertVFGenericFilter.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 clitkInvertVFGenericFilter_txx
19 #define clitkInvertVFGenericFilter_txx
20
21 /* =================================================
22  * @file   clitkInvertVFGenericFilter.txx
23  * @author 
24  * @date   
25  * 
26  * @brief 
27  * 
28  ===================================================*/
29
30
31 namespace clitk
32 {
33
34   //-----------------------------------------------------------
35   // Constructor
36   //-----------------------------------------------------------
37   template<class args_info_type>
38   InvertVFGenericFilter<args_info_type>::InvertVFGenericFilter()
39   {
40     m_Verbose=false;
41     m_InputFileName="";
42   }
43
44
45   //-----------------------------------------------------------
46   // Update
47   //-----------------------------------------------------------
48   template<class args_info_type>
49   void InvertVFGenericFilter<args_info_type>::Update()
50   {
51     // Read the Dimension and PixelType
52     int Dimension;
53     std::string PixelType;
54     ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
55
56     
57     // Call UpdateWithDim
58     if(Dimension==2) UpdateWithDim<2>(PixelType);
59     else if(Dimension==3) UpdateWithDim<3>(PixelType);
60     // else if (Dimension==4)UpdateWithDim<4>(PixelType); 
61     else 
62       {
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   InvertVFGenericFilter<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, itk::Vector<float, Dimension> >();
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   InvertVFGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
110   {
111
112     // ImageTypes
113     typedef itk::Image<PixelType, Dimension> InputImageType;
114     typedef itk::Image<PixelType, Dimension> OutputImageType;
115     
116     // Read the input
117     typedef itk::ImageFileReader<InputImageType> InputReaderType;
118     typename InputReaderType::Pointer reader = InputReaderType::New();
119     reader->SetFileName( m_InputFileName);
120     reader->Update();
121     typename InputImageType::Pointer input= reader->GetOutput();
122
123     // Filter
124     typename OutputImageType::Pointer output;
125     switch (m_ArgsInfo.type_arg)
126       {
127         
128         // clitk filter
129       case 0:
130         {
131           // Create the InvertVFFilter
132           typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
133           typename FilterType::Pointer filter =FilterType::New();
134           filter->SetInput(input);
135           filter->SetVerbose(m_Verbose);
136           if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
137           if (m_ArgsInfo.pad_given) 
138             {
139               PixelType pad;
140               if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
141                 pad.Fill(m_ArgsInfo.pad_arg[0]);
142               else
143                 for(unsigned int i=0; i<Dimension; i++)
144                   pad[i]=m_ArgsInfo.pad_arg[i];
145             }
146           filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
147           filter->Update();
148           output=filter->GetOutput();
149
150           break;
151         }
152         
153       case 1:
154         {
155           // Create the InverseDeformationFieldFilter
156           typedef itk::InverseDeformationFieldImageFilter<InputImageType,OutputImageType> FilterType;
157           typename FilterType::Pointer filter =FilterType::New();
158           filter->SetInput(input);
159           filter->SetOutputOrigin(input->GetOrigin());
160           filter->SetOutputSpacing(input->GetSpacing());
161           filter->SetSize(input->GetLargestPossibleRegion().GetSize());
162           filter->SetSubsamplingFactor(m_ArgsInfo.sampling_arg);
163           filter->Update();
164           output=filter->GetOutput();
165
166           break;
167         }
168         
169       }
170     
171     // Output
172     typedef itk::ImageFileWriter<OutputImageType> WriterType;
173     typename WriterType::Pointer writer = WriterType::New();
174     writer->SetFileName(m_ArgsInfo.output_arg);
175     writer->SetInput(output);
176     writer->Update();
177     
178   }
179   
180   
181 }//end clitk
182  
183 #endif //#define clitkInvertVFGenericFilter_txx