Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
to_image.hh
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 #ifndef MLN_CONVERT_TO_IMAGE_HH
27 # define MLN_CONVERT_TO_IMAGE_HH
28 
32 
33 # include <mln/trait/image_from_grid.hh>
34 
35 # include <mln/core/concept/site_set.hh>
36 # include <mln/core/concept/window.hh>
37 # include <mln/core/concept/weighted_window.hh>
38 # include <mln/core/concept/neighborhood.hh>
39 
40 # include <mln/geom/bbox.hh>
41 # include <mln/data/fill.hh>
42 # include <mln/histo/array.hh>
43 
44 # include <mln/core/image/image1d.hh>
45 # include <mln/core/image/image2d.hh>
46 # include <mln/core/image/image3d.hh>
47 # include <mln/convert/from_to.hh>
48 
49 
50 namespace mln
51 {
52 
53  namespace convert
54  {
55 
56 
59  template <typename S>
60  mln_image_from_grid(mln_site(S)::grid, bool)
61  to_image(const Site_Set<S>& pset, unsigned border = 1);
62 
64  template <typename W>
65  mln_image_from_grid(mln_site(W)::grid, bool)
66  to_image(const Window<W>& win);
67 
69  template <typename W>
70  mln_image_from_grid(mln_site(W)::grid, mln_weight(W))
71  to_image(const Weighted_Window<W>& w_win);
72 
74  template <typename N>
75  mln_image_from_grid(mln_site(N)::grid, bool)
76  to_image(const Neighborhood<N>& nbh);
77 
79  template <typename T>
80  image1d<unsigned>
81  to_image(const histo::array<T>& h);
82 
83 
84 
85 # ifndef MLN_INCLUDE_ONLY
86 
87  template <typename S>
88  inline
89  mln_image_from_grid(mln_site(S)::grid, bool)
90  to_image(const Site_Set<S>& pset_, unsigned border)
91  {
92  const S& pset = exact(pset_);
93  mln_image_from_grid(mln_site(S)::grid, bool) ima;
94  ima.init_(geom::bbox(pset).to_larger(border));
95  data::fill(ima, false);
96  mln_piter(S) p(pset);
97  for_all(p)
98  ima(p) = true;
99  return ima;
100  }
101 
102  template <typename W>
103  inline
104  mln_image_from_grid(mln_site(W)::grid, bool)
105  to_image(const Window<W>& win)
106  {
107  mln_is_simple_window(W)::check();
108  mln_precondition(exact(win).is_valid());
109  mln_image_from_grid(mln_site(W)::grid, bool) tmp;
110  convert::from_to(win, tmp);
111  return tmp;
112  }
113 
114  template <typename W>
115  inline
116  mln_image_from_grid(mln_site(W)::grid, mln_weight(W))
117  to_image(const Weighted_Window<W>& w_win)
118  {
119  mln_is_simple_window(W)::check();
120  mln_precondition(exact(w_win).is_valid());
121  mln_image_from_grid(mln_site(W)::grid, mln_weight(W)) tmp;
122  convert::from_to(exact(w_win), tmp);
123  return tmp;
124  }
125 
126  template <typename N>
127  inline
128  mln_image_from_grid(mln_site(N)::grid, bool)
129  to_image(const Neighborhood<N>& nbh)
130  {
131  mln_precondition(exact(nbh).is_valid());
132  return to_image(exact(nbh).win());
133  }
134 
135  template <typename T>
136  inline
139  {
140  def::coord
141  n = static_cast<def::coord>(h.vset().nvalues()),
142  v_min = static_cast<def::coord>(h.vset()[0]),
143  v_max = static_cast<def::coord>(h.vset()[n - 1]);
144  image1d<unsigned> ima(make::box1d(v_min, v_max));
145  for (def::coord i = 0; i < n; ++i)
146  ima(point1d(i)) = h[i];
147  return ima;
148  }
149 
150 # endif // ! MLN_INCLUDE_ONLY
151 
152  } // end of namespace mln::convert
153 
154 } // end of namespace mln
155 
156 
157 #endif // ! MLN_CONVERT_TO_IMAGE_HH