]> Creatis software - clitk.git/blob - tools/clitkInvertVFGenericFilter.txx
Initial revision
[clitk.git] / tools / clitkInvertVFGenericFilter.txx
1 #ifndef clitkInvertVFGenericFilter_txx
2 #define clitkInvertVFGenericFilter_txx
3
4 /* =================================================
5  * @file   clitkInvertVFGenericFilter.txx
6  * @author 
7  * @date   
8  * 
9  * @brief 
10  * 
11  ===================================================*/
12
13
14 namespace clitk
15 {
16
17   //-----------------------------------------------------------
18   // Constructor
19   //-----------------------------------------------------------
20   template<class args_info_type>
21   InvertVFGenericFilter<args_info_type>::InvertVFGenericFilter()
22   {
23     m_Verbose=false;
24     m_InputFileName="";
25   }
26
27
28   //-----------------------------------------------------------
29   // Update
30   //-----------------------------------------------------------
31   template<class args_info_type>
32   void InvertVFGenericFilter<args_info_type>::Update()
33   {
34     // Read the Dimension and PixelType
35     int Dimension;
36     std::string PixelType;
37     ReadImageDimensionAndPixelType(m_InputFileName, Dimension, PixelType);
38
39     
40     // Call UpdateWithDim
41     if(Dimension==2) UpdateWithDim<2>(PixelType);
42     else if(Dimension==3) UpdateWithDim<3>(PixelType);
43     // else if (Dimension==4)UpdateWithDim<4>(PixelType); 
44     else 
45       {
46         std::cout<<"Error, Only for 2 or 3  Dimensions!!!"<<std::endl ;
47         return;
48       }
49   }
50
51   //-------------------------------------------------------------------
52   // Update with the number of dimensions
53   //-------------------------------------------------------------------
54   template<class args_info_type>
55   template<unsigned int Dimension>
56   void 
57   InvertVFGenericFilter<args_info_type>::UpdateWithDim(std::string PixelType)
58   {
59     if (m_Verbose) std::cout << "Image was detected to be "<<Dimension<<"D and "<< PixelType<<"..."<<std::endl;
60
61     //    if(PixelType == "short"){  
62     //       if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed short..." << std::endl;
63     //       UpdateWithDimAndPixelType<Dimension, signed short>(); 
64     //     }
65     //    else if(PixelType == "unsigned_short"){  
66     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl;
67     //       UpdateWithDimAndPixelType<Dimension, unsigned short>(); 
68     //     }
69     
70     //     else if (PixelType == "unsigned_char"){ 
71     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl;
72     //       UpdateWithDimAndPixelType<Dimension, unsigned char>();
73     //     }
74     
75     //     else if (PixelType == "char"){ 
76     //       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl;
77     //       UpdateWithDimAndPixelType<Dimension, signed char>();
78     //     }
79     //  else {
80       if (m_Verbose) std::cout  << "Launching filter in "<< Dimension <<"D and float..." << std::endl;
81       UpdateWithDimAndPixelType<Dimension, itk::Vector<float, Dimension> >();
82       // }
83   }
84
85
86   //-------------------------------------------------------------------
87   // Update with the number of dimensions and the pixeltype
88   //-------------------------------------------------------------------
89   template<class args_info_type>
90   template <unsigned int Dimension, class  PixelType> 
91   void 
92   InvertVFGenericFilter<args_info_type>::UpdateWithDimAndPixelType()
93   {
94
95     // ImageTypes
96     typedef itk::Image<PixelType, Dimension> InputImageType;
97     typedef itk::Image<PixelType, Dimension> OutputImageType;
98     
99     // Read the input
100     typedef itk::ImageFileReader<InputImageType> InputReaderType;
101     typename InputReaderType::Pointer reader = InputReaderType::New();
102     reader->SetFileName( m_InputFileName);
103     reader->Update();
104     typename InputImageType::Pointer input= reader->GetOutput();
105
106     // Filter
107     typename OutputImageType::Pointer output;
108     switch (m_ArgsInfo.type_arg)
109       {
110         
111         // clitk filter
112       case 0:
113         {
114           // Create the InvertVFFilter
115           typedef clitk::InvertVFFilter<InputImageType,OutputImageType> FilterType;
116           typename FilterType::Pointer filter =FilterType::New();
117           filter->SetInput(input);
118           filter->SetVerbose(m_Verbose);
119           if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg);
120           if (m_ArgsInfo.pad_given) 
121             {
122               PixelType pad;
123               if (m_ArgsInfo.pad_given !=  (pad.GetNumberOfComponents()) )
124                 pad.Fill(m_ArgsInfo.pad_arg[0]);
125               else
126                 for(unsigned int i=0; i<Dimension; i++)
127                   pad[i]=m_ArgsInfo.pad_arg[i];
128             }
129           filter->SetThreadSafe(m_ArgsInfo.threadSafe_flag);
130           filter->Update();
131           output=filter->GetOutput();
132
133           break;
134         }
135         
136       case 1:
137         {
138           // Create the InverseDeformationFieldFilter
139           typedef itk::InverseDeformationFieldImageFilter<InputImageType,OutputImageType> FilterType;
140           typename FilterType::Pointer filter =FilterType::New();
141           filter->SetInput(input);
142           filter->SetOutputOrigin(input->GetOrigin());
143           filter->SetOutputSpacing(input->GetSpacing());
144           filter->SetSize(input->GetLargestPossibleRegion().GetSize());
145           filter->SetSubsamplingFactor(m_ArgsInfo.sampling_arg);
146           filter->Update();
147           output=filter->GetOutput();
148
149           break;
150         }
151         
152       }
153     
154     // Output
155     typedef itk::ImageFileWriter<OutputImageType> WriterType;
156     typename WriterType::Pointer writer = WriterType::New();
157     writer->SetFileName(m_ArgsInfo.output_arg);
158     writer->SetInput(output);
159     writer->Update();
160     
161   }
162   
163   
164 }//end clitk
165  
166 #endif //#define clitkInvertVFGenericFilter_txx