tesseract  4.0.0-1-g2a2b
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  * Created: Fri Nov 8
7  * Modified:
8  * Language: C++
9  * Package: N/A
10  * Status: Experimental (Do Not Distribute)
11  *
12  * (c) Copyright 1991, Hewlett-Packard Company.
13  ** Licensed under the Apache License, Version 2.0 (the "License");
14  ** you may not use this file except in compliance with the License.
15  ** You may obtain a copy of the License at
16  ** http://www.apache.org/licenses/LICENSE-2.0
17  ** Unless required by applicable law or agreed to in writing, software
18  ** distributed under the License is distributed on an "AS IS" BASIS,
19  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  ** See the License for the specific language governing permissions and
21  ** limitations under the License.
22  *
23  ******************************************************************************/
24 
25 /*
26 ----------------------------------------------------------------------
27  I n c l u d e s
28 ----------------------------------------------------------------------
29 */
30 
31 #include <cctype>
32 #include <cmath>
33 #include "errcode.h"
34 #include "drawtord.h"
35 #include "blkocc.h"
36 #include "helpers.h"
37 
38 double_VAR(textord_underline_threshold, 0.5, "Fraction of width occupied");
39 
40 // Forward declarations of static functions
41 static void horizontal_cblob_projection(C_BLOB *blob, // blob to project
42  STATS *stats); // output
43 static void horizontal_coutline_projection(C_OUTLINE *outline,
44  STATS *stats); // output
45 
53 bool test_underline( //look for underlines
54  bool testing_on, //< drawing blob
55  C_BLOB* blob, //< blob to test
56  int16_t baseline, //< coords of baseline
57  int16_t xheight //< height of line
58 ) {
59  int16_t occ;
60  int16_t blob_width; //width of blob
61  TBOX blob_box; //bounding box
62  int32_t desc_occ;
63  int32_t x_occ;
64  int32_t asc_occ;
65  STATS projection;
66 
67  blob_box = blob->bounding_box ();
68  blob_width = blob->bounding_box ().width ();
69  projection.set_range (blob_box.bottom (), blob_box.top () + 1);
70  if (testing_on) {
71  // blob->plot(to_win,GOLDENROD,GOLDENROD);
72  // line_color_index(to_win,GOLDENROD);
73  // move2d(to_win,blob_box.left(),baseline);
74  // draw2d(to_win,blob_box.right(),baseline);
75  // move2d(to_win,blob_box.left(),baseline+xheight);
76  // draw2d(to_win,blob_box.right(),baseline+xheight);
77  tprintf
78  ("Testing underline on blob at (%d,%d)->(%d,%d), base=%d\nOccs:",
79  blob->bounding_box ().left (), blob->bounding_box ().bottom (),
80  blob->bounding_box ().right (), blob->bounding_box ().top (),
81  baseline);
82  }
83  horizontal_cblob_projection(blob, &projection);
84  desc_occ = 0;
85  for (occ = blob_box.bottom (); occ < baseline; occ++)
86  if (occ <= blob_box.top () && projection.pile_count (occ) > desc_occ)
87  //max in region
88  desc_occ = projection.pile_count (occ);
89  x_occ = 0;
90  for (occ = baseline; occ <= baseline + xheight; occ++)
91  if (occ >= blob_box.bottom () && occ <= blob_box.top ()
92  && projection.pile_count (occ) > x_occ)
93  //max in region
94  x_occ = projection.pile_count (occ);
95  asc_occ = 0;
96  for (occ = baseline + xheight + 1; occ <= blob_box.top (); occ++)
97  if (occ >= blob_box.bottom () && projection.pile_count (occ) > asc_occ)
98  asc_occ = projection.pile_count (occ);
99  if (testing_on) {
100  tprintf ("%d %d %d\n", desc_occ, x_occ, asc_occ);
101  }
102  if (desc_occ == 0 && x_occ == 0 && asc_occ == 0) {
103  tprintf ("Bottom=%d, top=%d, base=%d, x=%d\n",
104  blob_box.bottom (), blob_box.top (), baseline, xheight);
105  projection.print();
106  }
107  if (desc_occ > x_occ + x_occ
108  && desc_occ > blob_width * textord_underline_threshold)
109  return true; //real underline
110  return asc_occ > x_occ + x_occ &&
111  asc_occ > blob_width * textord_underline_threshold; //overline
112  //neither
113 }
114 
115 
123 static void horizontal_cblob_projection( //project outlines
124  C_BLOB *blob, //< blob to project
125  STATS *stats //< output
126  ) {
127  //outlines of blob
128  C_OUTLINE_IT out_it = blob->out_list ();
129 
130  for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
131  horizontal_coutline_projection (out_it.data (), stats);
132  }
133 }
134 
135 
143 static void horizontal_coutline_projection( //project outlines
144  C_OUTLINE *outline, //< outline to project
145  STATS *stats //< output
146  ) {
147  ICOORD pos; //current point
148  ICOORD step; //edge step
149  int32_t length; //of outline
150  int16_t stepindex; //current step
151  C_OUTLINE_IT out_it = outline->child ();
152 
153  pos = outline->start_pos ();
154  length = outline->pathlength ();
155  for (stepindex = 0; stepindex < length; stepindex++) {
156  step = outline->step (stepindex);
157  if (step.y () > 0) {
158  stats->add (pos.y (), pos.x ());
159  }
160  else if (step.y () < 0) {
161  stats->add (pos.y () - 1, -pos.x ());
162  }
163  pos += step;
164  }
165 
166  for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
167  horizontal_coutline_projection (out_it.data (), stats);
168  }
169 }
int32_t pile_count(int32_t value) const
Definition: statistc.h:78
double textord_underline_threshold
Definition: blkocc.cpp:38
#define double_VAR(name, val, comment)
Definition: params.h:285
int16_t y() const
access_function
Definition: points.h:57
Definition: rect.h:34
const ICOORD & start_pos() const
Definition: coutln.h:148
Definition: statistc.h:33
int16_t width() const
Definition: rect.h:115
int16_t left() const
Definition: rect.h:72
int16_t top() const
Definition: rect.h:58
integer coordinate
Definition: points.h:32
int16_t x() const
access function
Definition: points.h:53
bool set_range(int32_t min_bucket_value, int32_t max_bucket_value_plus_1)
Definition: statistc.cpp:63
int32_t pathlength() const
Definition: coutln.h:135
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
bool test_underline(bool testing_on, C_BLOB *blob, int16_t baseline, int16_t xheight)
Definition: blkocc.cpp:53
void add(int32_t value, int32_t count)
Definition: statistc.cpp:100
C_OUTLINE_LIST * child()
Definition: coutln.h:108
TBOX bounding_box() const
Definition: stepblob.cpp:255
C_OUTLINE_LIST * out_list()
Definition: stepblob.h:70
void print() const
Definition: statistc.cpp:533
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65
ICOORD step(int index) const
Definition: coutln.h:144