tesseract  3.05.02
tabfind.cpp
Go to the documentation of this file.
1 // File: TabFind.cpp
3 // Description: Subclass of BBGrid to find vertically aligned blobs.
4 // Author: Ray Smith
5 // Created: Fri Mar 21 15:03:01 PST 2008
6 //
7 // (C) Copyright 2008, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #ifdef HAVE_CONFIG_H
21 #include "config_auto.h"
22 #endif
23 
24 #include "tabfind.h"
25 #include "alignedblob.h"
26 #include "blobbox.h"
27 #include "colpartitiongrid.h"
28 #include "detlinefit.h"
29 #include "linefind.h"
30 #include "ndminx.h"
31 
32 namespace tesseract {
33 
34 // Multiple of box size to search for initial gaps.
35 const int kTabRadiusFactor = 5;
36 // Min and Max multiple of height to search vertically when extrapolating.
37 const int kMinVerticalSearch = 3;
38 const int kMaxVerticalSearch = 12;
39 const int kMaxRaggedSearch = 25;
40 // Minimum number of lines in a column width to make it interesting.
41 const int kMinLinesInColumn = 10;
42 // Minimum width of a column to be interesting.
43 const int kMinColumnWidth = 200;
44 // Minimum fraction of total column lines for a column to be interesting.
45 const double kMinFractionalLinesInColumn = 0.125;
46 // Fraction of height used as alignment tolerance for aligned tabs.
47 const double kAlignedFraction = 0.03125;
48 // Maximum gutter width (in absolute inch) that we care about
49 const double kMaxGutterWidthAbsolute = 2.00;
50 // Multiplier of gridsize for min gutter width of TT_MAYBE_RAGGED blobs.
51 const int kRaggedGutterMultiple = 5;
52 // Min aspect ratio of tall objects to be considered a separator line.
53 // (These will be ignored in searching the gutter for obstructions.)
54 const double kLineFragmentAspectRatio = 10.0;
55 // Min number of points to accept after evaluation.
56 const int kMinEvaluatedTabs = 3;
57 // Up to 30 degrees is allowed for rotations of diacritic blobs.
58 // Keep this value slightly larger than kCosSmallAngle in blobbox.cpp
59 // so that the assert there never fails.
60 const double kCosMaxSkewAngle = 0.866025;
61 
62 BOOL_VAR(textord_tabfind_show_initialtabs, false, "Show tab candidates");
63 BOOL_VAR(textord_tabfind_show_finaltabs, false, "Show tab vectors");
64 
65 TabFind::TabFind(int gridsize, const ICOORD& bleft, const ICOORD& tright,
66  TabVector_LIST* vlines, int vertical_x, int vertical_y,
67  int resolution)
68  : AlignedBlob(gridsize, bleft, tright),
69  resolution_(resolution),
70  image_origin_(0, tright.y() - 1) {
71  width_cb_ = NULL;
72  v_it_.set_to_list(&vectors_);
73  v_it_.add_list_after(vlines);
74  SetVerticalSkewAndParellelize(vertical_x, vertical_y);
76 }
77 
79  if (width_cb_ != NULL)
80  delete width_cb_;
81 }
82 
84 
85 // Insert a list of blobs into the given grid (not necessarily this).
86 // If take_ownership is true, then the blobs are removed from the source list.
87 // See InsertBlob for the other arguments.
88 // It would seem to make more sense to swap this and grid, but this way
89 // around allows grid to not be derived from TabFind, eg a ColPartitionGrid,
90 // while the grid that provides the tab stops(this) has to be derived from
91 // TabFind.
92 void TabFind::InsertBlobsToGrid(bool h_spread, bool v_spread,
93  BLOBNBOX_LIST* blobs,
94  BBGrid<BLOBNBOX, BLOBNBOX_CLIST,
95  BLOBNBOX_C_IT>* grid) {
96  BLOBNBOX_IT blob_it(blobs);
97  int b_count = 0;
98  int reject_count = 0;
99  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
100  BLOBNBOX* blob = blob_it.data();
101 // if (InsertBlob(true, true, blob, grid)) {
102  if (InsertBlob(h_spread, v_spread, blob, grid)) {
103  ++b_count;
104  } else {
105  ++reject_count;
106  }
107  }
108  if (textord_debug_tabfind) {
109  tprintf("Inserted %d blobs into grid, %d rejected.\n",
110  b_count, reject_count);
111  }
112 }
113 
114 // Insert a single blob into the given grid (not necessarily this).
115 // If h_spread, then all cells covered horizontally by the box are
116 // used, otherwise, just the bottom-left. Similarly for v_spread.
117 // A side effect is that the left and right rule edges of the blob are
118 // set according to the tab vectors in this (not grid).
119 bool TabFind::InsertBlob(bool h_spread, bool v_spread, BLOBNBOX* blob,
120  BBGrid<BLOBNBOX, BLOBNBOX_CLIST,
121  BLOBNBOX_C_IT>* grid) {
122  TBOX box = blob->bounding_box();
123  blob->set_left_rule(LeftEdgeForBox(box, false, false));
124  blob->set_right_rule(RightEdgeForBox(box, false, false));
125  blob->set_left_crossing_rule(LeftEdgeForBox(box, true, false));
126  blob->set_right_crossing_rule(RightEdgeForBox(box, true, false));
127  if (blob->joined_to_prev())
128  return false;
129  grid->InsertBBox(h_spread, v_spread, blob);
130  return true;
131 }
132 
133 // Calls SetBlobRuleEdges for all the blobs in the given block.
135  SetBlobRuleEdges(&block->blobs);
136  SetBlobRuleEdges(&block->small_blobs);
137  SetBlobRuleEdges(&block->noise_blobs);
138  SetBlobRuleEdges(&block->large_blobs);
139 }
140 
141 // Sets the left and right rule and crossing_rules for the blobs in the given
142 // list by fiding the next outermost tabvectors for each blob.
143 void TabFind::SetBlobRuleEdges(BLOBNBOX_LIST* blobs) {
144  BLOBNBOX_IT blob_it(blobs);
145  for (blob_it.mark_cycle_pt(); !blob_it.cycled_list(); blob_it.forward()) {
146  BLOBNBOX* blob = blob_it.data();
147  TBOX box = blob->bounding_box();
148  blob->set_left_rule(LeftEdgeForBox(box, false, false));
149  blob->set_right_rule(RightEdgeForBox(box, false, false));
150  blob->set_left_crossing_rule(LeftEdgeForBox(box, true, false));
151  blob->set_right_crossing_rule(RightEdgeForBox(box, true, false));
152  }
153 }
154 
155 // Returns the gutter width of the given TabVector between the given y limits.
156 // Also returns x-shift to be added to the vector to clear any intersecting
157 // blobs. The shift is deducted from the returned gutter.
158 // If ignore_unmergeables is true, then blobs of UnMergeableType are
159 // ignored as if they don't exist. (Used for text on image.)
160 // max_gutter_width is used as the maximum width worth searching for in case
161 // there is nothing near the TabVector.
162 int TabFind::GutterWidth(int bottom_y, int top_y, const TabVector& v,
163  bool ignore_unmergeables, int max_gutter_width,
164  int* required_shift) {
165  bool right_to_left = v.IsLeftTab();
166  int bottom_x = v.XAtY(bottom_y);
167  int top_x = v.XAtY(top_y);
168  int start_x = right_to_left ? MAX(top_x, bottom_x) : MIN(top_x, bottom_x);
169  BlobGridSearch sidesearch(this);
170  sidesearch.StartSideSearch(start_x, bottom_y, top_y);
171  int min_gap = max_gutter_width;
172  *required_shift = 0;
173  BLOBNBOX* blob = NULL;
174  while ((blob = sidesearch.NextSideSearch(right_to_left)) != NULL) {
175  const TBOX& box = blob->bounding_box();
176  if (box.bottom() >= top_y || box.top() <= bottom_y)
177  continue; // Doesn't overlap enough.
178  if (box.height() >= gridsize() * 2 &&
179  box.height() > box.width() * kLineFragmentAspectRatio) {
180  // Skip likely separator line residue.
181  continue;
182  }
183  if (ignore_unmergeables && BLOBNBOX::UnMergeableType(blob->region_type()))
184  continue; // Skip non-text if required.
185  int mid_y = (box.bottom() + box.top()) / 2;
186  // We use the x at the mid-y so that the required_shift guarantees
187  // to clear all the blobs on the tab-stop. If we use the min/max
188  // of x at top/bottom of the blob, then exactness would be required,
189  // which is not a good thing.
190  int tab_x = v.XAtY(mid_y);
191  int gap;
192  if (right_to_left) {
193  gap = tab_x - box.right();
194  if (gap < 0 && box.left() - tab_x < *required_shift)
195  *required_shift = box.left() - tab_x;
196  } else {
197  gap = box.left() - tab_x;
198  if (gap < 0 && box.right() - tab_x > *required_shift)
199  *required_shift = box.right() - tab_x;
200  }
201  if (gap > 0 && gap < min_gap)
202  min_gap = gap;
203  }
204  // Result may be negative, in which case, this is a really bad tabstop.
205  return min_gap - abs(*required_shift);
206 }
207 
208 // Find the gutter width and distance to inner neighbour for the given blob.
209 void TabFind::GutterWidthAndNeighbourGap(int tab_x, int mean_height,
210  int max_gutter, bool left,
211  BLOBNBOX* bbox, int* gutter_width,
212  int* neighbour_gap ) {
213  const TBOX& box = bbox->bounding_box();
214  // The gutter and internal sides of the box.
215  int gutter_x = left ? box.left() : box.right();
216  int internal_x = left ? box.right() : box.left();
217  // On ragged edges, the gutter side of the box is away from the tabstop.
218  int tab_gap = left ? gutter_x - tab_x : tab_x - gutter_x;
219  *gutter_width = max_gutter;
220  // If the box is away from the tabstop, we need to increase
221  // the allowed gutter width.
222  if (tab_gap > 0)
223  *gutter_width += tab_gap;
224  bool debug = WithinTestRegion(2, box.left(), box.bottom());
225  if (debug)
226  tprintf("Looking in gutter\n");
227  // Find the nearest blob on the outside of the column.
228  BLOBNBOX* gutter_bbox = AdjacentBlob(bbox, left,
229  bbox->flow() == BTFT_TEXT_ON_IMAGE, 0.0,
230  *gutter_width, box.top(), box.bottom());
231  if (gutter_bbox != NULL) {
232  const TBOX& gutter_box = gutter_bbox->bounding_box();
233  *gutter_width = left ? tab_x - gutter_box.right()
234  : gutter_box.left() - tab_x;
235  }
236  if (*gutter_width >= max_gutter) {
237  // If there is no box because a tab was in the way, get the tab coord.
238  TBOX gutter_box(box);
239  if (left) {
240  gutter_box.set_left(tab_x - max_gutter - 1);
241  gutter_box.set_right(tab_x - max_gutter);
242  int tab_gutter = RightEdgeForBox(gutter_box, true, false);
243  if (tab_gutter < tab_x - 1)
244  *gutter_width = tab_x - tab_gutter;
245  } else {
246  gutter_box.set_left(tab_x + max_gutter);
247  gutter_box.set_right(tab_x + max_gutter + 1);
248  int tab_gutter = LeftEdgeForBox(gutter_box, true, false);
249  if (tab_gutter > tab_x + 1)
250  *gutter_width = tab_gutter - tab_x;
251  }
252  }
253  if (*gutter_width > max_gutter)
254  *gutter_width = max_gutter;
255  // Now look for a neighbour on the inside.
256  if (debug)
257  tprintf("Looking for neighbour\n");
258  BLOBNBOX* neighbour = AdjacentBlob(bbox, !left,
259  bbox->flow() == BTFT_TEXT_ON_IMAGE, 0.0,
260  *gutter_width, box.top(), box.bottom());
261  int neighbour_edge = left ? RightEdgeForBox(box, true, false)
262  : LeftEdgeForBox(box, true, false);
263  if (neighbour != NULL) {
264  const TBOX& n_box = neighbour->bounding_box();
265  if (debug) {
266  tprintf("Found neighbour:");
267  n_box.print();
268  }
269  if (left && n_box.left() < neighbour_edge)
270  neighbour_edge = n_box.left();
271  else if (!left && n_box.right() > neighbour_edge)
272  neighbour_edge = n_box.right();
273  }
274  *neighbour_gap = left ? neighbour_edge - internal_x
275  : internal_x - neighbour_edge;
276 }
277 
278 // Return the x-coord that corresponds to the right edge for the given
279 // box. If there is a rule line to the right that vertically overlaps it,
280 // then return the x-coord of the rule line, otherwise return the right
281 // edge of the page. For details see RightTabForBox below.
282 int TabFind::RightEdgeForBox(const TBOX& box, bool crossing, bool extended) {
283  TabVector* v = RightTabForBox(box, crossing, extended);
284  return v == NULL ? tright_.x() : v->XAtY((box.top() + box.bottom()) / 2);
285 }
286 // As RightEdgeForBox, but finds the left Edge instead.
287 int TabFind::LeftEdgeForBox(const TBOX& box, bool crossing, bool extended) {
288  TabVector* v = LeftTabForBox(box, crossing, extended);
289  return v == NULL ? bleft_.x() : v->XAtY((box.top() + box.bottom()) / 2);
290 }
291 
292 // This comment documents how this function works.
293 // For its purpose and arguments, see the comment in tabfind.h.
294 // TabVectors are stored sorted by perpendicular distance of middle from
295 // the global mean vertical vector. Since the individual vectors can have
296 // differing directions, their XAtY for a given y is not necessarily in the
297 // right order. Therefore the search has to be run with a margin.
298 // The middle of a vector that passes through (x,y) cannot be higher than
299 // halfway from y to the top, or lower than halfway from y to the bottom
300 // of the coordinate range; therefore, the search margin is the range of
301 // sort keys between these halfway points. Any vector with a sort key greater
302 // than the upper margin must be to the right of x at y, and likewise any
303 // vector with a sort key less than the lower margin must pass to the left
304 // of x at y.
305 TabVector* TabFind::RightTabForBox(const TBOX& box, bool crossing,
306  bool extended) {
307  if (v_it_.empty())
308  return NULL;
309  int top_y = box.top();
310  int bottom_y = box.bottom();
311  int mid_y = (top_y + bottom_y) / 2;
312  int right = crossing ? (box.left() + box.right()) / 2 : box.right();
313  int min_key, max_key;
314  SetupTabSearch(right, mid_y, &min_key, &max_key);
315  // Position the iterator at the first TabVector with sort_key >= min_key.
316  while (!v_it_.at_first() && v_it_.data()->sort_key() >= min_key)
317  v_it_.backward();
318  while (!v_it_.at_last() && v_it_.data()->sort_key() < min_key)
319  v_it_.forward();
320  // Find the leftmost tab vector that overlaps and has XAtY(mid_y) >= right.
321  TabVector* best_v = NULL;
322  int best_x = -1;
323  int key_limit = -1;
324  do {
325  TabVector* v = v_it_.data();
326  int x = v->XAtY(mid_y);
327  if (x >= right &&
328  (v->VOverlap(top_y, bottom_y) > 0 ||
329  (extended && v->ExtendedOverlap(top_y, bottom_y) > 0))) {
330  if (best_v == NULL || x < best_x) {
331  best_v = v;
332  best_x = x;
333  // We can guarantee that no better vector can be found if the
334  // sort key exceeds that of the best by max_key - min_key.
335  key_limit = v->sort_key() + max_key - min_key;
336  }
337  }
338  // Break when the search is done to avoid wrapping the iterator and
339  // thereby potentially slowing the next search.
340  if (v_it_.at_last() ||
341  (best_v != NULL && v->sort_key() > key_limit))
342  break; // Prevent restarting list for next call.
343  v_it_.forward();
344  } while (!v_it_.at_first());
345  return best_v;
346 }
347 
348 // As RightTabForBox, but finds the left TabVector instead.
349 TabVector* TabFind::LeftTabForBox(const TBOX& box, bool crossing,
350  bool extended) {
351  if (v_it_.empty())
352  return NULL;
353  int top_y = box.top();
354  int bottom_y = box.bottom();
355  int mid_y = (top_y + bottom_y) / 2;
356  int left = crossing ? (box.left() + box.right()) / 2 : box.left();
357  int min_key, max_key;
358  SetupTabSearch(left, mid_y, &min_key, &max_key);
359  // Position the iterator at the last TabVector with sort_key <= max_key.
360  while (!v_it_.at_last() && v_it_.data()->sort_key() <= max_key)
361  v_it_.forward();
362  while (!v_it_.at_first() && v_it_.data()->sort_key() > max_key) {
363  v_it_.backward();
364  }
365  // Find the rightmost tab vector that overlaps and has XAtY(mid_y) <= left.
366  TabVector* best_v = NULL;
367  int best_x = -1;
368  int key_limit = -1;
369  do {
370  TabVector* v = v_it_.data();
371  int x = v->XAtY(mid_y);
372  if (x <= left &&
373  (v->VOverlap(top_y, bottom_y) > 0 ||
374  (extended && v->ExtendedOverlap(top_y, bottom_y) > 0))) {
375  if (best_v == NULL || x > best_x) {
376  best_v = v;
377  best_x = x;
378  // We can guarantee that no better vector can be found if the
379  // sort key is less than that of the best by max_key - min_key.
380  key_limit = v->sort_key() - (max_key - min_key);
381  }
382  }
383  // Break when the search is done to avoid wrapping the iterator and
384  // thereby potentially slowing the next search.
385  if (v_it_.at_first() ||
386  (best_v != NULL && v->sort_key() < key_limit))
387  break; // Prevent restarting list for next call.
388  v_it_.backward();
389  } while (!v_it_.at_last());
390  return best_v;
391 }
392 
393 // Return true if the given width is close to one of the common
394 // widths in column_widths_.
395 bool TabFind::CommonWidth(int width) {
396  width /= kColumnWidthFactor;
397  ICOORDELT_IT it(&column_widths_);
398  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
399  ICOORDELT* w = it.data();
400  if (w->x() - 1 <= width && width <= w->y() + 1)
401  return true;
402  }
403  return false;
404 }
405 
406 // Return true if the sizes are more than a
407 // factor of 2 different.
408 bool TabFind::DifferentSizes(int size1, int size2) {
409  return size1 > size2 * 2 || size2 > size1 * 2;
410 }
411 
412 // Return true if the sizes are more than a
413 // factor of 5 different.
414 bool TabFind::VeryDifferentSizes(int size1, int size2) {
415  return size1 > size2 * 5 || size2 > size1 * 5;
416 }
417 
419 
420 // Top-level function to find TabVectors in an input page block.
421 // Returns false if the detected skew angle is impossible.
422 // Applies the detected skew angle to deskew the tabs, blobs and part_grid.
423 bool TabFind::FindTabVectors(TabVector_LIST* hlines,
424  BLOBNBOX_LIST* image_blobs, TO_BLOCK* block,
425  int min_gutter_width,
426  double tabfind_aligned_gap_fraction,
427  ColPartitionGrid* part_grid,
428  FCOORD* deskew, FCOORD* reskew) {
429  ScrollView* tab_win = FindInitialTabVectors(image_blobs, min_gutter_width,
430  tabfind_aligned_gap_fraction,
431  block);
432  ComputeColumnWidths(tab_win, part_grid);
434  SortVectors();
435  CleanupTabs();
436  if (!Deskew(hlines, image_blobs, block, deskew, reskew))
437  return false; // Skew angle is too large.
438  part_grid->Deskew(*deskew);
439  ApplyTabConstraints();
440  #ifndef GRAPHICS_DISABLED
442  tab_win = MakeWindow(640, 50, "FinalTabs");
443  if (textord_debug_images) {
444  tab_win->Image(AlignedBlob::textord_debug_pix().string(),
445  image_origin_.x(), image_origin_.y());
446  } else {
447  DisplayBoxes(tab_win);
448  DisplayTabs("FinalTabs", tab_win);
449  }
450  tab_win = DisplayTabVectors(tab_win);
451  }
452  #endif // GRAPHICS_DISABLED
453  return true;
454 }
455 
456 // Top-level function to not find TabVectors in an input page block,
457 // but setup for single column mode.
458 void TabFind::DontFindTabVectors(BLOBNBOX_LIST* image_blobs, TO_BLOCK* block,
459  FCOORD* deskew, FCOORD* reskew) {
460  InsertBlobsToGrid(false, false, image_blobs, this);
461  InsertBlobsToGrid(true, false, &block->blobs, this);
462  deskew->set_x(1.0f);
463  deskew->set_y(0.0f);
464  reskew->set_x(1.0f);
465  reskew->set_y(0.0f);
466 }
467 
468 // Cleans up the lists of blobs in the block ready for use by TabFind.
469 // Large blobs that look like text are moved to the main blobs list.
470 // Main blobs that are superseded by the image blobs are deleted.
472  BLOBNBOX_IT large_it = &block->large_blobs;
473  BLOBNBOX_IT blob_it = &block->blobs;
474  int b_count = 0;
475  for (large_it.mark_cycle_pt(); !large_it.cycled_list(); large_it.forward()) {
476  BLOBNBOX* large_blob = large_it.data();
477  if (large_blob->owner() != NULL) {
478  blob_it.add_to_end(large_it.extract());
479  ++b_count;
480  }
481  }
482  if (textord_debug_tabfind) {
483  tprintf("Moved %d large blobs to normal list\n",
484  b_count);
485  #ifndef GRAPHICS_DISABLED
486  ScrollView* rej_win = MakeWindow(500, 300, "Image blobs");
487  block->plot_graded_blobs(rej_win);
488  block->plot_noise_blobs(rej_win);
489  rej_win->Update();
490  #endif // GRAPHICS_DISABLED
491  }
492  block->DeleteUnownedNoise();
493 }
494 
495 // Helper function to setup search limits for *TabForBox.
496 void TabFind::SetupTabSearch(int x, int y, int* min_key, int* max_key) {
497  int key1 = TabVector::SortKey(vertical_skew_, x, (y + tright_.y()) / 2);
498  int key2 = TabVector::SortKey(vertical_skew_, x, (y + bleft_.y()) / 2);
499  *min_key = MIN(key1, key2);
500  *max_key = MAX(key1, key2);
501 }
502 
504 #ifndef GRAPHICS_DISABLED
505  // For every vector, display it.
506  TabVector_IT it(&vectors_);
507  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
508  TabVector* vector = it.data();
509  vector->Display(tab_win);
510  }
511  tab_win->Update();
512 #endif
513  return tab_win;
514 }
515 
516 // PRIVATE CODE.
517 //
518 // First part of FindTabVectors, which may be used twice if the text
519 // is mostly of vertical alignment.
520 ScrollView* TabFind::FindInitialTabVectors(BLOBNBOX_LIST* image_blobs,
521  int min_gutter_width,
522  double tabfind_aligned_gap_fraction,
523  TO_BLOCK* block) {
525  ScrollView* line_win = MakeWindow(0, 0, "VerticalLines");
526  line_win = DisplayTabVectors(line_win);
527  }
528  // Prepare the grid.
529  if (image_blobs != NULL)
530  InsertBlobsToGrid(true, false, image_blobs, this);
531  InsertBlobsToGrid(true, false, &block->blobs, this);
532  ScrollView* initial_win = FindTabBoxes(min_gutter_width,
533  tabfind_aligned_gap_fraction);
534  FindAllTabVectors(min_gutter_width);
535 
537  SortVectors();
538  EvaluateTabs();
539  if (textord_tabfind_show_initialtabs && initial_win != NULL)
540  initial_win = DisplayTabVectors(initial_win);
541  MarkVerticalText();
542  return initial_win;
543 }
544 
545 // Helper displays all the boxes in the given vector on the given window.
546 static void DisplayBoxVector(const GenericVector<BLOBNBOX*>& boxes,
547  ScrollView* win) {
548  #ifndef GRAPHICS_DISABLED
549  for (int i = 0; i < boxes.size(); ++i) {
550  TBOX box = boxes[i]->bounding_box();
551  int left_x = box.left();
552  int right_x = box.right();
553  int top_y = box.top();
554  int bottom_y = box.bottom();
555  ScrollView::Color box_color = boxes[i]->BoxColor();
556  win->Pen(box_color);
557  win->Rectangle(left_x, bottom_y, right_x, top_y);
558  }
559  win->Update();
560  #endif // GRAPHICS_DISABLED
561 }
562 
563 // For each box in the grid, decide whether it is a candidate tab-stop,
564 // and if so add it to the left/right tab boxes.
565 ScrollView* TabFind::FindTabBoxes(int min_gutter_width,
566  double tabfind_aligned_gap_fraction) {
567  left_tab_boxes_.clear();
568  right_tab_boxes_.clear();
569  // For every bbox in the grid, determine whether it uses a tab on an edge.
570  GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> gsearch(this);
571  gsearch.StartFullSearch();
572  BLOBNBOX* bbox;
573  while ((bbox = gsearch.NextFullSearch()) != NULL) {
574  if (TestBoxForTabs(bbox, min_gutter_width, tabfind_aligned_gap_fraction)) {
575  // If it is any kind of tab, insert it into the vectors.
576  if (bbox->left_tab_type() != TT_NONE)
577  left_tab_boxes_.push_back(bbox);
578  if (bbox->right_tab_type() != TT_NONE)
579  right_tab_boxes_.push_back(bbox);
580  }
581  }
582  // Sort left tabs by left and right by right to see the outermost one first
583  // on a ragged tab.
584  left_tab_boxes_.sort(SortByBoxLeft<BLOBNBOX>);
585  right_tab_boxes_.sort(SortRightToLeft<BLOBNBOX>);
586  ScrollView* tab_win = NULL;
587  #ifndef GRAPHICS_DISABLED
589  tab_win = MakeWindow(0, 100, "InitialTabs");
590  tab_win->Pen(ScrollView::BLUE);
591  tab_win->Brush(ScrollView::NONE);
592  // Display the left and right tab boxes.
593  DisplayBoxVector(left_tab_boxes_, tab_win);
594  DisplayBoxVector(right_tab_boxes_, tab_win);
595  tab_win = DisplayTabs("Tabs", tab_win);
596  }
597  #endif // GRAPHICS_DISABLED
598  return tab_win;
599 }
600 
601 bool TabFind::TestBoxForTabs(BLOBNBOX* bbox, int min_gutter_width,
602  double tabfind_aligned_gap_fraction) {
603  GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> radsearch(this);
604  TBOX box = bbox->bounding_box();
605  // If there are separator lines, get the column edges.
606  int left_column_edge = bbox->left_rule();
607  int right_column_edge = bbox->right_rule();
608  // The edges of the bounding box of the blob being processed.
609  int left_x = box.left();
610  int right_x = box.right();
611  int top_y = box.top();
612  int bottom_y = box.bottom();
613  int height = box.height();
614  bool debug = WithinTestRegion(3, left_x, top_y);
615  if (debug) {
616  tprintf("Column edges for blob at (%d,%d)->(%d,%d) are [%d, %d]\n",
617  left_x, top_y, right_x, bottom_y,
618  left_column_edge, right_column_edge);
619  }
620  // Compute a search radius based on a multiple of the height.
621  int radius = (height * kTabRadiusFactor + gridsize_ - 1) / gridsize_;
622  radsearch.StartRadSearch((left_x + right_x)/2, (top_y + bottom_y)/2, radius);
623  // In Vertical Page mode, once we have an estimate of the vertical line
624  // spacing, the minimum amount of gutter space before a possible tab is
625  // increased under the assumption that column partition is always larger
626  // than line spacing.
627  int min_spacing =
628  static_cast<int>(height * tabfind_aligned_gap_fraction);
629  if (min_gutter_width > min_spacing)
630  min_spacing = min_gutter_width;
631  int min_ragged_gutter = kRaggedGutterMultiple * gridsize();
632  if (min_gutter_width > min_ragged_gutter)
633  min_ragged_gutter = min_gutter_width;
634  int target_right = left_x - min_spacing;
635  int target_left = right_x + min_spacing;
636  // We will be evaluating whether the left edge could be a left tab, and
637  // whether the right edge could be a right tab.
638  // A box can be a tab if its bool is_(left/right)_tab remains true, meaning
639  // that no blobs have been found in the gutter during the radial search.
640  // A box can also be a tab if there are objects in the gutter only above
641  // or only below, and there are aligned objects on the opposite side, but
642  // not too many unaligned objects. The maybe_(left/right)_tab_up counts
643  // aligned objects above and negatively counts unaligned objects above,
644  // and is set to -MAX_INT32 if a gutter object is found above.
645  // The other 3 maybe ints work similarly for the other sides.
646  // These conditions are very strict, to minimize false positives, and really
647  // only aligned tabs and outermost ragged tab blobs will qualify, so we
648  // also have maybe_ragged_left/right with less stringent rules.
649  // A blob that is maybe_ragged_left/right will be further qualified later,
650  // using the min_ragged_gutter.
651  bool is_left_tab = true;
652  bool is_right_tab = true;
653  bool maybe_ragged_left = true;
654  bool maybe_ragged_right = true;
655  int maybe_left_tab_up = 0;
656  int maybe_right_tab_up = 0;
657  int maybe_left_tab_down = 0;
658  int maybe_right_tab_down = 0;
659  if (bbox->leader_on_left()) {
660  is_left_tab = false;
661  maybe_ragged_left = false;
662  maybe_left_tab_up = -MAX_INT32;
663  maybe_left_tab_down = -MAX_INT32;
664  }
665  if (bbox->leader_on_right()) {
666  is_right_tab = false;
667  maybe_ragged_right = false;
668  maybe_right_tab_up = -MAX_INT32;
669  maybe_right_tab_down = -MAX_INT32;
670  }
671  int alignment_tolerance = static_cast<int>(resolution_ * kAlignedFraction);
672  BLOBNBOX* neighbour = NULL;
673  while ((neighbour = radsearch.NextRadSearch()) != NULL) {
674  if (neighbour == bbox)
675  continue;
676  TBOX nbox = neighbour->bounding_box();
677  int n_left = nbox.left();
678  int n_right = nbox.right();
679  if (debug)
680  tprintf("Neighbour at (%d,%d)->(%d,%d)\n",
681  n_left, nbox.bottom(), n_right, nbox.top());
682  // If the neighbouring blob is the wrong side of a separator line, then it
683  // "doesn't exist" as far as we are concerned.
684  if (n_right > right_column_edge || n_left < left_column_edge ||
685  left_x < neighbour->left_rule() || right_x > neighbour->right_rule())
686  continue; // Separator line in the way.
687  int n_mid_x = (n_left + n_right) / 2;
688  int n_mid_y = (nbox.top() + nbox.bottom()) / 2;
689  if (n_mid_x <= left_x && n_right >= target_right) {
690  if (debug)
691  tprintf("Not a left tab\n");
692  is_left_tab = false;
693  if (n_mid_y < top_y)
694  maybe_left_tab_down = -MAX_INT32;
695  if (n_mid_y > bottom_y)
696  maybe_left_tab_up = -MAX_INT32;
697  } else if (NearlyEqual(left_x, n_left, alignment_tolerance)) {
698  if (debug)
699  tprintf("Maybe a left tab\n");
700  if (n_mid_y > top_y && maybe_left_tab_up > -MAX_INT32)
701  ++maybe_left_tab_up;
702  if (n_mid_y < bottom_y && maybe_left_tab_down > -MAX_INT32)
703  ++maybe_left_tab_down;
704  } else if (n_left < left_x && n_right >= left_x) {
705  // Overlaps but not aligned so negative points on a maybe.
706  if (debug)
707  tprintf("Maybe Not a left tab\n");
708  if (n_mid_y > top_y && maybe_left_tab_up > -MAX_INT32)
709  --maybe_left_tab_up;
710  if (n_mid_y < bottom_y && maybe_left_tab_down > -MAX_INT32)
711  --maybe_left_tab_down;
712  }
713  if (n_left < left_x && nbox.y_overlap(box) && n_right >= target_right) {
714  maybe_ragged_left = false;
715  if (debug)
716  tprintf("Not a ragged left\n");
717  }
718  if (n_mid_x >= right_x && n_left <= target_left) {
719  if (debug)
720  tprintf("Not a right tab\n");
721  is_right_tab = false;
722  if (n_mid_y < top_y)
723  maybe_right_tab_down = -MAX_INT32;
724  if (n_mid_y > bottom_y)
725  maybe_right_tab_up = -MAX_INT32;
726  } else if (NearlyEqual(right_x, n_right, alignment_tolerance)) {
727  if (debug)
728  tprintf("Maybe a right tab\n");
729  if (n_mid_y > top_y && maybe_right_tab_up > -MAX_INT32)
730  ++maybe_right_tab_up;
731  if (n_mid_y < bottom_y && maybe_right_tab_down > -MAX_INT32)
732  ++maybe_right_tab_down;
733  } else if (n_right > right_x && n_left <= right_x) {
734  // Overlaps but not aligned so negative points on a maybe.
735  if (debug)
736  tprintf("Maybe Not a right tab\n");
737  if (n_mid_y > top_y && maybe_right_tab_up > -MAX_INT32)
738  --maybe_right_tab_up;
739  if (n_mid_y < bottom_y && maybe_right_tab_down > -MAX_INT32)
740  --maybe_right_tab_down;
741  }
742  if (n_right > right_x && nbox.y_overlap(box) && n_left <= target_left) {
743  maybe_ragged_right = false;
744  if (debug)
745  tprintf("Not a ragged right\n");
746  }
747  if (maybe_left_tab_down == -MAX_INT32 && maybe_left_tab_up == -MAX_INT32 &&
748  maybe_right_tab_down == -MAX_INT32 && maybe_right_tab_up == -MAX_INT32)
749  break;
750  }
751  if (is_left_tab || maybe_left_tab_up > 1 || maybe_left_tab_down > 1) {
753  } else if (maybe_ragged_left && ConfirmRaggedLeft(bbox, min_ragged_gutter)) {
755  } else {
756  bbox->set_left_tab_type(TT_NONE);
757  }
758  if (is_right_tab || maybe_right_tab_up > 1 || maybe_right_tab_down > 1) {
760  } else if (maybe_ragged_right &&
761  ConfirmRaggedRight(bbox, min_ragged_gutter)) {
763  } else {
765  }
766  if (debug) {
767  tprintf("Left result = %s, Right result=%s\n",
768  bbox->left_tab_type() == TT_MAYBE_ALIGNED ? "Aligned" :
769  (bbox->left_tab_type() == TT_MAYBE_RAGGED ? "Ragged" : "None"),
770  bbox->right_tab_type() == TT_MAYBE_ALIGNED ? "Aligned" :
771  (bbox->right_tab_type() == TT_MAYBE_RAGGED ? "Ragged" : "None"));
772  }
773  return bbox->left_tab_type() != TT_NONE || bbox->right_tab_type() != TT_NONE;
774 }
775 
776 // Returns true if there is nothing in the rectangle of width min_gutter to
777 // the left of bbox.
778 bool TabFind::ConfirmRaggedLeft(BLOBNBOX* bbox, int min_gutter) {
779  TBOX search_box(bbox->bounding_box());
780  search_box.set_right(search_box.left());
781  search_box.set_left(search_box.left() - min_gutter);
782  return NothingYOverlapsInBox(search_box, bbox->bounding_box());
783 }
784 
785 // Returns true if there is nothing in the rectangle of width min_gutter to
786 // the right of bbox.
787 bool TabFind::ConfirmRaggedRight(BLOBNBOX* bbox, int min_gutter) {
788  TBOX search_box(bbox->bounding_box());
789  search_box.set_left(search_box.right());
790  search_box.set_right(search_box.right() + min_gutter);
791  return NothingYOverlapsInBox(search_box, bbox->bounding_box());
792 }
793 
794 // Returns true if there is nothing in the given search_box that vertically
795 // overlaps target_box other than target_box itself.
796 bool TabFind::NothingYOverlapsInBox(const TBOX& search_box,
797  const TBOX& target_box) {
798  BlobGridSearch rsearch(this);
799  rsearch.StartRectSearch(search_box);
800  BLOBNBOX* blob;
801  while ((blob = rsearch.NextRectSearch()) != NULL) {
802  const TBOX& box = blob->bounding_box();
803  if (box.y_overlap(target_box) && !(box == target_box))
804  return false;
805  }
806  return true;
807 }
808 
809 void TabFind::FindAllTabVectors(int min_gutter_width) {
810  // A list of vectors that will be created in estimating the skew.
811  TabVector_LIST dummy_vectors;
812  // An estimate of the vertical direction, revised as more lines are added.
813  int vertical_x = 0;
814  int vertical_y = 1;
815  // Find an estimate of the vertical direction by finding some tab vectors.
816  // Slowly up the search size until we get some vectors.
817  for (int search_size = kMinVerticalSearch; search_size < kMaxVerticalSearch;
818  search_size += kMinVerticalSearch) {
819  int vector_count = FindTabVectors(search_size, TA_LEFT_ALIGNED,
820  min_gutter_width,
821  &dummy_vectors,
822  &vertical_x, &vertical_y);
823  vector_count += FindTabVectors(search_size, TA_RIGHT_ALIGNED,
824  min_gutter_width,
825  &dummy_vectors,
826  &vertical_x, &vertical_y);
827  if (vector_count > 0)
828  break;
829  }
830  // Get rid of the test vectors and reset the types of the tabs.
831  dummy_vectors.clear();
832  for (int i = 0; i < left_tab_boxes_.size(); ++i) {
833  BLOBNBOX* bbox = left_tab_boxes_[i];
834  if (bbox->left_tab_type() == TT_CONFIRMED)
836  }
837  for (int i = 0; i < right_tab_boxes_.size(); ++i) {
838  BLOBNBOX* bbox = right_tab_boxes_[i];
839  if (bbox->right_tab_type() == TT_CONFIRMED)
841  }
842  if (textord_debug_tabfind) {
843  tprintf("Beginning real tab search with vertical = %d,%d...\n",
844  vertical_x, vertical_y);
845  }
846  // Now do the real thing ,but keep the vectors in the dummy_vectors list
847  // until they are all done, so we don't get the tab vectors confused with
848  // the rule line vectors.
850  &dummy_vectors, &vertical_x, &vertical_y);
852  &dummy_vectors, &vertical_x, &vertical_y);
854  &dummy_vectors, &vertical_x, &vertical_y);
856  &dummy_vectors, &vertical_x, &vertical_y);
857  // Now add the vectors to the vectors_ list.
858  TabVector_IT v_it(&vectors_);
859  v_it.add_list_after(&dummy_vectors);
860  // Now use the summed (mean) vertical vector as the direction for everything.
861  SetVerticalSkewAndParellelize(vertical_x, vertical_y);
862 }
863 
864 // Helper for FindAllTabVectors finds the vectors of a particular type.
865 int TabFind::FindTabVectors(int search_size_multiple, TabAlignment alignment,
866  int min_gutter_width, TabVector_LIST* vectors,
867  int* vertical_x, int* vertical_y) {
868  TabVector_IT vector_it(vectors);
869  int vector_count = 0;
870  // Search the right or left tab boxes, looking for tab vectors.
871  bool right = alignment == TA_RIGHT_ALIGNED || alignment == TA_RIGHT_RAGGED;
872  const GenericVector<BLOBNBOX*>& boxes = right ? right_tab_boxes_
873  : left_tab_boxes_;
874  for (int i = 0; i < boxes.size(); ++i) {
875  BLOBNBOX* bbox = boxes[i];
876  if ((!right && bbox->left_tab_type() == TT_MAYBE_ALIGNED) ||
877  (right && bbox->right_tab_type() == TT_MAYBE_ALIGNED)) {
878  TabVector* vector = FindTabVector(search_size_multiple, min_gutter_width,
879  alignment,
880  bbox, vertical_x, vertical_y);
881  if (vector != NULL) {
882  ++vector_count;
883  vector_it.add_to_end(vector);
884  }
885  }
886  }
887  return vector_count;
888 }
889 
890 // Finds a vector corresponding to a tabstop running through the
891 // given box of the given alignment type.
892 // search_size_multiple is a multiple of height used to control
893 // the size of the search.
894 // vertical_x and y are updated with an estimate of the real
895 // vertical direction. (skew finding.)
896 // Returns NULL if no decent tabstop can be found.
897 TabVector* TabFind::FindTabVector(int search_size_multiple,
898  int min_gutter_width,
899  TabAlignment alignment,
900  BLOBNBOX* bbox,
901  int* vertical_x, int* vertical_y) {
902  int height = MAX(bbox->bounding_box().height(), gridsize());
903  AlignedBlobParams align_params(*vertical_x, *vertical_y,
904  height,
905  search_size_multiple, min_gutter_width,
906  resolution_, alignment);
907  // FindVerticalAlignment is in the parent (AlignedBlob) class.
908  return FindVerticalAlignment(align_params, bbox, vertical_x, vertical_y);
909 }
910 
911 // Set the vertical_skew_ member from the given vector and refit
912 // all vectors parallel to the skew vector.
913 void TabFind::SetVerticalSkewAndParellelize(int vertical_x, int vertical_y) {
914  // Fit the vertical vector into an ICOORD, which is 16 bit.
915  vertical_skew_.set_with_shrink(vertical_x, vertical_y);
917  tprintf("Vertical skew vector=(%d,%d)\n",
919  v_it_.set_to_list(&vectors_);
920  for (v_it_.mark_cycle_pt(); !v_it_.cycled_list(); v_it_.forward()) {
921  TabVector* v = v_it_.data();
922  v->Fit(vertical_skew_, true);
923  }
924  // Now sort the vectors as their direction has potentially changed.
925  SortVectors();
926 }
927 
928 // Sort all the current vectors using the given vertical direction vector.
929 void TabFind::SortVectors() {
930  vectors_.sort(TabVector::SortVectorsByKey);
931  v_it_.set_to_list(&vectors_);
932 }
933 
934 // Evaluate all the current tab vectors.
935 void TabFind::EvaluateTabs() {
936  TabVector_IT rule_it(&vectors_);
937  for (rule_it.mark_cycle_pt(); !rule_it.cycled_list(); rule_it.forward()) {
938  TabVector* tab = rule_it.data();
939  if (!tab->IsSeparator()) {
940  tab->Evaluate(vertical_skew_, this);
941  if (tab->BoxCount() < kMinEvaluatedTabs) {
942  if (textord_debug_tabfind > 2)
943  tab->Print("Too few boxes");
944  delete rule_it.extract();
945  v_it_.set_to_list(&vectors_);
946  } else if (WithinTestRegion(3, tab->startpt().x(), tab->startpt().y())) {
947  tab->Print("Evaluated tab");
948  }
949  }
950  }
951 }
952 
953 // Trace textlines from one side to the other of each tab vector, saving
954 // the most frequent column widths found in a list so that a given width
955 // can be tested for being a common width with a simple callback function.
956 void TabFind::ComputeColumnWidths(ScrollView* tab_win,
957  ColPartitionGrid* part_grid) {
958  #ifndef GRAPHICS_DISABLED
959  if (tab_win != NULL)
960  tab_win->Pen(ScrollView::WHITE);
961  #endif // GRAPHICS_DISABLED
962  // Accumulate column sections into a STATS
963  int col_widths_size = (tright_.x() - bleft_.x()) / kColumnWidthFactor;
964  STATS col_widths(0, col_widths_size + 1);
965  ApplyPartitionsToColumnWidths(part_grid, &col_widths);
966  #ifndef GRAPHICS_DISABLED
967  if (tab_win != NULL) {
968  tab_win->Update();
969  }
970  #endif // GRAPHICS_DISABLED
971  if (textord_debug_tabfind > 1)
972  col_widths.print();
973  // Now make a list of column widths.
974  MakeColumnWidths(col_widths_size, &col_widths);
975  // Turn the column width into a range.
976  ApplyPartitionsToColumnWidths(part_grid, NULL);
977 }
978 
979 // Finds column width and:
980 // if col_widths is not null (pass1):
981 // pair-up tab vectors with existing ColPartitions and accumulate widths.
982 // else (pass2):
983 // find the largest real partition width for each recorded column width,
984 // to be used as the minimum acceptable width.
985 void TabFind::ApplyPartitionsToColumnWidths(ColPartitionGrid* part_grid,
986  STATS* col_widths) {
987  // For every ColPartition in the part_grid, add partners to the tabvectors
988  // and accumulate the column widths.
989  ColPartitionGridSearch gsearch(part_grid);
990  gsearch.StartFullSearch();
991  ColPartition* part;
992  while ((part = gsearch.NextFullSearch()) != NULL) {
993  BLOBNBOX_C_IT blob_it(part->boxes());
994  if (blob_it.empty())
995  continue;
996  BLOBNBOX* left_blob = blob_it.data();
997  blob_it.move_to_last();
998  BLOBNBOX* right_blob = blob_it.data();
999  TabVector* left_vector = LeftTabForBox(left_blob->bounding_box(),
1000  true, false);
1001  if (left_vector == NULL || left_vector->IsRightTab())
1002  continue;
1003  TabVector* right_vector = RightTabForBox(right_blob->bounding_box(),
1004  true, false);
1005  if (right_vector == NULL || right_vector->IsLeftTab())
1006  continue;
1007 
1008  int line_left = left_vector->XAtY(left_blob->bounding_box().bottom());
1009  int line_right = right_vector->XAtY(right_blob->bounding_box().bottom());
1010  // Add to STATS of measurements if the width is significant.
1011  int width = line_right - line_left;
1012  if (col_widths != NULL) {
1013  AddPartnerVector(left_blob, right_blob, left_vector, right_vector);
1014  if (width >= kMinColumnWidth)
1015  col_widths->add(width / kColumnWidthFactor, 1);
1016  } else {
1017  width /= kColumnWidthFactor;
1018  ICOORDELT_IT it(&column_widths_);
1019  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1020  ICOORDELT* w = it.data();
1021  if (NearlyEqual<int>(width, w->y(), 1)) {
1022  int true_width = part->bounding_box().width() / kColumnWidthFactor;
1023  if (true_width <= w->y() && true_width > w->x())
1024  w->set_x(true_width);
1025  break;
1026  }
1027  }
1028  }
1029  }
1030 }
1031 
1032 // Helper makes the list of common column widths in column_widths_ from the
1033 // input col_widths. Destroys the content of col_widths by repeatedly
1034 // finding the mode and erasing the peak.
1035 void TabFind::MakeColumnWidths(int col_widths_size, STATS* col_widths) {
1036  ICOORDELT_IT w_it(&column_widths_);
1037  int total_col_count = col_widths->get_total();
1038  while (col_widths->get_total() > 0) {
1039  int width = col_widths->mode();
1040  int col_count = col_widths->pile_count(width);
1041  col_widths->add(width, -col_count);
1042  // Get the entire peak.
1043  for (int left = width - 1; left > 0 &&
1044  col_widths->pile_count(left) > 0;
1045  --left) {
1046  int new_count = col_widths->pile_count(left);
1047  col_count += new_count;
1048  col_widths->add(left, -new_count);
1049  }
1050  for (int right = width + 1; right < col_widths_size &&
1051  col_widths->pile_count(right) > 0;
1052  ++right) {
1053  int new_count = col_widths->pile_count(right);
1054  col_count += new_count;
1055  col_widths->add(right, -new_count);
1056  }
1057  if (col_count > kMinLinesInColumn &&
1058  col_count > kMinFractionalLinesInColumn * total_col_count) {
1059  ICOORDELT* w = new ICOORDELT(0, width);
1060  w_it.add_after_then_move(w);
1062  tprintf("Column of width %d has %d = %.2f%% lines\n",
1063  width * kColumnWidthFactor, col_count,
1064  100.0 * col_count / total_col_count);
1065  }
1066  }
1067 }
1068 
1069 // Mark blobs as being in a vertical text line where that is the case.
1070 // Returns true if the majority of the image is vertical text lines.
1071 void TabFind::MarkVerticalText() {
1073  tprintf("Checking for vertical lines\n");
1074  BlobGridSearch gsearch(this);
1075  gsearch.StartFullSearch();
1076  BLOBNBOX* blob = NULL;
1077  while ((blob = gsearch.NextFullSearch()) != NULL) {
1078  if (blob->region_type() < BRT_UNKNOWN)
1079  continue;
1080  if (blob->UniquelyVertical()) {
1082  }
1083  }
1084 }
1085 
1086 int TabFind::FindMedianGutterWidth(TabVector_LIST *lines) {
1087  TabVector_IT it(lines);
1088  int prev_right = -1;
1089  int max_gap = static_cast<int>(kMaxGutterWidthAbsolute * resolution_);
1090  STATS gaps(0, max_gap);
1091  STATS heights(0, max_gap);
1092  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1093  TabVector* v = it.data();
1094  TabVector* partner = v->GetSinglePartner();
1095  if (!v->IsLeftTab() || v->IsSeparator() || !partner) continue;
1096  heights.add(partner->startpt().x() - v->startpt().x(), 1);
1097  if (prev_right > 0 && v->startpt().x() > prev_right) {
1098  gaps.add(v->startpt().x() - prev_right, 1);
1099  }
1100  prev_right = partner->startpt().x();
1101  }
1103  tprintf("TabGutter total %d median_gap %.2f median_hgt %.2f\n",
1104  gaps.get_total(), gaps.median(), heights.median());
1105  if (gaps.get_total() < kMinLinesInColumn) return 0;
1106  return static_cast<int>(gaps.median());
1107 }
1108 
1109 // Find the next adjacent (looking to the left or right) blob on this text
1110 // line, with the constraint that it must vertically significantly overlap
1111 // the [top_y, bottom_y] range.
1112 // If ignore_images is true, then blobs with aligned_text() < 0 are treated
1113 // as if they do not exist.
1114 BLOBNBOX* TabFind::AdjacentBlob(const BLOBNBOX* bbox,
1115  bool look_left, bool ignore_images,
1116  double min_overlap_fraction,
1117  int gap_limit, int top_y, int bottom_y) {
1118  GridSearch<BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT> sidesearch(this);
1119  const TBOX& box = bbox->bounding_box();
1120  int left = box.left();
1121  int right = box.right();
1122  int mid_x = (left + right) / 2;
1123  sidesearch.StartSideSearch(mid_x, bottom_y, top_y);
1124  int best_gap = 0;
1125  bool debug = WithinTestRegion(3, left, bottom_y);
1126  BLOBNBOX* result = NULL;
1127  BLOBNBOX* neighbour = NULL;
1128  while ((neighbour = sidesearch.NextSideSearch(look_left)) != NULL) {
1129  if (debug) {
1130  tprintf("Adjacent blob: considering box:");
1131  neighbour->bounding_box().print();
1132  }
1133  if (neighbour == bbox ||
1134  (ignore_images && neighbour->region_type() < BRT_UNKNOWN))
1135  continue;
1136  const TBOX& nbox = neighbour->bounding_box();
1137  int n_top_y = nbox.top();
1138  int n_bottom_y = nbox.bottom();
1139  int v_overlap = MIN(n_top_y, top_y) - MAX(n_bottom_y, bottom_y);
1140  int height = top_y - bottom_y;
1141  int n_height = n_top_y - n_bottom_y;
1142  if (v_overlap > min_overlap_fraction * MIN(height, n_height) &&
1143  (min_overlap_fraction == 0.0 || !DifferentSizes(height, n_height))) {
1144  int n_left = nbox.left();
1145  int n_right = nbox.right();
1146  int h_gap = MAX(n_left, left) - MIN(n_right, right);
1147  int n_mid_x = (n_left + n_right) / 2;
1148  if (look_left == (n_mid_x < mid_x) && n_mid_x != mid_x) {
1149  if (h_gap > gap_limit) {
1150  // Hit a big gap before next tab so don't return anything.
1151  if (debug)
1152  tprintf("Giving up due to big gap = %d vs %d\n",
1153  h_gap, gap_limit);
1154  return result;
1155  }
1156  if (h_gap > 0 && (look_left ? neighbour->right_tab_type()
1157  : neighbour->left_tab_type()) >= TT_CONFIRMED) {
1158  // Hit a tab facing the wrong way. Stop in case we are crossing
1159  // the column boundary.
1160  if (debug)
1161  tprintf("Collision with like tab of type %d at %d,%d\n",
1162  look_left ? neighbour->right_tab_type()
1163  : neighbour->left_tab_type(),
1164  n_left, nbox.bottom());
1165  return result;
1166  }
1167  // This is a good fit to the line. Continue with this
1168  // neighbour as the bbox if the best gap.
1169  if (result == NULL || h_gap < best_gap) {
1170  if (debug)
1171  tprintf("Good result\n");
1172  result = neighbour;
1173  best_gap = h_gap;
1174  } else {
1175  // The new one is worse, so we probably already have the best result.
1176  return result;
1177  }
1178  } else if (debug) {
1179  tprintf("Wrong way\n");
1180  }
1181  } else if (debug) {
1182  tprintf("Insufficient overlap\n");
1183  }
1184  }
1185  if (WithinTestRegion(3, left, box.top()))
1186  tprintf("Giving up due to end of search\n");
1187  return result; // Hit the edge and found nothing.
1188 }
1189 
1190 // Add a bi-directional partner relationship between the left
1191 // and the right. If one (or both) of the vectors is a separator,
1192 // extend a nearby extendable vector or create a new one of the
1193 // correct type, using the given left or right blob as a guide.
1194 void TabFind::AddPartnerVector(BLOBNBOX* left_blob, BLOBNBOX* right_blob,
1195  TabVector* left, TabVector* right) {
1196  const TBOX& left_box = left_blob->bounding_box();
1197  const TBOX& right_box = right_blob->bounding_box();
1198  if (left->IsSeparator()) {
1199  // Try to find a nearby left edge to extend.
1200  TabVector* v = LeftTabForBox(left_box, true, true);
1201  if (v != NULL && v != left && v->IsLeftTab() &&
1202  v->XAtY(left_box.top()) > left->XAtY(left_box.top())) {
1203  left = v; // Found a good replacement.
1204  left->ExtendToBox(left_blob);
1205  } else {
1206  // Fake a vector.
1207  left = new TabVector(*left, TA_LEFT_RAGGED, vertical_skew_, left_blob);
1208  vectors_.add_sorted(TabVector::SortVectorsByKey, left);
1209  v_it_.move_to_first();
1210  }
1211  }
1212  if (right->IsSeparator()) {
1213  // Try to find a nearby left edge to extend.
1214  if (WithinTestRegion(3, right_box.right(), right_box.bottom())) {
1215  tprintf("Box edge (%d,%d-%d)",
1216  right_box.right(), right_box.bottom(), right_box.top());
1217  right->Print(" looking for improvement for");
1218  }
1219  TabVector* v = RightTabForBox(right_box, true, true);
1220  if (v != NULL && v != right && v->IsRightTab() &&
1221  v->XAtY(right_box.top()) < right->XAtY(right_box.top())) {
1222  right = v; // Found a good replacement.
1223  right->ExtendToBox(right_blob);
1224  if (WithinTestRegion(3, right_box.right(), right_box.bottom())) {
1225  right->Print("Extended vector");
1226  }
1227  } else {
1228  // Fake a vector.
1229  right = new TabVector(*right, TA_RIGHT_RAGGED, vertical_skew_,
1230  right_blob);
1231  vectors_.add_sorted(TabVector::SortVectorsByKey, right);
1232  v_it_.move_to_first();
1233  if (WithinTestRegion(3, right_box.right(), right_box.bottom())) {
1234  right->Print("Created new vector");
1235  }
1236  }
1237  }
1238  left->AddPartner(right);
1239  right->AddPartner(left);
1240 }
1241 
1242 // Remove separators and unused tabs from the main vectors_ list
1243 // to the dead_vectors_ list.
1244 void TabFind::CleanupTabs() {
1245  // TODO(rays) Before getting rid of separators and unused vectors, it
1246  // would be useful to try moving ragged vectors outwards to see if this
1247  // allows useful extension. Could be combined with checking ends of partners.
1248  TabVector_IT it(&vectors_);
1249  TabVector_IT dead_it(&dead_vectors_);
1250  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1251  TabVector* v = it.data();
1252  if (v->IsSeparator() || v->Partnerless()) {
1253  dead_it.add_after_then_move(it.extract());
1254  v_it_.set_to_list(&vectors_);
1255  } else {
1256  v->FitAndEvaluateIfNeeded(vertical_skew_, this);
1257  }
1258  }
1259 }
1260 
1261 // Apply the given rotation to the given list of blobs.
1262 void TabFind::RotateBlobList(const FCOORD& rotation, BLOBNBOX_LIST* blobs) {
1263  BLOBNBOX_IT it(blobs);
1264  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1265  it.data()->rotate_box(rotation);
1266  }
1267 }
1268 
1269 // Recreate the grid with deskewed BLOBNBOXes.
1270 // Returns false if the detected skew angle is impossible.
1271 bool TabFind::Deskew(TabVector_LIST* hlines, BLOBNBOX_LIST* image_blobs,
1272  TO_BLOCK* block, FCOORD* deskew, FCOORD* reskew) {
1273  ComputeDeskewVectors(deskew, reskew);
1274  if (deskew->x() < kCosMaxSkewAngle)
1275  return false;
1276  RotateBlobList(*deskew, image_blobs);
1277  RotateBlobList(*deskew, &block->blobs);
1278  RotateBlobList(*deskew, &block->small_blobs);
1279  RotateBlobList(*deskew, &block->noise_blobs);
1280  if (textord_debug_images) {
1281  // Rotate the debug pix and arrange for it to be drawn at the correct
1282  // pixel offset.
1283  Pix* pix_grey = pixRead(AlignedBlob::textord_debug_pix().string());
1284  int width = pixGetWidth(pix_grey);
1285  int height = pixGetHeight(pix_grey);
1286  float angle = atan2(deskew->y(), deskew->x());
1287  // Positive angle is clockwise to pixRotate.
1288  Pix* pix_rot = pixRotate(pix_grey, -angle, L_ROTATE_AREA_MAP,
1289  L_BRING_IN_WHITE, width, height);
1290  // The image must be translated by the rotation of its center, since it
1291  // has just been rotated about its center.
1292  ICOORD center_offset(width / 2, height / 2);
1293  ICOORD new_center_offset(center_offset);
1294  new_center_offset.rotate(*deskew);
1295  image_origin_ += new_center_offset - center_offset;
1296  // The image grew as it was rotated, so offset the (top/left) origin
1297  // by half the change in size. y is opposite to x because it is drawn
1298  // at ist top/left, not bottom/left.
1299  ICOORD corner_offset((width - pixGetWidth(pix_rot)) / 2,
1300  (pixGetHeight(pix_rot) - height) / 2);
1301  image_origin_ += corner_offset;
1302  pixWrite(AlignedBlob::textord_debug_pix().string(), pix_rot, IFF_PNG);
1303  pixDestroy(&pix_grey);
1304  pixDestroy(&pix_rot);
1305  }
1306 
1307  // Rotate the horizontal vectors. The vertical vectors don't need
1308  // rotating as they can just be refitted.
1309  TabVector_IT h_it(hlines);
1310  for (h_it.mark_cycle_pt(); !h_it.cycled_list(); h_it.forward()) {
1311  TabVector* h = h_it.data();
1312  h->Rotate(*deskew);
1313  }
1314  TabVector_IT d_it(&dead_vectors_);
1315  for (d_it.mark_cycle_pt(); !d_it.cycled_list(); d_it.forward()) {
1316  TabVector* d = d_it.data();
1317  d->Rotate(*deskew);
1318  }
1319  SetVerticalSkewAndParellelize(0, 1);
1320  // Rebuild the grid to the new size.
1321  TBOX grid_box(bleft_, tright_);
1322  grid_box.rotate_large(*deskew);
1323  Init(gridsize(), grid_box.botleft(), grid_box.topright());
1324  InsertBlobsToGrid(false, false, image_blobs, this);
1325  InsertBlobsToGrid(true, false, &block->blobs, this);
1326  return true;
1327 }
1328 
1329 // Flip the vertical and horizontal lines and rotate the grid ready
1330 // for working on the rotated image.
1331 // This also makes parameter adjustments for FindInitialTabVectors().
1332 void TabFind::ResetForVerticalText(const FCOORD& rotate, const FCOORD& rerotate,
1333  TabVector_LIST* horizontal_lines,
1334  int* min_gutter_width) {
1335  // Rotate the horizontal and vertical vectors and swap them over.
1336  // Only the separators are kept and rotated; other tabs are used
1337  // to estimate the gutter width then thrown away.
1338  TabVector_LIST ex_verticals;
1339  TabVector_IT ex_v_it(&ex_verticals);
1340  TabVector_LIST vlines;
1341  TabVector_IT v_it(&vlines);
1342  while (!v_it_.empty()) {
1343  TabVector* v = v_it_.extract();
1344  if (v->IsSeparator()) {
1345  v->Rotate(rotate);
1346  ex_v_it.add_after_then_move(v);
1347  } else {
1348  v_it.add_after_then_move(v);
1349  }
1350  v_it_.forward();
1351  }
1352 
1353  // Adjust the min gutter width for better tabbox selection
1354  // in 2nd call to FindInitialTabVectors().
1355  int median_gutter = FindMedianGutterWidth(&vlines);
1356  if (median_gutter > *min_gutter_width)
1357  *min_gutter_width = median_gutter;
1358 
1359  TabVector_IT h_it(horizontal_lines);
1360  for (h_it.mark_cycle_pt(); !h_it.cycled_list(); h_it.forward()) {
1361  TabVector* h = h_it.data();
1362  h->Rotate(rotate);
1363  }
1364  v_it_.add_list_after(horizontal_lines);
1365  v_it_.move_to_first();
1366  h_it.set_to_list(horizontal_lines);
1367  h_it.add_list_after(&ex_verticals);
1368 
1369  // Rebuild the grid to the new size.
1370  TBOX grid_box(bleft(), tright());
1371  grid_box.rotate_large(rotate);
1372  Init(gridsize(), grid_box.botleft(), grid_box.topright());
1373 }
1374 
1375 // Clear the grid and get rid of the tab vectors, but not separators,
1376 // ready to start again.
1378  v_it_.move_to_first();
1379  for (v_it_.mark_cycle_pt(); !v_it_.cycled_list(); v_it_.forward()) {
1380  if (!v_it_.data()->IsSeparator())
1381  delete v_it_.extract();
1382  }
1383  Clear();
1384 }
1385 
1386 // Reflect the separator tab vectors and the grids in the y-axis.
1387 // Can only be called after Reset!
1389  TabVector_LIST temp_list;
1390  TabVector_IT temp_it(&temp_list);
1391  v_it_.move_to_first();
1392  // The TabVector list only contains vertical lines, but they need to be
1393  // reflected and the list needs to be reversed, so they are still in
1394  // sort_key order.
1395  while (!v_it_.empty()) {
1396  TabVector* v = v_it_.extract();
1397  v_it_.forward();
1398  v->ReflectInYAxis();
1399  temp_it.add_before_then_move(v);
1400  }
1401  v_it_.add_list_after(&temp_list);
1402  v_it_.move_to_first();
1403  // Reset this grid with reflected bounding boxes.
1404  TBOX grid_box(bleft(), tright());
1405  int tmp = grid_box.left();
1406  grid_box.set_left(-grid_box.right());
1407  grid_box.set_right(-tmp);
1408  Init(gridsize(), grid_box.botleft(), grid_box.topright());
1409 }
1410 
1411 // Compute the rotation required to deskew, and its inverse rotation.
1412 void TabFind::ComputeDeskewVectors(FCOORD* deskew, FCOORD* reskew) {
1413  double length = vertical_skew_ % vertical_skew_;
1414  length = sqrt(length);
1415  deskew->set_x(static_cast<float>(vertical_skew_.y() / length));
1416  deskew->set_y(static_cast<float>(vertical_skew_.x() / length));
1417  reskew->set_x(deskew->x());
1418  reskew->set_y(-deskew->y());
1419 }
1420 
1421 // Compute and apply constraints to the end positions of TabVectors so
1422 // that where possible partners end at the same y coordinate.
1423 void TabFind::ApplyTabConstraints() {
1424  TabVector_IT it(&vectors_);
1425  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1426  TabVector* v = it.data();
1427  v->SetupConstraints();
1428  }
1429  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1430  TabVector* v = it.data();
1431  // With the first and last partner, we want a common bottom and top,
1432  // respectively, and for each change of partner, we want a common
1433  // top of first with bottom of next.
1434  v->SetupPartnerConstraints();
1435  }
1436  // TODO(rays) The back-to-back pairs should really be done like the
1437  // front-to-front pairs, but there is no convenient way of producing the
1438  // list of partners like there is with the front-to-front.
1439  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1440  TabVector* v = it.data();
1441  if (!v->IsRightTab())
1442  continue;
1443  // For each back-to-back pair of vectors, try for common top and bottom.
1444  TabVector_IT partner_it(it);
1445  for (partner_it.forward(); !partner_it.at_first(); partner_it.forward()) {
1446  TabVector* partner = partner_it.data();
1447  if (!partner->IsLeftTab() || !v->VOverlap(*partner))
1448  continue;
1449  v->SetupPartnerConstraints(partner);
1450  }
1451  }
1452  // Now actually apply the constraints to get common start/end points.
1453  for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
1454  TabVector* v = it.data();
1455  if (!v->IsSeparator())
1456  v->ApplyConstraints();
1457  }
1458  // TODO(rays) Where constraint application fails, it would be good to try
1459  // checking the ends to see if they really should be moved.
1460 }
1461 
1462 } // namespace tesseract.
bool leader_on_left() const
Definition: blobbox.h:343
tesseract::ColPartition * owner() const
Definition: blobbox.h:337
const TBOX & bounding_box() const
Definition: blobbox.h:215
void Image(struct Pix *image, int x_pos, int y_pos)
Definition: scrollview.cpp:773
TabVector * LeftTabForBox(const TBOX &box, bool crossing, bool extended)
Definition: tabfind.cpp:349
void SetBlockRuleEdges(TO_BLOCK *block)
Definition: tabfind.cpp:134
bool y_overlap(const TBOX &box) const
Definition: rect.h:418
const double kLineFragmentAspectRatio
Definition: tabfind.cpp:54
static bool UnMergeableType(BlobRegionType type)
Definition: blobbox.h:415
inT32 mode() const
Definition: statistc.cpp:115
const double kMaxGutterWidthAbsolute
Definition: tabfind.cpp:49
const double kAlignedFraction
Definition: alignedblob.cpp:39
bool NearlyEqual(T x, T y, T tolerance)
Definition: host.h:77
void Display(ScrollView *tab_win)
Definition: tabvector.cpp:547
bool UniquelyVertical() const
Definition: blobbox.h:395
bool IsSeparator() const
Definition: tabvector.h:221
int right_rule() const
Definition: blobbox.h:304
const ICOORD & tright() const
Definition: bbgrid.h:75
const ICOORD & topright() const
Definition: rect.h:100
bool textord_debug_images
Definition: alignedblob.cpp:33
integer coordinate
Definition: points.h:30
bool InsertBlob(bool h_spread, bool v_spread, BLOBNBOX *blob, BBGrid< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT > *grid)
Definition: tabfind.cpp:119
GridSearch< ColPartition, ColPartition_CLIST, ColPartition_C_IT > ColPartitionGridSearch
Definition: colpartition.h:932
void TidyBlobs(TO_BLOCK *block)
Definition: tabfind.cpp:471
void ResetForVerticalText(const FCOORD &rotate, const FCOORD &rerotate, TabVector_LIST *horizontal_lines, int *min_gutter_width)
Definition: tabfind.cpp:1332
static void Update()
Definition: scrollview.cpp:715
static bool WithinTestRegion(int detail_level, int x, int y)
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
static bool DifferentSizes(int size1, int size2)
Definition: tabfind.cpp:408
BLOBNBOX_LIST blobs
Definition: blobbox.h:768
void add(inT32 value, inT32 count)
Definition: statistc.cpp:101
const int kMaxVerticalSearch
Definition: tabfind.cpp:38
inT16 width() const
Definition: rect.h:111
#define MIN(x, y)
Definition: ndminx.h:28
ScrollView * DisplayTabVectors(ScrollView *tab_win)
Definition: tabfind.cpp:503
const int kTabRadiusFactor
Definition: tabfind.cpp:35
const int kRaggedGutterMultiple
Definition: tabfind.cpp:51
bool FindTabVectors(TabVector_LIST *hlines, BLOBNBOX_LIST *image_blobs, TO_BLOCK *block, int min_gutter_width, double tabfind_aligned_gap_fraction, ColPartitionGrid *part_grid, FCOORD *deskew, FCOORD *reskew)
Definition: tabfind.cpp:423
BlobTextFlowType flow() const
Definition: blobbox.h:280
TabVector * RightTabForBox(const TBOX &box, bool crossing, bool extended)
Definition: tabfind.cpp:305
static int SortVectorsByKey(const void *v1, const void *v2)
Definition: tabvector.h:294
static bool VeryDifferentSizes(int size1, int size2)
Definition: tabfind.cpp:414
int RightEdgeForBox(const TBOX &box, bool crossing, bool extended)
Definition: tabfind.cpp:282
const int kMinEvaluatedTabs
Definition: tabfind.cpp:56
const int kColumnWidthFactor
Definition: tabfind.h:42
void Brush(Color color)
Definition: scrollview.cpp:732
void set_with_shrink(int x, int y)
Set from the given x,y, shrinking the vector to fit if needed.
Definition: points.cpp:43
TabType right_tab_type() const
Definition: blobbox.h:262
static int SortKey(const ICOORD &vertical, int x, int y)
Definition: tabvector.h:280
virtual ~TabFind()
Definition: tabfind.cpp:78
void DeleteUnownedNoise()
Definition: blobbox.cpp:1033
int ExtendedOverlap(int top_y, int bottom_y) const
Definition: tabvector.h:208
int push_back(T object)
void set_left(int x)
Definition: rect.h:71
bool CommonWidth(int width)
Definition: tabfind.cpp:395
bool leader_on_right() const
Definition: blobbox.h:349
inT32 get_total() const
Definition: statistc.h:86
ICOORD vertical_skew_
Definition: tabfind.h:367
inT16 bottom() const
Definition: rect.h:61
void plot_noise_blobs(ScrollView *to_win)
Definition: blobbox.cpp:1059
static const STRING & textord_debug_pix()
Definition: alignedblob.h:112
int VOverlap(const TabVector &other) const
Definition: tabvector.h:199
void set_right_rule(int new_right)
Definition: blobbox.h:307
void set_x(float xin)
rewrite function
Definition: points.h:216
const double kCosMaxSkewAngle
Definition: tabfind.cpp:60
bool textord_tabfind_show_finaltabs
Definition: tabfind.cpp:63
BLOBNBOX_LIST noise_blobs
Definition: blobbox.h:770
bool joined_to_prev() const
Definition: blobbox.h:241
static void MergeSimilarTabVectors(const ICOORD &vertical, TabVector_LIST *vectors, BlobGrid *grid)
Definition: tabvector.cpp:361
ScrollView * MakeWindow(int x, int y, const char *window_name)
Definition: bbgrid.h:592
void set_left_rule(int new_left)
Definition: blobbox.h:301
void StartSideSearch(int x, int ymin, int ymax)
Definition: bbgrid.h:749
int LeftEdgeForBox(const TBOX &box, bool crossing, bool extended)
Definition: tabfind.cpp:287
inT16 x() const
access function
Definition: points.h:52
void InsertBlobsToGrid(bool h_spread, bool v_spread, BLOBNBOX_LIST *blobs, BBGrid< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT > *grid)
Definition: tabfind.cpp:92
BLOBNBOX_LIST large_blobs
Definition: blobbox.h:772
BBC * NextSideSearch(bool right_to_left)
Definition: bbgrid.h:764
void set_y(float yin)
rewrite function
Definition: points.h:220
void Rotate(const FCOORD &rotation)
Definition: tabvector.cpp:281
inT16 left() const
Definition: rect.h:68
void Pen(Color color)
Definition: scrollview.cpp:726
float y() const
Definition: points.h:212
TabVector_LIST * vectors()
Definition: tabfind.h:173
void Deskew(const FCOORD &deskew)
void set_right_tab_type(TabType new_type)
Definition: blobbox.h:265
GridSearch< BLOBNBOX, BLOBNBOX_CLIST, BLOBNBOX_C_IT > BlobGridSearch
Definition: blobgrid.h:31
void print() const
Definition: rect.h:270
const int kMinLinesInColumn
Definition: tabfind.cpp:41
const double kMinColumnWidth
inT16 height() const
Definition: rect.h:104
#define MAX(x, y)
Definition: ndminx.h:24
void Rectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:606
int GutterWidth(int bottom_y, int top_y, const TabVector &v, bool ignore_unmergeables, int max_gutter_width, int *required_shift)
Definition: tabfind.cpp:162
int textord_debug_tabfind
Definition: alignedblob.cpp:27
int gridsize() const
Definition: bbgrid.h:63
void set_right(int x)
Definition: rect.h:78
int XAtY(int y) const
Definition: tabvector.h:189
#define tprintf(...)
Definition: tprintf.h:31
const int kMaxRaggedSearch
Definition: tabfind.cpp:39
#define MAX_INT32
Definition: host.h:53
int size() const
Definition: genericvector.h:72
Definition: points.h:189
void set_left_tab_type(TabType new_type)
Definition: blobbox.h:259
int sort_key() const
Definition: tabvector.h:158
TabVector * FindVerticalAlignment(AlignedBlobParams align_params, BLOBNBOX *bbox, int *vertical_x, int *vertical_y)
inT32 pile_count(inT32 value) const
Definition: statistc.h:78
inT16 top() const
Definition: rect.h:54
BLOBNBOX_LIST small_blobs
Definition: blobbox.h:771
void plot_graded_blobs(ScrollView *to_win)
Definition: blobbox.cpp:1067
void rotate_large(const FCOORD &vec)
Definition: rect.cpp:72
const int kMinVerticalSearch
Definition: tabfind.cpp:37
void set_x(inT16 xin)
rewrite function
Definition: points.h:61
ScrollView * FindInitialTabVectors(BLOBNBOX_LIST *image_blobs, int min_gutter_width, double tabfind_aligned_gap_fraction, TO_BLOCK *block)
Definition: tabfind.cpp:520
const ICOORD & bleft() const
Definition: bbgrid.h:72
float x() const
Definition: points.h:209
bool IsLeftTab() const
Definition: tabvector.h:213
bool textord_tabfind_show_initialtabs
Definition: tabfind.cpp:62
void SetupTabSearch(int x, int y, int *min_key, int *max_key)
Definition: tabfind.cpp:496
void GutterWidthAndNeighbourGap(int tab_x, int mean_height, int max_gutter, bool left, BLOBNBOX *bbox, int *gutter_width, int *neighbour_gap)
Definition: tabfind.cpp:209
Definition: rect.h:30
inT16 right() const
Definition: rect.h:75
void set_region_type(BlobRegionType new_type)
Definition: blobbox.h:271
int left_rule() const
Definition: blobbox.h:298
TabFind(int gridsize, const ICOORD &bleft, const ICOORD &tright, TabVector_LIST *vlines, int vertical_x, int vertical_y, int resolution)
Definition: tabfind.cpp:65
void set_right_crossing_rule(int new_right)
Definition: blobbox.h:319
ICOORD tright_
Definition: bbgrid.h:91
const double kMinFractionalLinesInColumn
Definition: tabfind.cpp:45
BlobRegionType region_type() const
Definition: blobbox.h:268
void SetBlobRuleEdges(BLOBNBOX_LIST *blobs)
Definition: tabfind.cpp:143
#define BOOL_VAR(name, val, comment)
Definition: params.h:280
void Init(int gridsize, const ICOORD &bleft, const ICOORD &tright)
Definition: bbgrid.h:447
static void RotateBlobList(const FCOORD &rotation, BLOBNBOX_LIST *blobs)
Definition: tabfind.cpp:1262
void DontFindTabVectors(BLOBNBOX_LIST *image_blobs, TO_BLOCK *block, FCOORD *deskew, FCOORD *reskew)
Definition: tabfind.cpp:458
Definition: statistc.h:33
const ICOORD & botleft() const
Definition: rect.h:88
void set_left_crossing_rule(int new_left)
Definition: blobbox.h:313
TabType left_tab_type() const
Definition: blobbox.h:256
ScrollView * DisplayTabs(const char *window_name, ScrollView *tab_win)
inT16 y() const
access_function
Definition: points.h:56