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