$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
int_u_sat.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_VALUE_INT_U_SAT_HH
27 # define MLN_VALUE_INT_U_SAT_HH
28 
33 
34 # include <mln/metal/math/pow.hh>
35 # include <mln/value/internal/value_like.hh>
36 # include <mln/value/concept/integer.hh>
37 # include <mln/value/internal/encoding.hh>
38 # include <mln/value/int_u.hh>
39 # include <mln/trait/value_.hh>
40 # include <mln/debug/format.hh>
41 
42 
43 namespace mln
44 {
45 
46  namespace value
47  {
48  // Fwd decls.
49  template <unsigned n> struct int_u_sat;
50  }
51 
52  namespace trait
53  {
54 
55  template <unsigned n>
56  struct value_< mln::value::int_u_sat<n> >
57  {
58  enum {
59  dim = 1,
61  nbits = n
62  };
63 
64  // FIXME: Overhaul these traits (see other value traits).
65  static const mln::value::int_u_sat<n> min() { return 0; }
66  static const mln::value::int_u_sat<n> max() { return card - 1; }
67 
68  typedef trait::value::nature::integer nature;
69  typedef trait::value::kind::data kind;
70  // FIXME: Is that right?
71  typedef mln_value_quant_from_(card) quant;
72 
73  typedef unsigned comp;
74 
75  typedef float sum;
76  };
77 
78  } // end of namespace mln::trait
79 
80 
81  namespace value
82  {
83 
84 
89  template <unsigned n>
90  struct int_u_sat
91  :
92  public Integer< int_u_sat<n> >,
93 
94  public internal::value_like_< int_u<n>, // Equivalent.
95  mln_enc(int_u<n>), // Encoding.
96  unsigned, // Interoperation.
97  int_u_sat<n> > // Exact.
98  {
100  int_u_sat();
101 
103  int_u_sat(int i);
104 
106  operator int() const;
107 
109  int_u_sat<n>& operator=(int i);
110 
112  static const int_u_sat<n> zero;
113 
115  static const int_u_sat<n> one;
116 
118  int_u_sat<n>& operator+=(int i);
119 
121  int_u_sat<n>& operator-=(int i);
122 
123  private:
124  typedef mln_enc(int_u<n>) enc_;
125  };
126 
127 
128  // Safety.
129  // FIXME: We shouldn't have to do that.
130  template <> struct int_u_sat<0>;
131  template <> struct int_u_sat<1>;
132 
141  template <unsigned n>
142  std::ostream& operator<<(std::ostream& ostr, const int_u_sat<n>& i);
143 
144 
145 # ifndef MLN_INCLUDE_ONLY
146 
147  template <unsigned n>
148  inline
150  {
151  }
152 
153  template <unsigned n>
154  inline
156  {
157  static const unsigned max_ = mln_max(int_u<n>);
158  if (i < 0)
159  this->v_ = 0;
160  // Explicitly cast I to unsigned to avoid a warning between
161  // signed and unsigned values from the compiler.
162  else if (static_cast<unsigned>(i) > max_)
163  this->v_ = static_cast<enc_>(max_);
164  else
165  this->v_ = static_cast<enc_>(i);
166  }
167 
168  template <unsigned n>
169  inline
171  {
172  return this->v_;
173  }
174 
175  template <unsigned n>
176  inline
177  int_u_sat<n>&
178  int_u_sat<n>::operator=(int i)
179  {
180  static const unsigned max_ = mln_max(int_u<n>);
181  if (i < 0)
182  this->v_ = 0;
183  // Explicitly cast I to unsigned to avoid a warning between
184  // signed and unsigned values from the compiler.
185  else if (static_cast<unsigned>(i) > max_)
186  this->v_ = static_cast<enc_>(max_);
187  else
188  this->v_ = static_cast<enc_>(i);
189  return *this;
190  }
191 
192  template <unsigned n>
193  inline
194  int_u_sat<n>&
196  {
197  int v = int(this->v_) + i;
198  *this = v;
199  return *this;
200  }
201 
202  template <unsigned n>
203  inline
204  int_u_sat<n>&
206  {
207  int v = int(this->v_) - i;
208  *this = v;
209  return *this;
210  }
211 
212  template <unsigned n>
213  const int_u_sat<n> int_u_sat<n>::zero = 0;
214 
215  template <unsigned n>
216  const int_u_sat<n> int_u_sat<n>::one = 1;
217 
218  template <unsigned n>
219  inline
220  std::ostream& operator<<(std::ostream& ostr, const int_u_sat<n>& i)
221  {
222  return ostr << debug::format(i.to_equiv());
223  }
224 
225 # endif // ! MLN_INCLUDE_ONLY
226 
227  } // end of namespace mln::value
228 
229 } // end of namespace mln
230 
231 
232 #endif // ! MLN_VALUE_INT_U_SAT_HH