tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
intproto.h
1 /******************************************************************************
2  ** Filename: intproto.h
3  ** Purpose: Definition of data structures for integer protos.
4  ** Author: Dan Johnson
5  ** History: Thu Feb 7 12:58:45 1991, DSJ, Created.
6  **
7  ** (c) Copyright Hewlett-Packard Company, 1988.
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 #ifndef INTPROTO_H
20 #define INTPROTO_H
21 
25 #include "genericvector.h"
26 #include "matchdefs.h"
27 #include "mfoutline.h"
28 #include "protos.h"
29 #include "scrollview.h"
30 #include "unicharset.h"
31 
32 class FCOORD;
33 
34 /* define order of params in pruners */
35 #define PRUNER_X 0
36 #define PRUNER_Y 1
37 #define PRUNER_ANGLE 2
38 
39 /* definition of coordinate system offsets for each table parameter */
40 #define ANGLE_SHIFT (0.0)
41 #define X_SHIFT (0.5)
42 #define Y_SHIFT (0.5)
43 
44 #define MAX_PROTO_INDEX 24
45 #define BITS_PER_WERD static_cast<int>(8 * sizeof(uint32_t))
46 /* Script detection: increase this number to 128 */
47 #define MAX_NUM_CONFIGS 64
48 #define MAX_NUM_PROTOS 512
49 #define PROTOS_PER_PROTO_SET 64
50 #define MAX_NUM_PROTO_SETS (MAX_NUM_PROTOS / PROTOS_PER_PROTO_SET)
51 #define NUM_PP_PARAMS 3
52 #define NUM_PP_BUCKETS 64
53 #define NUM_CP_BUCKETS 24
54 #define CLASSES_PER_CP 32
55 #define NUM_BITS_PER_CLASS 2
56 #define CLASS_PRUNER_CLASS_MASK (~(~0u << NUM_BITS_PER_CLASS))
57 #define CLASSES_PER_CP_WERD (CLASSES_PER_CP / NUM_BITS_PER_CLASS)
58 #define PROTOS_PER_PP_WERD BITS_PER_WERD
59 #define BITS_PER_CP_VECTOR (CLASSES_PER_CP * NUM_BITS_PER_CLASS)
60 #define MAX_NUM_CLASS_PRUNERS \
61  ((MAX_NUM_CLASSES + CLASSES_PER_CP - 1) / CLASSES_PER_CP)
62 #define WERDS_PER_CP_VECTOR (BITS_PER_CP_VECTOR / BITS_PER_WERD)
63 #define WERDS_PER_PP_VECTOR \
64  ((PROTOS_PER_PROTO_SET + BITS_PER_WERD - 1) / BITS_PER_WERD)
65 #define WERDS_PER_PP (NUM_PP_PARAMS * NUM_PP_BUCKETS * WERDS_PER_PP_VECTOR)
66 #define WERDS_PER_CP \
67  (NUM_CP_BUCKETS * NUM_CP_BUCKETS * NUM_CP_BUCKETS * WERDS_PER_CP_VECTOR)
68 #define WERDS_PER_CONFIG_VEC \
69  ((MAX_NUM_CONFIGS + BITS_PER_WERD - 1) / BITS_PER_WERD)
70 
71 /* The first 3 dimensions of the CLASS_PRUNER_STRUCT are the
72  * 3 axes of the quantized feature space.
73  * The position of the the bits recorded for each class in the
74  * 4th dimension is determined by using CPrunerWordIndexFor(c),
75  * where c is the corresponding class id. */
77  uint32_t p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS]
78  [WERDS_PER_CP_VECTOR];
79 };
80 
81 typedef struct {
82  int8_t A;
83  uint8_t B;
84  int8_t C;
85  uint8_t Angle;
86  uint32_t Configs[WERDS_PER_CONFIG_VEC];
87 }
88 
90  *INT_PROTO;
91 
92 typedef uint32_t PROTO_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS]
93  [WERDS_PER_PP_VECTOR];
94 
95 typedef struct {
96  PROTO_PRUNER ProtoPruner;
97  INT_PROTO_STRUCT Protos[PROTOS_PER_PROTO_SET];
98 }
99 
101  *PROTO_SET;
102 
103 typedef uint32_t CONFIG_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][4];
104 
105 typedef struct {
106  uint16_t NumProtos;
107  uint8_t NumProtoSets;
108  uint8_t NumConfigs;
109  PROTO_SET ProtoSets[MAX_NUM_PROTO_SETS];
110  uint8_t* ProtoLengths;
111  uint16_t ConfigLengths[MAX_NUM_CONFIGS];
112  int font_set_id; // FontSet id, see above
113 }
114 
116  *INT_CLASS;
117 
118 typedef struct {
121  INT_CLASS Class[MAX_NUM_CLASSES];
122  CLASS_PRUNER_STRUCT* ClassPruners[MAX_NUM_CLASS_PRUNERS];
123 }
124 
126  *INT_TEMPLATES;
127 
128 /* definitions of integer features*/
129 #define MAX_NUM_INT_FEATURES 512
130 #define INT_CHAR_NORM_RANGE 256
131 
133  INT_FEATURE_STRUCT() : X(0), Y(0), Theta(0), CP_misses(0) {}
134  // Builds a feature from an FCOORD for position with all the necessary
135  // clipping and rounding.
136  INT_FEATURE_STRUCT(const FCOORD& pos, uint8_t theta);
137  // Builds a feature from ints with all the necessary clipping and casting.
138  INT_FEATURE_STRUCT(int x, int y, int theta);
139 
140  uint8_t X;
141  uint8_t Y;
142  uint8_t Theta;
143  int8_t CP_misses;
144 
145  void print() const { tprintf("(%d,%d):%d\n", X, Y, Theta); }
146 };
147 
149 
150 typedef INT_FEATURE_STRUCT INT_FEATURE_ARRAY[MAX_NUM_INT_FEATURES];
151 
152 enum IntmatcherDebugAction {
153  IDA_ADAPTIVE,
154  IDA_STATIC,
155  IDA_SHAPE_INDEX,
156  IDA_BOTH
157 };
158 
163 #define MaxNumIntProtosIn(C) (C->NumProtoSets * PROTOS_PER_PROTO_SET)
164 #define SetForProto(P) (P / PROTOS_PER_PROTO_SET)
165 #define IndexForProto(P) (P % PROTOS_PER_PROTO_SET)
166 #define ProtoForProtoId(C, P) \
167  (&((C->ProtoSets[SetForProto(P)])->Protos[IndexForProto(P)]))
168 #define PPrunerWordIndexFor(I) \
169  (((I) % PROTOS_PER_PROTO_SET) / PROTOS_PER_PP_WERD)
170 #define PPrunerBitIndexFor(I) ((I) % PROTOS_PER_PP_WERD)
171 #define PPrunerMaskFor(I) (1 << PPrunerBitIndexFor(I))
172 
173 #define MaxNumClassesIn(T) (T->NumClassPruners * CLASSES_PER_CP)
174 #define LegalClassId(c) ((c) >= 0 && (c) <= MAX_CLASS_ID)
175 #define UnusedClassIdIn(T, c) ((T)->Class[c] == nullptr)
176 #define ClassForClassId(T, c) ((T)->Class[c])
177 #define ClassPrunersFor(T) ((T)->ClassPruner)
178 #define CPrunerIdFor(c) ((c) / CLASSES_PER_CP)
179 #define CPrunerFor(T, c) ((T)->ClassPruners[CPrunerIdFor(c)])
180 #define CPrunerWordIndexFor(c) (((c) % CLASSES_PER_CP) / CLASSES_PER_CP_WERD)
181 #define CPrunerBitIndexFor(c) (((c) % CLASSES_PER_CP) % CLASSES_PER_CP_WERD)
182 #define CPrunerMaskFor(L, c) \
183  (((L) + 1) << CPrunerBitIndexFor(c) * NUM_BITS_PER_CLASS)
184 
185 /* DEBUG macros*/
186 #define PRINT_MATCH_SUMMARY 0x001
187 #define DISPLAY_FEATURE_MATCHES 0x002
188 #define DISPLAY_PROTO_MATCHES 0x004
189 #define PRINT_FEATURE_MATCHES 0x008
190 #define PRINT_PROTO_MATCHES 0x010
191 #define CLIP_MATCH_EVIDENCE 0x020
192 
193 #define MatchDebuggingOn(D) (D)
194 #define PrintMatchSummaryOn(D) ((D)&PRINT_MATCH_SUMMARY)
195 #define DisplayFeatureMatchesOn(D) ((D)&DISPLAY_FEATURE_MATCHES)
196 #define DisplayProtoMatchesOn(D) ((D)&DISPLAY_PROTO_MATCHES)
197 #define PrintFeatureMatchesOn(D) ((D)&PRINT_FEATURE_MATCHES)
198 #define PrintProtoMatchesOn(D) ((D)&PRINT_PROTO_MATCHES)
199 #define ClipMatchEvidenceOn(D) ((D)&CLIP_MATCH_EVIDENCE)
200 
204 void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class);
205 
206 int AddIntConfig(INT_CLASS Class);
207 
208 int AddIntProto(INT_CLASS Class);
209 
210 void AddProtoToClassPruner(PROTO Proto, CLASS_ID ClassId,
211  INT_TEMPLATES Templates);
212 
213 void AddProtoToProtoPruner(PROTO Proto, int ProtoId, INT_CLASS Class,
214  bool debug);
215 
216 uint8_t Bucket8For(float param, float offset, int num_buckets);
217 uint16_t Bucket16For(float param, float offset, int num_buckets);
218 
219 uint8_t CircBucketFor(float param, float offset, int num_buckets);
220 
221 void UpdateMatchDisplay();
222 
223 void ConvertConfig(BIT_VECTOR Config, int ConfigId, INT_CLASS Class);
224 
225 void DisplayIntFeature(const INT_FEATURE_STRUCT* Feature, float Evidence);
226 
227 void DisplayIntProto(INT_CLASS Class, PROTO_ID ProtoId, float Evidence);
228 
229 INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs);
230 
231 INT_TEMPLATES NewIntTemplates();
232 
233 void free_int_templates(INT_TEMPLATES templates);
234 
235 void ShowMatchDisplay();
236 
237 namespace tesseract {
238 
239 // Clears the given window and draws the featurespace guides for the
240 // appropriate normalization method.
241 void ClearFeatureSpaceWindow(NORM_METHOD norm_method, ScrollView* window);
242 
243 } // namespace tesseract.
244 
245 /*----------------------------------------------------------------------------*/
246 #ifndef GRAPHICS_DISABLED
247 void RenderIntFeature(ScrollView* window, const INT_FEATURE_STRUCT* Feature,
248  ScrollView::Color color);
249 
250 void InitIntMatchWindowIfReqd();
251 
252 void InitProtoDisplayWindowIfReqd();
253 
254 void InitFeatureDisplayWindowIfReqd();
255 
256 // Creates a window of the appropriate size for displaying elements
257 // in feature space.
258 ScrollView* CreateFeatureSpaceWindow(const char* name, int xpos, int ypos);
259 #endif // GRAPHICS_DISABLED
260 
261 #endif
int8_t C
Definition: intproto.h:84
INT_FEATURE_STRUCT()
Definition: intproto.h:133
void print() const
Definition: intproto.h:145
uint8_t NumProtoSets
Definition: intproto.h:107
int8_t CP_misses
Definition: intproto.h:143
uint8_t B
Definition: intproto.h:83
Definition: intproto.h:118
uint8_t Y
Definition: intproto.h:141
Color
Definition: scrollview.h:105
uint8_t NumConfigs
Definition: intproto.h:108
Definition: intproto.h:81
Definition: baseapi.cpp:94
Definition: intproto.h:95
Definition: protos.h:42
uint32_t p[24][24][24][((32 *2)/static_cast< int >(8 *sizeof(uint32_t)))]
Definition: intproto.h:78
Definition: intproto.h:105
uint8_t X
Definition: intproto.h:140
int font_set_id
Definition: intproto.h:112
Definition: scrollview.h:102
PROTO_PRUNER ProtoPruner
Definition: intproto.h:96
int8_t A
Definition: intproto.h:82
uint8_t * ProtoLengths
Definition: intproto.h:110
int NumClassPruners
Definition: intproto.h:120
uint8_t Theta
Definition: intproto.h:142
int NumClasses
Definition: intproto.h:119
Definition: intproto.h:132
Definition: intproto.h:76
uint8_t Angle
Definition: intproto.h:85
Definition: points.h:189
uint16_t NumProtos
Definition: intproto.h:106