$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
debug_img_visitor.hh
1 // Copyright (C) 2011 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef SCRIBO_IO_IMG_INTERNAL_DEBUG_IMG_VISITOR_HH
27 # define SCRIBO_IO_IMG_INTERNAL_DEBUG_IMG_VISITOR_HH
28 
32 
33 # include <fstream>
34 
35 # include <mln/core/image/image2d.hh>
36 # include <mln/value/rgb8.hh>
37 # include <mln/draw/polygon.hh>
38 # include <mln/subsampling/antialiased.hh>
39 # include <mln/morpho/elementary/gradient_external.hh>
40 # include <mln/draw/box.hh>
41 
42 # include <scribo/core/internal/doc_serializer.hh>
43 # include <scribo/core/document.hh>
44 # include <scribo/core/paragraph_set.hh>
45 # include <scribo/core/line_info.hh>
46 
47 # include <scribo/util/component_precise_outline.hh>
48 # include <scribo/io/img/internal/draw_edges.hh>
49 # include <scribo/text/paragraphs_closing.hh>
50 
51 
52 namespace scribo
53 {
54 
55  namespace io
56  {
57 
58  namespace img
59  {
60 
61  namespace internal
62  {
63 
64 
65  template <typename L>
66  class debug_img_visitor : public doc_serializer<debug_img_visitor<L> >
67  {
68  public:
69  // Constructor
71  unsigned output_ratio);
72 
73  // Visit overloads
74  void visit(const document<L>& doc) const;
75 
76  void visit(const component_info<L>& info) const;
77 
78  void visit(const paragraph_set<L>& parset) const;
79 
80  void visit(const line_info<L>& line) const;
81 
82  private: // Attributes
84  unsigned output_ratio;
85  mutable L lbl_;
86 
87  private: // Methods
88  box2d compute_bbox(const box2d& b) const;
89  };
90 
91 
92 
93 # ifndef MLN_INCLUDE_ONLY
94 
95 
96  template <typename L>
97  box2d
99  {
100  point2d
101  pmin = b.pmin() / output_ratio,
102  pmax = b.pmax() / output_ratio;
103 
104  return box2d(pmin, pmax);
105  }
106 
107 
108  template <typename L>
109  debug_img_visitor<L>::debug_img_visitor(mln::image2d<value::rgb8>& out,
110  unsigned output_ratio)
111  : output(out), output_ratio(output_ratio)
112  {
113  mln_assertion(output.is_valid());
114  }
115 
116 
118  //
119  template <typename L>
120  void
121  debug_img_visitor<L>::visit(const document<L>& doc) const
122  {
123  // Text
124  if (doc.has_text())
125  doc.paragraphs().accept(*this);
126 
127  // Page elements (Pictures, ...)
128  if (doc.has_elements())
129  {
130  for_all_comps(e, doc.elements())
131  {
132  lbl_ = doc.elements().labeled_image();
133  if (doc.elements()(e).is_valid())
134  doc.elements()(e).accept(*this);
135  }
136  }
137 
138  // line seraparators
139  if (doc.has_vline_seps())
140  {
141  lbl_ = doc.vline_seps_comps().labeled_image();
142  for_all_comps(c, doc.vline_seps_comps())
143  if (doc.vline_seps_comps()(c).is_valid())
144  doc.vline_seps_comps()(c).accept(*this);
145  }
146  if (doc.has_hline_seps())
147  {
148  lbl_ = doc.hline_seps_comps().labeled_image();
149  for_all_comps(c, doc.hline_seps_comps())
150  if (doc.hline_seps_comps()(c).is_valid())
151  doc.hline_seps_comps()(c).accept(*this);
152  }
153 
154  }
155 
156 
158  //
159  template <typename L>
160  void
161  debug_img_visitor<L>::visit(const component_info<L>& info) const
162  {
163  // Getting component outline
165  //const L& lbl = info.holder().labeled_image();
167  par = scribo::util::component_precise_outline(lbl_ | info.bbox(), id);
168 
169  switch (info.type())
170  {
173  {
174  mln::draw::polygon(output, par, literal::cyan, output_ratio);
175  }
176  break;
177 
178 
180  {
181  mln::draw::polygon(output, par, literal::violet, output_ratio);
182  }
183  break;
184 
185  default:
186  case component::Image:
187  {
188  mln::draw::polygon(output, par, literal::orange, output_ratio);
189  }
190  break;
191  }
192  }
193 
195  //
196  template <typename L>
197  void
198  debug_img_visitor<L>::visit(const paragraph_set<L>& parset) const
199  {
200  const line_set<L>& lines = parset.lines();
201 
202  // Prepare paragraph outlines.
203  L par_clo = text::paragraphs_closing(parset);
204 
205  for_all_paragraphs(p, parset)
206  if (parset(p).is_valid())
207  {
208  const mln::util::array<line_id_t>& line_ids = parset(p).line_ids();
209 
210  for_all_paragraph_lines(lid, line_ids)
211  {
212  line_id_t l = line_ids(lid);
213  lines(l).accept(*this);
214  }
215 
216  // Adjust bbox to output image size.
217  box2d b = compute_bbox(parset(p).bbox());
218  b.enlarge(1);
219  b.crop_wrt(output.domain());
220  mln::draw::box(output, b, literal::blue);
221  }
222  }
223 
224 
225  template <typename L>
226  void
227  debug_img_visitor<L>::visit(const line_info<L>& line) const
228  {
229  point2d
230  pmin = line.bbox().pmin(),
231  pmax = line.bbox().pmax();
232  pmax.row() = line.baseline();
233  pmin.row() = line.baseline();
234 
235  pmin = pmin / output_ratio;
236  pmax = pmax / output_ratio;
237 
238  mln::draw::line(output, pmin, pmax, literal::red);
239  }
240 
241 #endif // MLN_INCLUDE_ONLY
242 
243  } // end of namespace scribo::io::img::internal
244 
245  } // end of namespace scribo::io::img
246 
247  } // end of namespace scribo::io
248 
249 } // end of namespace scribo
250 
251 #endif // SCRIBO_IO_IMG_INTERNAL_DEBUG_IMG_VISITOR_HH