27 #ifndef SCRIBO_TEXT_PARAGRAPHS_CLOSING_HH
28 # define SCRIBO_TEXT_PARAGRAPHS_CLOSING_HH
34 # include <mln/draw/line.hh>
36 # include <scribo/core/paragraph_set.hh>
37 # include <scribo/draw/line_components.hh>
38 # include <scribo/debug/logger.hh>
50 paragraphs_closing(const paragraph_set<L>& parset);
53 # ifndef MLN_INCLUDE_ONLY
60 void horizontal_CRLA(
const Image<L>& input_,
64 const L& input =
exact(input_);
65 L& output =
exact(output_);
66 mln_precondition(input.is_valid());
67 mln_precondition(output.is_valid());
69 mln_piter(L) p(input.domain());
73 mln_value(L) last_pixel_value = 0;
75 for (
unsigned i = 0; i < nrows; ++i)
77 for (
unsigned j = 0; j <
ncols; ++j)
79 const mln_value(L)& current_pixel = input.at_(i, j);
86 for ( ; !(input.at_(i, k)) && (k < ncols); ++k)
90 const int threshold = deltas(last_pixel_value);
92 if (last_pixel_value == input.at_(i, k) && count <
threshold)
93 for (
unsigned l = j; l <= k; ++l)
94 output.at_(i, l) = last_pixel_value;
102 output.at_(i, j) = current_pixel;
103 last_pixel_value = current_pixel;
111 void vertical_CRLA(
const Image<L>& input_,
115 const L& input =
exact(input_);
116 L& output =
exact(output_);
117 mln_precondition(input.is_valid());
118 mln_precondition(output.is_valid());
120 mln_piter(L) p(input.domain());
122 unsigned nrows = input.nrows();
123 unsigned ncols = input.ncols();
124 mln_value(L) last_pixel_value = 0;
126 for (
unsigned j = 0; j < ncols; ++j)
128 for (
unsigned i = 0; i <
nrows; ++i)
130 const mln_value(L)& current_pixel = input.at_(i, j);
134 if (last_pixel_value)
137 for ( ; !(input.at_(k, j)) && (k < nrows); ++k)
141 const int threshold = deltas(last_pixel_value);
143 if (last_pixel_value == input.at_(k, j)
145 for (
unsigned l = i; l <= k; ++l)
146 output.at_(l, j) = last_pixel_value;
149 last_pixel_value = 0;
154 output.at_(i, j) = current_pixel;
155 last_pixel_value = current_pixel;
168 horizontal_CRLA(input, output, deltas_factor);
172 "paragraph_closing_horizontal_CRLA");
175 vertical_CRLA(output, output, deltas);
179 "paragraph_closing_vertical_CRLA");
181 horizontal_CRLA(output, output, deltas_factor);
189 paragraphs_closing(const paragraph_set<L>& parset)
191 mln_trace(
"scribo::text::paragraphs_closing");
196 initialize(debug, output);
198 mln::util::array<
int> deltas(parset.nelements() + 1, 0);
199 mln::util::array<
int> deltas_factor(parset.nelements() + 1, 0);
204 const line_set<L>& lines = parset.lines();
206 for_all_paragraphs(p, parset)
207 if (parset(p).is_valid())
209 const paragraph_info<L>& current_par = parset(p);
212 for_all_elements(i, line_ids)
214 const line_id_t& line_id = line_ids(i);
215 const line_info<L>& current_line = lines(line_id);
217 scribo::draw::line_components(debug, lines, current_line, p);
225 for_all_elements(i, lines(line_id).component_ids())
227 const unsigned c = lines(line_id).component_ids()(i);
229 lines.components()(c).mass_center(),
230 lines.components()(last_comp).mass_center(),
237 int delta_baseline = current_par.delta_baseline();
239 if (delta_baseline % 2 == 0)
242 deltas(p) = 2 * delta_baseline;
243 deltas_factor(p) = 3 * delta_baseline;
248 "paragraph_closing_input_CRLA");
250 internal::CRLA(debug, output, deltas, deltas_factor);
254 "paragraph_closing");
266 #endif // ! SCRIBO_TEXT_PARAGRAPHS_CLOSING_HH