$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
paragraph_info.hh
1 // Copyright (C) 2011, 2013 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_CORE_PARAGRAPH_INFO_HH
28 # define SCRIBO_CORE_PARAGRAPH_INFO_HH
29 
30 # include <scribo/core/line_info.hh>
31 # include <scribo/core/line_links.hh>
32 # include <scribo/core/tag/paragraph.hh>
33 # include <mln/util/array.hh>
34 # include <mln/accu/shape/bbox.hh>
35 
36 namespace scribo
37 {
38 
42  template <typename L>
44  {
45  public:
47  paragraph_info(const line_links<L>& llinks);
48 
56  void add_line(const line_info<L>& line);
57 
58  const mln::box2d& bbox() const;
59 
60  const line_info<L>& line(line_id_t id) const;
61 
62  const mln::util::array<line_id_t>& line_ids() const;
63 
64  unsigned nlines() const;
65 
66  const line_links<L>& llinks() const;
67 
68  // FIXME: add boldness?
69 
70  const mln::value::rgb8& color() const;
71  void set_color_(const mln::value::rgb8& v);
72  float color_reliability() const;
73  void set_color_reliability_(float v);
74 
75  bool is_valid() const;
76  void invalidate();
77 
78  bool needs_stats_update() const;
79  void force_stats_update();
80 
81  void set_delta_baseline(const int delta_baseline);
82  int delta_baseline() const;
83 
84  void fast_merge(paragraph_info<L>& info);
85 
86  void update_tag(paragraph::Tag tag);
87  paragraph::Tag tag() const;
88 
89  private:
92  line_links<L> llinks_;
93 
94  mln::value::rgb8 color_;
95  float color_reliability_;
96 
97  int delta_baseline_;
98  paragraph::Tag tag_;
99  bool is_valid_;
100  };
101 
102  template <typename L>
103  std::ostream& operator<<(std::ostream& ostr, const paragraph_info<L>& info);
104 
105  template <typename L>
106  bool operator==(const paragraph_info<L>& lhs, const paragraph_info<L>& rhs);
107 
108 # ifndef MLN_INCLUDE_ONLY
109 
110 
111  template <typename L>
113  : tag_(paragraph::None), is_valid_(false)
114  {
115  }
116 
117  template <typename L>
118  paragraph_info<L>::paragraph_info(const line_links<L>& llinks)
119  : llinks_(llinks), tag_(paragraph::None), is_valid_(true)
120  {
121  }
122 
123  template <typename L>
124  void
125  paragraph_info<L>::add_line(const line_info<L>& line)
126  {
127  line_ids_.append(line.id());
128  bbox_.take(line.bbox());
129 
130  // More data may need to be updated!
131  tag_ = paragraph::Needs_Precise_Stats_Update;
132  is_valid_ = true;
133  }
134 
135  template <typename L>
136  const mln::box2d&
138  {
139  return bbox_.to_result();
140  }
141 
142  template <typename L>
143  const line_info<L>&
144  paragraph_info<L>::line(line_id_t id) const
145  {
146  mln_precondition(is_valid());
147  return llinks_.lines()(id);
148  }
149 
150  template <typename L>
152  paragraph_info<L>::line_ids() const
153  {
154  return line_ids_;
155  }
156 
157  template <typename L>
158  unsigned
159  paragraph_info<L>::nlines() const
160  {
161  return line_ids_.nelements();
162  }
163 
164  template <typename L>
165  const line_links<L>&
166  paragraph_info<L>::llinks() const
167  {
168  return llinks_;
169  }
170 
171  template <typename L>
172  const mln::value::rgb8&
173  paragraph_info<L>::color() const
174  {
175  return color_;
176  }
177 
178  template <typename L>
179  void
180  paragraph_info<L>::set_color_(const mln::value::rgb8& v)
181  {
182  color_ = v;
183  }
184 
185  template <typename L>
186  float
187  paragraph_info<L>::color_reliability() const
188  {
189  return color_reliability_;
190  }
191 
192  template <typename L>
193  void
194  paragraph_info<L>::set_color_reliability_(float v)
195  {
196  color_reliability_ = v;
197  }
198 
199  template <typename L>
200  bool
201  paragraph_info<L>::is_valid() const
202  {
203  return llinks_.is_valid() && is_valid_;
204  }
205 
206  template <typename L>
207  void
208  paragraph_info<L>::invalidate()
209  {
210  is_valid_ = false;
211  }
212 
213  template <typename L>
214  bool
215  paragraph_info<L>::needs_stats_update() const
216  {
217  return tag_ == paragraph::Needs_Precise_Stats_Update;
218  }
219 
220  template <typename L>
221  void
222  paragraph_info<L>::force_stats_update()
223  {
224  if (!needs_stats_update())
225  return;
226 
227  const line_set<L>& lines = llinks_.lines();
228 
229  // Update color
230 
232  color_red,
233  color_green,
234  color_blue;
235 
236  float
237  sum2_red = 0,
238  sum2_green = 0,
239  sum2_blue = 0;
240 
241  for_all_elements(e, line_ids_)
242  {
243  unsigned lid = line_ids_(e);
244 
245  color_red.take(lines(lid).color().red());
246  color_green.take(lines(lid).color().green());
247  color_blue.take(lines(lid).color().blue());
248  sum2_red += mln::math::sqr<unsigned>(lines(lid).color().red());
249  sum2_green += mln::math::sqr<unsigned>(lines(lid).color().green());
250  sum2_blue += mln::math::sqr<unsigned>(lines(lid).color().blue());
251  }
252 
253  color_ = mln::value::rgb8(color_red.to_result(),
254  color_green.to_result(),
255  color_blue.to_result());
256 
257  float
258  var_red = sum2_red / (float)line_ids_.nelements()
259  - mln::math::sqr<float>(color_red.to_result()),
260  var_green = sum2_green / (float)line_ids_.nelements()
261  - mln::math::sqr<float>(color_green.to_result()),
262  var_blue = sum2_blue / (float)line_ids_.nelements()
263  - mln::math::sqr<float>(color_blue.to_result());
264 
265  color_reliability_ = std::sqrt(std::max(var_red,
266  std::max(var_green, var_blue)));
267 
268  // FIXME: Update paragraph stats
269 
270 
271  // Compute paragraph's delta baseline
272  stats< float > delta(nlines());
273  const unsigned nelements = line_ids_.nelements();
274 
275  for (unsigned i = 0; i < nelements; ++i)
276  {
277  const line_id_t& current_id = line_ids_(i);
278 
279  if (llinks_(current_id) != current_id)
280  {
281  const line_info<L>& current_line = lines(current_id);
282  const line_info<L>& left_line = lines(llinks_(current_id));
283 
284  delta.take(left_line.baseline() - current_line.baseline());
285  }
286  }
287 
288  int median = delta.median();
289 
290  if (!median)
291  median = lines(line_ids_(0)).x_height();
292 
293  set_delta_baseline(median);
294 
295 
296  // Update tag
297  tag_ = paragraph::None;
298  }
299 
300  template <typename L>
301  void
302  paragraph_info<L>::set_delta_baseline(const int delta_baseline)
303  {
304  delta_baseline_ = delta_baseline;
305  }
306 
307  template <typename L>
308  int
309  paragraph_info<L>::delta_baseline() const
310  {
311  return delta_baseline_;
312  }
313 
314  template <typename L>
315  void
316  paragraph_info<L>::fast_merge(paragraph_info<L>& other)
317  {
318  tag_ = paragraph::Needs_Precise_Stats_Update;
319  other.update_tag(paragraph::Merged);
320  other.invalidate();
321 
322  // Merge bboxes.
323  bbox_.take(other.bbox());
324 
325  // Update delta_baseline
326  // FIXME: delta base line should be updated correctly!!
327  set_delta_baseline(std::max(other.delta_baseline_, delta_baseline_));
328 
329  line_ids_.append(other.line_ids());
330  }
331 
332  template <typename L>
333  void
334  paragraph_info<L>::update_tag(paragraph::Tag tag)
335  {
336  tag_ = tag;
337  }
338 
339  template <typename L>
340  paragraph::Tag
341  paragraph_info<L>::tag() const
342  {
343  return tag_;
344  }
345 
346  template <typename L>
347  bool
348  operator==(const paragraph_info<L>& lhs, const paragraph_info<L>& rhs)
349  {
350 
351 
352 
353  return
354  lhs.line_ids() == rhs.line_ids()
355  && lhs.bbox() == rhs.bbox()
356  && lhs.llinks() == rhs.llinks()
357  && lhs.color() == rhs.color()
358  && lhs.color_reliability() == rhs.color_reliability()
359  && lhs.needs_stats_update() == rhs.needs_stats_update();
360  }
361 
362  template <typename L>
363  std::ostream&
364  operator<<(std::ostream& ostr, const paragraph_info<L>& info)
365  {
366  return ostr << "paragraph_info("
367  << "line_ids=" << info.line_ids()
368  << ", bbox=" << info.bbox()
369  << ", color=" << info.color()
370  << ", color_reliability=" << info.color_reliability()
371  << ")" << std::endl;
372  }
373 
374 
375 # endif // ! MLN_INCLUDE_ONLY
376 
377 } // end of namespace scribo
378 
379 #endif // ! SCRIBO_CORE_PARAGRAPH_INFO_HH