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