$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
stretch.hh
1 // Copyright (C) 2007, 2008, 2009, 2012, 2013 EPITA Research and
2 // Development 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 # include <mln/value/internal/encoding.hh>
45 
46 
47 namespace mln
48 {
49 
50  namespace data
51  {
52 
65  template <typename V, typename I>
66  mln_ch_value(I, V)
67  stretch(const V& v, const Image<I>& input);
68 
69 
70 # ifndef MLN_INCLUDE_ONLY
71 
72  namespace impl
73  {
74 
81  //
82  template <typename V, typename I>
83  inline
84  mln_ch_value(I, V)
85  stretch(const V&, const Image<I>& input)
86  {
87  mln_trace("data::impl::stretch");
88 
89  mlc_converts_to(float, V)::check();
90 
91  mln_ch_value(I, V) output;
92 
93  mln_value(I) min_, max_;
94  estim::min_max(input, min_, max_);
95  if (max_ != min_)
96  {
97  //FIXME: we would like to use float instead of double but we
98  //can't for precision reasons. See ticket #179.
99  double
100  min = double(min_),
101  max = double(max_),
102  epsilon = mln_epsilon(float),
103  M = mln_max(V) + 0.5f - epsilon,
104  m = 0.0f - 0.5f + epsilon,
105  a = (M - m) / (max - min),
106  b = (m * max - M * min) / (max - min);
107  fun::v2v::linear_sat<mln_value(I), double, V> f(a, b);
108  output = data::transform(input, f);
109  }
110  else
111  {
112  initialize(output, input);
113  mln_trace_warning("output has no significative data!");
114  }
115 
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&, const Image<I>& input)
129  {
130  mln_trace("data::stretch");
131 
132  mln_precondition(exact(input).is_valid());
133 
134  mln_ch_value(I, V) output = impl::stretch(V(), input);
135 
136  return output;
137  }
138 
139 # endif // ! MLN_INCLUDE_ONLY
140 
141  } // end of namespace mln::data
142 
143 } // end of namespace mln
144 
145 
146 #endif // ! MLN_DATA_STRETCH_HH