1 /******************************************************************************
2  *
3  * File:         blkocc.h  (Formerly blockocc.h)
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 #ifndef BLKOCC_H
21 #define BLKOCC_H
22 
23 #include "elst.h"
24 #include "params.h"
25 
26 namespace tesseract {
27 
28 class C_BLOB;
29 
30 /***************************************************************************
31 CLASS REGION_OCC
32 
33   The class REGION_OCC defines a section of outline which exists entirely
34   within a single region. The only data held is the min and max x limits of
35   the outline within the region.
36 
37   REGION_OCCs are held on lists, one list for each region.  The lists are
38   built in sorted order of min x. Overlapping REGION_OCCs are not permitted on
39   a single list. An overlapping region to be added causes the existing region
40   to be extended. This extension may result in the following REGION_OCC on the
41   list overlapping the amended one. In this case the amended REGION_OCC is
42   further extended to include the range of the following one, so that the
43   following one can be deleted.
44 
45 ****************************************************************************/
46 
47 class REGION_OCC : public ELIST_LINK {
48 public:
49   float min_x;         // Lowest x in region
50   float max_x;         // Highest x in region
51   int16_t region_type; // Type of crossing
52 
53   REGION_OCC() = default; // constructor used
54   // only in COPIER etc
REGION_OCC(float min,float max,int16_t region)55   REGION_OCC( // constructor
56       float min, float max, int16_t region) {
57     min_x = min;
58     max_x = max;
59     region_type = region;
60   }
61 };
62 
ELISTIZEH(REGION_OCC)63 ELISTIZEH(REGION_OCC)
64 #define RANGE_IN_BAND(band_max, band_min, range_max, range_min) \
65   (((range_min) >= (band_min)) && ((range_max) < (band_max)))
66 /************************************************************************
67 Adapted from the following procedure so that it can be used in the bands
68 class in an include file...
69 
70 bool    range_in_band[
71               range within band?
72 int16_t band_max,
73 int16_t band_min,
74 int16_t range_max,
75 int16_t range_min]
76 {
77   if ((range_min >= band_min) && (range_max < band_max))
78     return true;
79   else
80     return false;
81 }
82 ***********************************************************************/
83 #define RANGE_OVERLAPS_BAND(band_max, band_min, range_max, range_min) \
84   (((range_max) >= (band_min)) && ((range_min) < (band_max)))
85 /************************************************************************
86 Adapted from the following procedure so that it can be used in the bands
87 class in an include file...
88 
89 bool    range_overlaps_band[
90               range crosses band?
91 int16_t band_max,
92 int16_t band_min,
93 int16_t range_max,
94 int16_t range_min]
95 {
96   if ((range_max >= band_min) && (range_min < band_max))
97     return true;
98   else
99     return false;
100 }
101 ***********************************************************************/
102 /**********************************************************************
103   Bands
104   -----
105 
106   BAND 4
107 --------------------------------
108   BAND 3
109 --------------------------------
110 
111   BAND 2
112 
113 --------------------------------
114 
115   BAND 1
116 
117 Band 0 is the dot band
118 
119 Each band has an error margin above and below. An outline is not considered to
120 have significantly changed bands until it has moved out of the error margin.
121 *************************************************************************/
122 class BAND {
123 public:
124   int16_t max_max; // upper max
125   int16_t max;     // nominal max
126   int16_t min_max; // lower max
127   int16_t max_min; // upper min
128   int16_t min;     // nominal min
129   int16_t min_min; // lower min
130 
131   BAND() = default; // constructor
132 
133   void set(                  // initialise a band
134       int16_t new_max_max,   // upper max
135       int16_t new_max,       // new nominal max
136       int16_t new_min_max,   // new lower max
137       int16_t new_max_min,   // new upper min
138       int16_t new_min,       // new nominal min
139       int16_t new_min_min) { // new lower min
140     max_max = new_max_max;
141     max = new_max;
142     min_max = new_min_max;
143     max_min = new_max_min;
144     min = new_min;
145     min_min = new_min_min;
146   }
147 
148   bool in_minimal( // in minimal limits?
149       float y) {   // y value
150     return (y >= max_min) && (y < min_max);
151   }
152 
153   bool in_nominal( // in nominal limits?
154       float y) {   // y value
155     return (y >= min) && (y < max);
156   }
157 
158   bool in_maximal( // in maximal limits?
159       float y) {   // y value
160     return (y >= min_min) && (y < max_max);
161   }
162 
163   // overlaps min limits?
164   bool range_overlaps_minimal(float y1,   // one range limit
165                               float y2) { // other range limit
166     if (y1 > y2) {
167       return RANGE_OVERLAPS_BAND(min_max, max_min, y1, y2);
168     } else {
169       return RANGE_OVERLAPS_BAND(min_max, max_min, y2, y1);
170     }
171   }
172 
173   // overlaps nom limits?
174   bool range_overlaps_nominal(float y1,   // one range limit
175                               float y2) { // other range limit
176     if (y1 > y2) {
177       return RANGE_OVERLAPS_BAND(max, min, y1, y2);
178     } else {
179       return RANGE_OVERLAPS_BAND(max, min, y2, y1);
180     }
181   }
182 
183   // overlaps max limits?
184   bool range_overlaps_maximal(float y1,   // one range limit
185                               float y2) { // other range limit
186     if (y1 > y2) {
187       return RANGE_OVERLAPS_BAND(max_max, min_min, y1, y2);
188     } else {
189       return RANGE_OVERLAPS_BAND(max_max, min_min, y2, y1);
190     }
191   }
192 
193   bool range_in_minimal( // within min limits?
194       float y1,          // one range limit
195       float y2) {        // other range limit
196     if (y1 > y2) {
197       return RANGE_IN_BAND(min_max, max_min, y1, y2);
198     } else {
199       return RANGE_IN_BAND(min_max, max_min, y2, y1);
200     }
201   }
202 
203   bool range_in_nominal( // within nom limits?
204       float y1,          // one range limit
205       float y2) {        // other range limit
206     if (y1 > y2) {
207       return RANGE_IN_BAND(max, min, y1, y2);
208     } else {
209       return RANGE_IN_BAND(max, min, y2, y1);
210     }
211   }
212 
213   bool range_in_maximal( // within max limits?
214       float y1,          // one range limit
215       float y2) {        // other range limit
216     if (y1 > y2) {
217       return RANGE_IN_BAND(max_max, min_min, y1, y2);
218     } else {
219       return RANGE_IN_BAND(max_max, min_min, y2, y1);
220     }
221   }
222 };
223 
224 /* Standard positions */
225 
226 #define MAX_NUM_BANDS 5
227 #define UNDEFINED_BAND 99
228 #define NO_LOWER_LIMIT -9999
229 #define NO_UPPER_LIMIT 9999
230 
231 #define DOT_BAND 0
232 
233 /* Special occupancy code emitted for the 0 region at the end of a word */
234 
235 #define END_OF_WERD_CODE 255
236 
237 extern double_VAR_H(textord_underline_threshold);
238 
239 bool test_underline(  // look for underlines
240     bool testing_on,  // drawing blob
241     C_BLOB *blob,     // blob to test
242     int16_t baseline, // coords of baseline
243     int16_t xheight   // height of line
244 );
245 
246 } // namespace tesseract
247 
248 #endif
249