Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
border/fill.hh
1 // Copyright (C) 2007, 2008, 2009, 2011 EPITA Research and Development
2 // Laboratory (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 MLN_BORDER_FILL_HH
28 # define MLN_BORDER_FILL_HH
29 
35 # include <cstring>
36 
37 # include <mln/core/concept/image.hh>
38 # include <mln/core/box_runstart_piter.hh>
39 # include <mln/opt/element.hh>
40 
41 
42 namespace mln
43 {
44 
45  namespace border
46  {
47 
58  template <typename I>
59  void fill(const Image<I>& ima, const mln_value(I)& v);
60 
61 
62 # ifndef MLN_INCLUDE_ONLY
63 
64 
65  namespace internal
66  {
67 
68  template <typename I>
69  inline
70  void fill_tests(const Image<I>& ima, const mln_value(I)&)
71  {
72  mln_precondition(exact(ima).is_valid());
73  (void) ima;
74  }
75 
76  } // end of namespace mln::border::internal
77 
78 
79  namespace impl
80  {
81 
82  template <typename I>
83  inline
84  void fill_size_1(const Image<I>& ima_, const mln_value(I)& v)
85  {
86  trace::entering("border::impl::fill_size_1");
87 
88  const I& ima = exact(ima_);
89  internal::fill_tests(ima, v);
90 
91  typedef mln_psite(I) P;
92  typedef mln_psite(I) P;
93  mln_box_runstart_piter(I) pl(ima.domain());
94 
95  unsigned len_r = pl.run_length();
96  unsigned st = 0;
97 
98  for_all (pl)
99  {
100  unsigned end = ima.index_of_point (pl);
101  if (st < end)
102  std::memset((void*)&opt::element(ima, st),
103  *(const int*)(&v),
104  end - st);
105  st = end + len_r;
106  }
107  if (st < opt::nelements(ima))
108  std::memset((void*)&opt::element(ima, st),
109  *(const int*)(&v),
110  opt::nelements(ima) - st);
111 
112  trace::exiting("border::impl::fill_size_1");
113  }
114 
115 
116  template <typename I>
117  inline
118  void fill_size_n(const I& ima_, const mln_value(I)& v)
119  {
120  trace::entering("border::impl::fill_size_n");
121 
122  I& ima = const_cast<I&>( exact(ima_) );
123  internal::fill_tests(ima, v);
124 
125  typedef mln_psite(I) P;
126  mln_box_runstart_piter(I) pl(ima.domain());
127  unsigned len_r = pl.run_length();
128  unsigned st = 0;
129 
130  for_all (pl)
131  {
132  unsigned end = ima.index_of_point (pl);
133  for (unsigned i = st; i < end; ++i)
134  opt::element(ima, i) = v;
135  st = end + len_r;
136  }
137  for (unsigned i = st; i < opt::nelements(ima); ++i)
138  opt::element(ima, i) = v;
139 
140  trace::exiting("border::impl::fill_size_n");
141  }
142 
143 
144  } // end of namespace mln::border::impl
145 
146 
147  namespace internal
148  {
149 
150  // Dispatch.
151 
152  template <typename I>
153  inline
154  void fill_dispatch(const Image<I>& ima, const mln_value(I)& v);
155 
156  template <typename I>
157  inline
158  void fill_dispatch(mln::trait::image::category::primary,
159  mln::trait::image::speed::fastest,
160  I& ima, const mln_value(I)& v)
161  {
162  if (sizeof(mln_value(I)) == 1)
163  impl::fill_size_1(ima, v);
164  else
165  impl::fill_size_n(ima, v);
166  }
167 
168  template <typename I>
169  inline
170  void fill_dispatch(mln::trait::image::category::primary,
171  mln::trait::image::speed::any,
172  I& /* ima */, const mln_value(I)& /* v */)
173  {
174  // No border so no-op.
175  }
176 
177  template <typename I>
178  inline
179  void fill_dispatch(mln::trait::image::category::morpher,
180  mln::trait::image::speed::any,
181  I& ima, const mln_value(I)& v)
182  {
183  fill_dispatch(ima.unmorph_(), v);
184  }
185 
186  template <typename I>
187  inline
188  void fill_dispatch(const Image<I>& ima_, const mln_value(I)& v)
189  {
190  I& ima = const_cast<I&>(exact(ima_));
191  fill_dispatch(mln_trait_image_category(I)(),
192  mln_trait_image_speed(I)(),
193  ima, v);
194  }
195 
196  } // end of namespace mln::border::internal
197 
198 
199 
200  // Facade.
201 
202  template <typename I>
203  inline
204  void fill(const Image<I>& ima, const mln_value(I)& v)
205  {
206  trace::entering("border::fill");
207 
208  internal::fill_tests(ima, v);
209  internal::fill_dispatch(ima, v);
210 
211  trace::exiting("border::fill");
212  }
213 
214 
215 # endif // ! MLN_INCLUDE_ONLY
216 
217  } // end of namespace mln::border
218 
219 } // end of namespace mln
220 
221 
222 #endif // ! MLN_BORDER_FILL_HH