tesseract  5.0.0-alpha-619-ge9db
GAPMAP Class Reference

#include <gap_map.h>

Public Member Functions

 GAPMAP (TO_BLOCK *block)
 
 ~GAPMAP ()
 
bool table_gap (int16_t left, int16_t right)
 

Detailed Description

Definition at line 16 of file gap_map.h.

Constructor & Destructor Documentation

◆ GAPMAP()

GAPMAP::GAPMAP ( TO_BLOCK block)

Definition at line 33 of file gap_map.cpp.

36  {
37  TO_ROW *row; //current row
38  BLOBNBOX_IT blob_it; //iterator
39  TBOX blob_box;
40  TBOX prev_blob_box;
41  int16_t gap_width;
42  int16_t start_of_row;
43  int16_t end_of_row;
44  STATS xht_stats (0, 128);
45  int16_t min_quantum;
46  int16_t max_quantum;
47  int16_t i;
48 
49  /*
50  Find left and right extremes and bucket size
51  */
52  map = nullptr;
53  min_left = INT16_MAX;
54  max_right = -INT16_MAX;
55  total_rows = 0;
56  any_tabs = false;
57 
58  // row iterator
59  TO_ROW_IT row_it(block->get_rows());
60  for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
61  row = row_it.data ();
62  if (!row->blob_list ()->empty ()) {
63  total_rows++;
64  xht_stats.add (static_cast<int16_t>(floor (row->xheight + 0.5)), 1);
65  blob_it.set_to_list (row->blob_list ());
66  start_of_row = blob_it.data ()->bounding_box ().left ();
67  end_of_row = blob_it.data_relative (-1)->bounding_box ().right ();
68  if (min_left > start_of_row)
69  min_left = start_of_row;
70  if (max_right < end_of_row)
71  max_right = end_of_row;
72  }
73  }
74  if ((total_rows < 3) || (min_left >= max_right)) {
75  bucket_size = 0;
76  map_max = 0;
77  total_rows = 0;
78  min_left = max_right = 0;
79  return;
80  }
81  bucket_size = static_cast<int16_t>(floor (xht_stats.median () + 0.5)) / 2;
82  map_max = (max_right - min_left) / bucket_size;
83  map = new int16_t[map_max + 1];
84  for (i = 0; i <= map_max; i++)
85  map[i] = 0;
86 
87  for (row_it.mark_cycle_pt (); !row_it.cycled_list (); row_it.forward ()) {
88  row = row_it.data ();
89  if (!row->blob_list ()->empty ()) {
90  blob_it.set_to_list (row->blob_list ());
91  blob_it.mark_cycle_pt ();
92  blob_box = box_next (&blob_it);
93  prev_blob_box = blob_box;
94  if (gapmap_use_ends) {
95  /* Leading space */
96  gap_width = blob_box.left () - min_left;
97  if ((gap_width > gapmap_big_gaps * row->xheight)
98  && gap_width > 2) {
99  max_quantum = (blob_box.left () - min_left) / bucket_size;
100  if (max_quantum > map_max) max_quantum = map_max;
101  for (i = 0; i <= max_quantum; i++)
102  map[i]++;
103  }
104  }
105  while (!blob_it.cycled_list ()) {
106  blob_box = box_next (&blob_it);
107  gap_width = blob_box.left () - prev_blob_box.right ();
108  if ((gap_width > gapmap_big_gaps * row->xheight)
109  && gap_width > 2) {
110  min_quantum =
111  (prev_blob_box.right () - min_left) / bucket_size;
112  max_quantum = (blob_box.left () - min_left) / bucket_size;
113  if (max_quantum > map_max) max_quantum = map_max;
114  for (i = min_quantum; i <= max_quantum; i++)
115  map[i]++;
116  }
117  prev_blob_box = blob_box;
118  }
119  if (gapmap_use_ends) {
120  /* Trailing space */
121  gap_width = max_right - prev_blob_box.right ();
122  if ((gap_width > gapmap_big_gaps * row->xheight)
123  && gap_width > 2) {
124  min_quantum =
125  (prev_blob_box.right () - min_left) / bucket_size;
126  if (min_quantum < 0) min_quantum = 0;
127  for (i = min_quantum; i <= map_max; i++)
128  map[i]++;
129  }
130  }
131  }
132  }
133  for (i = 0; i <= map_max; i++) {
134  if (map[i] > total_rows / 2) {
136  (((i == 0) &&
137  (map[i + 1] <= total_rows / 2)) ||
138  ((i == map_max) &&
139  (map[i - 1] <= total_rows / 2)) ||
140  ((i > 0) &&
141  (i < map_max) &&
142  (map[i - 1] <= total_rows / 2) &&
143  (map[i + 1] <= total_rows / 2)))) {
144  map[i] = 0; //prevent isolated quantum
145  }
146  else
147  any_tabs = true;
148  }
149  }
150  if (gapmap_debug && any_tabs)
151  tprintf ("Table found\n");

◆ ~GAPMAP()

GAPMAP::~GAPMAP ( )
inline

Definition at line 22 of file gap_map.h.

22  { //destructor
23  delete[] map;
24  }

Member Function Documentation

◆ table_gap()

bool GAPMAP::table_gap ( int16_t  left,
int16_t  right 
)

Definition at line 159 of file gap_map.cpp.

164  {
165  int16_t min_quantum;
166  int16_t max_quantum;
167  int16_t i;
168  bool tab_found = false;
169 
170  if (!any_tabs)
171  return false;
172 
173  min_quantum = (left - min_left) / bucket_size;
174  max_quantum = (right - min_left) / bucket_size;
175  // Clip to the bounds of the array. In some circumstances (big blob followed
176  // by small blob) max_quantum can exceed the map_max bounds, but we clip
177  // here instead, as it provides better long-term safety.
178  if (min_quantum < 0) min_quantum = 0;
179  if (max_quantum > map_max) max_quantum = map_max;
180  for (i = min_quantum; (!tab_found && (i <= max_quantum)); i++)
181  if (map[i] > total_rows / 2)
182  tab_found = true;

The documentation for this class was generated from the following files:
gapmap_no_isolated_quanta
bool gapmap_no_isolated_quanta
Definition: gap_map.cpp:17
gapmap_debug
bool gapmap_debug
Definition: gap_map.cpp:14
TO_ROW::xheight
float xheight
Definition: blobbox.h:656
STATS
Definition: statistc.h:30
TO_BLOCK::get_rows
TO_ROW_LIST * get_rows()
Definition: blobbox.h:703
box_next
TBOX box_next(BLOBNBOX_IT *it)
Definition: blobbox.cpp:629
TBOX::left
int16_t left() const
Definition: rect.h:71
TBOX::right
int16_t right() const
Definition: rect.h:78
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
TO_ROW
Definition: blobbox.h:543
gapmap_big_gaps
double gapmap_big_gaps
Definition: gap_map.cpp:18
TO_ROW::blob_list
BLOBNBOX_LIST * blob_list()
Definition: blobbox.h:599
gapmap_use_ends
bool gapmap_use_ends
Definition: gap_map.cpp:15
TBOX
Definition: rect.h:33