]> Creatis software - cpPlugins.git/blob - lib/cpExtensions/Algorithms/ImageFunctionFilter.h
c025bcb30837580abecb12ddb8e7f18273ffa272
[cpPlugins.git] / lib / cpExtensions / Algorithms / ImageFunctionFilter.h
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTIONFILTER__H__
6 #define __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTIONFILTER__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 ImageFunctionFilter
21         : public itk::ImageToImageFilter< I, O >
22       {
23       public:
24         typedef ImageFunctionFilter             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 TFunction;
32
33         typedef typename O::RegionType TRegion;
34
35       public:
36         itkNewMacro( Self );
37         itkTypeMacro( ImageFunctionFilter, itkImageToImageFilter );
38
39         itkGetObjectMacro( Function, F );
40         itkSetObjectMacro( Function, F );
41
42       protected:
43         ImageFunctionFilter( )
44           : Superclass( )
45           {
46           }
47         virtual ~ImageFunctionFilter( )
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_Function->EvaluateAtIndex( inIt.GetIndex( ) ) );
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         ImageFunctionFilter( const Self& );
90         void operator=( const Self& );
91
92       protected:
93         typename F::Pointer m_Function;
94       };
95
96     } // ecapseman
97
98 } // ecapseman
99
100 // TODO: #include <cpExtensions/Algorithms/ImageFunctionFilter.hxx>
101
102 #endif // __CPEXTENSIONS__ALGORITHMS__IMAGEFUNCTIONFILTER__H__
103
104 // eof - $RCSfile$