tesseract  5.0.0-alpha-619-ge9db
blkocc.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  *
3  * File: blkocc.cpp (Formerly blockocc.c)
4  * Description: Block Occupancy routines
5  * Author: Chris Newton
6  *
7  * (c) Copyright 1991, Hewlett-Packard Company.
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  *
18  ******************************************************************************/
19 
20 /*
21 ----------------------------------------------------------------------
22  I n c l u d e s
23 ----------------------------------------------------------------------
24 */
25 
26 #include <cctype>
27 #include <cmath>
28 #include "errcode.h"
29 #include "drawtord.h"
30 #include "blkocc.h"
31 #include <tesseract/helpers.h>
32 
33 double_VAR(textord_underline_threshold, 0.5, "Fraction of width occupied");
34 
35 // Forward declarations of static functions
36 static void horizontal_cblob_projection(C_BLOB *blob, // blob to project
37  STATS *stats); // output
38 static void horizontal_coutline_projection(C_OUTLINE *outline,
39  STATS *stats); // output
40 
48 bool test_underline( //look for underlines
49  bool testing_on,
50  C_BLOB* blob,
51  int16_t baseline,
52  int16_t xheight
53 ) {
54  int16_t occ;
55  int16_t blob_width; //width of blob
56  TBOX blob_box; //bounding box
57  int32_t desc_occ;
58  int32_t x_occ;
59  int32_t asc_occ;
60  STATS projection;
61 
62  blob_box = blob->bounding_box ();
63  blob_width = blob->bounding_box ().width ();
64  projection.set_range (blob_box.bottom (), blob_box.top () + 1);
65  if (testing_on) {
66  // blob->plot(to_win,GOLDENROD,GOLDENROD);
67  // line_color_index(to_win,GOLDENROD);
68  // move2d(to_win,blob_box.left(),baseline);
69  // draw2d(to_win,blob_box.right(),baseline);
70  // move2d(to_win,blob_box.left(),baseline+xheight);
71  // draw2d(to_win,blob_box.right(),baseline+xheight);
72  tprintf
73  ("Testing underline on blob at (%d,%d)->(%d,%d), base=%d\nOccs:",
74  blob->bounding_box ().left (), blob->bounding_box ().bottom (),
75  blob->bounding_box ().right (), blob->bounding_box ().top (),
76  baseline);
77  }
78  horizontal_cblob_projection(blob, &projection);
79  desc_occ = 0;
80  for (occ = blob_box.bottom (); occ < baseline; occ++)
81  if (occ <= blob_box.top () && projection.pile_count (occ) > desc_occ)
82  //max in region
83  desc_occ = projection.pile_count (occ);
84  x_occ = 0;
85  for (occ = baseline; occ <= baseline + xheight; occ++)
86  if (occ >= blob_box.bottom () && occ <= blob_box.top ()
87  && projection.pile_count (occ) > x_occ)
88  //max in region
89  x_occ = projection.pile_count (occ);
90  asc_occ = 0;
91  for (occ = baseline + xheight + 1; occ <= blob_box.top (); occ++)
92  if (occ >= blob_box.bottom () && projection.pile_count (occ) > asc_occ)
93  asc_occ = projection.pile_count (occ);
94  if (testing_on) {
95  tprintf ("%d %d %d\n", desc_occ, x_occ, asc_occ);
96  }
97  if (desc_occ == 0 && x_occ == 0 && asc_occ == 0) {
98  tprintf ("Bottom=%d, top=%d, base=%d, x=%d\n",
99  blob_box.bottom (), blob_box.top (), baseline, xheight);
100  projection.print();
101  }
102  if (desc_occ > x_occ + x_occ
103  && desc_occ > blob_width * textord_underline_threshold)
104  return true; //real underline
105  return asc_occ > x_occ + x_occ &&
106  asc_occ > blob_width * textord_underline_threshold; //overline
107  //neither
108 }
109 
110 
118 static void horizontal_cblob_projection( //project outlines
119  C_BLOB *blob,
120  STATS *stats
121  ) {
122  //outlines of blob
123  C_OUTLINE_IT out_it = blob->out_list ();
124 
125  for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
126  horizontal_coutline_projection (out_it.data (), stats);
127  }
128 }
129 
130 
138 static void horizontal_coutline_projection( //project outlines
139  C_OUTLINE *outline,
140  STATS *stats
141  ) {
142  ICOORD pos; //current point
143  ICOORD step; //edge step
144  int32_t length; //of outline
145  int16_t stepindex; //current step
146  C_OUTLINE_IT out_it = outline->child ();
147 
148  pos = outline->start_pos ();
149  length = outline->pathlength ();
150  for (stepindex = 0; stepindex < length; stepindex++) {
151  step = outline->step (stepindex);
152  if (step.y () > 0) {
153  stats->add (pos.y (), pos.x ());
154  }
155  else if (step.y () < 0) {
156  stats->add (pos.y () - 1, -pos.x ());
157  }
158  pos += step;
159  }
160 
161  for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
162  horizontal_coutline_projection (out_it.data (), stats);
163  }
164 }
C_BLOB::bounding_box
TBOX bounding_box() const
Definition: stepblob.cpp:247
C_BLOB::out_list
C_OUTLINE_LIST * out_list()
Definition: stepblob.h:69
test_underline
bool test_underline(bool testing_on, C_BLOB *blob, int16_t baseline, int16_t xheight)
Definition: blkocc.cpp:48
baseline
Definition: mfoutline.h:62
ICOORD
integer coordinate
Definition: points.h:30
TBOX::top
int16_t top() const
Definition: rect.h:57
STATS::pile_count
int32_t pile_count(int32_t value) const
Definition: statistc.h:75
ICOORD::x
int16_t x() const
access function
Definition: points.h:51
C_BLOB
Definition: stepblob.h:36
C_OUTLINE
Definition: coutln.h:71
textord_underline_threshold
double textord_underline_threshold
Definition: blkocc.cpp:33
TBOX::width
int16_t width() const
Definition: rect.h:114
TBOX::bottom
int16_t bottom() const
Definition: rect.h:64
helpers.h
double_VAR
#define double_VAR(name, val, comment)
Definition: params.h:309
STATS
Definition: statistc.h:30
drawtord.h
TBOX::left
int16_t left() const
Definition: rect.h:71
STATS::add
void add(int32_t value, int32_t count)
Definition: statistc.cpp:87
TBOX::right
int16_t right() const
Definition: rect.h:78
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
errcode.h
C_OUTLINE::pathlength
int32_t pathlength() const
Definition: coutln.h:134
C_OUTLINE::step
ICOORD step(int index) const
Definition: coutln.h:143
C_OUTLINE::child
C_OUTLINE_LIST * child()
Definition: coutln.h:107
STATS::set_range
bool set_range(int32_t min_bucket_value, int32_t max_bucket_value_plus_1)
Definition: statistc.cpp:53
STATS::print
void print() const
Definition: statistc.cpp:509
C_OUTLINE::start_pos
const ICOORD & start_pos() const
Definition: coutln.h:147
ICOORD::y
int16_t y() const
access_function
Definition: points.h:55
TBOX
Definition: rect.h:33
blkocc.h