]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Plugins/BasicFilters/FloodFillImageFilter.cxx
Widget integration (step 5/6): Just one step leftgit shortlog !
[cpPlugins.git] / lib / cpPlugins / Plugins / BasicFilters / FloodFillImageFilter.cxx
1 #include "FloodFillImageFilter.h"
2 #include <cpPlugins/Interface/Image.h>
3
4 #include <itkFloodFilledImageFunctionConditionalConstIterator.h>
5 #include <itkImageFunction.h>
6
7 // -------------------------------------------------------------------------
8 cpPlugins::BasicFilters::FloodFillImageFilter::
9 FloodFillImageFilter( )
10   : Superclass( )
11 {
12   this->SetNumberOfInputs( 1 );
13   this->SetNumberOfOutputs( 1 );
14   this->_MakeOutput< cpPlugins::Interface::Image >( 0 );
15
16   using namespace cpPlugins::Interface;
17   this->m_DefaultParameters.Configure( Parameters::Point, "Seed" );
18   this->m_DefaultParameters.Configure( Parameters::Real, "InsideValue" );
19   this->m_DefaultParameters.Configure( Parameters::Real, "OutsideValue" );
20   this->m_DefaultParameters.SetValueAsPoint( "Seed", 3, 0, 0, 0 );
21   this->m_DefaultParameters.SetValueAsReal( "InsideValue", 255 );
22   this->m_DefaultParameters.SetValueAsReal( "OutsideValue", 0 );
23   this->m_Parameters = this->m_DefaultParameters;
24 }
25
26 // -------------------------------------------------------------------------
27 cpPlugins::BasicFilters::FloodFillImageFilter::
28 ~FloodFillImageFilter( )
29 {
30 }
31
32 // -------------------------------------------------------------------------
33 std::string cpPlugins::BasicFilters::FloodFillImageFilter::
34 _GenerateData( )
35 {
36   cpPlugins::Interface::Image* image =
37     this->GetInput< cpPlugins::Interface::Image >( 0 );
38   if( image == NULL )
39     return( "FloodFillImageFilter: No input image." );
40
41   itk::DataObject* itk_image = NULL;
42   std::string r = "";
43   cpPlugins_Image_Demangle_AllScalarTypes( 2, image, itk_image, r, _GD0 );
44   else cpPlugins_Image_Demangle_AllScalarTypes( 3, image, itk_image, r, _GD0 );
45   else r = "FloodFillImageFilter: Input image type not supported.";
46   return( r );
47 }
48
49 // -------------------------------------------------------------------------
50 template< class I >
51 std::string cpPlugins::BasicFilters::FloodFillImageFilter::
52 _GD0( itk::DataObject* image )
53 {
54   return(
55     this->_RealGD< I, itk::Image< unsigned char, I::ImageDimension > >(
56       image
57       )
58     );
59 }
60
61 // -------------------------------------------------------------------------
62 template< class I, class R = float >
63 class cpPlugins_BasicFilters_FloodFillImageFilter_Function
64   : public itk::ImageFunction< I, bool, R >
65 {
66 public:
67   typedef cpPlugins_BasicFilters_FloodFillImageFilter_Function Self;
68   typedef itk::ImageFunction< I, bool, R >                     Superclass;
69   typedef itk::SmartPointer< Self >                            Pointer;
70   typedef itk::SmartPointer< const Self >                      ConstPointer;
71
72   typedef typename Superclass::PointType           TPoint;
73   typedef typename Superclass::IndexType           TIndex;
74   typedef typename Superclass::ContinuousIndexType TCIndex;
75
76 public:
77   itkNewMacro( Self );
78   itkTypeMacro(
79     cpPlugins_BasicFilters_FloodFillImageFilter_Function,
80     itkImageFunction
81     );
82
83 public:
84   virtual bool Evaluate( const TPoint& point ) const
85     {
86       return( true );
87     }
88   virtual bool EvaluateAtIndex( const TIndex& index ) const
89     {
90       return( true );
91     }
92   virtual bool EvaluateAtContinuousIndex( const TCIndex& index ) const
93     {
94       return( true );
95     }
96
97 protected:
98   cpPlugins_BasicFilters_FloodFillImageFilter_Function( )
99     : Superclass( )
100     {
101     }
102   virtual ~cpPlugins_BasicFilters_FloodFillImageFilter_Function( )
103     {
104     }
105
106 private:
107   // Purposely not implemented
108   cpPlugins_BasicFilters_FloodFillImageFilter_Function( const Self& other );
109   Self& operator=( const Self& other );
110 };
111
112 // -------------------------------------------------------------------------
113 template< class I, class O >
114 inline std::string cpPlugins::BasicFilters::FloodFillImageFilter::
115 _RealGD( itk::DataObject* image )
116 {
117   typedef typename O::PixelType _OP;
118   typedef cpPlugins_BasicFilters_FloodFillImageFilter_Function< I > _F;
119   typedef itk::FloodFilledImageFunctionConditionalConstIterator< I, _F > _It;
120
121   typename I::PointType pseed;
122   pseed = this->m_Parameters.GetValueAsPoint< typename I::PointType >( "Seed" );
123   _OP in_val = _OP( this->m_Parameters.GetValueAsReal( "InsideValue" ) );
124   _OP out_val = _OP( this->m_Parameters.GetValueAsReal( "OutsideValue" ) );
125
126   const I* in = dynamic_cast< const I* >( image );
127   typename I::IndexType seed;
128   in->TransformPhysicalPointToIndex( pseed, seed );
129
130   typename O::Pointer out = O::New( );
131   out->SetLargestPossibleRegion( in->GetLargestPossibleRegion( ) );
132   out->SetRequestedRegion( in->GetRequestedRegion( ) );
133   out->SetBufferedRegion( in->GetBufferedRegion( ) );
134   out->SetOrigin( in->GetOrigin( ) );
135   out->SetDirection( in->GetDirection( ) );
136   out->SetSpacing( in->GetSpacing( ) );
137   out->Allocate( );
138   out->FillBuffer( out_val );
139
140   typename _F::Pointer f = _F::New( );
141   _It i( in, f );
142   i.AddSeed( seed );
143
144   for( i.GoToBegin( ); !i.IsAtEnd( ); ++i )
145     out->SetPixel( i.GetIndex( ), in_val );
146
147   // Connect output
148   cpPlugins::Interface::Image* out_port =
149     this->GetOutput< cpPlugins::Interface::Image >( 0 );
150   if( out_port != NULL )
151   {
152     out_port->SetITK< O >( out );
153     return( "" );
154   }
155   else
156     return( "FloodFillImageFilter: output not correctly created." );
157 }
158
159 // eof - $RCSfile$