$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
data/transform.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 <cmath>
27 
28 #include <mln/core/image/image1d.hh>
29 #include <mln/core/image/image2d.hh>
30 #include <mln/core/image/image3d.hh>
31 #include <mln/pw/image.hh>
32 #include <mln/core/image/flat_image.hh>
33 #include <mln/core/image/vmorph/cast_image.hh>
34 #include <mln/core/image/dmorph/image_if.hh>
35 #include <mln/core/image/dmorph/sub_image.hh>
36 #include <mln/core/image/dmorph/extension_val.hh>
37 
38 #include <mln/data/fill.hh>
39 #include <mln/data/transform.hh>
40 #include <mln/data/paste.hh>
41 #include <mln/fun/p2b/chess.hh>
42 #include <mln/fun/p2v/iota.hh>
43 
44 
45 namespace my
46 {
47 
48  // FIXME: Use debug::iota instead of this hand-made version.
49  template <typename I>
50  void iota(I& ima)
51  {
52  unsigned i = 0;
53  mln_piter(I) p(ima.domain());
54  for_all(p)
55  {
56  ima(p) = i * i;
57  i += 1;
58  }
59  }
60 
61  // FIXME: Shouldn't this functor be part of Milena?
62  struct sqrt : mln::Function_v2v<sqrt>
63  {
64  typedef unsigned short result;
65 
66  template <typename T>
67  result operator()(T c) const
68  {
69  return static_cast<result>( std::sqrt(float(c)) );
70  }
71  };
72 
73 } // end of namespace my
74 
75 
76 
77 int main()
78 {
79  using namespace mln;
80  const unsigned size = 5;
81  box2d b = make::box2d(1,1, 3,3);
82 
83 
85  {
86  image1d<unsigned short> ima(size);
87  image1d<unsigned short> out(size);
88 
89  my::iota(ima);
90  out = data::transform(ima, my::sqrt());
91 
92  // FIXME: Use mln_piter().
93  // FIXME: Or use mln::test instead?
94  // (And so on for the rest of the file.)
95  box_fwd_piter_<point1d> p(out.domain());
96  for_all(p)
97  mln_assertion(ima(p) == out(p) * out(p));
98  }
99 
100 
102  {
103  image2d<unsigned short> ima(size, size);
104  image2d<unsigned short> out(size, size);
105 
106  my::iota(ima);
107  out = data::transform(ima, my::sqrt());
108 
109  box_fwd_piter_<point2d> p(out.domain());
110  for_all(p)
111  mln_assertion(ima(p) == out(p) * out(p));
112  }
113 
115  {
116  image2d<unsigned short> ima(size, size);
117 
118  data::fill_with_value(ima, 51);
119  data::transform(ima, my::sqrt());
120  }
121 
123  {
124  image3d<unsigned short> ima(size, size, size);
125  image3d<unsigned short> out(size, size, size);
126 
127  my::iota(ima);
128  out = data::transform(ima, my::sqrt());
129 
130  box_fwd_piter_<point3d> p(out.domain());
131  for_all(p)
132  mln_assertion(ima(p) == out(p) * out(p));
133  }
134 
136  {
137  fun::p2v::iota f;
138  const pw::image<fun::p2v::iota, box2d> ima(f, b);
139  image2d<unsigned short> out(size, size);
140 
141  data::fill(out, 0u);
142  out = data::transform(ima, my::sqrt());
143  }
144 
145  // flat image test
146  {
147  flat_image<short, box2d> ima(5, b);
148  image2d<unsigned short> out(size, size);
149 
150  data::fill_with_value(ima, 169);
151  out = data::transform(ima, my::sqrt());
152 
153  box2d::piter p(out.domain());
154  for_all(p)
155  mln_assertion(ima(p) == out(p) * out(p));
156  }
157 
158  // image if test
159  {
160  typedef image2d<unsigned short> I;
161  typedef image_if<I, fun::p2b::chess> II;
162 
163  I ima(size, size);
164  II ima_if = ima | fun::p2b::chess();
165 
166  data::fill_with_value(ima, 0);
167  my::iota(ima);
168  II out = data::transform(ima_if, my::sqrt());
169 
170  II::piter p(ima_if.domain());
171  for_all(p)
172  mln_assertion(ima_if(p) == out(p) * out(p));
173  }
174 
175  // cast image test
176  {
177  typedef image2d<unsigned short> I;
178  typedef cast_image_<int, I> II;
179  typedef image2d<unsigned short> III;
180 
181  I in(size, size);
182  II cast(in);
183  III out(size, size);
184 
185  data::fill(in, 169u);
186  data::fill(out, 81u);
187 
188  out = data::transform(cast, my::sqrt());
189 
190  II::piter p(cast.domain());
191  for_all(p)
192  mln_assertion(cast(p) == out(p) * out(p));
193  }
194 
195  // sub_image test
196  {
197  typedef image2d<int> I;
198  typedef sub_image< image2d<int>, box2d > II;
200 
201  I ima(size, size);
202  II sub_ima(ima, b);
203 
204  data::fill(ima, 169);
205  III out = data::transform(sub_ima, my::sqrt());
206 
207  II::piter p(sub_ima.domain());
208  for_all(p)
209  mln_assertion(sub_ima(p) == out(p) * out(p));
210  }
211 
212  // extended image test
213  {
214  typedef image2d<int> I;
215  typedef extension_val< image2d<int> > II;
217 
218  I ima(size, size);
219  II extend_ima(ima, 169);
220 
221  data::fill(ima, 169);
222  III out = data::transform(extend_ima, my::sqrt());
223 
224  II::piter p(extend_ima.domain());
225  for_all(p)
226  mln_assertion(extend_ima(p) == out(p) * out(p));
227  }
228 }