$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
border/fill.hh
1 // Copyright (C) 2007, 2008, 2009, 2011, 2012 EPITA Research and
2 // Development 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 
60  template <typename I>
61  void fill(const Image<I>& ima, const mln_value(I)& v);
62 
63 
64 # ifndef MLN_INCLUDE_ONLY
65 
66 
67  namespace internal
68  {
69 
70  template <typename I>
71  inline
72  void fill_tests(const Image<I>& ima, const mln_value(I)&)
73  {
74  mln_precondition(exact(ima).is_valid());
75  (void) ima;
76  }
77 
78  } // end of namespace mln::border::internal
79 
80 
81  namespace impl
82  {
83 
84  template <typename I>
85  inline
86  void fill_size_1(const Image<I>& ima_, const mln_value(I)& v)
87  {
88  mln_trace("border::impl::fill_size_1");
89 
90  const I& ima = exact(ima_);
91  internal::fill_tests(ima, v);
92 
93  typedef mln_psite(I) P;
94  typedef mln_psite(I) P;
95  mln_box_runstart_piter(I) pl(ima.domain());
96 
97  unsigned len_r = pl.run_length();
98  unsigned st = 0;
99 
100  for_all (pl)
101  {
102  unsigned end = ima.offset_of_point (pl);
103  if (st < end)
104  std::memset((void*)&opt::element(ima, st),
105  *(const int*)(&v),
106  end - st);
107  st = end + len_r;
108  }
109  if (st < opt::nelements(ima))
110  std::memset((void*)&opt::element(ima, st),
111  *(const int*)(&v),
112  opt::nelements(ima) - st);
113 
114  }
115 
116 
117  template <typename I>
118  inline
119  void fill_size_n(const I& ima_, const mln_value(I)& v)
120  {
121  mln_trace("border::impl::fill_size_n");
122 
123  I& ima = const_cast<I&>( exact(ima_) );
124  internal::fill_tests(ima, v);
125 
126  typedef mln_psite(I) P;
127  mln_box_runstart_piter(I) pl(ima.domain());
128  unsigned len_r = pl.run_length();
129  unsigned st = 0;
130 
131  for_all (pl)
132  {
133  unsigned end = ima.offset_of_point (pl);
134  for (unsigned i = st; i < end; ++i)
135  opt::element(ima, i) = v;
136  st = end + len_r;
137  }
138  for (unsigned i = st; i < opt::nelements(ima); ++i)
139  opt::element(ima, i) = v;
140 
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  mln_trace("border::fill");
207 
208  internal::fill_tests(ima, v);
209  internal::fill_dispatch(ima, v);
210 
211  }
212 
213 
214 # endif // ! MLN_INCLUDE_ONLY
215 
216  } // end of namespace mln::border
217 
218 } // end of namespace mln
219 
220 
221 #endif // ! MLN_BORDER_FILL_HH