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

gray_f.hh

00001 // Copyright (C) 2006, 2007, 2008, 2009 EPITA Research and Development
00002 // 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_INTERNAL_GRAY_F_HH
00028 # define MLN_VALUE_INTERNAL_GRAY_F_HH
00029 
00033 
00034 
00035 # include <iostream>
00036 
00037 # include <mln/value/ops.hh>
00038 
00039 # include <mln/core/contract.hh>
00040 # include <mln/metal/math/pow.hh>
00041 # include <mln/metal/bexpr.hh>
00042 # include <mln/literal/ops.hh>
00043 
00044 # include <mln/value/float01_f.hh>
00045 # include <mln/trait/value_.hh>
00046 
00047 # include <mln/value/graylevel_f.hh>
00048 
00049 namespace mln
00050 {
00051 
00052   namespace literal
00053   {
00055     struct black_t;
00056     struct medium_gray_t;
00057     struct white_t;
00059   }
00060   namespace value
00061   {
00063     namespace internal
00064     {
00065       template <unsigned n> class gray_;
00066       class gray_f;
00067     }
00068 
00069     struct float01_f;
00071   }
00072 
00073 
00074 
00075   namespace trait
00076   {
00077 
00078 
00079     template < template <class, class> class Name>
00080     struct set_precise_binary_< Name, mln::value::internal::gray_f, mln::value::internal::gray_f >
00081     {
00082       typedef mln::value::internal::gray_f ret;
00083     };
00084 
00085     template <>
00086     struct set_precise_binary_< op::greater, mln::value::internal::gray_f, mln::value::internal::gray_f >
00087     {
00088       typedef bool ret;
00089     };
00090 
00091     template <>
00092     struct set_precise_binary_< op::eq, mln::value::internal::gray_f, mln::value::internal::gray_f >
00093     {
00094       typedef bool ret;
00095     };
00096 
00097     // Necessary??
00098 //     template <typename F>
00099 //     struct set_binary_< op::eq,
00100 //                      mln::value::Floating,  mln::value::internal::gray_f,
00101 //                      mln::value::Floating, F >
00102 //     {
00103 //       typedef bool ret;
00104 //     };
00105 
00107     template <typename T> struct value_;
00108 
00109     // 'gray_f' as a value.
00110     template <>
00111     struct value_<mln::value::internal::gray_f>
00112     {
00113     private:
00114       typedef mln::value::internal::gray_f self_;
00115       typedef float equiv_;
00116 
00117     public:
00118 
00119       enum {
00120         nbits = mln_nbits(equiv_),
00121         card  = 0
00122       };
00123 
00124       typedef trait::value::nature::floating nature;
00125       typedef trait::value::kind::gray       kind;
00126       typedef mln_trait_value_quant_(equiv_)   quant;
00127 
00128       static equiv_ min() { return 0; }
00129       static equiv_ max() { return 1; }
00130       static equiv_ epsilon() { return mln_epsilon(equiv_); }
00131 
00132       typedef float sum;
00133     };
00134 
00135   } // end of namespace mln::trait
00136 
00137 
00138 
00139   namespace value
00140   {
00141 
00142     namespace internal
00143     {
00144 
00146       struct gray_f
00147         :
00148         public Floating< gray_f >,
00149 
00150         public internal::value_like_< float,     // Equivalent.
00151                                       float,     // Encoding.
00152                                       gray_f,    // Interoperation.
00153                                       gray_f >   // Exact.
00154       {
00156         gray_f();
00157 
00159         gray_f(const gray_f& rhs);
00160         gray_f& operator=(const gray_f& rhs);
00162 
00164         template <unsigned n>
00165         gray_f(const gray_<n>& rhs);
00166         template <unsigned n>
00167         gray_f& operator=(const gray_<n>& rhs);
00169 
00171         gray_f(float val);
00172         gray_f& operator=(float val);
00174 
00176         gray_f(const graylevel_f& val);
00177         gray_f& operator=(const graylevel_f& val);
00179 
00181         template <unsigned m>
00182         operator graylevel<m>() const;
00183 
00185         template <unsigned m>
00186         operator gray_<m>() const;
00187 
00189         operator graylevel_f() const;
00190 
00192         float value() const;
00193       };
00194 
00195       // Operators.
00196 
00198       std::ostream& operator<<(std::ostream& ostr, const gray_f& g);
00199 
00200     } // end of namespace mln::value::internal
00201 
00202   } // end of namespace mln::value
00203 
00204 } // end of namespace mln
00205 
00206 
00207 # include <mln/value/internal/gray_.hh>
00208 
00209 
00210 namespace mln
00211 {
00212 
00213   namespace value
00214   {
00215 
00216     namespace internal
00217     {
00218 
00219 # ifndef MLN_INCLUDE_ONLY
00220 
00221       // gray_f.
00222 
00223       inline
00224       gray_f::gray_f()
00225       {
00226       }
00227 
00228       inline
00229       gray_f::gray_f(const gray_f& g)
00230         : Floating<gray_f>()
00231       {
00232         this->v_ = g.v_;
00233       }
00234 
00235       inline
00236       gray_f&
00237       gray_f::operator=(const gray_f& g)
00238       {
00239         this->v_ = g.v_;
00240         return *this;
00241       }
00242 
00243       template <unsigned n>
00244       gray_f::gray_f(const gray_<n>& rhs)
00245       {
00246         static const float denom = float(metal::math::pow_int<2, n>::value) - 1.f;
00247         this->v_ = float(rhs.value()) / denom;
00248       }
00249 
00250       template <unsigned n>
00251       gray_f&
00252       gray_f::operator=(const gray_<n>& rhs)
00253       {
00254         static const float denom = float(metal::math::pow_int<2, n>::value) - 1.f;
00255         this->v_ = float(rhs.value()) / denom;
00256         return *this;
00257       }
00258 
00259       inline
00260       gray_f::gray_f(float val)
00261       {
00262         this->v_ = val;
00263       }
00264 
00265       inline
00266       gray_f&
00267       gray_f::operator=(float val)
00268       {
00269         this->v_ = val;
00270         return *this;
00271       }
00272 
00273       inline
00274       gray_f::gray_f(const graylevel_f& rhs)
00275       {
00276         this->v_ = rhs.value();
00277       }
00278 
00279       inline
00280       gray_f&
00281       gray_f::operator=(const graylevel_f& rhs)
00282       {
00283         this->v_ = rhs.value();
00284         return *this;
00285       }
00286 
00287       template <unsigned m>
00288       inline
00289       gray_f::operator graylevel<m>() const
00290       {
00291         return graylevel<m>(int(round(this->v_ * (mlc_pow_int(2, m) - 1))));
00292       }
00293 
00294 
00295       template <unsigned m>
00296       inline
00297       gray_f::operator gray_<m>() const
00298       {
00299         return gray_<m>(int(round(this->v_ * (mlc_pow_int(2, m) - 1))));
00300       }
00301 
00302       inline
00303       gray_f::operator graylevel_f() const
00304       {
00305         return graylevel_f(this->v_);
00306       }
00307 
00308       inline
00309       float
00310       gray_f::value() const
00311       {
00312         return this->v_;
00313       }
00314 
00315       // Operators.
00316 
00317       inline
00318       std::ostream& operator<<(std::ostream& ostr, const gray_f& g)
00319       {
00320         return ostr << g.value() << "/gl_f"; // FIXME: Be more explicit!
00321       }
00322 
00323 # endif // ! MLN_INCLUDE_ONLY
00324 
00325     } // end of namespace mln::value::internal
00326 
00327 
00328     // Graylevel_F operators.
00329 
00330     // Op glf == Int
00331 
00332     template <typename I>
00333     inline
00334     bool
00335     operator==(const Integer<I>& lhs, const graylevel_f& rhs)
00336     {
00337       return rhs.value() == exact(lhs);
00338     }
00339 
00340     // Op glf == glf
00341     inline
00342     bool
00343     operator==(const graylevel_f& lhs, const graylevel_f& rhs)
00344     {
00345       return rhs.value() == lhs.value();
00346     }
00347 
00348     // Op glf + glf
00349     inline
00350     mln_trait_op_plus_(graylevel_f, graylevel_f)
00351       operator+(const graylevel_f& lhs, const graylevel_f& rhs)
00352     {
00353       return lhs.value() + rhs.value();
00354     }
00355 
00356     // Op glf + gl<n>
00357     template <unsigned n>
00358     inline
00359      mln_trait_op_plus(graylevel_f, graylevel<n>)
00360       operator+(const graylevel_f& lhs, const graylevel<n>& rhs)
00361     {
00362       return lhs.value() + graylevel_f(rhs).value();
00363     }
00364 
00365     // Op gl<n> + glf
00366     template <unsigned n>
00367     inline
00368      mln_trait_op_plus(graylevel_f, graylevel<n>)
00369       operator+(const graylevel<n>& lhs, const graylevel_f& rhs)
00370     {
00371       return rhs.value() + graylevel_f(lhs).value();
00372     }
00373 
00374 
00375     // Op glf + Another type
00376     template <typename I>
00377     inline
00378     void
00379     operator+(const graylevel_f& lhs, const I& i)
00380     {
00381       typename Object<I>::wrong_use_of_graylevel_f___Please_use_the__to_enc__method a;
00382     }
00383 
00384 
00385     // Op  Another type + glf
00386     template <typename I>
00387     inline
00388     void
00389     operator+(const I& i, const graylevel_f& rhs)
00390     {
00391       typename Object<I>::wrong_use_of_graylevel_f___Please_use_the__to_enc__method a;
00392     }
00393 
00394 
00395     // Op glf - Another type
00396     template <typename I>
00397     inline
00398     void
00399     operator-(const graylevel_f& lhs, const I& i)
00400     {
00401       typename Object<I>::wrong_use_of_graylevel_f___Please_use_the__to_enc__method a;
00402     }
00403 
00404 
00405     // Op  Another type - glf
00406     template <typename I>
00407     inline
00408     void
00409     operator-(const I& i, const graylevel_f& rhs)
00410     {
00411       typename Object<I>::wrong_use_of_graylevel_f___Please_use_the__to_enc__method a;
00412     }
00413 
00414     // Op glf - glf
00415 
00416     inline
00417     mln_trait_op_minus_(graylevel_f, graylevel_f)
00418       operator-(const graylevel_f& lhs, const graylevel_f& rhs)
00419     {
00420       return lhs.value() - rhs.value();
00421     }
00422 
00423     // Op glf * glf
00424     inline
00425     mln_trait_op_times_(graylevel_f, graylevel_f)
00426       operator*(const graylevel_f& lhs, const graylevel_f& rhs)
00427     {
00428       return lhs.value() * rhs.value();
00429     }
00430 
00431     // Op symm glf * Int
00432 
00433     template <typename I>
00434     inline
00435     mln_trait_op_times(graylevel_f, I)
00436       operator*(const graylevel_f& lhs, const Integer<I>& rhs)
00437     {
00438       return lhs.value() * int(exact(rhs));
00439     }
00440 
00441     template <typename I>
00442     inline
00443     mln_trait_op_times(I, graylevel_f)
00444       operator*(const Integer<I>& lhs, const graylevel_f& rhs)
00445     {
00446       return rhs.value() * int(exact(lhs));
00447     }
00448 
00449     // Op symm glf * Float
00450 
00451     template <typename F>
00452     inline
00453     mln_trait_op_times(graylevel_f, F)
00454       operator*(const graylevel_f& lhs, const Floating<F>& rhs)
00455     {
00456       return lhs.value() * exact(rhs);
00457     }
00458 
00459     template <typename F>
00460     inline
00461     mln_trait_op_times(F, graylevel_f)
00462       operator*(const Floating<F>& lhs, const graylevel_f& rhs)
00463     {
00464       return rhs.value() * exact(lhs);
00465     }
00466 
00467 
00468 
00469     // Op * scalar
00470     template <typename S>
00471     inline
00472     mln_trait_op_times(graylevel_f, S)
00473       operator*(const graylevel_f& lhs, const scalar_<S>& rhs)
00474     {
00475       return lhs.value() * rhs.to_equiv();
00476     }
00477 
00478     // Op / scalar
00479     template <typename S>
00480     inline
00481     mln_trait_op_div(graylevel_f, S)
00482       operator/(const graylevel_f& lhs, const scalar_<S>& rhs)
00483     {
00484       mln_precondition(rhs.to_equiv() != S(0));
00485       return lhs.value() / rhs.to_equiv();
00486     }
00487 
00488   } // end of namespace mln::value
00489 
00490 } // end of namespace mln
00491 
00492 #endif // ! MLN_VALUE_INTERNAL_GRAY_F_HH

Generated on Tue Oct 4 2011 15:23:55 for Milena (Olena) by  doxygen 1.7.1