]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/ImageFunctorFilter.h
...
[cpPlugins.git] / lib / cpExtensions / Algorithms / ImageFunctorFilter.h
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTORFILTER__H__
6 #define __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTORFILTER__H__
7
8 #include <itkImageToImageFilter.h>
9 #include <itkImageScanlineConstIterator.h>
10 #include <itkImageScanlineIterator.h>
11 #include <itkProgressReporter.h>
12
13 namespace cpExtensions
14 {
15   namespace Algorithms
16   {
17     /**
18      */
19     template< class I, class O, class F >
20     class ImageFunctorFilter
21       : public itk::ImageToImageFilter< I, O >
22     {
23     public:
24       typedef ImageFunctorFilter             Self;
25       typedef itk::ImageToImageFilter< I, O > Superclass;
26       typedef itk::SmartPointer< Self >       Pointer;
27       typedef itk::SmartPointer< const Self > ConstPointer;
28
29       typedef I TInputImage;
30       typedef O TOutputImage;
31       typedef F TFunctor;
32
33       typedef typename O::RegionType TRegion;
34
35     public:
36       itkNewMacro( Self );
37       itkTypeMacro( ImageFunctorFilter, itkImageToImageFilter );
38
39       itkGetMacro( Functor, F );
40       itkGetConstMacro( Functor, F );
41
42     protected:
43       ImageFunctorFilter( )
44         : Superclass( )
45         {
46         }
47       virtual ~ImageFunctorFilter( )
48         {
49         }
50
51       virtual void ThreadedGenerateData(
52         const TRegion& region,
53         itk::ThreadIdType threadId
54         )
55         {
56           const typename TRegion::SizeType& regionSize = region.GetSize( );
57           if( regionSize[ 0 ] == 0 )
58             return;
59           const I* in = this->GetInput( );
60           O* out = this->GetOutput( 0 );
61
62           const size_t nLines = region.GetNumberOfPixels( ) / regionSize[ 0 ];
63           itk::ProgressReporter progress( this, threadId, nLines );
64
65           // Define the iterators
66           itk::ImageScanlineConstIterator< I > inIt( in, region );
67           itk::ImageScanlineIterator< O > outIt( out, region );
68
69           inIt.GoToBegin( );
70           outIt.GoToBegin( );
71           while( !inIt.IsAtEnd( ) )
72           {
73             while( !inIt.IsAtEndOfLine( ) )
74             {
75               outIt.Set( this->m_Functor( inIt.Get( ) ) );
76               ++inIt;
77               ++outIt;
78
79             } // elihw
80             inIt.NextLine( );
81             outIt.NextLine( );
82             progress.CompletedPixel( );
83
84           } // elihw
85         }
86
87     private:
88       // Purposely not implemented.
89       ImageFunctorFilter( const Self& );
90       void operator=( const Self& );
91
92     protected:
93       F m_Functor;
94     };
95
96   } // ecapseman
97
98 } // ecapseman
99
100 #endif // __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTORFILTER__H__
101
102 // eof - $RCSfile$