TuttleOFX  1
NormalizeAlgorithm.hpp
Go to the documentation of this file.
00001 #ifndef _TUTTLE_PLUGIN_NORMALIZE_ALGORITHM_HPP_
00002 #define _TUTTLE_PLUGIN_NORMALIZE_ALGORITHM_HPP_
00003 
00004 #include <terry/channel.hpp>
00005 #include <terry/channel_view.hpp>
00006 #include <terry/algorithm/static_channel_recursion.hpp>
00007 #include <terry/numeric/operations.hpp>
00008 #include <terry/numeric/assign.hpp>
00009 #include <terry/numeric/minmax.hpp>
00010 #include <terry/algorithm/transform_pixels_progress.hpp>
00011 
00012 namespace tuttle {
00013 namespace plugin {
00014 namespace normalize {
00015 
00016 template< class View, typename LocalChannel >
00017 void analyseChannel( View& src, typename View::value_type& min, typename View::value_type& max, IProgress& p )
00018 {
00019         using namespace terry;
00020         using namespace terry::numeric;
00021         using namespace terry::algorithm;
00022         
00023         typedef channel_view_type<LocalChannel,View> LocalView;
00024         typename LocalView::type localView( LocalView::make(src) );
00025         pixel_minmax_by_channel_t<typename LocalView::type::value_type> minmax( localView(0,0) );
00026         transform_pixels_progress(
00027                 localView,
00028                 minmax,
00029                 p );
00030         static_fill( min, minmax.min[0] );
00031         static_fill( max, minmax.max[0] );
00032 }
00033 
00034 
00035 /**
00036  * @brief compute min and max from input view analyse.
00037  *
00038  * @param[in] src: input image to analyse
00039  * @param[in] analyseMode: choose the analyse method
00040  * @param[out] min: output min values
00041  * @param[out] max: output max values
00042  */
00043 template<class View>
00044 void analyseInputMinMax( const View& src, const EParamAnalyseMode analyseMode, typename View::value_type& min, typename View::value_type& max, IProgress& p )
00045 {
00046         using namespace terry;
00047         using namespace terry::numeric;
00048         using namespace terry::algorithm;
00049         
00050         typedef typename View::value_type Pixel;
00051 
00052         switch( analyseMode )
00053         {
00054                 case eParamAnalyseModePerChannel:
00055                 {
00056                         pixel_minmax_by_channel_t<Pixel> minmax( src(0,0) );
00057                         // compute the maximum value
00058                         transform_pixels_progress(
00059                                 src,
00060                                 minmax,
00061                                 p );
00062                         min = minmax.min;
00063                         max = minmax.max;
00064                         break;
00065                 }
00066                 case eParamAnalyseModeLuminosity:
00067                 {
00068                         typedef pixel<typename channel_type<View>::type, gray_layout_t> PixelGray;
00069                         typedef typename color_converted_view_type<View, PixelGray>::type LocalView;
00070                         LocalView localView(src);
00071                         pixel_minmax_by_channel_t<typename LocalView::value_type> minmax( localView(0,0) );
00072                         transform_pixels_progress(
00073                                 localView,
00074                                 minmax,
00075                                 p );
00076                         static_fill( min, minmax.min[0] );
00077                         static_fill( max, minmax.max[0] );
00078                         break;
00079                 }
00080                 case eParamAnalyseModeR:
00081                 {
00082                         typedef channel_view_type<red_t, View> LocalView;
00083                         typename LocalView::type localView( LocalView::make(src) );
00084                         pixel_minmax_by_channel_t< typename LocalView::type::value_type> minmax( localView(0,0) );
00085                         transform_pixels_progress(
00086                                         localView,
00087                                         minmax,
00088                                         p );
00089                         static_fill( min, minmax.min[0] );
00090                         static_fill( max, minmax.max[0] );
00091                         break;
00092                 }
00093                 case eParamAnalyseModeG:
00094                 {
00095                         typedef channel_view_type<green_t,View> LocalView;
00096                         typename LocalView::type localView( LocalView::make(src) );
00097                         pixel_minmax_by_channel_t< typename LocalView::type::value_type> minmax( localView(0,0) );
00098                         transform_pixels_progress(
00099                                         localView,
00100                                         minmax,
00101                                         p );
00102                         static_fill( min, minmax.min[0] );
00103                         static_fill( max, minmax.max[0] );
00104                         break;
00105                 }
00106                 case eParamAnalyseModeB:
00107                 {
00108                         typedef channel_view_type<blue_t,View> LocalView;
00109                         typename LocalView::type localView( LocalView::make(src) );
00110                         pixel_minmax_by_channel_t< typename LocalView::type::value_type> minmax( localView(0,0) );
00111                         transform_pixels_progress(
00112                                         localView,
00113                                         minmax,
00114                                         p );
00115                         static_fill( min, minmax.min[0] );
00116                         static_fill( max, minmax.max[0] );
00117                         break;
00118                 }
00119                 case eParamAnalyseModeA:
00120                 {
00121                         typedef channel_view_type<alpha_t,View> LocalView;
00122                         typename LocalView::type localView( LocalView::make(src) );
00123                         pixel_minmax_by_channel_t< typename LocalView::type::value_type> minmax( localView(0,0) );
00124                         transform_pixels_progress(
00125                                         localView,
00126                                         minmax,
00127                                         p );
00128                         static_fill( min, minmax.min[0] );
00129                         static_fill( max, minmax.max[0] );
00130                         break;
00131                 }
00132         }
00133 }
00134 
00135 template<>
00136 void analyseInputMinMax( const boost::gil::rgb32f_view_t& src, const EParamAnalyseMode analyseMode, boost::gil::rgb32f_view_t::value_type& min, boost::gil::rgb32f_view_t::value_type& max, IProgress& progress )
00137 {
00138         using namespace terry;
00139         using namespace terry::numeric;
00140         using namespace terry::algorithm;
00141         
00142         typedef rgb32f_view_t::value_type Pixel;
00143 
00144         switch( analyseMode )
00145         {
00146                 case eParamAnalyseModePerChannel:
00147                 {
00148                         pixel_minmax_by_channel_t<Pixel> minmax( src(0,0) );
00149                         // compute the maximum value
00150                         transform_pixels_progress(
00151                                 src,
00152                                 minmax,
00153                                 progress );
00154                         min = minmax.min;
00155                         max = minmax.max;
00156                         break;
00157                 }
00158                 case eParamAnalyseModeLuminosity:
00159                 {
00160                         typedef pixel<channel_type<rgb32f_view_t>::type, gray_layout_t> PixelGray;
00161                         typedef color_converted_view_type<rgb32f_view_t, PixelGray>::type LocalView;
00162                         LocalView localView(src);
00163                         pixel_minmax_by_channel_t<LocalView::value_type> minmax( localView(0,0) );
00164                         transform_pixels_progress(
00165                                 localView,
00166                                 minmax,
00167                                 progress );
00168                         static_fill( min, minmax.min[0] );
00169                         static_fill( max, minmax.max[0] );
00170                         break;
00171                 }
00172                 case eParamAnalyseModeR:
00173                 {
00174                         typedef channel_view_type<red_t,rgb32f_view_t> LocalView;
00175                         LocalView::type localView( LocalView::make(src) );
00176                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00177                         transform_pixels_progress(
00178                                         localView,
00179                                         minmax,
00180                                         progress );
00181                         static_fill( min, minmax.min[0] );
00182                         static_fill( max, minmax.max[0] );
00183                         break;
00184                 }
00185                 case eParamAnalyseModeG:
00186                 {
00187                         typedef channel_view_type<green_t,rgb32f_view_t> LocalView;
00188                         LocalView::type localView( LocalView::make(src) );
00189                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00190                         transform_pixels_progress(
00191                                         localView,
00192                                         minmax,
00193                                         progress );
00194                         static_fill( min, minmax.min[0] );
00195                         static_fill( max, minmax.max[0] );
00196                         break;
00197                 }
00198                 case eParamAnalyseModeB:
00199                 {
00200                         typedef channel_view_type<blue_t,rgb32f_view_t> LocalView;
00201                         LocalView::type localView( LocalView::make(src) );
00202                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00203                         transform_pixels_progress(
00204                                         localView,
00205                                         minmax,
00206                                         progress );
00207                         static_fill( min, minmax.min[0] );
00208                         static_fill( max, minmax.max[0] );
00209                         break;
00210                 }
00211                 case eParamAnalyseModeA:
00212                 {
00213                         static_fill( min, 0 );
00214                         static_fill( max, 1 );
00215                         break;
00216                 }
00217         }
00218 }
00219 
00220 template<>
00221 void analyseInputMinMax( const boost::gil::rgb16_view_t& src, const EParamAnalyseMode analyseMode, boost::gil::rgb16_view_t::value_type& min, boost::gil::rgb16_view_t::value_type& max, IProgress& progress )
00222 {
00223         using namespace terry;
00224         using namespace terry::numeric;
00225         using namespace terry::algorithm;
00226         
00227         typedef rgb16_view_t::value_type Pixel;
00228 
00229         switch( analyseMode )
00230         {
00231                 case eParamAnalyseModePerChannel:
00232                 {
00233                         pixel_minmax_by_channel_t<Pixel> minmax( src(0,0) );
00234                         // compute the maximum value
00235                         transform_pixels_progress(
00236                                 src,
00237                                 minmax,
00238                                 progress );
00239                         min = minmax.min;
00240                         max = minmax.max;
00241                         break;
00242                 }
00243                 case eParamAnalyseModeLuminosity:
00244                 {
00245                         typedef pixel<channel_type<rgb16_view_t>::type, gray_layout_t> PixelGray;
00246                         typedef color_converted_view_type<rgb16_view_t, PixelGray>::type LocalView;
00247                         LocalView localView(src);
00248                         pixel_minmax_by_channel_t<LocalView::value_type> minmax( localView(0,0) );
00249                         transform_pixels_progress(
00250                                 localView,
00251                                 minmax,
00252                                 progress );
00253                         static_fill( min, minmax.min[0] );
00254                         static_fill( max, minmax.max[0] );
00255                         break;
00256                 }
00257                 case eParamAnalyseModeR:
00258                 {
00259                         typedef channel_view_type<red_t,rgb16_view_t> LocalView;
00260                         LocalView::type localView( LocalView::make(src) );
00261                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00262                         transform_pixels_progress(
00263                                         localView,
00264                                         minmax,
00265                                         progress );
00266                         static_fill( min, minmax.min[0] );
00267                         static_fill( max, minmax.max[0] );
00268                         break;
00269                 }
00270                 case eParamAnalyseModeG:
00271                 {
00272                         typedef channel_view_type<green_t,rgb16_view_t> LocalView;
00273                         LocalView::type localView( LocalView::make(src) );
00274                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00275                         transform_pixels_progress(
00276                                         localView,
00277                                         minmax,
00278                                         progress );
00279                         static_fill( min, minmax.min[0] );
00280                         static_fill( max, minmax.max[0] );
00281                         break;
00282                 }
00283                 case eParamAnalyseModeB:
00284                 {
00285                         typedef channel_view_type<blue_t,rgb16_view_t> LocalView;
00286                         LocalView::type localView( LocalView::make(src) );
00287                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00288                         transform_pixels_progress(
00289                                         localView,
00290                                         minmax,
00291                                         progress );
00292                         static_fill( min, minmax.min[0] );
00293                         static_fill( max, minmax.max[0] );
00294                         break;
00295                 }
00296                 case eParamAnalyseModeA:
00297                 {
00298                         static_fill( min, 0 );
00299                         static_fill( max, 1 );
00300                         break;
00301                 }
00302         }
00303 }
00304 
00305 template<>
00306 void analyseInputMinMax( const boost::gil::rgb8_view_t& src, const EParamAnalyseMode analyseMode, boost::gil::rgb8_view_t::value_type& min, boost::gil::rgb8_view_t::value_type& max, IProgress& p )
00307 {
00308         using namespace terry;
00309         using namespace terry::numeric;
00310         using namespace terry::algorithm;
00311         
00312         typedef rgb8_view_t::value_type Pixel;
00313 
00314         switch( analyseMode )
00315         {
00316                 case eParamAnalyseModePerChannel:
00317                 {
00318                         pixel_minmax_by_channel_t<Pixel> minmax( src(0,0) );
00319                         // compute the maximum value
00320                         transform_pixels_progress(
00321                                 src,
00322                                 minmax,
00323                                 p );
00324                         min = minmax.min;
00325                         max = minmax.max;
00326                         break;
00327                 }
00328                 case eParamAnalyseModeLuminosity:
00329                 {
00330                         typedef pixel<channel_type<rgb8_view_t>::type, gray_layout_t> PixelGray;
00331                         typedef color_converted_view_type<rgb8_view_t, PixelGray>::type LocalView;
00332                         LocalView localView(src);
00333                         pixel_minmax_by_channel_t<LocalView::value_type> minmax( localView(0,0) );
00334                         transform_pixels_progress(
00335                                 localView,
00336                                 minmax,
00337                                 p );
00338                         static_fill( min, minmax.min[0] );
00339                         static_fill( max, minmax.max[0] );
00340                         break;
00341                 }
00342                 case eParamAnalyseModeR:
00343                 {
00344                         typedef channel_view_type<red_t,rgb8_view_t> LocalView;
00345                         LocalView::type localView( LocalView::make(src) );
00346                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00347                         transform_pixels_progress(
00348                                         localView,
00349                                         minmax,
00350                                         p );
00351                         static_fill( min, minmax.min[0] );
00352                         static_fill( max, minmax.max[0] );
00353                         break;
00354                 }
00355                 case eParamAnalyseModeG:
00356                 {
00357                         typedef channel_view_type<green_t,rgb8_view_t> LocalView;
00358                         LocalView::type localView( LocalView::make(src) );
00359                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00360                         transform_pixels_progress(
00361                                         localView,
00362                                         minmax,
00363                                         p );
00364                         static_fill( min, minmax.min[0] );
00365                         static_fill( max, minmax.max[0] );
00366                         break;
00367                 }
00368                 case eParamAnalyseModeB:
00369                 {
00370                         typedef channel_view_type<blue_t,rgb8_view_t> LocalView;
00371                         LocalView::type localView( LocalView::make(src) );
00372                         pixel_minmax_by_channel_t<LocalView::type::value_type> minmax( localView(0,0) );
00373                         transform_pixels_progress(
00374                                         localView,
00375                                         minmax,
00376                                         p );
00377                         static_fill( min, minmax.min[0] );
00378                         static_fill( max, minmax.max[0] );
00379                         break;
00380                 }
00381                 case eParamAnalyseModeA:
00382                 {
00383                         static_fill( min, 0 );
00384                         static_fill( max, 1 );
00385                         break;
00386                 }
00387         }
00388 }
00389 
00390 template<>
00391 void analyseInputMinMax( const boost::gil::gray32f_view_t& src, const EParamAnalyseMode analyseMode, boost::gil::gray32f_view_t::value_type& min, boost::gil::gray32f_view_t::value_type& max, IProgress& p )
00392 {
00393         using namespace terry;
00394         using namespace terry::numeric;
00395         using namespace terry::algorithm;
00396         
00397         typedef gray32f_view_t::value_type Pixel;
00398 
00399         switch( analyseMode )
00400         {
00401                 case eParamAnalyseModePerChannel:
00402                 {
00403                         pixel_minmax_by_channel_t<Pixel> minmax( src(0,0) );
00404                         // compute the maximum value
00405                         transform_pixels_progress(
00406                                 src,
00407                                 minmax,
00408                                 p );
00409                         min = minmax.min;
00410                         max = minmax.max;
00411                         break;
00412                 }
00413                 case eParamAnalyseModeLuminosity:
00414                 {
00415                         typedef pixel<channel_type<gray32f_view_t>::type, gray_layout_t> PixelGray;
00416                         typedef color_converted_view_type<gray32f_view_t, PixelGray>::type LocalView;
00417                         LocalView localView(src);
00418                         pixel_minmax_by_channel_t<LocalView::value_type> minmax( localView(0,0) );
00419                         transform_pixels_progress(
00420                                 localView,
00421                                 minmax,
00422                                 p );
00423                         static_fill( min, minmax.min[0] );
00424                         static_fill( max, minmax.max[0] );
00425                         break;
00426                 }
00427                 case eParamAnalyseModeR:
00428                 case eParamAnalyseModeG:
00429                 case eParamAnalyseModeB:
00430                 case eParamAnalyseModeA:
00431                 {
00432                         static_fill( min, 0 );
00433                         static_fill( max, 1 );
00434                         break;
00435                 }
00436         }
00437 }
00438 
00439 template<>
00440 void analyseInputMinMax( const boost::gil::gray16_view_t& src, const EParamAnalyseMode analyseMode, boost::gil::gray16_view_t::value_type& min, boost::gil::gray16_view_t::value_type& max, IProgress& p )
00441 {
00442         using namespace terry;
00443         using namespace terry::numeric;
00444         using namespace terry::algorithm;
00445         
00446         typedef gray16_view_t::value_type Pixel;
00447 
00448         switch( analyseMode )
00449         {
00450                 case eParamAnalyseModePerChannel:
00451                 {
00452                         pixel_minmax_by_channel_t<Pixel> minmax( src(0,0) );
00453                         // compute the maximum value
00454                         transform_pixels_progress(
00455                                 src,
00456                                 minmax,
00457                                 p );
00458                         min = minmax.min;
00459                         max = minmax.max;
00460                         break;
00461                 }
00462                 case eParamAnalyseModeLuminosity:
00463                 {
00464                         typedef pixel<channel_type<gray16_view_t>::type, gray_layout_t> PixelGray;
00465                         typedef color_converted_view_type<gray16_view_t, PixelGray>::type LocalView;
00466                         LocalView localView(src);
00467                         pixel_minmax_by_channel_t<LocalView::value_type> minmax( localView(0,0) );
00468                         transform_pixels_progress(
00469                                 localView,
00470                                 minmax,
00471                                 p );
00472                         static_fill( min, minmax.min[0] );
00473                         static_fill( max, minmax.max[0] );
00474                         break;
00475                 }
00476                 case eParamAnalyseModeR:
00477                 case eParamAnalyseModeG:
00478                 case eParamAnalyseModeB:
00479                 case eParamAnalyseModeA:
00480                 {
00481                         static_fill( min, 0 );
00482                         static_fill( max, 1 );
00483                         break;
00484                 }
00485         }
00486 }
00487 
00488 template<>
00489 void analyseInputMinMax( const boost::gil::gray8_view_t& src, const EParamAnalyseMode analyseMode, boost::gil::gray8_view_t::value_type& min, boost::gil::gray8_view_t::value_type& max, IProgress& p )
00490 {
00491         using namespace terry;
00492         using namespace terry::numeric;
00493         using namespace terry::algorithm;
00494         
00495         typedef gray8_view_t::value_type Pixel;
00496 
00497         switch( analyseMode )
00498         {
00499                 case eParamAnalyseModePerChannel:
00500                 {
00501                         pixel_minmax_by_channel_t<Pixel> minmax( src(0,0) );
00502                         // compute the maximum value
00503                         transform_pixels_progress(
00504                                 src,
00505                                 minmax,
00506                                 p );
00507                         min = minmax.min;
00508                         max = minmax.max;
00509                         break;
00510                 }
00511                 case eParamAnalyseModeLuminosity:
00512                 {
00513                         typedef pixel<channel_type<gray8_view_t>::type, gray_layout_t> PixelGray;
00514                         typedef color_converted_view_type<gray8_view_t, PixelGray>::type LocalView;
00515                         LocalView localView(src);
00516                         pixel_minmax_by_channel_t<LocalView::value_type> minmax( localView(0,0) );
00517                         transform_pixels_progress(
00518                                 localView,
00519                                 minmax,
00520                                 p );
00521                         static_fill( min, minmax.min[0] );
00522                         static_fill( max, minmax.max[0] );
00523                         break;
00524                 }
00525                 case eParamAnalyseModeR:
00526                 case eParamAnalyseModeG:
00527                 case eParamAnalyseModeB:
00528                 case eParamAnalyseModeA:
00529                 {
00530                         static_fill( min, 0 );
00531                         static_fill( max, 1 );
00532                         break;
00533                 }
00534         }
00535 }
00536 
00537 }
00538 }
00539 }
00540 
00541 #endif