• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

hsl.hh

00001 // Copyright (C) 2008, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_VALUE_HSL_HH
00027 # define MLN_VALUE_HSL_HH
00028 
00029 #include <mln/value/ops.hh>
00030 
00031 #include <mln/value/concept/vectorial.hh>
00032 #include <mln/value/int_u.hh>
00033 #include <mln/algebra/vec.hh>
00034 
00035 // Used in from_to
00036 #include <mln/fun/v2v/rgb_to_hsl.hh>
00037 
00038 
00039 namespace mln
00040 {
00041 
00042   // Forward declarations.
00043   namespace value
00044   {
00045 
00046     template <typename H, typename S, typename L>
00047     class hsl_;
00048 
00049   }
00050 
00051 
00052 
00053   namespace convert
00054   {
00055 
00056     namespace over_load
00057     {
00058 
00059       // rgb to hsl_
00060       void
00061       from_to_(const value::rgb<16>& from, value::hsl_<float,float,float>& to);
00062 
00063       // rgb to hsl_
00064       void
00065       from_to_(const value::rgb<8>& from, value::hsl_<float,float,float>& to);
00066 
00067     } // end of namespace mln::convert::over_load
00068 
00069   } // end of namespace mln::convert
00070 
00071 
00072 
00073   namespace trait
00074   {
00075 
00076     template <typename H, typename S, typename L>
00077     struct set_precise_binary_< op::plus, mln::value::hsl_<H,S,L>, mln::value::hsl_<H,S,L> >
00078     {
00079       typedef mln::value::hsl_<H,S,L> ret;
00080     };
00081 
00082     template <typename H, typename S, typename L>
00083     struct set_precise_binary_< op::minus, mln::value::hsl_<H,S,L>, mln::value::hsl_<H,S,L> >
00084     {
00085       typedef mln::value::hsl_<H,S,L> ret;
00086     };
00087 
00088     template <typename H, typename S, typename L, typename S2>
00089     struct set_precise_binary_< op::times, mln::value::hsl_<H,S,L>, mln::value::scalar_<S2> >
00090     {
00091       typedef mln::value::hsl_<H,S,L> ret;
00092     };
00093 
00094     template <typename H, typename S, typename L, typename S2>
00095     struct set_precise_binary_< op::div, mln::value::hsl_<H,S,L>, mln::value::scalar_<S2> >
00096     {
00097       typedef mln::value::hsl_<H,S,L> ret;
00098     };
00099 
00100 
00101     // FIXME : Is there any way more generic? a way to factor
00102     //  set_precise_binary_< op::div, mln::value::hsl_<H,S,L>, mln::value::scalar_<S> >
00103     //  and
00104     //  set_precise_binary_< op::div, mln::value::hsl_<H,S,L>, mln::value::int_u<m> >
00105     //  as for op::times.
00106 
00107     template <typename H, typename S, typename L, unsigned m>
00108     struct set_precise_binary_< op::times, mln::value::hsl_<H,S,L>, mln::value::int_u<m> >
00109     {
00110       typedef mln::value::hsl_<H,S,L> ret;
00111     };
00112 
00113     template <typename H, typename S, typename L, unsigned m>
00114     struct set_precise_binary_< op::div, mln::value::hsl_<H,S,L>, mln::value::int_u<m> >
00115     {
00116       typedef mln::value::hsl_<H,S,L> ret;
00117     };
00118 
00119     template <typename H, typename S, typename L>
00120     struct value_< mln::value::hsl_<H,S,L> >
00121     {
00122       enum {
00123         dim = 3,
00124         nbits = (sizeof (H) + sizeof (S) + sizeof (L)) * 8,
00125         card  = mln_value_card_from_(nbits)
00126       };
00127 
00128       typedef trait::value::nature::vectorial nature;
00129       typedef trait::value::kind::color       kind;
00130       typedef mln_value_quant_from_(card)     quant;
00131 
00132       typedef void comp;
00133       typedef H comp_0;
00134       typedef S comp_1;
00135       typedef L comp_2;
00136 
00137       template <typename V> static comp_0 get_comp_0(const V& v) { return v.hue(); }
00138       template <typename V> static comp_1 get_comp_1(const V& v) { return v.sat(); }
00139       template <typename V> static comp_2 get_comp_2(const V& v) { return v.lum(); }
00140 
00141 //      typedef algebra::vec<3, float> sum;
00142       typedef mln::value::hsl_<H,S,L> sum;
00143     };
00144 
00145   } // end of namespace trait
00146 
00147 
00148   namespace value
00149   {
00150 
00151     template <typename E>
00152     struct HSL : Object<E>
00153     {
00154     };
00155 
00156     template <typename H, typename S, typename L>
00157     class hsl_ : public HSL< hsl_<H,S,L> >
00158     {
00159     public:
00160 
00161       typedef H h_type;
00162       typedef S s_type;
00163       typedef L l_type;
00164 
00166       hsl_()
00167       {
00168       }
00169 
00170       hsl_(const literal::zero_t&)
00171         : hue_(0),
00172           sat_(0),
00173           lum_(0)
00174       {
00175       }
00176 
00178       hsl_(const H& hue, const S& sat, const L& lum)
00179         : hue_(hue),
00180           sat_(sat),
00181           lum_(lum)
00182       {
00183       }
00184 
00186       const H& hue() const;
00187       const S& sat() const;
00188       const L& lum() const;
00189 
00191       H& hue();
00192       S& sat();
00193       L& lum();
00194 
00195     private:
00196       //FIXME: Don't we want to store these values in a vector?
00197       H hue_;
00198       S sat_;
00199       L lum_;
00200     };
00201 
00202     typedef hsl_<float, float, float> hsl_f;
00203 
00204     typedef hsl_<double, double, double> hsl_d;
00205 
00206 
00213     template <typename H, typename S, typename L>
00214     std::ostream& operator<<(std::ostream& ostr, const hsl_<H,S,L>& c);
00215 
00216 
00219     template <typename H, typename S, typename L>
00220     hsl_<H,S,L>
00221     operator+(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs);
00223 
00226     template <typename H, typename S, typename L>
00227     hsl_<H,S,L>
00228     operator-(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs);
00230 
00233     template <typename H, typename S, typename L, typename S2>
00234     hsl_<H,S,L>
00235     operator*(const hsl_<H,S,L>& lhs, const mln::value::scalar_<S2>& s);
00237 
00240     template <typename H, typename S, typename L, typename S2>
00241     hsl_<H,S,L>
00242     operator/(const hsl_<H,S,L>& lhs, const mln::value::scalar_<S2>& s);
00244 
00247     template <typename H, typename S, typename L>
00248     bool
00249     operator==(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs);
00251 
00252   } // end of namespace mln::value
00253 
00254 
00255 
00256   // More forward declarations
00257   namespace fun
00258   {
00259     namespace v2v
00260     {
00261 
00262       template <typename T_hsl>
00263       struct f_rgb_to_hsl_;
00264 
00265       typedef f_rgb_to_hsl_<value::hsl_f> f_rgb_to_hsl_f_t;
00266 
00267       extern f_rgb_to_hsl_f_t f_rgb_to_hsl_f;
00268 
00269     }
00270 
00271   }
00272 
00273 # ifndef MLN_INCLUDE_ONLY
00274 
00275 
00276   namespace value
00277   {
00278 
00279     template <typename H, typename S, typename L>
00280     const H&
00281     hsl_<H,S,L>::hue() const
00282     {
00283       return this->hue_;
00284     }
00285 
00286     template <typename H, typename S, typename L>
00287     const S&
00288     hsl_<H,S,L>::sat() const
00289     {
00290       return this->sat_;
00291     }
00292 
00293     template <typename H, typename S, typename L>
00294     const L&
00295     hsl_<H,S,L>::lum() const
00296     {
00297       return this->lum_;
00298     }
00299 
00300     template <typename H, typename S, typename L>
00301     H&
00302     hsl_<H,S,L>::hue()
00303     {
00304       return this->hue_;
00305     }
00306 
00307     template <typename H, typename S, typename L>
00308     S&
00309     hsl_<H,S,L>::sat()
00310     {
00311       return this->sat_;
00312     }
00313 
00314     template <typename H, typename S, typename L>
00315     L&
00316     hsl_<H,S,L>::lum()
00317     {
00318       return this->lum_;
00319     }
00320 
00321 
00322     template <typename H, typename S, typename L>
00323     inline
00324     std::ostream& operator<<(std::ostream& ostr, const hsl_<H,S,L>& v)
00325     {
00326       return ostr << '(' << debug::format(v.hue())
00327                   << ',' << debug::format(v.sat())
00328                   << ',' << debug::format(v.lum())
00329                   << ')';
00330     }
00331 
00332 
00333     template <typename H, typename S, typename L>
00334     hsl_<H,S,L>
00335     operator+(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs)
00336     {
00337       return hsl_<H,S,L>(lhs.hue() + rhs.hue(),
00338                          lhs.sat() + rhs.sat(),
00339                          lhs.lum() + rhs.lum());
00340     }
00341 
00342 
00343     template <typename H, typename S, typename L>
00344     hsl_<H,S,L>
00345     operator-(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs)
00346     {
00347       return hsl_<H,S,L>(lhs.hue() - rhs.hue(),
00348                          lhs.sat() - rhs.sat(),
00349                          lhs.lum() - rhs.lum());
00350     }
00351 
00352 
00353     template <typename H, typename S, typename L, typename S2>
00354     hsl_<H,S,L>
00355     operator*(const hsl_<H,S,L>& lhs, const mln::value::scalar_<S2>& s)
00356     {
00357       return hsl_<H,S,L>(lhs.hue() * s,
00358                          lhs.sat() * s,
00359                          lhs.lum() * s);
00360     }
00361 
00362 
00363     template <typename H, typename S, typename L, typename S2>
00364     hsl_<H,S,L>
00365     operator/(const hsl_<H,S,L>& lhs, const mln::value::scalar_<S2>& s)
00366     {
00367       return hsl_<H,S,L>(lhs.hue() / s,
00368                          lhs.sat() / s,
00369                          lhs.lum() / s);
00370     }
00371 
00372     template <typename H, typename S, typename L>
00373     bool
00374     operator==(const hsl_<H,S,L>& lhs, const hsl_<H,S,L>& rhs)
00375     {
00376       return lhs.hue() == rhs.hue()
00377           && lhs.sat() == rhs.sat()
00378           && lhs.lum() == rhs.lum();
00379     }
00380 
00381   } // end of namespace mln::value
00382 
00383 
00384   namespace convert
00385   {
00386 
00387     namespace over_load
00388     {
00389 
00390       inline
00391       void
00392       from_to_(const value::rgb<16>& from, value::hsl_<float,float,float>& to)
00393       {
00394         to = fun::v2v::f_rgb_to_hsl_f(from);
00395       }
00396 
00397       inline
00398       void
00399       from_to_(const value::rgb<8>& from, value::hsl_<float,float,float>& to)
00400       {
00401         to = fun::v2v::f_rgb_to_hsl_f(from);
00402       }
00403 
00404     } // end of namespace mln::convert::over_load
00405 
00406   } // end of namespace mln::convert
00407 
00408 
00409 # endif // ! MLN_INCLUDE_ONLY
00410 
00411 
00412 } // end of namespace mln
00413 
00414 #endif // ! MLN_VALUE_HSL_HH

Generated on Thu Sep 8 2011 18:31:57 for Milena (Olena) by  doxygen 1.7.1