]> Creatis software - cpPlugins.git/blob - lib/cpPlugins/Extensions/Algorithms/LightCompensationFilter.hxx
71dc0f60c0767e828eafa94f03bcd0bc1a522e98
[cpPlugins.git] / lib / cpPlugins / Extensions / Algorithms / LightCompensationFilter.hxx
1 // -------------------------------------------------------------------------
2 // @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
3 // -------------------------------------------------------------------------
4
5 #ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
6 #define __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
7
8 #include <itkImageRegionIterator.h>
9 #include <itkImageRegionConstIterator.h>
10 #include <itkNumericTraits.h>
11
12 // -------------------------------------------------------------------------
13 template< class I >
14 cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
15 LightCompensationFilter( )
16   : Superclass( )
17 {
18   this->SetNumberOfRequiredInputs( 1 );
19   this->InPlaceOff( );
20 }
21
22 // -------------------------------------------------------------------------
23 template< class I >
24 cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
25 ~LightCompensationFilter( )
26 {
27 }
28
29 // -------------------------------------------------------------------------
30 template< class I >
31 void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
32 BeforeThreadedGenerateData( )
33 {
34   I* input = const_cast< I* >( this->GetInput( ) );
35
36   typename TMeanCalculator::Pointer mc = TMeanCalculator::New( );
37   mc->Execute( input, input->GetRequestedRegion( ) );
38   this->m_Mean = mc->GetMean( );
39
40   double gm = this->m_Mean[ 0 ] + this->m_Mean[ 1 ] + this->m_Mean[ 2 ];
41   gm /= double( 3 );
42   this->m_Coefficient = this->m_Mean;
43   this->m_Coefficient.Fill( gm );
44   this->m_Coefficient[ 0 ] /= this->m_Mean[ 0 ];
45   this->m_Coefficient[ 1 ] /= this->m_Mean[ 1 ];
46   this->m_Coefficient[ 2 ] /= this->m_Mean[ 2 ];
47 }
48
49 // -------------------------------------------------------------------------
50 template< class I >
51 void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
52 ThreadedGenerateData( const TRegion& region, itk::ThreadIdType id )
53 {
54   typedef itk::NumericTraits< typename I::PixelType > _TPixelTraits;
55   typedef typename _TPixelTraits::ValueType           _TPixelChannel;
56   typedef itk::NumericTraits< _TPixelChannel >        _TChannelTraits;
57   static const double max_value = double( _TChannelTraits::max( ) );
58
59   itk::ImageRegionConstIterator< I > iIt( this->GetInput( ), region );
60   itk::ImageRegionIterator< I > oIt( this->GetOutput( ), region );
61
62   iIt.GoToBegin( );
63   oIt.GoToBegin( );
64   for( ; !iIt.IsAtEnd( ); ++iIt, ++oIt )
65   {
66     double r = double( iIt.Get( )[ 0 ] ) * this->m_Coefficient[ 0 ];
67     double g = double( iIt.Get( )[ 1 ] ) * this->m_Coefficient[ 1 ];
68     double b = double( iIt.Get( )[ 2 ] ) * this->m_Coefficient[ 2 ];
69     if( r > max_value ) r = max_value;
70     if( g > max_value ) g = max_value;
71     if( b > max_value ) b = max_value;
72     oIt.Get( ).Set(
73       _TPixelChannel( r ), _TPixelChannel( g ), _TPixelChannel( b )
74       );
75
76   } // rof
77 }
78
79 // -------------------------------------------------------------------------
80 template< class I >
81 void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
82 AfterThreadedGenerateData( )
83 {
84 }
85
86 #endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
87
88 // eof - $RCSfile$