Milena (Olena)  User documentation 2.0a Id
graylevel_f.hh
00001 // Copyright (C) 2006, 2007, 2008, 2009, 2011 EPITA Research and
00002 // Development Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
00026 
00027 #ifndef MLN_VALUE_GRAYLEVEL_F_HH
00028 # define MLN_VALUE_GRAYLEVEL_F_HH
00029 
00033 
00034 # include <iostream>
00035 
00036 # include <mln/value/ops.hh>
00037 
00038 # include <mln/core/contract.hh>
00039 # include <mln/metal/math/pow.hh>
00040 # include <mln/metal/bexpr.hh>
00041 # include <mln/literal/ops.hh>
00042 
00043 # include <mln/value/float01_f.hh>
00044 # include <mln/trait/value_.hh>
00045 
00046 
00047 namespace mln
00048 {
00049 
00050   namespace literal
00051   {
00053     struct black_t;
00054     struct medium_gray_t;
00055     struct white_t;
00057   }
00058   namespace value
00059   {
00061     namespace internal { struct gray_f; }
00062     struct graylevel_f;
00063     template <unsigned n> struct graylevel;
00064     struct float01_f;
00066   }
00067 
00068 
00069 
00070   namespace trait
00071   {
00072 
00073     template <>
00074     struct set_precise_binary_< op::plus, mln::value::graylevel_f, mln::value::graylevel_f >
00075     {
00076       typedef mln::value::internal::gray_f ret;
00077     };
00078 
00079     template <unsigned n>
00080     struct set_precise_binary_< op::plus, mln::value::graylevel_f, mln::value::graylevel<n> >
00081     {
00082       typedef mln::value::internal::gray_f ret;
00083     };
00084     template <unsigned n>
00085     struct set_precise_binary_< op::plus, mln::value::graylevel<n>, mln::value::graylevel_f  >
00086     {
00087       typedef mln::value::internal::gray_f ret;
00088     };
00089 
00090     template <>
00091     struct set_precise_binary_< op::minus, mln::value::graylevel_f, mln::value::graylevel_f >
00092     {
00093       typedef mln::value::internal::gray_f ret;
00094     };
00095 
00096     template <>
00097     struct set_precise_binary_< op::times, mln::value::graylevel_f, mln::value::graylevel_f >
00098     {
00099       typedef mln::value::internal::gray_f ret;
00100     };
00101 
00102     template <>
00103     struct set_precise_binary_< op::div, mln::value::graylevel_f, mln::value::graylevel_f >
00104     {
00105       typedef mln::value::internal::gray_f ret;
00106     };
00107 
00108     template < typename I >
00109     struct set_binary_< op::times,
00110                         mln::value::Integer, mln::value::graylevel_f,
00111                         mln::value::Integer, I >
00112     {
00113       typedef mln::value::internal::gray_f ret;
00114     };
00115 
00116     template < typename I >
00117     struct set_binary_< op::times,
00118                         mln::value::Integer, I,
00119                         mln::value::Integer, mln::value::graylevel_f >
00120     {
00121       typedef mln::value::internal::gray_f ret;
00122     };
00123 
00124 
00125     template < typename F >
00126     struct set_binary_< op::times,
00127                         mln::value::Integer,  mln::value::graylevel_f,
00128                         mln::value::Floating, F >
00129     {
00130       typedef mln::value::internal::gray_f ret;
00131     };
00132 
00133     template < typename F >
00134     struct set_binary_< op::times,
00135                         mln::value::Floating, F,
00136                         mln::value::Integer,  mln::value::graylevel_f >
00137     {
00138       typedef mln::value::internal::gray_f ret;
00139     };
00140 
00141 
00142     template < typename S >
00143     struct set_precise_binary_< op::times, mln::value::graylevel_f, mln::value::scalar_<S> >
00144     {
00145       typedef mln::value::internal::gray_f ret;
00146     };
00147 
00148     template < typename S >
00149     struct set_precise_binary_< op::div, mln::value::graylevel_f, mln::value::scalar_<S> >
00150     {
00151       typedef mln::value::internal::gray_f ret;
00152     };
00153 
00154 
00156     template <typename T> struct value_;
00157 
00158     // 'graylevel_f' as a value.
00159     template <>
00160     struct value_<mln::value::graylevel_f>
00161     {
00162     private:
00163       typedef mln::value::graylevel_f self_;
00164       typedef mln::value::float01_f equiv_;
00165 
00166     public:
00167 
00168       enum {
00169         dim = 1,
00170         nbits = mln_nbits(equiv_),
00171         card  = 0
00172       };
00173 
00174       typedef trait::value::nature::floating nature;
00175       typedef trait::value::kind::gray       kind;
00176       typedef mln_trait_value_quant_(equiv_)   quant;
00177 
00178       static const equiv_ min() { return 0; }
00179       static const equiv_ max() { return 1; }
00180       static const equiv_ epsilon() { return mln_epsilon(equiv_); }
00181 
00182       typedef float comp;
00183 
00184       typedef float sum;
00185     };
00186 
00187   } // end of namespace mln::trait
00188 
00189 
00190   namespace value
00191   {
00192 
00194     struct graylevel_f
00195       :
00196       public Floating< graylevel_f >,
00197 
00198       public internal::value_like_< float01_f,          // Equivalent.
00199                                     mln_enc_(float01_f),// Encoding.
00200                                     internal::gray_f,   // Interoperation.
00201                                     graylevel_f >       // Exact.
00202     {
00204       graylevel_f();
00206       graylevel_f(const graylevel_f& rhs);
00208       graylevel_f& operator=(const graylevel_f& rhs);
00209 
00210 
00212       graylevel_f(float val);
00214       graylevel_f& operator=(float val);
00215 
00217       template <unsigned n>
00218         graylevel_f(const graylevel<n>& rhs);
00220       template <unsigned n>
00221         graylevel_f& operator=(const graylevel<n>& rhs);
00222 
00225       graylevel_f(const mln::literal::black_t&);
00226       graylevel_f(const mln::literal::medium_gray_t&);
00227       graylevel_f(const mln::literal::white_t&);
00229 
00232       graylevel_f& operator=(const mln::literal::black_t&);
00233       graylevel_f& operator=(const mln::literal::medium_gray_t&);
00234       graylevel_f& operator=(const mln::literal::white_t&);
00236 
00238       template <unsigned n>
00239         operator graylevel<n>() const;
00240 
00242       float value() const;
00243     };
00244 
00245 
00246     // Operators.
00247 
00249     std::ostream& operator<<(std::ostream& ostr, const graylevel_f& g);
00250 
00251     // graylevel_f + graylevel_f.
00252     mln_trait_op_plus_(graylevel_f, graylevel_f)
00253       operator+(const graylevel_f& lhs, const graylevel_f& rhs);
00254 
00255 
00256     // graylevel_f + graylevel<n>.
00257     template <unsigned n>
00258     mln_trait_op_plus(graylevel_f, graylevel<n>)
00259       operator+(const graylevel_f& lhs, const graylevel<n>& rhs);
00260     // graylevel<n> + graylevel_f.
00261     template <unsigned n>
00262     mln_trait_op_plus(graylevel_f, graylevel<n>)
00263       operator+(const graylevel<n>& lhs, const graylevel_f& rhs);
00264 
00265     // graylevel_f - graylevel_f.
00266     mln_trait_op_minus_(graylevel_f, graylevel_f)
00267       operator-(const graylevel_f& lhs, const graylevel_f& rhs);
00268 
00269     // graylevel_f * graylevel_f.
00270     mln_trait_op_times_(graylevel_f, graylevel_f)
00271       operator*(const graylevel_f& lhs, const graylevel_f& rhs);
00272 
00273 
00274     // With Builtins.
00275 
00276     // graylevel_f * T.
00277     template <unsigned n, typename T>
00278     mln_trait_op_times(graylevel_f, T)
00279       operator*(const graylevel_f& lhs, const T& rhs);
00280 
00281     // T * graylevel_f.
00282     template <unsigned n, typename T>
00283     mln_trait_op_times(graylevel_f, T)
00284       operator*(const T& lhs, const graylevel_f& rhs);
00285 
00286     // graylevel_f / T.
00287     template <unsigned n, typename T>
00288     internal::gray_f
00289       operator/(const graylevel_f& lhs, const T& rhs);
00290 
00291     // With Integer.
00292 
00293     // graylevel_f * Integer<I>.
00294     template <typename I>
00295     mln_trait_op_times(graylevel_f, I)
00296       operator*(const graylevel_f& lhs, const Integer<I>& rhs);
00297 
00298     // Integer<I> * graylevel_f.
00299     template <typename I>
00300     mln_trait_op_times(I, graylevel_f)
00301       operator*(const Integer<I>& lhs, const graylevel_f& rhs);
00302 
00303     // graylevel_f / Integer<I>.
00304     template <typename I>
00305     mln_trait_op_div(graylevel_f, I)
00306       operator/(const graylevel_f& lhs, const Integer<I>& rhs);
00307 
00308     // Integer<I> / graylevel_f.
00309     template <typename I>
00310     mln_trait_op_div(I, graylevel_f)
00311       operator/(const Integer<I>& lhs, const graylevel_f& rhs);
00312 
00313     // With Floating.
00314 
00315     // graylevel_f * Floating<F>.
00316     template <typename F>
00317     mln_trait_op_times(graylevel_f, F)
00318       operator*(const graylevel_f& lhs, const Floating<F>& rhs);
00319 
00320     // Floating<F>, graylevel_f.
00321     template <typename F>
00322     mln_trait_op_times(F, graylevel_f)
00323       operator*(const Floating<F>& lhs, const graylevel_f& rhs);
00324 
00325 
00326     // graylevel_f / Floating<F>.
00327     template <typename F>
00328     mln_trait_op_div(graylevel_f, F)
00329       operator/(const graylevel_f& lhs, const Floating<F>& rhs);
00330 
00331     // Floating<F> / graylevel_f.
00332     template <typename F>
00333     mln_trait_op_div(F, graylevel_f)
00334       operator/(const Floating<F>& lhs, const graylevel_f& rhs);
00335 
00336 
00337 # ifndef MLN_INCLUDE_ONLY
00338 
00339     // graylevel_f.
00340 
00341     inline
00342     graylevel_f::graylevel_f()
00343     {
00344     }
00345 
00346 
00347     inline
00348     graylevel_f::graylevel_f(float val)
00349     {
00350       mln_precondition(val >= 0);
00351       mln_precondition(val <= 1);
00352       this->v_ = val;
00353     }
00354 
00355     inline
00356     graylevel_f&
00357     graylevel_f::operator=(float val)
00358     {
00359       mln_precondition(val >= 0);
00360       mln_precondition(val <= 1);
00361       this->v_ = val;
00362       return *this;
00363     }
00364 
00365     template <unsigned n>
00366     graylevel_f::graylevel_f(const graylevel<n>& rhs)
00367     {
00368       mln_precondition(rhs.to_float() >= 0);
00369       mln_precondition(rhs.to_float() <= 1);
00370       this->v_ = rhs.to_float();
00371     }
00372 
00373     template <unsigned n>
00374     graylevel_f&
00375     graylevel_f::operator=(const graylevel<n>& rhs)
00376     {
00377       mln_precondition(rhs.to_float() >= 0);
00378       mln_precondition(rhs.to_float() <= 1);
00379       this->v_ = rhs.to_float();
00380       return *this;
00381     }
00382 
00383     inline
00384     graylevel_f::graylevel_f(const graylevel_f& rhs)
00385       : Floating<graylevel_f>()
00386     {
00387       this->v_ = rhs.v_;
00388     }
00389 
00390     inline
00391     graylevel_f&
00392     graylevel_f::operator=(const graylevel_f& rhs)
00393     {
00394       mln_precondition(rhs.v_ >= 0);
00395       mln_precondition(rhs.v_ <= 1);
00396       this->v_ = rhs.v_;
00397       return *this;
00398     }
00399 
00400     inline
00401     graylevel_f::graylevel_f(const mln::literal::black_t&)
00402     {
00403       this->v_ = 0.0f;
00404     }
00405 
00406     inline
00407     graylevel_f&
00408     graylevel_f::operator=(const mln::literal::black_t&)
00409     {
00410       this->v_ = 0.0f;
00411       return *this;
00412     }
00413 
00414     inline
00415     graylevel_f::graylevel_f(const mln::literal::medium_gray_t&)
00416     {
00417       this->v_ = 0.5f;
00418     }
00419 
00420     inline
00421     graylevel_f&
00422     graylevel_f::operator=(const mln::literal::medium_gray_t&)
00423     {
00424       this->v_ = 0.5f;
00425       return *this;
00426     }
00427 
00428     inline
00429     graylevel_f::graylevel_f(const mln::literal::white_t&)
00430     {
00431       this->v_ = 1.0f;
00432     }
00433 
00434     inline
00435     graylevel_f&
00436     graylevel_f::operator=(const mln::literal::white_t&)
00437     {
00438       this->v_ = 1.0f;
00439       return *this;
00440     }
00441 
00442     template <unsigned n>
00443     inline
00444     graylevel_f::operator graylevel<n>() const
00445     {
00446       return graylevel<n>(internal::gray_f(*this));
00447     }
00448 
00449     inline
00450     float
00451     graylevel_f::value() const
00452     {
00453       return this->v_;
00454     }
00455 
00456     // Operators.
00457 
00458     inline
00459     std::ostream& operator<<(std::ostream& ostr, const graylevel_f& g)
00460     {
00461       return ostr << g.value() << "/gl_f"; // FIXME: Be more explicit!.
00462     }
00463 
00464 # endif // ! MLN_INCLUDE_ONLY
00465 
00466   } // end of namespace mln::value
00467 
00468 } // end of namespace mln
00469 
00470 
00471 //# include <mln/value/internal/gray_f.hh>
00473 
00474 #endif // ! MLN_VALUE_GRAYLEVEL_F_HH
 All Classes Namespaces Functions Variables Typedefs Enumerator