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 #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. */ 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 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 #define INT_CHAR_NORM_RANGE 256 118 119 struct INT_FEATURE_STRUCT { INT_FEATURE_STRUCTINT_FEATURE_STRUCT120 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 printINT_FEATURE_STRUCT132 void print() const { 133 tprintf("(%d,%d):%d\n", X, Y, Theta); 134 } 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 #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 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 236