Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator 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  template <typename I>
49  void iota(I& ima)
50  {
51  unsigned i = 0;
52  mln_piter(I) p(ima.domain());
53  for_all(p)
54  {
55  ima(p) = i * i;
56  i += 1;
57  }
58  }
59 
60  struct sqrt : mln::Function_v2v<sqrt>
61  {
62  typedef unsigned short result;
63 
64  template <typename T>
65  result operator()(T c) const
66  {
67  return static_cast<result>( std::sqrt(float(c)) );
68  }
69  };
70 
71 } // end of namespace mln
72 
73 
74 
75 int main()
76 {
77  using namespace mln;
78  const unsigned size = 5;
79  box2d b = make::box2d(1,1, 3,3);
80 
81 
83  {
84  image1d<unsigned short> ima(size);
85  image1d<unsigned short> out(size);
86 
87  my::iota(ima);
88  out = data::transform(ima, my::sqrt());
89 
90  box_fwd_piter_<point1d> p(out.domain());
91  for_all(p)
92  mln_assertion(ima(p) == out(p) * out(p));
93  }
94 
95 
97  {
98  image2d<unsigned short> ima(size, size);
99  image2d<unsigned short> out(size, size);
100 
101  my::iota(ima);
102  out = data::transform(ima, my::sqrt());
103 
104  box_fwd_piter_<point2d> p(out.domain());
105  for_all(p)
106  mln_assertion(ima(p) == out(p) * out(p));
107  }
108 
110  {
111  image2d<unsigned short> ima(size, size);
112 
113  data::fill_with_value(ima, 51);
114  data::transform(ima, my::sqrt());
115 
116  }
117 
119  {
120  image3d<unsigned short> ima(size, size, size);
121  image3d<unsigned short> out(size, size, size);
122 
123  my::iota(ima);
124  out = data::transform(ima, my::sqrt());
125 
126  box_fwd_piter_<point3d> p(out.domain());
127  for_all(p)
128  mln_assertion(ima(p) == out(p) * out(p));
129  }
130 
132  {
133  fun::p2v::iota f;
134  const pw::image<fun::p2v::iota, box2d> ima(f, b);
135  image2d<unsigned short> out(size, size);
136 
137  data::fill(out, 0u);
138  out = data::transform(ima, my::sqrt());
139  }
140 
141  // flat image test
142  {
143  flat_image<short, box2d> ima(5, b);
144  image2d<unsigned short> out(size, size);
145 
146  data::fill_with_value(ima, 169);
147  out = data::transform(ima, my::sqrt());
148 
149  box2d::piter p(out.domain());
150  for_all(p)
151  mln_assertion(ima(p) == out(p) * out(p));
152  }
153 
154  // image if test
155  {
156  typedef image2d<unsigned short> I;
157  typedef image_if<I, fun::p2b::chess> II;
158 
159  I ima(size, size);
160  II ima_if = ima | fun::p2b::chess();
161 
162  data::fill_with_value(ima, 0);
163  my::iota(ima);
164  II out = data::transform(ima_if, my::sqrt());
165 
166  II::piter p(ima_if.domain());
167  for_all(p)
168  mln_assertion(ima_if(p) == out(p) * out(p));
169  }
170 
171  // cast image test
172  {
173  typedef image2d<unsigned short> I;
174  typedef cast_image_<int, I> II;
175  typedef image2d<unsigned short> III;
176 
177  I in(size, size);
178  II cast(in);
179  III out(size, size);
180 
181  data::fill(in, 169u);
182  data::fill(out, 81u);
183 
184  out = data::transform(cast, my::sqrt());
185 
186  II::piter p(cast.domain());
187  for_all(p)
188  mln_assertion(cast(p) == out(p) * out(p));
189  }
190 
191  // sub_image test
192  {
193  typedef image2d<int> I;
194  typedef sub_image< image2d<int>, box2d > II;
196 
197  I ima(size, size);
198  II sub_ima(ima, b);
199 
200  data::fill(ima, 169);
201  III out = data::transform(sub_ima, my::sqrt());
202 
203  II::piter p(sub_ima.domain());
204  for_all(p)
205  mln_assertion(sub_ima(p) == out(p) * out(p));
206  }
207 
208  // extended image test
209  {
210  typedef image2d<int> I;
211  typedef extension_val< image2d<int> > II;
213 
214  I ima(size, size);
215  II extend_ima(ima, 169);
216 
217  data::fill(ima, 169);
218  III out = data::transform(extend_ima, my::sqrt());
219 
220  II::piter p(extend_ima.domain());
221  for_all(p)
222  mln_assertion(extend_ima(p) == out(p) * out(p));
223  }
224 }