Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
stretch.hh
1 // Copyright (C) 2007, 2008, 2009 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_DATA_STRETCH_HH
28 # define MLN_DATA_STRETCH_HH
29 
38 
39 # include <mln/estim/min_max.hh>
40 # include <mln/value/int_u.hh>
41 # include <mln/fun/v2v/linear.hh>
42 # include <mln/data/transform.hh>
43 
44 
45 # include <mln/value/internal/encoding.hh>
46 # include <iomanip>
47 namespace mln
48 {
49 
50  namespace data
51  {
52 
63  template <typename V, typename I>
64  mln_ch_value(I, V)
65  stretch(const V& v, const Image<I>& input);
66 
67 
68 # ifndef MLN_INCLUDE_ONLY
69 
70  namespace impl
71  {
72 
79  //
80  template <typename V, typename I>
81  inline
82  mln_ch_value(I, V)
83  stretch(const V& v, const Image<I>& input)
84  {
85  trace::entering("data::impl::stretch");
86 
87  (void) v;
88  mlc_converts_to(float, V)::check();
89 
90  mln_ch_value(I, V) output;
91 
92  mln_value(I) min_, max_;
93  estim::min_max(input, min_, max_);
94  if (max_ != min_)
95  {
96  //FIXME: we would like to use float instead of double but we
97  //can't for precision reasons. See ticket #179.
98  double
99  min = double(min_),
100  max = double(max_),
101  epsilon = mln_epsilon(float),
102  M = mln_max(V) + 0.5f - epsilon,
103  m = 0.0f - 0.5f + epsilon,
104  a = (M - m) / (max - min),
105  b = (m * max - M * min) / (max - min);
106  fun::v2v::linear_sat<mln_value(I), double, V> f(a, b);
107  output = data::transform(input, f);
108  }
109  else
110  {
111  initialize(output, input);
112  trace::warning("output has no significative data!");
113  }
114 
115  trace::exiting("data::impl::stretch");
116  return output;
117  }
118 
119  } // end of namespace mln::data::impl
120 
121 
122 
123  // Facade
124 
125  template <typename V, typename I>
126  inline
127  mln_ch_value(I, V)
128  stretch(const V& v, const Image<I>& input)
129  {
130  trace::entering("data::stretch");
131 
132  (void) v;
133  mln_precondition(exact(input).is_valid());
134 
135  mln_ch_value(I, V) output = impl::stretch(V(), input);
136 
137  trace::exiting("data::stretch");
138  return output;
139  }
140 
141 # endif // ! MLN_INCLUDE_ONLY
142 
143  } // end of namespace mln::data
144 
145 } // end of namespace mln
146 
147 
148 #endif // ! MLN_DATA_STRETCH_HH