tesseract 4.1.1
Loading...
Searching...
No Matches
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 "helpers.h"
32
33double_VAR(textord_underline_threshold, 0.5, "Fraction of width occupied");
34
35// Forward declarations of static functions
36static void horizontal_cblob_projection(C_BLOB *blob, // blob to project
37 STATS *stats); // output
38static void horizontal_coutline_projection(C_OUTLINE *outline,
39 STATS *stats); // output
40
48bool 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);
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
118static 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
138static 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}
#define double_VAR(name, val, comment)
Definition: params.h:312
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:35
@ baseline
Definition: mfoutline.h:63
bool test_underline(bool testing_on, C_BLOB *blob, int16_t baseline, int16_t xheight)
Definition: blkocc.cpp:48
double textord_underline_threshold
Definition: blkocc.cpp:33
C_OUTLINE_LIST * child()
Definition: coutln.h:108
ICOORD step(int index) const
Definition: coutln.h:144
const ICOORD & start_pos() const
Definition: coutln.h:148
int32_t pathlength() const
Definition: coutln.h:135
integer coordinate
Definition: points.h:32
int16_t y() const
access_function
Definition: points.h:56
int16_t x() const
access function
Definition: points.h:52
Definition: rect.h:34
int16_t top() const
Definition: rect.h:58
int16_t width() const
Definition: rect.h:115
int16_t left() const
Definition: rect.h:72
int16_t bottom() const
Definition: rect.h:65
int16_t right() const
Definition: rect.h:79
Definition: statistc.h:31
int32_t pile_count(int32_t value) const
Definition: statistc.h:76
void add(int32_t value, int32_t count)
Definition: statistc.cpp:93
bool set_range(int32_t min_bucket_value, int32_t max_bucket_value_plus_1)
Definition: statistc.cpp:56
void print() const
Definition: statistc.cpp:526
TBOX bounding_box() const
Definition: stepblob.cpp:253
C_OUTLINE_LIST * out_list()
Definition: stepblob.h:70