]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/FloodFillImageFilter.cxx
...
[cpPlugins.git] / lib / cpPlugins / Plugins / BasicFilters / FloodFillImageFilter.cxx
1 #include "FloodFillImageFilter.h"
2 #include <cpPlugins/Interface/Image.h>
3 #include <cpPlugins/Interface/PointList.h>
4
5 #include <itkFloodFilledImageFunctionConditionalConstIterator.h>
6 #include <itkImageFunction.h>
7
8 // -------------------------------------------------------------------------
9 cpPlugins::BasicFilters::FloodFillImageFilter::
10 FloodFillImageFilter( )
11   : Superclass( )
12 {
13   /* TODO
14   this->_AddInput( "Input" );
15   this->_AddInput( "Seed" );
16   this->_AddOutput< cpPlugins::Interface::Image >( "Output" );
17
18   this->m_Parameters->ConfigureAsReal( "Window" );
19   this->m_Parameters->ConfigureAsReal( "Level" );
20   this->m_Parameters->ConfigureAsUint( "InsideValue" );
21   this->m_Parameters->ConfigureAsUint( "OutsideValue" );
22
23   this->m_Parameters->SetReal( "Window", 0 );
24   this->m_Parameters->SetReal( "Level", 0 );
25   this->m_Parameters->SetUint( "InsideValue", 0 );
26   this->m_Parameters->SetUint( "OutsideValue", 255 );
27   */
28 }
29
30 // -------------------------------------------------------------------------
31 cpPlugins::BasicFilters::FloodFillImageFilter::
32 ~FloodFillImageFilter( )
33 {
34 }
35
36 // -------------------------------------------------------------------------
37 std::string cpPlugins::BasicFilters::FloodFillImageFilter::
38 _GenerateData( )
39 {
40   /* TODO
41   cpPlugins::Interface::Image* image =
42     this->GetInput< cpPlugins::Interface::Image >( "Input" );
43   if( image == NULL )
44     return( "FloodFillImageFilter: No input image." );
45
46   itk::DataObject* itk_image = NULL;
47   std::string r = "";
48   cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _GD0 );
49   else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _GD0 );
50   else r = "FloodFillImageFilter: Input image type not supported.";
51   return( r );
52   */
53   return( "" );
54 }
55
56 // -------------------------------------------------------------------------
57 template< class I >
58 std::string cpPlugins::BasicFilters::FloodFillImageFilter::
59 _GD0( itk::DataObject* image )
60 {
61   /* TODO
62   return(
63     this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
64       image
65       )
66     );
67   */
68   return( "" );
69 }
70
71 // -------------------------------------------------------------------------
72 /* TODO
73 template< class I, class R = float >
74 class cpPlugins_BasicFilters_FloodFillImageFilter_Function
75   : public itk::ImageFunction< I, bool, R >
76 {
77 public:
78   typedef cpPlugins_BasicFilters_FloodFillImageFilter_Function Self;
79   typedef itk::ImageFunction< I, bool, R >                     Superclass;
80   typedef itk::SmartPointer< Self >                            Pointer;
81   typedef itk::SmartPointer< const Self >                      ConstPointer;
82
83   typedef typename Superclass::PointType           TPoint;
84   typedef typename Superclass::IndexType           TIndex;
85   typedef typename Superclass::ContinuousIndexType TCIndex;
86
87 public:
88   itkNewMacro( Self );
89   itkTypeMacro(
90     cpPlugins_BasicFilters_FloodFillImageFilter_Function,
91     itkImageFunction
92     );
93
94   itkSetMacro( Window, double );
95   itkSetMacro( Level, double );
96
97 public:
98   virtual bool Evaluate( const TPoint& point ) const
99     {
100       return( true );
101     }
102   virtual bool EvaluateAtIndex( const TIndex& index ) const
103     {
104       if( !( this->IsInsideBuffer( index ) ) )
105         return( false );
106
107       const I* image = this->GetInputImage( );
108       double w2 = this->m_Window / double( 2 );
109       double min = this->m_Level - w2;
110       double max = this->m_Level + w2;
111       unsigned char val = double( 0 );
112       double x = double( image->GetPixel( index ) );
113       double m = double( 100 ) / this->m_Window;
114       double b = ( this->m_Window - ( double( 2 ) * this->m_Level ) );
115       b *= double( 50 ) / this->m_Window;
116       if( x > min && x < max )
117         val = ( unsigned char )( ( m * x ) + b );
118
119       if( this->m_Start )
120       {
121         this->m_StartValue = val;
122         this->m_Start = false;
123         return( true );
124       }
125       else
126         return( std::abs( this->m_StartValue - val ) <= 2 );
127     }
128   virtual bool EvaluateAtContinuousIndex( const TCIndex& index ) const
129     {
130       return( true );
131     }
132
133 protected:
134   cpPlugins_BasicFilters_FloodFillImageFilter_Function( )
135     : Superclass( ),
136       m_Window( double( 0 ) ),
137       m_Level( double( 0 ) ),
138       m_Start( true )
139     {
140     }
141   virtual ~cpPlugins_BasicFilters_FloodFillImageFilter_Function( )
142     {
143     }
144
145 private:
146   // Purposely not implemented
147   cpPlugins_BasicFilters_FloodFillImageFilter_Function( const Self& other );
148   Self& operator=( const Self& other );
149
150 protected:
151   double m_Window;
152   double m_Level;
153   mutable unsigned char m_StartValue;
154   mutable bool m_Start;
155 };
156 */
157
158 // -------------------------------------------------------------------------
159 template< class I, class O >
160 inline std::string cpPlugins::BasicFilters::FloodFillImageFilter::
161 _RealGD( itk::DataObject* image )
162 {
163   /* TODO
164   typedef typename O::PixelType _OP;
165   typedef cpPlugins_BasicFilters_FloodFillImageFilter_Function< I > _F;
166   typedef itk::FloodFilledImageFunctionConditionalConstIterator< I, _F > _It;
167
168   typename I::PointType pseed;
169   pseed = this->m_Parameters->GetPoint< typename I::PointType >(
170     "Seed", I::ImageDimension
171     );
172   double window = this->m_Parameters->GetReal( "Window" );
173   double level = this->m_Parameters->GetReal( "Level" );
174   _OP in_val = _OP( this->m_Parameters->GetUint( "InsideValue" ) );
175   _OP out_val = _OP( this->m_Parameters->GetUint( "OutsideValue" ) );
176
177   const I* in = dynamic_cast< const I* >( image );
178   typename I::IndexType seed;
179   in->TransformPhysicalPointToIndex( pseed, seed );
180
181   typename O::Pointer out = O::New( );
182   out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
183   out->SetRequestedRegion( in->GetRequestedRegion( ) );
184   out->SetBufferedRegion( in->GetBufferedRegion( ) );
185   out->SetOrigin( in->GetOrigin( ) );
186   out->SetDirection( in->GetDirection( ) );
187   out->SetSpacing( in->GetSpacing( ) );
188   out->Allocate( );
189   out->FillBuffer( out_val );
190
191   typename _F::Pointer f = _F::New( );
192   f->SetInputImage( in );
193   f->SetWindow( window );
194   f->SetLevel( level );
195   _It i( in, f );
196   i.AddSeed( seed );
197
198   for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
199     out->SetPixel( i.GetIndex( ), in_val );
200
201   // Connect output
202   cpPlugins::Interface::Image* out_port =
203     this->GetOutput< cpPlugins::Interface::Image >( "Output" );
204   if( out_port != NULL )
205   {
206     out_port->SetITK< O >( out );
207     return( "" );
208   }
209   else
210     return( "FloodFillImageFilter: output not correctly created." );
211   */
212   return( "" );
213 }
214
215 // eof - $RCSfile$