$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
document.hh
1 // Copyright (C) 2010, 2011, 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_CORE_DOCUMENT_HH
28 # define SCRIBO_CORE_DOCUMENT_HH
29 
33 
34 # include <mln/core/image/image2d.hh>
35 # include <mln/core/alias/neighb2d.hh>
36 # include <mln/value/rgb8.hh>
37 
38 # ifndef SCRIBO_NMAGICK
39 # include <mln/io/magick/load.hh>
40 # endif // ! SCRIBO_NMAGICK
41 
42 # include <scribo/core/component_set.hh>
43 # include <scribo/core/line_set.hh>
44 # include <scribo/core/paragraph_set.hh>
45 
46 # include <scribo/core/concept/serializable.hh>
47 
48 # include <scribo/primitive/extract/components.hh>
49 
50 namespace scribo
51 {
52 
53  namespace internal
54  {
56  template <typename L>
58  {
59  document_data();
60  document_data(const char *filename);
61  document_data(const char *filename,
62  const mln::image2d<mln::value::rgb8>& input);
63 
64  std::string filename_;
66  mln::image2d<bool> binary_image_;
67  mln::image2d<bool> binary_image_wo_seps_;
68 
69  paragraph_set<L> parset_;
70  component_set<L> elements_;
71 
72  mln::image2d<bool> whitespace_seps_;
73  component_set<L> whitespace_seps_comps_;
74 
75  mln::image2d<bool> hline_seps_;
76  component_set<L> hline_seps_comps_;
77 
78  mln::image2d<bool> vline_seps_;
79  component_set<L> vline_seps_comps_;
80  };
81 
82  } // end of namespace scribo::internal
83 
84 
88  //
89  template <typename L>
90  class document : public Serializable<document<L> >
91  {
93 
94  public:
95 
96  document();
97  document(const char *filename);
98  document(const char *filename,
99  const mln::image2d<mln::value::rgb8>& input);
100 
101  /* FIXME: This is bad, as it changes the interface of the class.
102  But this is probably the smallest change to lift the dependency
103  of this class on Magick++. We shall probably get rid of this
104  open() method anyway. */
105 # ifndef SCRIBO_NMAGICK
106  void open();
107 # endif // ! SCRIBO_NMAGICK
108  bool is_open() const;
109 
110  const char * filename() const;
111  void set_filename(const char*name);
112 
113  bool is_valid() const;
114 
121  bool has_text() const;
122 
123  mln::def::coord height() const;
124  mln::def::coord width() const;
125 
126  const line_set<L>& lines() const;
127 
128  const paragraph_set<L>& paragraphs() const;
129  void set_paragraphs(const paragraph_set<L>& parset);
130 
131  bool has_elements() const;
132  const component_set<L>& elements() const;
133  void set_elements(const component_set<L>& elements);
134 
135  bool has_whitespace_seps() const;
136  const mln::image2d<bool>& whitespace_seps() const;
137  const component_set<L>& whitespace_seps_comps() const;
138  void set_whitespace_separators(const image2d<bool>& whitespace_seps,
139  const component_set<L>& whitespace_seps_comps);
140 
141  // Horizontal separators
142  bool has_hline_seps() const;
143  const mln::image2d<bool>& hline_seps() const;
144  const component_set<L>& hline_seps_comps() const;
145  // Set vline separators image. The component is automatically computed.
146  void set_hline_separators(const image2d<bool>& line_seps);
147  void set_hline_separators(const image2d<bool>& line_seps,
148  const component_set<L>& hline_seps_comps);
149 
150  // Vertical separators
151  bool has_vline_seps() const;
152  const mln::image2d<bool>& vline_seps() const;
153  const component_set<L>& vline_seps_comps() const;
154 
155  // Set vline separators image. The component is automatically computed.
156  void set_vline_separators(const image2d<bool>& vline_seps);
157  void set_vline_separators(const image2d<bool>& vline_seps,
158  const component_set<L>& vline_seps_comps);
159 
160  const mln::image2d<value::rgb8>& image() const;
161  void set_image(const mln::image2d<value::rgb8>& image);
162 
163  const mln::image2d<bool>& binary_image() const;
164  void set_binary_image(const mln::image2d<bool>& binary_image);
165 
166  const mln::image2d<bool>& binary_image_wo_seps() const;
167  void set_binary_image_wo_seps(const mln::image2d<bool>& binary_image_wo_seps);
168 
169  private:
171  };
172 
173 
174  template <typename L>
175  bool operator==(const document<L>& lhs, const document<L>& rhs);
176 
177 
178 # ifndef MLN_INCLUDE_ONLY
179 
180  namespace internal
181  {
182 
183  template <typename L>
184  document_data<L>::document_data()
185  {
186  }
187 
188  template <typename L>
189  document_data<L>::document_data(const char *filename,
190  const mln::image2d<mln::value::rgb8>& input)
191  : filename_(filename),
192  image_(input)
193  {
194  }
195 
196  template <typename L>
197  document_data<L>::document_data(const char *filename)
198  : filename_(filename)
199  {
200  }
201 
202 
203  } // end of namespace scribo::internal
204 
205 
206 
207  template <typename L>
208  document<L>::document()
209  : data_(0)
210  {
211  }
212 
213 
214  template <typename L>
215  document<L>::document(const char *filename)
216  {
217  data_ = new internal::document_data<L>(filename);
218  }
219 
220 
221  template <typename L>
222  document<L>::document(const char *filename,
223  const mln::image2d<mln::value::rgb8>& input)
224  {
225  data_ = new internal::document_data<L>(filename, input);
226  }
227 
228 
229  template <typename L>
230  const char *
231  document<L>::filename() const
232  {
233  mln_precondition(data_ != 0);
234  return data_->filename_.c_str();
235  }
236 
237 
238  template <typename L>
239  void
240  document<L>::set_filename(const char *filename)
241  {
242  mln_precondition(is_valid());
243  data_->filename_ = filename;
244  }
245 
246 
247 # ifndef SCRIBO_NMAGICK
248  template <typename L>
249  void
250  document<L>::open()
251  {
252  mln_precondition(data_ != 0);
253  mln::io::magick::load(data_->image_, data_->filename_);
254  }
255 # endif // ! SCRIBO_NMAGICK
256 
257 
258  template <typename L>
259  bool
260  document<L>::is_open() const
261  {
262  mln_precondition(data_ != 0);
263  return data_->image_.is_valid();
264  }
265 
266 
267  template <typename L>
268  bool
269  document<L>::is_valid() const
270  {
271  return data_ != 0 && data_->image_.is_valid();
272  }
273 
274 
275  template <typename L>
277  document<L>::width() const
278  {
279  mln_precondition(is_valid());
280  mln_precondition(data_->image_.is_valid());
281  return data_->image_.ncols();
282  }
283 
284 
285  template <typename L>
287  document<L>::height() const
288  {
289  mln_precondition(is_valid());
290  mln_precondition(data_->image_.is_valid());
291  return data_->image_.nrows();
292  }
293 
294 
295  template <typename L>
296  bool
297  document<L>::has_text() const
298  {
299  mln_precondition(is_valid());
300  return data_->parset_.is_valid();
301  }
302 
303 
304  template <typename L>
305  const line_set<L>&
306  document<L>::lines() const
307  {
308  mln_precondition(is_valid());
309  mln_precondition(data_->parset_.is_valid());
310  return data_->parset_.lines();
311  }
312 
313  template <typename L>
314  const paragraph_set<L>&
315  document<L>::paragraphs() const
316  {
317  mln_precondition(is_valid());
318  return data_->parset_;
319  }
320 
321 
322  template <typename L>
323  void
324  document<L>::set_paragraphs(const paragraph_set<L>& parset)
325  {
326  mln_precondition(is_valid());
327  mln_precondition(parset.is_valid());
328  data_->parset_ = parset;
329  }
330 
331 
332  template <typename L>
333  const component_set<L>&
334  document<L>::elements() const
335  {
336  mln_precondition(is_valid());
337  return data_->elements_;
338  }
339 
340 
341  template <typename L>
342  bool
343  document<L>::has_elements() const
344  {
345  mln_precondition(is_valid());
346  return data_->elements_.is_valid();
347  }
348 
349 
350  template <typename L>
351  void
352  document<L>::set_elements(const component_set<L>& elements)
353  {
354  mln_precondition(is_valid());
355  mln_precondition(elements.is_valid());
356  data_->elements_ = elements;
357  }
358 
359  template <typename L>
360  bool
361  document<L>::has_whitespace_seps() const
362  {
363  mln_precondition(is_valid());
364  return data_->whitespace_seps_.is_valid();
365  }
366 
367 
368  template <typename L>
369  const mln::image2d<bool>&
370  document<L>::whitespace_seps() const
371  {
372  mln_precondition(is_valid());
373  return data_->whitespace_seps_;
374  }
375 
376 
377  template <typename L>
378  const component_set<L>&
379  document<L>::whitespace_seps_comps() const
380  {
381  mln_precondition(is_valid());
382  return data_->whitespace_seps_comps_;
383  }
384 
385 
386  template <typename L>
387  void
388  document<L>::set_whitespace_separators(const image2d<bool>& whitespace_seps,
389  const component_set<L>& whitespace_seps_comps)
390  {
391  mln_precondition(is_valid());
392  mln_precondition(whitespace_seps.is_valid());
393  mln_precondition(whitespace_seps_comps.is_valid());
394  data_->whitespace_seps_ = whitespace_seps;
395  data_->whitespace_seps_comps_ = whitespace_seps_comps;
396  }
397 
398 
399  template <typename L>
400  bool
401  document<L>::has_hline_seps() const
402  {
403  mln_precondition(is_valid());
404  return data_->hline_seps_.is_valid();
405  }
406 
407 
408  template <typename L>
409  const mln::image2d<bool>&
410  document<L>::hline_seps() const
411  {
412  mln_precondition(is_valid());
413  return data_->hline_seps_;
414  }
415 
416 
417  template <typename L>
418  const component_set<L>&
419  document<L>::hline_seps_comps() const
420  {
421  mln_precondition(is_valid());
422  return data_->hline_seps_comps_;
423  }
424 
425 
426  template <typename L>
427  void
428  document<L>::set_hline_separators(const image2d<bool>& hline_seps)
429  {
430  mln_precondition(is_valid());
431  mln_precondition(hline_seps.is_valid());
432  data_->hline_seps_ = hline_seps;
433 
434  mln_value(L) ncomps;
435  data_->hline_seps_comps_ = primitive::extract::components(
436  hline_seps,
437  mln::c8(), ncomps,
438  component::HorizontalLineSeparator);
439  }
440 
441 
442  template <typename L>
443  void
444  document<L>::set_hline_separators(const image2d<bool>& hline_seps,
445  const component_set<L>& hline_seps_comps)
446  {
447  mln_precondition(is_valid());
448  mln_precondition(hline_seps.is_valid());
449  mln_precondition(hline_seps_comps.is_valid());
450  data_->hline_seps_ = hline_seps;
451  data_->hline_seps_comps_ = hline_seps_comps;
452  }
453 
454 
455  template <typename L>
456  bool
457  document<L>::has_vline_seps() const
458  {
459  mln_precondition(is_valid());
460  return data_->vline_seps_.is_valid();
461  }
462 
463 
464  template <typename L>
465  const mln::image2d<bool>&
466  document<L>::vline_seps() const
467  {
468  mln_precondition(is_valid());
469  return data_->vline_seps_;
470  }
471 
472 
473  template <typename L>
474  const component_set<L>&
475  document<L>::vline_seps_comps() const
476  {
477  mln_precondition(is_valid());
478  return data_->vline_seps_comps_;
479  }
480 
481 
482  template <typename L>
483  void
484  document<L>::set_vline_separators(const image2d<bool>& vline_seps)
485  {
486  mln_precondition(is_valid());
487  mln_precondition(vline_seps.is_valid());
488  data_->vline_seps_ = vline_seps;
489 
490  mln_value(L) ncomps;
491  data_->vline_seps_comps_ = primitive::extract::components(
492  vline_seps,
493  mln::c8(), ncomps,
494  component::VerticalLineSeparator);
495  }
496 
497 
498  template <typename L>
499  void
500  document<L>::set_vline_separators(const image2d<bool>& vline_seps,
501  const component_set<L>& vline_seps_comps)
502  {
503  mln_precondition(is_valid());
504  mln_precondition(vline_seps.is_valid());
505  mln_precondition(vline_seps_comps.is_valid());
506  data_->vline_seps_ = vline_seps;
507  data_->vline_seps_comps_ = vline_seps_comps;
508  }
509 
510 
511  template <typename L>
513  document<L>::image() const
514  {
515  mln_precondition(is_valid());
516  return data_->image_;
517  }
518 
519 
520  template <typename L>
521  void
522  document<L>::set_image(const mln::image2d<value::rgb8>& image)
523  {
524  mln_precondition(data_ != 0);
525  mln_precondition(image.is_valid());
526  data_->image_ = image;
527  }
528 
529 
530  template <typename L>
531  const mln::image2d<bool>&
532  document<L>::binary_image() const
533  {
534  mln_precondition(is_valid());
535  return data_->binary_image_;
536  }
537 
538 
539  template <typename L>
540  void
541  document<L>::set_binary_image(const mln::image2d<bool>& binary_image)
542  {
543  mln_precondition(is_valid());
544  mln_precondition(binary_image.is_valid());
545  data_->binary_image_ = binary_image;
546  }
547 
548 
549  template <typename L>
550  const mln::image2d<bool>&
551  document<L>::binary_image_wo_seps() const
552  {
553  mln_precondition(is_valid());
554  return data_->binary_image_wo_seps_;
555  }
556 
557 
558  template <typename L>
559  void
560  document<L>::set_binary_image_wo_seps(
561  const mln::image2d<bool>& binary_image_wo_seps)
562  {
563  mln_precondition(is_valid());
564  mln_precondition(binary_image_wo_seps.is_valid());
565  data_->binary_image_wo_seps_ = binary_image_wo_seps;
566  }
567 
568 
569  template <typename L>
570  bool operator==(const document<L>& lhs, const document<L>& rhs)
571  {
572 
573 
574  return
575  lhs.filename() == rhs.filename()
576  && lhs.image() == rhs.image()
577  && lhs.binary_image() == rhs.binary_image()
578  && lhs.has_text() == rhs.has_text()
579  && lhs.paragraphs() == rhs.paragraphs()
580  && lhs.has_elements() == rhs.has_elements()
581  && lhs.elements() == rhs.elements()
582  && lhs.has_whitespace_seps() == rhs.has_whitespace_seps()
583  && lhs.whitespace_seps() == rhs.whitespace_seps()
584  && lhs.whitespace_seps_comps() == rhs.whitespace_seps_comps()
585  && lhs.has_hline_seps() == rhs.has_hline_seps()
586  && lhs.hline_seps() == rhs.hline_seps()
587  && lhs.hline_seps_comps() == rhs.hline_seps_comps()
588  && lhs.has_vline_seps() == rhs.has_vline_seps()
589  && lhs.vline_seps() == rhs.vline_seps()
590  && lhs.vline_seps_comps() == rhs.vline_seps_comps();
591  }
592 
593 # endif // ! MLN_INCLUDE_ONLY
594 
595 
596 } // end of namespace scribo
597 
598 
599 #endif // ! SCRIBO_CORE_DOCUMENT_HH