$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
fun/v2v/saturate.hh
1 // Copyright (C) 2007, 2009, 2010 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_FUN_V2V_SATURATE_HH
28 # define MLN_FUN_V2V_SATURATE_HH
29 
35 # include <mln/core/concept/function.hh>
36 # include <mln/metal/converts_to.hh>
37 # include <mln/trait/value_.hh>
38 # include <mln/value/cast.hh>
39 # include <mln/convert/from_to.hh>
40 
41 
42 namespace mln
43 {
44 
45  namespace fun
46  {
47 
48  namespace v2v
49  {
50 
51  // FIXME: Doc!
52 
53  template <typename V>
54  struct saturate : public Function_v2v< saturate<V> >
55  {
56  saturate();
57  saturate(const V& min, const V& max);
58 
59  typedef V result;
60 
61  template <typename W>
62  V operator()(const W& w) const;
63 
64  protected:
65  V min_, max_;
66  mutable bool needs_update_;
67  };
68 
69 
70 # ifndef MLN_INCLUDE_ONLY
71 
72  template <typename V>
73  inline
75  : min_(mln_min(V)),
76  max_(mln_max(V))
77  {
78  needs_update_ = true;
79  }
80 
81  template <typename V>
82  inline
83  saturate<V>::saturate(const V& min, const V& max)
84  : min_(min),
85  max_(max)
86  {
87  mln_precondition(max > min);
88  needs_update_ = true;
89  }
90 
91  template <typename V>
92  template <typename W>
93  inline
94  V
95  saturate<V>::operator()(const W& w) const
96  {
97  // FIXME: Check that W is a larger type than V; otherwise
98  // alt code.
99 
100  static W min_W, max_W;
101  if (needs_update_)
102  {
103  min_W = mln::value::cast<W>(min_);
104  max_W = mln::value::cast<W>(max_);
105  needs_update_ = false;
106  }
107 
108  // FIXME: Below we need something more powerful that mlc_converts_to
109  // for instance, with W=int_s<10u> and V=int_u<8u>, it does not
110  // works cause the cast is not obvious...
111  // mlc_converts_to(W, V)::check();
112 
113  if (w < min_W)
114  return min_;
115  if (w > max_W)
116  return max_;
117 
118  V output;
119  mln::convert::from_to(w, output);
120  return output;
121  }
122 
123 # endif // ! MLN_INCLUDE_ONLY
124 
125  } // end of namespace mln::fun::v2v
126 
127  } // end of namespace mln::fun
128 
129 } // end of namespace mln
130 
131 
132 #endif // ! MLN_FUN_V2V_SATURATE_HH