+// -------------------------------------------------------------------------
+// @author Leonardo Florez-Valencia (florez-l@javeriana.edu.co)
+// -------------------------------------------------------------------------
+
+#ifndef __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
+#define __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
+
+#include <itkImageRegionIterator.h>
+#include <itkImageRegionConstIterator.h>
+#include <itkNumericTraits.h>
+
+// -------------------------------------------------------------------------
+template< class I >
+cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
+LightCompensationFilter( )
+ : Superclass( )
+{
+ this->SetNumberOfRequiredInputs( 1 );
+ this->InPlaceOff( );
+}
+
+// -------------------------------------------------------------------------
+template< class I >
+cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
+~LightCompensationFilter( )
+{
+}
+
+// -------------------------------------------------------------------------
+template< class I >
+void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
+BeforeThreadedGenerateData( )
+{
+ I* input = const_cast< I* >( this->GetInput( ) );
+
+ typename TMeanCalculator::Pointer mc = TMeanCalculator::New( );
+ mc->Execute( input, input->GetRequestedRegion( ) );
+ this->m_Mean = mc->GetMean( );
+
+ double gm = this->m_Mean[ 0 ] + this->m_Mean[ 1 ] + this->m_Mean[ 2 ];
+ gm /= double( 3 );
+ this->m_Coefficient = this->m_Mean;
+ this->m_Coefficient.Fill( gm );
+ this->m_Coefficient[ 0 ] /= this->m_Mean[ 0 ];
+ this->m_Coefficient[ 1 ] /= this->m_Mean[ 1 ];
+ this->m_Coefficient[ 2 ] /= this->m_Mean[ 2 ];
+}
+
+// -------------------------------------------------------------------------
+template< class I >
+void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
+ThreadedGenerateData( const TRegion& region, itk::ThreadIdType id )
+{
+ typedef itk::NumericTraits< typename I::PixelType > _TPixelTraits;
+ typedef typename _TPixelTraits::ValueType _TPixelChannel;
+ typedef itk::NumericTraits< _TPixelChannel > _TChannelTraits;
+ static const double max_value = double( _TChannelTraits::max( ) );
+
+ itk::ImageRegionConstIterator< I > iIt( this->GetInput( ), region );
+ itk::ImageRegionIterator< I > oIt( this->GetOutput( ), region );
+
+ iIt.GoToBegin( );
+ oIt.GoToBegin( );
+ for( ; !iIt.IsAtEnd( ); ++iIt, ++oIt )
+ {
+ double r = double( iIt.Get( )[ 0 ] ) * this->m_Coefficient[ 0 ];
+ double g = double( iIt.Get( )[ 1 ] ) * this->m_Coefficient[ 1 ];
+ double b = double( iIt.Get( )[ 2 ] ) * this->m_Coefficient[ 2 ];
+ if( r > max_value ) r = max_value;
+ if( g > max_value ) g = max_value;
+ if( b > max_value ) b = max_value;
+ oIt.Get( ).Set(
+ _TPixelChannel( r ), _TPixelChannel( g ), _TPixelChannel( b )
+ );
+
+ } // rof
+}
+
+// -------------------------------------------------------------------------
+template< class I >
+void cpPlugins::Extensions::Algorithms::LightCompensationFilter< I >::
+AfterThreadedGenerateData( )
+{
+}
+
+#endif // __CPPLUGINS__EXTENSIONS__ALGORITHMS__LIGHTCOMPENSATIONFILTER__HXX__
+
+// eof - $RCSfile$