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