$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
compute_full.cc
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 #include <mln/core/image/image1d.hh>
27 #include <mln/core/image/image2d.hh>
28 #include <mln/core/image/image3d.hh>
29 
30 #include <mln/value/int_u8.hh>
31 #include <mln/value/int_u16.hh>
32 #include <mln/value/int_s8.hh>
33 #include <mln/value/int_s16.hh>
34 
35 #include <mln/core/image/dmorph/sub_image.hh>
36 #include <mln/core/image/dmorph/image_if.hh>
37 #include <mln/fun/p2b/chess.hh>
38 
39 #include <mln/accu/stat/min.hh>
40 #include <mln/accu/stat/max.hh>
41 #include <mln/debug/iota.hh>
42 #include <mln/debug/println.hh>
43 #include <mln/data/compute.hh>
44 
45 
46 
47 struct f_box1d_t : mln::Function_v2b< f_box1d_t >
48 {
49  f_box1d_t(const mln::box1d& b)
50  : b_(b)
51  {
52  }
53  mln::box1d b_;
54  bool operator()(const mln::point1d& p) const
55  {
56  return b_.has(p);
57  }
58 };
59 
60 struct f_box2d_t : mln::Function_v2b< f_box2d_t >
61 {
62  f_box2d_t(const mln::box2d& b)
63  : b_(b)
64  {
65  }
66  mln::box2d b_;
67  bool operator()(const mln::point2d& p) const
68  {
69  return b_.has(p);
70  }
71 };
72 
73 struct f_box3d_t : mln::Function_v2b< f_box3d_t >
74 {
75  f_box3d_t(const mln::box3d& b)
76  : b_(b)
77  {
78  }
79  mln::box3d b_;
80  bool operator()(const mln::point3d& p) const
81  {
82  return b_.has(p);
83  }
84 };
85 
86 
87 namespace mln
88 {
89  template <typename I>
90  void
91  chk1d(unsigned cols)
92  {
94 
95  image1d<I> ima (cols);
96  debug::iota (ima);
97  I real_min = 1;
98  I real_min2 = 1;
99  I real_max = (I)(cols % (unsigned) mln_max(I));
100  I real_max2 = 2;
101  if (cols >= (unsigned)mln_max(I))
102  {
103  real_min = 0;
104  real_max = mln_max(I) - 1;
105  }
106 
107 
108  {
109  accu::stat::min<I> acu_min;
110  accu::stat::max<I> acu_max;
111 
112  I min = data::compute(acu_min, ima);
113  I max = data::compute(acu_max, ima);
114  mln_assertion(min == real_min);
115  mln_assertion(max == real_max);
116  }
117 
118  {
119  sub_image<image1d<I>, box1d> sub_ima (ima, b1);
120 
121  accu::stat::min<I> acu_min;
122  accu::stat::max<I> acu_max;
123 
124  I min = data::compute(acu_min, sub_ima);
125  I max = data::compute(acu_max, sub_ima);
126  mln_assertion(min == real_min2);
127  mln_assertion(max == real_max2);
128  }
129 
130  {
131  f_box1d_t f_b(b1);
132  image_if<image1d<I>, f_box1d_t> if_ima(ima, f_b);
133 
134  accu::stat::min<I> acu_min;
135  accu::stat::max<I> acu_max;
136 
137  I min = data::compute(acu_min, if_ima);
138  I max = data::compute(acu_max, if_ima);
139  mln_assertion(min == real_min2);
140  mln_assertion(max == real_max2);
141  }
142  }
143 
144  template <typename I>
145  void
146  chk2d(unsigned rows, unsigned cols)
147  {
148  box2d b2(literal::origin, point2d(1, 1));
149 
150  image2d<I> ima (rows, cols);
151  debug::iota (ima);
152  I real_min = 1;
153  I real_min2 = 1;
154  I real_max;
155  if (rows * cols >= (unsigned)mln_max(I))
156  {
157  real_min = 0;
158  real_max = mln_max(I) - 1;
159  }
160  else
161  {
162  real_max = (I)(rows * cols);
163  if ((cols == (unsigned)mln_max(I) - 2) ||
164  (cols == (unsigned)mln_max(I) - 1))
165  real_min2 = 0;
166  }
167 
168  {
169  accu::stat::min<I> acu_min;
170  accu::stat::max<I> acu_max;
171 
172  I min = data::compute(acu_min, ima);
173  I max = data::compute(acu_max, ima);
174 
175  mln_assertion(min == real_min);
176  mln_assertion(max == real_max);
177  }
178 
179  }
180 
181  template <typename I>
182  void
183  chk3d(unsigned slis, unsigned rows, unsigned cols)
184  {
185  box3d b3(literal::origin, point3d(1, 1, 1));
186 
187  image3d<I> ima (slis, rows, cols);
188  debug::iota (ima);
189  I real_min = 1;
190  I real_max;
191  if (slis * rows * cols >= (unsigned)mln_max(I))
192  {
193  real_min = 0;
194  real_max = mln_max(I) - 1;
195  }
196  else
197  real_max = (I)(slis * rows * cols);
198  {
199  accu::stat::min<I> acu_min;
200  accu::stat::max<I> acu_max;
201 
202  I min = data::compute(acu_min, ima);
203  I max = data::compute(acu_max, ima);
204 
205  mln_assertion(min == real_min);
206  mln_assertion(max == real_max);
207  }
208 
209  }
210 }
211 
212 
213 int main()
214 {
215  using namespace mln;
216 
217  unsigned slis_start = 2;
218  unsigned slis_end = 3;
219 
220  unsigned rows_start = 2;
221  unsigned rows_end = 16;
222 
223  unsigned cols_start = 2;
224  unsigned cols_end = 256;
225 
226 
227  std::cerr << "Tests data::compute:" << std::endl;
228 
229  (std::cerr << "in 1d ... ").flush ();
230  {
231  for (unsigned i = cols_start; i < cols_end; ++i)
232  {
233  chk1d<int>(i);
234  chk1d<unsigned>(i);
235  chk1d<value::int_u8>(i);
236  chk1d<value::int_u16>(i);
237  chk1d<value::int_s8>(i);
238  chk1d<value::int_s16>(i);
239  }
240  }
241  std::cerr << "OK" << std::endl;
242 
243  (std::cerr << "in 2d ... ").flush ();
244  {
245  for (unsigned j = rows_start; j < rows_end; ++j)
246  for (unsigned i = cols_start; i < cols_end; ++i)
247  {
248  chk2d<int>(j, i);
249  chk2d<unsigned>(j, i);
250  chk2d<value::int_u8>(j, i);
251  chk2d<value::int_u16>(j, i);
252  chk2d<value::int_s8>(j, i);
253  chk2d<value::int_s16>(j, i);
254  }
255  }
256  std::cerr << "OK" << std::endl;
257 
258  (std::cerr << "in 3d ... ").flush ();
259  {
260  for (unsigned k = slis_start; k < slis_end; ++k)
261  for (unsigned j = rows_start; j < rows_end; ++j)
262  for (unsigned i = cols_start; i < cols_end; ++i)
263  {
264  chk3d<int>(k, j, i);
265  chk3d<unsigned>(k, j, i);
266  chk3d<value::int_u8>(k, j, i);
267  chk3d<value::int_u16>(k, j, i);
268  chk3d<value::int_s8>(k, j, i);
269  }
270  }
271  std::cerr << "OK" << std::endl;
272 }