$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
images_to_drop_capital.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_POSTPROCESSING_IMAGES_TO_DROP_CAPITAL_HH
27 # define SCRIBO_POSTPROCESSING_IMAGES_TO_DROP_CAPITAL_HH
28 
33 
34 
35 # include <mln/core/concept/image.hh>
36 # include <mln/draw/box_plain.hh>
37 # include <scribo/core/component_set.hh>
38 # include <scribo/core/document.hh>
39 
40 
41 namespace scribo
42 {
43 
44  namespace postprocessing
45  {
46 
47  using namespace mln;
48 
49 
54  //
55  template <typename L>
56  void
57  images_to_drop_capital(document<L>& doc);
58 
59 
60 # ifndef MLN_INCLUDE_ONLY
61 
62  template <typename L>
63  void
64  images_to_drop_capital(document<L>& doc)
65  {
66  mln_trace("scribo::postprocessing::images_to_drop_capital");
67 
68  mln_precondition(doc.is_valid());
69 
70  if (! doc.has_elements())
71  return;
72 
73  mln_ch_value(L,bool) billboard;
74  initialize(billboard, doc.image());
75  data::fill(billboard, false);
76 
77  for_all_comps(p, doc.paragraphs())
78  if (doc.paragraphs()(p).is_valid())
79  mln::draw::box_plain(billboard, doc.paragraphs()(p).bbox(), true);
80 
81  float min_img_size = 0.2 * (doc.image().domain().width()
82  + doc.image().domain().height());
83 
84  component_set<L> elts = doc.elements();
85  for_all_comps(c, elts)
86  if (elts(c).is_valid() && elts(c).type() == component::Image)
87  {
88  const mln_box(L)& b_ = elts(c).bbox();
89 
90  const bool tl = billboard(b_.pmin());
91  const bool tr = billboard.at_(b_.pmin().row(), b_.pmax().col());
92  const bool mb = billboard.at_(b_.pmax().row(), b_.pcenter().col());
93  const bool mc = billboard.at_(b_.pcenter().row(), b_.pcenter().col());
94  const bool mr = billboard.at_(b_.pcenter().row(), b_.pmax().col());
95  const bool bl = billboard.at_(b_.pmax().row(), b_.pmin().col());
96  const bool br = billboard(b_.pmax());
97 
98  typedef mln::util::set<int> set_t;
99  set_t s;
100  s.insert(tl);
101  s.insert(tr);
102  s.insert(mb);
103  s.insert(mc);
104  s.insert(mr);
105  s.insert(bl);
106  s.insert(br);
107 
108  if (s.nelements() > 2 || (s.nelements() == 2 && !s.has(0)))
109  continue;
110 
111  float elt_size = elts(c).bbox().width() + elts(c).bbox().height();
112  for_all_elements(e, s)
113  if (s[e] != 0
114  && (mc != 0 && mc == s[e]
115  && ((tl == mc && bl == mc)
116  || (tr == mc && br == mc)
117  || (bl == mc && br == mc)
118  || (tl == mc && tr == mc)
119  || (br == mc && mr == mc && mb == mc)))
120  && (elt_size < min_img_size))
121  {
122  elts(c).update_type(component::DropCapital);
123  break;
124  }
125  }
126 
127  // FIXME: warning this call may produce inconsistent data
128  // Ignored components are still in the separator image...
129  doc.set_elements(elts);
130 
131  }
132 
133 # endif // ! MLN_INCLUDE_ONLY
134 
135  } // end of namespace scribo::postprocessing
136 
137 } // end of namespace scribo
138 
139 #endif // ! SCRIBO_POSTPROCESSING_IMAGES_TO_DROP_CAPITAL_HH