tesseract  5.0.0
intproto.h
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: intproto.h
3  ** Purpose: Definition of data structures for integer protos.
4  ** Author: Dan Johnson
5  **
6  ** (c) Copyright Hewlett-Packard Company, 1988.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  *****************************************************************************/
17 
18 #ifndef INTPROTO_H
19 #define INTPROTO_H
20 
24 #include "matchdefs.h"
25 #include "mfoutline.h"
26 #include "protos.h"
27 #include "scrollview.h"
28 #include "unicharset.h"
29 
30 namespace tesseract {
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 ((MAX_NUM_CLASSES + CLASSES_PER_CP - 1) / CLASSES_PER_CP)
61 #define WERDS_PER_CP_VECTOR (BITS_PER_CP_VECTOR / BITS_PER_WERD)
62 #define WERDS_PER_PP_VECTOR ((PROTOS_PER_PROTO_SET + BITS_PER_WERD - 1) / BITS_PER_WERD)
63 #define WERDS_PER_PP (NUM_PP_PARAMS * NUM_PP_BUCKETS * WERDS_PER_PP_VECTOR)
64 #define WERDS_PER_CP (NUM_CP_BUCKETS * NUM_CP_BUCKETS * NUM_CP_BUCKETS * WERDS_PER_CP_VECTOR)
65 #define WERDS_PER_CONFIG_VEC ((MAX_NUM_CONFIGS + BITS_PER_WERD - 1) / BITS_PER_WERD)
66 
67 /* The first 3 dimensions of the CLASS_PRUNER_STRUCT are the
68  * 3 axes of the quantized feature space.
69  * The position of the the bits recorded for each class in the
70  * 4th dimension is determined by using CPrunerWordIndexFor(c),
71  * where c is the corresponding class id. */
74 };
75 
77  int8_t A;
78  uint8_t B;
79  int8_t C;
80  uint8_t Angle;
82 };
83 
85 
89 };
90 
92 
94  INT_CLASS_STRUCT() = default;
95  INT_CLASS_STRUCT(int MaxNumProtos, int MaxNumConfigs);
97  uint16_t NumProtos = 0;
98  uint8_t NumProtoSets = 0;
99  uint8_t NumConfigs = 0;
101  std::vector<uint8_t> ProtoLengths;
103  int font_set_id = 0; // FontSet id, see above
104 };
105 
109  unsigned NumClasses;
110  unsigned NumClassPruners;
113 };
114 
115 /* definitions of integer features*/
116 #define MAX_NUM_INT_FEATURES 512
117 #define INT_CHAR_NORM_RANGE 256
118 
120  INT_FEATURE_STRUCT() : X(0), Y(0), Theta(0), CP_misses(0) {}
121  // Builds a feature from an FCOORD for position with all the necessary
122  // clipping and rounding.
123  INT_FEATURE_STRUCT(const FCOORD &pos, uint8_t theta);
124  // Builds a feature from ints with all the necessary clipping and casting.
125  INT_FEATURE_STRUCT(int x, int y, int theta);
126 
127  uint8_t X;
128  uint8_t Y;
129  uint8_t Theta;
130  int8_t CP_misses;
131 
132  void print() const {
133  tprintf("(%d,%d):%d\n", X, Y, Theta);
134  }
135 };
136 
138 
140 
145 #define MaxNumIntProtosIn(C) (C->NumProtoSets * PROTOS_PER_PROTO_SET)
146 #define SetForProto(P) (P / PROTOS_PER_PROTO_SET)
147 #define IndexForProto(P) (P % PROTOS_PER_PROTO_SET)
148 #define ProtoForProtoId(C, P) (&((C->ProtoSets[SetForProto(P)])->Protos[IndexForProto(P)]))
149 #define PPrunerWordIndexFor(I) (((I) % PROTOS_PER_PROTO_SET) / PROTOS_PER_PP_WERD)
150 #define PPrunerBitIndexFor(I) ((I) % PROTOS_PER_PP_WERD)
151 #define PPrunerMaskFor(I) (1 << PPrunerBitIndexFor(I))
152 
153 #define MaxNumClassesIn(T) (T->NumClassPruners * CLASSES_PER_CP)
154 #define LegalClassId(c) ((c) >= 0 && (c) < MAX_NUM_CLASSES)
155 #define UnusedClassIdIn(T, c) ((T)->Class[c] == nullptr)
156 #define ClassForClassId(T, c) ((T)->Class[c])
157 #define ClassPrunersFor(T) ((T)->ClassPruner)
158 #define CPrunerIdFor(c) ((c) / CLASSES_PER_CP)
159 #define CPrunerFor(T, c) ((T)->ClassPruners[CPrunerIdFor(c)])
160 #define CPrunerWordIndexFor(c) (((c) % CLASSES_PER_CP) / CLASSES_PER_CP_WERD)
161 #define CPrunerBitIndexFor(c) (((c) % CLASSES_PER_CP) % CLASSES_PER_CP_WERD)
162 #define CPrunerMaskFor(L, c) (((L) + 1) << CPrunerBitIndexFor(c) * NUM_BITS_PER_CLASS)
163 
164 /* DEBUG macros*/
165 #define PRINT_MATCH_SUMMARY 0x001
166 #define DISPLAY_FEATURE_MATCHES 0x002
167 #define DISPLAY_PROTO_MATCHES 0x004
168 #define PRINT_FEATURE_MATCHES 0x008
169 #define PRINT_PROTO_MATCHES 0x010
170 #define CLIP_MATCH_EVIDENCE 0x020
171 
172 #define MatchDebuggingOn(D) (D)
173 #define PrintMatchSummaryOn(D) ((D)&PRINT_MATCH_SUMMARY)
174 #define DisplayFeatureMatchesOn(D) ((D)&DISPLAY_FEATURE_MATCHES)
175 #define DisplayProtoMatchesOn(D) ((D)&DISPLAY_PROTO_MATCHES)
176 #define PrintFeatureMatchesOn(D) ((D)&PRINT_FEATURE_MATCHES)
177 #define PrintProtoMatchesOn(D) ((D)&PRINT_PROTO_MATCHES)
178 #define ClipMatchEvidenceOn(D) ((D)&CLIP_MATCH_EVIDENCE)
179 
183 void AddIntClass(INT_TEMPLATES_STRUCT *Templates, CLASS_ID ClassId, INT_CLASS_STRUCT *Class);
184 
185 int AddIntConfig(INT_CLASS_STRUCT *Class);
186 
187 int AddIntProto(INT_CLASS_STRUCT *Class);
188 
189 void AddProtoToClassPruner(PROTO_STRUCT *Proto, CLASS_ID ClassId, INT_TEMPLATES_STRUCT *Templates);
190 
191 void AddProtoToProtoPruner(PROTO_STRUCT *Proto, int ProtoId, INT_CLASS_STRUCT *Class, bool debug);
192 
193 uint8_t Bucket8For(float param, float offset, int num_buckets);
194 uint16_t Bucket16For(float param, float offset, int num_buckets);
195 
196 uint8_t CircBucketFor(float param, float offset, int num_buckets);
197 
198 void UpdateMatchDisplay();
199 
200 void ConvertConfig(BIT_VECTOR Config, int ConfigId, INT_CLASS_STRUCT *Class);
201 
202 void DisplayIntFeature(const INT_FEATURE_STRUCT *Feature, float Evidence);
203 
204 void DisplayIntProto(INT_CLASS_STRUCT *Class, PROTO_ID ProtoId, float Evidence);
205 
207 
208 #ifndef GRAPHICS_DISABLED
209 // Clears the given window and draws the featurespace guides for the
210 // appropriate normalization method.
211 TESS_API
212 void ClearFeatureSpaceWindow(NORM_METHOD norm_method, ScrollView *window);
213 #endif // !GRAPHICS_DISABLED
214 
215 /*----------------------------------------------------------------------------*/
216 #ifndef GRAPHICS_DISABLED
217 TESS_API
218 void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT *Feature,
219  ScrollView::Color color);
220 
222 
224 
226 
227 // Creates a window of the appropriate size for displaying elements
228 // in feature space.
229 TESS_API
230 ScrollView *CreateFeatureSpaceWindow(const char *name, int xpos, int ypos);
231 #endif // !GRAPHICS_DISABLED
232 
233 } // namespace tesseract
234 
235 #endif
#define MAX_NUM_PROTO_SETS
Definition: intproto.h:50
#define NUM_PP_PARAMS
Definition: intproto.h:51
#define MAX_NUM_INT_FEATURES
Definition: intproto.h:116
#define WERDS_PER_PP_VECTOR
Definition: intproto.h:62
#define MAX_NUM_CONFIGS
Definition: intproto.h:47
#define WERDS_PER_CONFIG_VEC
Definition: intproto.h:65
#define NUM_CP_BUCKETS
Definition: intproto.h:53
#define MAX_NUM_CLASS_PRUNERS
Definition: intproto.h:60
#define WERDS_PER_CP_VECTOR
Definition: intproto.h:61
#define PROTOS_PER_PROTO_SET
Definition: intproto.h:49
#define NUM_PP_BUCKETS
Definition: intproto.h:52
uint32_t * BIT_VECTOR
Definition: bitvec.h:28
#define MAX_NUM_CLASSES
Definition: matchdefs.h:31
void AddIntClass(INT_TEMPLATES_STRUCT *Templates, CLASS_ID ClassId, INT_CLASS_STRUCT *Class)
Definition: intproto.cpp:220
void AddProtoToProtoPruner(PROTO_STRUCT *Proto, int ProtoId, INT_CLASS_STRUCT *Class, bool debug)
Definition: intproto.cpp:344
uint8_t Bucket8For(float param, float offset, int num_buckets)
Definition: intproto.cpp:385
void ConvertConfig(BIT_VECTOR Config, int ConfigId, INT_CLASS_STRUCT *Class)
Definition: intproto.cpp:430
void DisplayIntFeature(const INT_FEATURE_STRUCT *Feature, float Evidence)
Definition: intproto.cpp:541
void tprintf(const char *format,...)
Definition: tprintf.cpp:41
uint8_t CircBucketFor(float param, float offset, int num_buckets)
Definition: intproto.cpp:399
ScrollView * CreateFeatureSpaceWindow(const char *name, int xpos, int ypos)
Definition: intproto.cpp:1622
void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT *Feature, ScrollView::Color color)
Definition: intproto.cpp:1500
CLUSTERCONFIG Config
void InitIntMatchWindowIfReqd()
Definition: intproto.cpp:1587
void InitFeatureDisplayWindowIfReqd()
Definition: intproto.cpp:1614
int16_t PROTO_ID
Definition: matchdefs.h:40
uint32_t CONFIG_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][4]
Definition: intproto.h:91
uint16_t Bucket16For(float param, float offset, int num_buckets)
Definition: intproto.cpp:389
IntmatcherDebugAction
Definition: intproto.h:139
@ IDA_BOTH
Definition: intproto.h:139
@ IDA_SHAPE_INDEX
Definition: intproto.h:139
@ IDA_ADAPTIVE
Definition: intproto.h:139
@ IDA_STATIC
Definition: intproto.h:139
void DisplayIntProto(INT_CLASS_STRUCT *Class, PROTO_ID ProtoId, float Evidence)
Definition: intproto.cpp:559
void AddProtoToClassPruner(PROTO_STRUCT *Proto, CLASS_ID ClassId, INT_TEMPLATES_STRUCT *Templates)
Definition: intproto.cpp:306
void InitProtoDisplayWindowIfReqd()
Definition: intproto.cpp:1604
void UpdateMatchDisplay()
Definition: intproto.cpp:413
void ClearFeatureSpaceWindow(NORM_METHOD norm_method, ScrollView *window)
Definition: intproto.cpp:887
void ShowMatchDisplay()
int AddIntConfig(INT_CLASS_STRUCT *Class)
Definition: intproto.cpp:250
UNICHAR_ID CLASS_ID
Definition: matchdefs.h:34
INT_FEATURE_STRUCT INT_FEATURE_ARRAY[MAX_NUM_INT_FEATURES]
Definition: intproto.h:137
uint32_t PROTO_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR]
Definition: intproto.h:84
int AddIntProto(INT_CLASS_STRUCT *Class)
Definition: intproto.cpp:270
uint32_t p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR]
Definition: intproto.h:73
uint32_t Configs[WERDS_PER_CONFIG_VEC]
Definition: intproto.h:81
PROTO_PRUNER ProtoPruner
Definition: intproto.h:87
INT_PROTO_STRUCT Protos[PROTOS_PER_PROTO_SET]
Definition: intproto.h:88
PROTO_SET_STRUCT * ProtoSets[MAX_NUM_PROTO_SETS]
Definition: intproto.h:100
uint16_t ConfigLengths[MAX_NUM_CONFIGS]
Definition: intproto.h:102
std::vector< uint8_t > ProtoLengths
Definition: intproto.h:101
#define TESS_API
Definition: export.h:34