00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef MLN_FUN_V2V_RGB_TO_HSI_HH
00028 # define MLN_FUN_V2V_RGB_TO_HSI_HH
00029
00030 #include <cmath>
00031
00032 #include <mln/value/rgb8.hh>
00033 #include <mln/math/round.hh>
00034
00035 #include <mln/value/hsi.hh>
00036
00037
00038 namespace mln
00039 {
00040
00041
00042 namespace fun
00043 {
00044
00045 namespace v2v
00046 {
00047
00048 template <typename T_hsi>
00049 struct f_rgb_to_hsi_ : public Function_v2v< f_rgb_to_hsi_<T_hsi> >
00050 {
00051 typedef T_hsi result;
00052
00053 f_rgb_to_hsi_();
00054
00055 template <typename T_rgb>
00056 T_hsi operator()(const T_rgb& rgb) const;
00057
00058 };
00059
00060 typedef f_rgb_to_hsi_<value::hsi_f> f_rgb_to_hsi_f_t;
00061
00062 extern f_rgb_to_hsi_f_t f_rgb_to_hsi_f;
00063
00064
00065 # ifndef MLN_INCLUDE_ONLY
00066
00067 # ifndef MLN_WO_GLOBAL_VARS
00068
00071 f_rgb_to_hsi_f_t f_rgb_to_hsi_f;
00073
00074 # endif // ! MLN_WO_GLOBAL_VARS
00075
00076
00077 template <typename T_hsi>
00078 f_rgb_to_hsi_<T_hsi>::f_rgb_to_hsi_()
00079 {
00080 }
00081
00082 template <typename T_hsi>
00083 template <typename T_rgb>
00084 inline
00085 T_hsi
00086 f_rgb_to_hsi_<T_hsi>::operator()(const T_rgb& rgb) const
00087 {
00088
00089 static const double sqrt3_3 = std::sqrt(3) / 3;
00090 static const double inv_sqrt6 = 1 / std::sqrt(6);
00091 static const double inv_sqrt2 = 1 / std::sqrt(2);
00092
00093 T_hsi hsi;
00094
00095 double alpha = inv_sqrt2 * rgb.green() - inv_sqrt2 * rgb.blue();
00096 double beta =
00097 2 * inv_sqrt6 * rgb.red() -
00098 inv_sqrt6 * rgb.green() -
00099 inv_sqrt6 * rgb.blue();
00100
00101
00102 hsi.hue() = atan2(beta, alpha) / 3.1415 * 180.0;
00103 if (hsi.hue() < 0)
00104 hsi.hue() = hsi.hue() + 360.0;
00105 mln_invariant(hsi.hue() >= 0);
00106 hsi.sat() = std::sqrt(alpha * alpha + beta * beta);
00107 hsi.inty() =
00108 sqrt3_3 * rgb.red() +
00109 sqrt3_3 * rgb.green() +
00110 sqrt3_3 * rgb.blue();
00111
00112 return hsi;
00113 }
00114
00115
00116 # endif // !MLN_INCLUDE_ONLY
00117
00118 }
00119
00120 }
00121
00122 }
00123
00124 #endif // ! MLN_FUN_V2V_RGB_TO_HSI_HH