$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
highlight_text_area.hh
1 // Copyright (C) 2010, 2011 EPITA Research and Development Laboratory
2 // (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 SCRIBO_DEBUG_HIGHLIGHT_TEXT_AREA_HH
28 # define SCRIBO_DEBUG_HIGHLIGHT_TEXT_AREA_HH
29 
33 
34 # include <mln/core/concept/image.hh>
35 # include <mln/core/concept/function.hh>
36 
37 # include <mln/draw/box.hh>
38 
39 # include <mln/data/fill.hh>
40 # include <mln/data/transform.hh>
41 
42 # include <mln/literal/colors.hh>
43 
44 # include <mln/value/rgb8.hh>
45 
46 # include <scribo/core/component_set.hh>
47 # include <scribo/core/line_set.hh>
48 
49 # include <mln/geom/rotate.hh>
50 
51 namespace scribo
52 {
53 
54  namespace debug
55  {
56 
57  using namespace mln;
58 
68  template <typename I>
69  mln_ch_value(I, value::rgb8)
70  highlight_text_area(const Image<I>& input,
71  const mln::util::array<box<mln_site(I)> >& bbox);
72 
73 
83  template <typename I, typename L>
84  mln_ch_value(I, value::rgb8)
85  highlight_text_area(const Image<I>& input,
86  const line_set<L>& lines);
87 
100  template <typename I, typename L>
101  mln_ch_value(I, value::rgb8)
102  highlight_text_area(const Image<I>& input,
103  const scribo::component_set<L>& components);
104 
105 
106 # ifndef MLN_INCLUDE_ONLY
107 
108  namespace internal
109  {
110 
111  template <typename M, typename R>
112  struct mask_non_text
113  : Function_v2v<mask_non_text<M, R> >,
114  private mlc_is(mln_value(M), bool)::check_t
115  {
116  typedef R result;
117 
118  mask_non_text(const Image<M>& mask)
119  : mask_(exact(mask)), p_(mask_)
120  {
121  p_.start();
122  }
123 
124  result operator()(const result& v) const
125  {
126  bool b = p_.val();
127  p_.next();
128  if (!b)
129  return v / 2;
130  else
131  return v;
132 
133  }
134 
135  M mask_;
136  mutable mln_pixter(M) p_;
137  };
138 
139  } // end of namespace scribo::debug::internal
140 
141 
142 
143  template <typename I>
144  mln_ch_value(I, value::rgb8)
145  highlight_text_area(const Image<I>& input_,
146  const mln::util::array<box<mln_site(I)> >& bbox)
147  {
148  mln_trace("scribo::debug::highlight_text_area");
149 
150  const I& input = exact(input_);
151 
152  mln_precondition(input.is_valid());
153  mlc_is(mln_value(I), value::rgb8)::check();
154 
155  typedef mln_ch_value(I, bool) mask_t;
156  mask_t mask;
157  initialize(mask, input);
158  data::fill(mask, false);
159 
160  for_all_elements(i, bbox)
161  data::fill((mask | bbox(i)).rw(), true);
162 
163  internal::mask_non_text<mask_t, mln_value(I)> f(mask);
164  mln_concrete(I) output = data::transform(input, f);
165 
166  for_all_elements(i, bbox)
167  mln::draw::box(output, bbox(i), literal::red);
168 
169  return output;
170  }
171 
172 
173  template <typename I, typename L>
174  mln_ch_value(I, value::rgb8)
175  highlight_text_area(const Image<I>& input_,
176  const line_set<L>& lines)
177  {
178  mln_trace("scribo::debug::highlight_text_area");
179 
180  const I& input = exact(input_);
181 
182  mln_precondition(input.is_valid());
183  mlc_is(mln_value(I), value::rgb8)::check();
184 
185  typedef mln_ch_value(I, bool) mask_t;
186  mask_t mask;
187  initialize(mask, input);
188  data::fill(mask, false);
189 
190  for_all_lines(i, lines)
191  {
192  if (! lines(i).is_valid()
193  || lines(i).tag() != line::None
194  || lines(i).type() != line::Text)
195  continue;
196 
197  data::fill((mask | lines(i).bbox()).rw(), true);
198  }
199 
200  internal::mask_non_text<mask_t, mln_value(I)> f(mask);
201  mln_concrete(I) output = data::transform(input, f);
202 
203  for_all_lines(i, lines)
204  {
205  if (! lines(i).is_valid()
206  || lines(i).tag() != line::None
207  || lines(i).type() != line::Text)
208  continue;
209 
210  mln::draw::box(output, lines(i).bbox(), literal::red);
211  }
212 
213  return output;
214  }
215 
216 
217  template <typename I, typename L>
218  mln_ch_value(I, value::rgb8)
219  highlight_text_area(const Image<I>& input_,
220  const scribo::component_set<L>& components)
221  {
222  mln_trace("scribo::debug::highlight_text_area");
223 
224  const I& input = exact(input_);
225 
226  mln_precondition(input.is_valid());
227  mlc_is(mln_value(I), value::rgb8)::check();
228 
229  typedef mln_ch_value(I, bool) mask_t;
230  mask_t mask;
231  initialize(mask, input);
232  data::fill(mask, false);
233 
234  for_all_comps(i, components)
235  if (components(i).is_valid())
236  data::fill((mask | components(i).bbox()).rw(), true);
237 
238  internal::mask_non_text<mask_t, mln_value(I)> f(mask);
239  mln_ch_value(I, value::rgb8) output = data::transform(input, f);
240 
241  for_all_comps(i, components)
242  if (components(i).is_valid())
243  mln::draw::box(output, components(i).bbox(), literal::red);
244 
245  return output;
246  }
247 
248 
249  template <typename I, typename L>
250  mln_ch_value(I, value::rgb8)
251  highlight_text_area_rotated(const Image<I>& input_,
252  const scribo::component_set<L>& components,
253  double angle, box2d rb)
254  {
255  mln_trace("scribo::debug::highlight_text_area");
256 
257  const I& input = exact(input_);
258 
259  mln_precondition(input.is_valid());
260  mlc_is(mln_value(I), value::rgb8)::check();
261 
262  typedef mln_ch_value(I, bool) mask_t;
263  mask_t mask;
264  initialize(mask, input);
265  data::fill(mask, false);
266 
267  mln::util::array<mln_box(I)> bbox(value::next(components.nelements()));
268 
269  for_all_comps(i, components)
270  if (components(i).is_valid())
271  {
272  bbox(i) = components(i).bbox();
273  bbox(i).pmin().row() += rb.pmin().row();
274  bbox(i).pmin().col() += rb.pmin().col();
275  bbox(i).pmax().row() += rb.pmin().row();
276  bbox(i).pmax().col() += rb.pmin().col();
277 
278  bbox(i) = mln::geom::rotate(bbox(i), - angle,
279  input.domain().pcenter());
280  }
281 
282  for_all_comps(i, components)
283  if (components(i).is_valid())
284  data::fill((mask | bbox(i)).rw(), true);
285 
286  internal::mask_non_text<mask_t, mln_value(I)> f(mask);
287  mln_ch_value(I, value::rgb8) output = data::transform(input, f);
288 
289  for_all_comps(i, components)
290  if (components(i).is_valid())
291  mln::draw::box(output, bbox(i), literal::red);
292 
293  return output;
294  }
295 
296 
297 # endif // ! MLN_INCLUDE_ONLY
298 
299 
300  } // end of namespace scribo::debug
301 
302 } // end of namespace scribo
303 
304 
305 #endif // ! SCRIBO_DEBUG_HIGHLIGHT_TEXT_AREA_HH