73 int AlignedBlob::debug_pix_index_ = 0;
81 snprintf(numbuf,
sizeof(numbuf),
"%d", debug_pix_index_);
82 textord_debug_pix_ += numbuf;
83 textord_debug_pix_ +=
".pix";
94 int height,
int v_gap_multiple,
136 : gutter_fraction(0.0),
165 :
BlobGrid(gridsize, bleft, tright) {
183 #ifndef GRAPHICS_DISABLED 192 int left_x = box.
left();
193 int right_x = box.
right();
194 int top_y = box.
top();
195 int bottom_y = box.
bottom();
206 tab_win->
Line(left_x, top_y, left_x, bottom_y);
218 tab_win->
Line(right_x, top_y, right_x, bottom_y);
228 static bool AtLeast2LineCrossings(BLOBNBOX_CLIST* blobs) {
229 BLOBNBOX_C_IT it(blobs);
230 int total_crossings = 0;
231 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
232 total_crossings += it.data()->line_crossings();
234 return total_crossings >= 2;
247 int ext_start_y, ext_end_y;
248 BLOBNBOX_CLIST good_points;
252 int pt_count = AlignTabs(align_params,
false, bbox, &good_points, &ext_end_y);
253 pt_count += AlignTabs(align_params,
true, bbox, &good_points, &ext_start_y);
254 BLOBNBOX_C_IT it(&good_points);
256 box = it.data()->bounding_box();
257 int end_y = box.
top();
260 box = it.data()->bounding_box();
262 int start_y = box.
bottom();
268 bool at_least_2_crossings = AtLeast2LineCrossings(&good_points);
273 at_least_2_crossings) {
274 int confirmed_points = 0;
276 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
287 if (!align_params.
ragged ||
288 confirmed_points + confirmed_points < pt_count) {
291 tprintf(
"Confirming tab vector of %d pts starting at %d,%d\n",
295 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
309 ext_start_y, ext_end_y,
311 vertical_x, vertical_y);
315 result->
Print(
"After fitting");
319 tprintf(
"Ragged tab used too many used points: %d out of %d\n",
320 confirmed_points, pt_count);
323 tprintf(
"Tab vector failed basic tests: pt count %d vs min %d, " 324 "length %d vs min %d, min grad %g\n",
325 pt_count, align_params.
min_points, end_y - start_y,
337 BLOBNBOX_CLIST* good_points,
int* end_y) {
339 BLOBNBOX_C_IT it(good_points);
344 tprintf(
"Starting alignment run at blob:");
348 while (bbox != NULL) {
354 (it.empty() || it.data() != bbox)) {
356 it.add_before_then_move(bbox);
358 it.add_after_then_move(bbox);
365 bbox = FindAlignedBlob(params, top_to_bottom, bbox, x_start, end_y);
373 tprintf(
"Alignment run ended with %d pts at blob:", ptcount);
386 BLOBNBOX* AlignedBlob::FindAlignedBlob(
const AlignedBlobParams& p,
388 int x_start,
int* end_y) {
391 int left_column_edge = bbox->
left_rule();
396 int start_y = top_to_bottom ? box.
bottom() : box.
top();
398 tprintf(
"Column edges for blob at (%d,%d)->(%d,%d) are [%d, %d]\n",
400 left_column_edge, right_column_edge);
408 int x2 = (p.max_v_gap * p.vertical.x() + p.vertical.y()/2) / p.vertical.y();
411 *end_y = start_y - p.max_v_gap;
414 *end_y = start_y + p.max_v_gap;
417 int xmin =
MIN(x_start, x2) - skew_tolerance;
418 int xmax =
MAX(x_start, x2) + skew_tolerance;
421 xmax += p.min_gutter;
422 xmin -= p.l_align_tolerance;
424 xmax += p.r_align_tolerance;
425 xmin -= p.min_gutter;
428 GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> vsearch(
this);
430 tprintf(
"Starting %s %s search at %d-%d,%d, search_size=%d, gutter=%d\n",
431 p.ragged ?
"Ragged" :
"Aligned", p.right_tab ?
"Right" :
"Left",
432 xmin, xmax, start_y, p.max_v_gap, p.min_gutter);
433 vsearch.StartVerticalSearch(xmin, xmax, start_y);
441 while ((neighbour = vsearch.NextVerticalSearch(top_to_bottom)) != NULL) {
442 if (neighbour == bbox)
445 int n_y = (nbox.
top() + nbox.
bottom()) / 2;
446 if ((!top_to_bottom && n_y > start_y + p.max_v_gap) ||
447 (top_to_bottom && n_y < start_y - p.max_v_gap)) {
449 tprintf(
"Neighbour too far at (%d,%d)->(%d,%d)\n",
458 if ((n_y < start_y) != top_to_bottom || nbox.
y_overlap(box))
462 if (backup_result != NULL && p.ragged && result == NULL &&
464 return backup_result;
468 int x_at_n_y = x_start + (n_y - start_y) * p.vertical.x() / p.vertical.y();
469 if (x_at_n_y < neighbour->left_crossing_rule() ||
472 int n_left = nbox.
left();
473 int n_right = nbox.
right();
474 int n_x = p.right_tab ? n_right : n_left;
476 tprintf(
"neighbour at (%d,%d)->(%d,%d), n_x=%d, n_y=%d, xatn=%d\n",
480 n_left < x_at_n_y + p.min_gutter &&
481 n_right > x_at_n_y + p.r_align_tolerance &&
482 (p.ragged || n_left < x_at_n_y + p.gutter_fraction * nbox.
height())) {
486 *end_y = top_to_bottom ? nbox.
top() : nbox.
bottom();
492 n_left < x_at_n_y - p.l_align_tolerance &&
493 n_right > x_at_n_y - p.min_gutter &&
494 (p.ragged || n_right > x_at_n_y - p.gutter_fraction * nbox.
height())) {
498 *end_y = top_to_bottom ? nbox.
top() : nbox.
bottom();
506 if (n_x <= x_at_n_y + p.r_align_tolerance &&
507 n_x >= x_at_n_y - p.l_align_tolerance) {
511 tprintf(
"aligned, seeking%d, l=%d, r=%d\n",
517 if (result == NULL) {
523 int x_diff = p.right_tab ? old_box.
right() : old_box.
left();
525 int y_diff = (old_box.
top() + old_box.
bottom()) / 2 - start_y;
526 int old_dist = x_diff * x_diff + y_diff * y_diff;
527 x_diff = n_x - x_at_n_y;
528 y_diff = n_y - start_y;
529 int new_dist = x_diff * x_diff + y_diff * y_diff;
530 if (new_dist < old_dist)
533 }
else if (backup_result == NULL) {
536 backup_result = neighbour;
539 if ((p.right_tab && backup_box.
right() < nbox.
right()) ||
540 (!p.right_tab && backup_box.
left() > nbox.
left())) {
543 backup_result = neighbour;
548 return result != NULL ? result : backup_result;
bool leader_on_left() const
const TBOX & bounding_box() const
bool y_overlap(const TBOX &box) const
const double kAlignedFraction
int textord_testregion_left
const double kRaggedFraction
void set_intersects_other_lines(bool value)
bool textord_debug_images
static bool WithinTestRegion(int detail_level, int x, int y)
const int kVLineMinLength
const double kRaggedGapFraction
void Line(int x1, int y1, int x2, int y2)
static void IncrementDebugPix()
const double kAlignedGapFraction
TabType right_tab_type() const
const double kMinTabGradient
bool leader_on_right() const
int y_gap(const TBOX &box) const
ScrollView * MakeWindow(int x, int y, const char *window_name)
void set_vertical(int vertical_x, int vertical_y)
int right_crossing_rule() const
const int kVLineSearchSize
void set_right_tab_type(TabType new_type)
void Print(const char *prefix)
static TabVector * FitVector(TabAlignment alignment, ICOORD vertical, int extended_start_y, int extended_end_y, BLOBNBOX_CLIST *good_points, int *vertical_x, int *vertical_y)
bool textord_debug_printable
int textord_testregion_bottom
int textord_debug_tabfind
const int kVLineAlignment
const char * kTextordDebugPix
void set_left_tab_type(TabType new_type)
const int kMinAlignedTabs
TabVector * FindVerticalAlignment(AlignedBlobParams align_params, BLOBNBOX *bbox, int *vertical_x, int *vertical_y)
void set_y(inT16 yin)
rewrite function
void set_x(inT16 xin)
rewrite function
int textord_testregion_right
#define BOOL_VAR(name, val, comment)
AlignedBlob(int gridsize, const ICOORD &bleft, const ICOORD &tright)
int textord_testregion_top
TabType left_tab_type() const
ScrollView * DisplayTabs(const char *window_name, ScrollView *tab_win)
#define INT_VAR(name, val, comment)
AlignedBlobParams(int vertical_x, int vertical_y, int height, int v_gap_multiple, int min_gutter_width, int resolution, TabAlignment alignment0)