Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 "mfcpch.h"
32 #include <ctype.h>
33 #include <math.h>
34 #include "errcode.h"
35 #include "drawtord.h"
36 #include "blkocc.h"
37 #include "helpers.h"
38 #include "notdll.h"
39
40 double_VAR(textord_underline_threshold, 0.5, "Fraction of width occupied");
41
42 // Forward declarations of static functions
43 static void horizontal_cblob_projection(C_BLOB *blob, // blob to project
44 STATS *stats); // output
45 static void horizontal_coutline_projection(C_OUTLINE *outline,
46 STATS *stats); // output
47
55 BOOL8 test_underline( //look for underlines
56 BOOL8 testing_on, //< drawing blob
57 C_BLOB *blob, //< blob to test
58 inT16 baseline, //< coords of baseline
59 inT16 xheight //< height of line
60 ) {
61 inT16 occ;
62 inT16 blob_width; //width of blob
63 TBOX blob_box; //bounding box
64 inT32 desc_occ;
65 inT32 x_occ;
66 inT32 asc_occ;
67 STATS projection;
68
69 blob_box = blob->bounding_box ();
70 blob_width = blob->bounding_box ().width ();
71 projection.set_range (blob_box.bottom (), blob_box.top () + 1);
72 if (testing_on) {
73 // blob->plot(to_win,GOLDENROD,GOLDENROD);
74 // line_color_index(to_win,GOLDENROD);
75 // move2d(to_win,blob_box.left(),baseline);
76 // draw2d(to_win,blob_box.right(),baseline);
77 // move2d(to_win,blob_box.left(),baseline+xheight);
78 // draw2d(to_win,blob_box.right(),baseline+xheight);
79 tprintf
80 ("Testing underline on blob at (%d,%d)->(%d,%d), base=%d\nOccs:",
81 blob->bounding_box ().left (), blob->bounding_box ().bottom (),
82 blob->bounding_box ().right (), blob->bounding_box ().top (),
83 baseline);
84 }
85 horizontal_cblob_projection(blob, &projection);
86 desc_occ = 0;
87 for (occ = blob_box.bottom (); occ < baseline; occ++)
88 if (occ <= blob_box.top () && projection.pile_count (occ) > desc_occ)
89 //max in region
90 desc_occ = projection.pile_count (occ);
91 x_occ = 0;
92 for (occ = baseline; occ <= baseline + xheight; occ++)
93 if (occ >= blob_box.bottom () && occ <= blob_box.top ()
94 && projection.pile_count (occ) > x_occ)
95 //max in region
96 x_occ = projection.pile_count (occ);
97 asc_occ = 0;
98 for (occ = baseline + xheight + 1; occ <= blob_box.top (); occ++)
99 if (occ >= blob_box.bottom () && projection.pile_count (occ) > asc_occ)
100 asc_occ = projection.pile_count (occ);
101 if (testing_on) {
102 tprintf ("%d %d %d\n", desc_occ, x_occ, asc_occ);
103 }
104 if (desc_occ == 0 && x_occ == 0 && asc_occ == 0) {
105 tprintf ("Bottom=%d, top=%d, base=%d, x=%d\n",
106 blob_box.bottom (), blob_box.top (), baseline, xheight);
107 projection.print();
108 }
109 if (desc_occ > x_occ + x_occ
110 && desc_occ > blob_width * textord_underline_threshold)
111 return TRUE; //real underline
112 if (asc_occ > x_occ + x_occ
113 && asc_occ > blob_width * textord_underline_threshold)
114 return TRUE; //overline
115 return FALSE; //neither
116 }
117
118
126 static void horizontal_cblob_projection( //project outlines
127 C_BLOB *blob, //< blob to project
128 STATS *stats //< output
129 ) {
130 //outlines of blob
131 C_OUTLINE_IT out_it = blob->out_list ();
132
133 for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
134 horizontal_coutline_projection (out_it.data (), stats);
135 }
136 }
137
138
146 static void horizontal_coutline_projection( //project outlines
147 C_OUTLINE *outline, //< outline to project
148 STATS *stats //< output
149 ) {
150 ICOORD pos; //current point
151 ICOORD step; //edge step
152 inT32 length; //of outline
153 inT16 stepindex; //current step
154 C_OUTLINE_IT out_it = outline->child ();
155
156 pos = outline->start_pos ();
157 length = outline->pathlength ();
158 for (stepindex = 0; stepindex < length; stepindex++) {
159 step = outline->step (stepindex);
160 if (step.y () > 0) {
161 stats->add (pos.y (), pos.x ());
162 }
163 else if (step.y () < 0) {
164 stats->add (pos.y () - 1, -pos.x ());
165 }
166 pos += step;
167 }
168
169 for (out_it.mark_cycle_pt (); !out_it.cycled_list (); out_it.forward ()) {
170 horizontal_coutline_projection (out_it.data (), stats);
171 }
172 }