]> Creatis software - clitk.git/blob - itk/clitkSetBackgroundImageFilter.h
Set Directions to CropImage Like
[clitk.git] / itk / clitkSetBackgroundImageFilter.h
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 __clitkSetBackgroundImageFilter_h
19 #define __clitkSetBackgroundImageFilter_h
20 #include "itkFlexibleBinaryFunctorImageFilter.h"
21 #include "itkNumericTraits.h"
22
23 #include "clitkCommon.h"
24
25
26 namespace clitk
27 {
28   
29 namespace Functor {  
30   
31 template< class TInput, class TMask, class TOutput=TInput >
32 class SetBackground
33 {
34 public:
35   typedef typename itk::NumericTraits< TInput >::AccumulateType AccumulatorType;
36
37   //SetBackground(): m_OutsideValue(itk::NumericTraits< TOutput >::Zero),m_Fg(false),m_MaskValue( static_cast<TMask>(0) ) {};
38   SetBackground()
39   { 
40     m_OutsideValue=itk::NumericTraits< TOutput >::Zero;
41     m_Fg=false;
42     m_MaskValue= static_cast<TMask>(0);
43   }
44   ~SetBackground() {};
45   bool operator!=( const SetBackground & ) const
46     {
47     return false;
48     }
49   bool operator==( const SetBackground & other ) const
50     {
51     return !(*this != other);
52     }
53
54   inline TOutput operator()( const TInput & A, const TMask & B)
55     {
56       // Background mode: set everything = background value
57       if(!m_Fg)
58         {
59           if (B == m_MaskValue ) 
60             {
61               return static_cast<TOutput>( m_OutsideValue );
62             }
63           else
64             {
65               return A;
66             }
67         }
68
69       // Foreground mode: set everything != foreground value
70       else
71         {
72           if (B != m_MaskValue ) 
73             {
74               return static_cast<TOutput>( m_OutsideValue );
75             }
76           else
77             {
78               return A;
79             }
80         }
81     }
82   
83   /** Method to explicitly set the outside value of the mask */
84   void SetOutsideValue( const TOutput &outsideValue )
85   {
86     m_OutsideValue = static_cast<TOutput>(outsideValue);
87   }
88   
89   /** Method to get the outside value of the mask */
90   const TOutput &GetOutsideValue() const
91   {
92     return m_OutsideValue;
93   }
94   
95   /** Method to explicitly set the relevant value of the mask */
96   void SetMaskValue( const TMask &maskValue )
97   {
98     m_MaskValue = static_cast<TMask>(maskValue);
99   }
100   
101   /** Method to get the relevant value of the mask */
102   const TMask &GetMaskValue() const
103   {
104     return m_MaskValue;
105   }
106
107   /** Method to explicitly set the foreground mode of the mask */
108   void SetForeground( const bool &fg )
109   {
110     m_Fg = fg;
111   }
112   
113   /** Method to get the foregroundmode of the mask */
114   const bool &GetForeground() const
115   {
116     return m_Fg;
117   }
118
119 private:
120   TOutput m_OutsideValue;
121   TMask m_MaskValue;
122   bool m_Fg;
123 }; 
124
125 } //end namespace
126
127 template <class TInputImage, class TMaskImage, class TOutputImage=TInputImage>
128 class ITK_EXPORT SetBackgroundImageFilter :
129     public
130     itk::FlexibleBinaryFunctorImageFilter<TInputImage,TMaskImage,TOutputImage, 
131                          Functor::SetBackground< 
132   typename TInputImage::PixelType, 
133   typename TMaskImage::PixelType,
134   typename TOutputImage::PixelType>   >
135
136
137 {
138 public:
139   /** Standard class typedefs. */
140   typedef SetBackgroundImageFilter           Self;
141   typedef itk::FlexibleBinaryFunctorImageFilter<TInputImage,TMaskImage,TOutputImage, 
142                                    Functor::SetBackground< 
143     typename TInputImage::PixelType, 
144     typename TMaskImage::PixelType,
145     typename TOutputImage::PixelType>   
146   >                                 Superclass;
147   typedef itk::SmartPointer<Self>        Pointer;
148   typedef itk::SmartPointer<const Self>  ConstPointer;
149
150   /** Method for creation through the object factory. */
151   itkNewMacro(Self);
152
153   /** Runtime information support. */
154   itkTypeMacro(SetBackgroundImageFilter, 
155                FlexibleBinaryFunctorImageFilter);
156
157   /** Method to explicitly set the outside value of the mask. Defaults to 0 */
158   void SetOutsideValue( const typename TOutputImage::PixelType & outsideValue ) 
159     {
160     if( this->GetOutsideValue() != outsideValue )
161       {
162       this->Modified();
163       this->GetFunctor().SetOutsideValue( outsideValue );
164       }
165     }
166
167   const typename TOutputImage::PixelType & GetOutsideValue() const
168     {
169     return this->GetFunctor().GetOutsideValue();
170     }
171
172   /** Method to explicitly set the value of the mask. Defaults to 0 */
173   void SetMaskValue( const typename TMaskImage::PixelType & maskValue ) 
174     {
175     if( this->GetMaskValue() != maskValue )
176       {
177       this->Modified();
178       this->GetFunctor().SetMaskValue( maskValue );
179       }
180     }
181
182   const typename TMaskImage::PixelType & GetMaskValue() const
183     {
184     return this->GetFunctor().GetMaskValue();
185     }
186
187   /** Method to set the foreground mode. Defaults to 0 */
188   void SetForeground( const bool & fg ) 
189     {
190     if( this->GetForeground() != fg )
191       {
192       this->Modified();
193       this->GetFunctor().SetForeground( fg );
194       }
195     }
196
197   const bool & GetForeground() const
198     {
199     return this->GetFunctor().GetForeground();
200     }
201
202 #ifdef ITK_USE_CONCEPT_CHECKING
203   /** Begin concept checking */
204   itkConceptMacro(MaskEqualityComparableCheck,
205                   (itk::Concept::EqualityComparable<typename TMaskImage::PixelType>));
206   itkConceptMacro(InputConvertibleToOutputCheck,
207     (itk::Concept::Convertible<typename TInputImage::PixelType,
208                           typename TOutputImage::PixelType>));
209   /** End concept checking */
210 #endif
211
212 protected:
213   SetBackgroundImageFilter() {}
214   virtual ~SetBackgroundImageFilter() {}
215
216   void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
217     {
218     Superclass::PrintSelf(os, indent);
219     os << indent << "OutsideValue: "  << this->GetOutsideValue() << std::endl;
220     os << indent << "MaskValue: "  << this->GetMaskValue() << std::endl;
221     os << indent << "Foreground mode: "  << this->GetForeground() << std::endl;
222     }
223
224 private:
225   SetBackgroundImageFilter(const Self&); //purposely not implemented
226   void operator=(const Self&); //purposely not implemented
227
228 };
229
230 } // end namespace clitk
231
232
233 #endif