TuttleOFX
1
|
00001 #ifndef _TERRY_NUMERIC_SCALE_HPP_ 00002 #define _TERRY_NUMERIC_SCALE_HPP_ 00003 00004 #include "assign.hpp" 00005 00006 #include <terry/channel.hpp> 00007 00008 #include <boost/gil/gil_config.hpp> 00009 #include <boost/gil/pixel.hpp> 00010 #include <boost/gil/color_base_algorithm.hpp> 00011 00012 00013 namespace terry { 00014 namespace numeric { 00015 00016 /** 00017 * @brief Rescale pixel values. Usefull to normalize an image. 00018 */ 00019 template<typename Pixel, typename CPixel = Pixel> 00020 struct pixel_scale_t 00021 { 00022 typedef typename boost::gil::channel_type<CPixel>::type Channel; 00023 typedef typename terry::channel_base_type<Channel>::type ChannelBaseType; 00024 00025 const CPixel _ratio; 00026 CPixel _shift; 00027 00028 pixel_scale_t( const CPixel& ratio, const CPixel& sCenter, const CPixel& dCenter ) 00029 : _ratio(ratio) 00030 { 00031 BOOST_STATIC_ASSERT( boost::is_floating_point<ChannelBaseType>::value ); 00032 // res = ( ( src - sCenter ) * _ratio ) + dCenter; 00033 // res = (src * _ratio) + (dCenter - (sCenter*ratio)) 00034 // shift = (dCenter - (sCenter*ratio)) 00035 pixel_assigns_t<CPixel,CPixel>()( 00036 pixel_minus_t<CPixel,CPixel,CPixel>()( 00037 dCenter, 00038 pixel_multiplies_t<CPixel,CPixel,CPixel>()( 00039 sCenter, 00040 ratio 00041 ) 00042 ), 00043 _shift ); 00044 } 00045 00046 pixel_scale_t( const CPixel& ratio, const CPixel& shift ) 00047 : _ratio(ratio) 00048 , _shift(shift) 00049 { 00050 BOOST_STATIC_ASSERT( boost::is_floating_point<ChannelBaseType>::value ); 00051 } 00052 00053 GIL_FORCEINLINE 00054 Pixel operator()( const Pixel& src ) const 00055 { 00056 Pixel res; 00057 // res = (src * _ratio) + _shift 00058 pixel_assigns_t<CPixel,Pixel>()( 00059 pixel_plus_t<CPixel,CPixel,CPixel>()( 00060 pixel_multiplies_t<Pixel,CPixel,CPixel>()( 00061 src, 00062 _ratio 00063 ), 00064 _shift ), 00065 res ); 00066 00067 return res; 00068 } 00069 }; 00070 00071 } 00072 } 00073 00074 #endif