$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
integral_sub_sum_sum2_functor.hh
1 // Copyright (C) 2011, 2013 EPITA Research and Development Laboratory
2 // (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 SCRIBO_UTIL_INTEGRAL_SUB_SUM_SUM2_FUNCTOR_HH
28 # define SCRIBO_UTIL_INTEGRAL_SUB_SUM_SUM2_FUNCTOR_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/site_set/box.hh>
36 # include <mln/make/box2d.hh>
37 # include <mln/util/couple.hh>
38 
39 namespace scribo
40 {
41 
42  namespace util
43  {
44 
45  using namespace mln;
46 
47  template <typename I, typename S = mln_sum(mln_value(I))>
49  {
50  typedef mln_concrete(I) J;
51  typedef mln_value(I) V;
52 
53  public:
55 
56  integral_sub_sum_sum2_functor(const I& ima, unsigned scale);
57  integral_sub_sum_sum2_functor(unsigned scale,
58  const mln_box(I)& output_domain,
59  unsigned border);
60 
61 
62  void take(const V& v);
63 
64  result to_result_first_row() const;
65  result to_result(const result& up_result) const;
66 
67  void begin_of_col();
68  void end_of_col();
69 
70  void begin_of_row();
71  void begin_of_first_row();
72  void end_of_row();
73 
74  J sub;
75 
76  private:
77  void init_(unsigned scale, const mln_box(I)& b, unsigned border);
78 
79  S local_sum;
80  S h_sum;
81  S h_sum_2;
82 
83  V *p_sub;
84  unsigned b_next;
85  unsigned area;
86  };
87 
88 
89 # ifndef MLN_INCLUDE_ONLY
90 
91  template <typename I, typename S>
93  const mln_box(I)& output_domain,
94  unsigned border)
95  {
96  init_(scale, output_domain, border);
97  }
98 
99  template <typename I, typename S>
100  integral_sub_sum_sum2_functor<I,S>::integral_sub_sum_sum2_functor(const I& ima,
101  unsigned scale)
102  {
104  b = mln::make::box2d((ima.nrows() + scale - 1) / scale,
105  (ima.ncols() + scale - 1) / scale);
106  init_(scale, b, ima.border());
107  }
108 
109  template <typename I, typename S>
110  void
111  integral_sub_sum_sum2_functor<I,S>::init_(unsigned scale, const mln_box(I)& b,
112  unsigned border)
113  {
114  sub = J(b, border);
115 
116  area = scale * scale;
117 
118  b_next = 2 * border;
119  unsigned b_offset = sub.delta_offset(dpoint2d(border, border));
120  p_sub = sub.buffer() + b_offset;
121  }
122 
123  template <typename I, typename S>
124  inline
125  void
126  integral_sub_sum_sum2_functor<I,S>::begin_of_first_row()
127  {
128  h_sum = 0;
129  h_sum_2 = 0;
130  }
131 
132  template <typename I, typename S>
133  inline
134  void
135  integral_sub_sum_sum2_functor<I,S>::begin_of_row()
136  {
137  h_sum = 0;
138  h_sum_2 = 0;
139  }
140 
141  template <typename I, typename S>
142  inline
143  void
144  integral_sub_sum_sum2_functor<I,S>::take(const V& v)
145  {
146  local_sum += v;
147  h_sum_2 += v * v;
148  }
149 
150  template <typename I, typename S>
151  inline
152  typename integral_sub_sum_sum2_functor<I,S>::result
153  integral_sub_sum_sum2_functor<I,S>::to_result_first_row() const
154  {
155  return result(h_sum + local_sum, h_sum_2);
156  }
157 
158  template <typename I, typename S>
159  inline
160  typename integral_sub_sum_sum2_functor<I,S>::result
161  integral_sub_sum_sum2_functor<I,S>::to_result(const result& up_result) const
162  {
163  return result(h_sum + local_sum + up_result.first(),
164  h_sum_2 + up_result.second());
165  }
166 
167  template <typename I, typename S>
168  inline
169  void
170  integral_sub_sum_sum2_functor<I,S>::begin_of_col()
171  {
172  local_sum = 0;
173  }
174 
175  template <typename I, typename S>
176  inline
177  void
178  integral_sub_sum_sum2_functor<I,S>::end_of_col()
179  {
180  mln::convert::from_to(local_sum / area, *p_sub++);
181  h_sum += local_sum;
182  }
183 
184  template <typename I, typename S>
185  inline
186  void
187  integral_sub_sum_sum2_functor<I,S>::end_of_row()
188  {
189  p_sub += b_next;
190  }
191 
192 
193 #endif // ! MLN_INCLUDE_ONLY
194 
195  } // end of namespace scribo::util
196 
197 } // end of namespace scribo
198 
199 #endif // ! SCRIBO_UTIL_INTEGRAL_SUB_SUM_SUM2_FUNCTOR_HH