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