Coverage Report

Created: 2025-06-13 07:15

/src/tesseract/src/classify/intproto.h
Line
Count
Source (jump to first uncovered line)
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
21
/**----------------------------------------------------------------------------
22
          Include Files and Type Defines
23
----------------------------------------------------------------------------**/
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
19.0k
#define PRUNER_X 0
36
19.0k
#define PRUNER_Y 1
37
19.0k
#define PRUNER_ANGLE 2
38
39
/* definition of coordinate system offsets for each table parameter */
40
139k
#define ANGLE_SHIFT (0.0)
41
149k
#define X_SHIFT (0.5)
42
55.9k
#define Y_SHIFT (0.5)
43
44
19.5G
#define MAX_PROTO_INDEX 24
45
6.35G
#define BITS_PER_WERD static_cast<int>(8 * sizeof(uint32_t))
46
/* Script detection: increase this number to 128 */
47
2.78G
#define MAX_NUM_CONFIGS 64
48
12.5M
#define MAX_NUM_PROTOS 512
49
19.5G
#define PROTOS_PER_PROTO_SET 64
50
#define MAX_NUM_PROTO_SETS (MAX_NUM_PROTOS / PROTOS_PER_PROTO_SET)
51
796
#define NUM_PP_PARAMS 3
52
1.94G
#define NUM_PP_BUCKETS 64
53
698M
#define NUM_CP_BUCKETS 24
54
6.36G
#define CLASSES_PER_CP 32
55
137G
#define NUM_BITS_PER_CLASS 2
56
67.7G
#define CLASS_PRUNER_CLASS_MASK (~(~0u << NUM_BITS_PER_CLASS))
57
7.93k
#define CLASSES_PER_CP_WERD (CLASSES_PER_CP / NUM_BITS_PER_CLASS)
58
#define PROTOS_PER_PP_WERD BITS_PER_WERD
59
6.35G
#define BITS_PER_CP_VECTOR (CLASSES_PER_CP * NUM_BITS_PER_CLASS)
60
4
#define MAX_NUM_CLASS_PRUNERS ((MAX_NUM_CLASSES + CLASSES_PER_CP - 1) / CLASSES_PER_CP)
61
6.35G
#define WERDS_PER_CP_VECTOR (BITS_PER_CP_VECTOR / BITS_PER_WERD)
62
796
#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
57.2k
#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 bits recorded for each class in the
70
 * 4th dimension is determined by using CPrunerWordIndexFor(c),
71
 * where c is the corresponding class id. */
72
struct CLASS_PRUNER_STRUCT {
73
  uint32_t p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR];
74
};
75
76
struct INT_PROTO_STRUCT {
77
  int8_t A;
78
  uint8_t B;
79
  int8_t C;
80
  uint8_t Angle;
81
  uint32_t Configs[WERDS_PER_CONFIG_VEC];
82
};
83
84
typedef uint32_t PROTO_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR];
85
86
struct PROTO_SET_STRUCT {
87
  PROTO_PRUNER ProtoPruner;
88
  INT_PROTO_STRUCT Protos[PROTOS_PER_PROTO_SET];
89
};
90
91
typedef uint32_t CONFIG_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][4];
92
93
struct INT_CLASS_STRUCT {
94
452
  INT_CLASS_STRUCT() = default;
95
  INT_CLASS_STRUCT(int MaxNumProtos, int MaxNumConfigs);
96
  ~INT_CLASS_STRUCT();
97
  uint16_t NumProtos = 0;
98
  uint8_t NumProtoSets = 0;
99
  uint8_t NumConfigs = 0;
100
  PROTO_SET_STRUCT *ProtoSets[MAX_NUM_PROTO_SETS];
101
  std::vector<uint8_t> ProtoLengths;
102
  uint16_t ConfigLengths[MAX_NUM_CONFIGS];
103
  int font_set_id = 0; // FontSet id, see above
104
};
105
106
struct TESS_API INT_TEMPLATES_STRUCT {
107
  INT_TEMPLATES_STRUCT();
108
  ~INT_TEMPLATES_STRUCT();
109
  unsigned NumClasses;
110
  unsigned NumClassPruners;
111
  INT_CLASS_STRUCT *Class[MAX_NUM_CLASSES];
112
  CLASS_PRUNER_STRUCT *ClassPruners[MAX_NUM_CLASS_PRUNERS];
113
};
114
115
/* definitions of integer features*/
116
#define MAX_NUM_INT_FEATURES 512
117
1.16G
#define INT_CHAR_NORM_RANGE 256
118
119
struct INT_FEATURE_STRUCT {
120
127M
  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
0
  void print() const {
133
0
    tprintf("(%d,%d):%d\n", X, Y, Theta);
134
0
  }
135
};
136
137
typedef INT_FEATURE_STRUCT INT_FEATURE_ARRAY[MAX_NUM_INT_FEATURES];
138
139
enum IntmatcherDebugAction { IDA_ADAPTIVE, IDA_STATIC, IDA_SHAPE_INDEX, IDA_BOTH };
140
141
/**----------------------------------------------------------------------------
142
            Macros
143
----------------------------------------------------------------------------**/
144
145
6.27M
#define MaxNumIntProtosIn(C) (C->NumProtoSets * PROTOS_PER_PROTO_SET)
146
81.9k
#define SetForProto(P) (P / PROTOS_PER_PROTO_SET)
147
81.9k
#define IndexForProto(P) (P % PROTOS_PER_PROTO_SET)
148
62.8k
#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
6.25M
#define MaxNumClassesIn(T) (T->NumClassPruners * CLASSES_PER_CP)
154
5.45k
#define LegalClassId(c) ((c) >= 0 && (c) < MAX_NUM_CLASSES)
155
0
#define UnusedClassIdIn(T, c) ((T)->Class[c] == nullptr)
156
448M
#define ClassForClassId(T, c) ((T)->Class[c])
157
#define ClassPrunersFor(T) ((T)->ClassPruner)
158
1.58k
#define CPrunerIdFor(c) ((c) / CLASSES_PER_CP)
159
1.58k
#define CPrunerFor(T, c) ((T)->ClassPruners[CPrunerIdFor(c)])
160
1.58k
#define CPrunerWordIndexFor(c) (((c) % CLASSES_PER_CP) / CLASSES_PER_CP_WERD)
161
6.34k
#define CPrunerBitIndexFor(c) (((c) % CLASSES_PER_CP) % CLASSES_PER_CP_WERD)
162
6.34k
#define CPrunerMaskFor(L, c) (((L) + 1) << CPrunerBitIndexFor(c) * NUM_BITS_PER_CLASS)
163
164
/* DEBUG macros*/
165
0
#define PRINT_MATCH_SUMMARY 0x001
166
#define DISPLAY_FEATURE_MATCHES 0x002
167
#define DISPLAY_PROTO_MATCHES 0x004
168
5.19G
#define PRINT_FEATURE_MATCHES 0x008
169
0
#define PRINT_PROTO_MATCHES 0x010
170
#define CLIP_MATCH_EVIDENCE 0x020
171
172
14.7M
#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
5.19G
#define PrintFeatureMatchesOn(D) ((D)&PRINT_FEATURE_MATCHES)
177
#define PrintProtoMatchesOn(D) ((D)&PRINT_PROTO_MATCHES)
178
#define ClipMatchEvidenceOn(D) ((D)&CLIP_MATCH_EVIDENCE)
179
180
/**----------------------------------------------------------------------------
181
          Public Function Prototypes
182
----------------------------------------------------------------------------**/
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
206
void ShowMatchDisplay();
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
221
void InitIntMatchWindowIfReqd();
222
223
void InitProtoDisplayWindowIfReqd();
224
225
void InitFeatureDisplayWindowIfReqd();
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