$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
link_lines.hh
1 // Copyright (C) 2011, 2012, 2013 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 SCRIBO_TEXT_LINK_LINES_HH
28 # define SCRIBO_TEXT_LINK_LINES_HH
29 
34 
35 #include <mln/value/int_u16.hh>
36 #include <mln/data/fill.hh>
37 #include <mln/geom/rotate.hh>
38 #include <mln/geom/ncols.hh>
39 #include <mln/draw/box_plain.hh>
40 #include <mln/util/array.hh>
41 
42 #include <scribo/core/line_links.hh>
43 
44 
45 namespace scribo
46 {
47 
48  namespace text
49  {
50 
51  using namespace mln;
52 
53 
61  template <typename L>
62  line_links<L>
63  link_lines(const line_set<L>& lines);
64 
65 
66 # ifndef MLN_INCLUDE_ONLY
67 
68 
69  template <typename L>
70  line_links<L>
71  link_lines(const line_set<L>& lines)
72  {
73  typedef value::int_u16 V;
74 
75  const mln_concrete(L)& lbl = lines.components().labeled_image();
76 
77  // Rotate the domain in order to browse the image efficiently.
78  mln_ch_value(L,V) blocks(geom::rotate(lbl.domain(),
79  -90, lbl.domain().pcenter()));
80  mln::data::fill(blocks, literal::zero);
81 
82  // Construct a list of rotated bboxes for each lines.
83  mln::util::array<mln_box(L)> rbbox(1);
84  rbbox.reserve(lines.nelements());
85 
86  for_all_lines(l, lines)
87  {
88  if (! lines(l).is_valid() || lines(l).is_hidden() || lines(l).type() != line::Text)
89  {
90  rbbox.resize(rbbox.nelements() + 1);
91  continue;
92  }
93 
94  mln_box(L) b = mln::geom::rotate(lines(l).bbox(), -90, lbl.domain().pcenter());
95  rbbox.append(b);
96  mln::draw::box_plain(blocks, b, l);
97  }
98 
99  // Looking for neighbor lines.
100  line_links<L> links(lines);
101  links.init();
102 
103  for_all_lines(l, lines)
104  {
105  if (! lines(l).is_valid() || lines(l).is_hidden() || lines(l).type() != line::Text)
106  continue;
107 
108  int dmax = 1.5 * lines(l).x_height(); // FIXME: better ratio?
109  mln_site(L) c = rbbox(l).pcenter();
110 
111  int
112  midcol = (rbbox(l).pmax().col()
113  - rbbox(l).pmin().col()) / 2;
114 
115  int
116  nleftima = std::abs(c.col() - blocks.domain().pmin().col()), // abs, useful?
117  nleft = std::min(nleftima, midcol + dmax);
118 
119  // Left
120  {
121  const V
122  *p = &blocks(c),
123  *pstop = p - nleft;
124 
125  for (; p != pstop; --p)
126  {
127  if (*p != literal::zero // Not the background
128  && *p != l // Not the current component
129  && links(*p) != l) // No loops
130  {
131  links(l) = *p;
132  break;
133  }
134  }
135  }
136 
137  // Right
138  {
139  int
140  nrightima = geom::ncols(blocks) - c.col() + blocks.domain().pmin().col(),
141  nright = std::min(nrightima, midcol + dmax);
142 
143  const V
144  *p = &blocks(c),
145  *pstop = p + nright - 1;
146 
147  for (; p != pstop; ++p)
148  {
149  if (*p != literal::zero // Not the background
150  && *p != l // Not the current component
151  && links(l) != *p // No loops
152  && links(*p) == *p) // Not already connected
153  {
154  links(*p) = l;
155  break;
156  }
157  }
158  }
159  }
160 
161  return links;
162  }
163 
164 
165 # endif // ! MLN_INCLUDE_ONLY
166 
167 
168  } // end of namespace scribo::text
169 
170 } // end of namespace scribo
171 
172 
173 #endif // ! SCRIBO_TEXT_LINK_LINES_HH