Tesseract  3.02
tesseract-ocr/classify/intproto.h
Go to the documentation of this file.
00001 /******************************************************************************
00002  **     Filename:    intproto.h
00003  **     Purpose:     Definition of data structures for integer protos.
00004  **     Author:      Dan Johnson
00005  **     History:     Thu Feb  7 12:58:45 1991, DSJ, Created.
00006  **
00007  **     (c) Copyright Hewlett-Packard Company, 1988.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  ******************************************************************************/
00018 #ifndef INTPROTO_H
00019 #define INTPROTO_H
00020 
00024 #include "genericvector.h"
00025 #include "matchdefs.h"
00026 #include "mfoutline.h"
00027 #include "protos.h"
00028 #include "scrollview.h"
00029 #include "unicharset.h"
00030 
00031 /* define order of params in pruners */
00032 #define PRUNER_X      0
00033 #define PRUNER_Y      1
00034 #define PRUNER_ANGLE    2
00035 
00036 /* definition of coordinate system offsets for each table parameter */
00037 #define ANGLE_SHIFT (0.0)
00038 #define X_SHIFT   (0.5)
00039 #define Y_SHIFT   (0.5)
00040 
00041 #define MAX_PROTO_INDEX   24
00042 #define BITS_PER_WERD   (8 * sizeof (uinT32))
00043 /* Script detection: increase this number to 128 */
00044 #define MAX_NUM_CONFIGS   64
00045 #define MAX_NUM_PROTOS    512
00046 #define PROTOS_PER_PROTO_SET  64
00047 #define MAX_NUM_PROTO_SETS  (MAX_NUM_PROTOS / PROTOS_PER_PROTO_SET)
00048 #define NUM_PP_PARAMS   3
00049 #define NUM_PP_BUCKETS    64
00050 #define NUM_CP_BUCKETS    24
00051 #define CLASSES_PER_CP    32
00052 #define NUM_BITS_PER_CLASS  2
00053 #define CLASS_PRUNER_CLASS_MASK (~(~0 << NUM_BITS_PER_CLASS))
00054 #define CLASSES_PER_CP_WERD (CLASSES_PER_CP / NUM_BITS_PER_CLASS)
00055 #define PROTOS_PER_PP_WERD  BITS_PER_WERD
00056 #define BITS_PER_CP_VECTOR  (CLASSES_PER_CP * NUM_BITS_PER_CLASS)
00057 #define MAX_NUM_CLASS_PRUNERS   ((MAX_NUM_CLASSES + CLASSES_PER_CP - 1) /   \
00058                                 CLASSES_PER_CP)
00059 #define WERDS_PER_CP_VECTOR (BITS_PER_CP_VECTOR / BITS_PER_WERD)
00060 #define WERDS_PER_PP_VECTOR     ((PROTOS_PER_PROTO_SET+BITS_PER_WERD-1)/    \
00061                                 BITS_PER_WERD)
00062 #define WERDS_PER_PP            (NUM_PP_PARAMS * NUM_PP_BUCKETS *               \
00063                                 WERDS_PER_PP_VECTOR)
00064 #define WERDS_PER_CP            (NUM_CP_BUCKETS * NUM_CP_BUCKETS *              \
00065                                 NUM_CP_BUCKETS * WERDS_PER_CP_VECTOR)
00066 #define WERDS_PER_CONFIG_VEC    ((MAX_NUM_CONFIGS + BITS_PER_WERD - 1) /    \
00067                                 BITS_PER_WERD)
00068 
00069 /* The first 3 dimensions of the CLASS_PRUNER_STRUCT are the
00070  * 3 axes of the quantized feature space.
00071  * The position of the the bits recorded for each class in the
00072  * 4th dimension is determined by using CPrunerWordIndexFor(c),
00073  * where c is the corresponding class id. */
00074 struct CLASS_PRUNER_STRUCT {
00075   uinT32 p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR];
00076 };
00077 
00078 typedef struct
00079 {
00080   inT8 A;
00081   uinT8 B;
00082   inT8 C;
00083   uinT8 Angle;
00084   uinT32 Configs[WERDS_PER_CONFIG_VEC];
00085 }
00086 
00087 
00088 INT_PROTO_STRUCT, *INT_PROTO;
00089 
00090 typedef uinT32 PROTO_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR];
00091 
00092 typedef struct
00093 {
00094   PROTO_PRUNER ProtoPruner;
00095   INT_PROTO_STRUCT Protos[PROTOS_PER_PROTO_SET];
00096 }
00097 
00098 
00099 PROTO_SET_STRUCT, *PROTO_SET;
00100 
00101 typedef uinT32 CONFIG_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][4];
00102 
00103 
00104 typedef struct
00105 {
00106   uinT16 NumProtos;
00107   uinT8 NumProtoSets;
00108   uinT8 NumConfigs;
00109   PROTO_SET ProtoSets[MAX_NUM_PROTO_SETS];
00110   uinT8 *ProtoLengths;
00111   uinT16 ConfigLengths[MAX_NUM_CONFIGS];
00112   int font_set_id;  // FontSet id, see above
00113 }
00114 
00115 
00116 INT_CLASS_STRUCT, *INT_CLASS;
00117 
00118 typedef struct
00119 {
00120   int NumClasses;
00121   int NumClassPruners;
00122   INT_CLASS Class[MAX_NUM_CLASSES];
00123   CLASS_PRUNER_STRUCT* ClassPruners[MAX_NUM_CLASS_PRUNERS];
00124 }
00125 
00126 
00127 INT_TEMPLATES_STRUCT, *INT_TEMPLATES;
00128 
00129 /* definitions of integer features*/
00130 #define MAX_NUM_INT_FEATURES 512
00131 #define INT_CHAR_NORM_RANGE  256
00132 
00133 struct INT_FEATURE_STRUCT
00134 {
00135   uinT8 X;
00136   uinT8 Y;
00137   uinT8 Theta;
00138   inT8 CP_misses;
00139 
00140   void print() const {
00141     tprintf("(%d,%d):%d\n", X, Y, Theta);
00142   }
00143 };
00144 
00145 typedef INT_FEATURE_STRUCT *INT_FEATURE;
00146 
00147 typedef INT_FEATURE_STRUCT INT_FEATURE_ARRAY[MAX_NUM_INT_FEATURES];
00148 
00149 enum IntmatcherDebugAction {
00150   IDA_ADAPTIVE,
00151   IDA_STATIC,
00152   IDA_SHAPE_INDEX,
00153   IDA_BOTH
00154 };
00155 
00160 #define MaxNumIntProtosIn(C)  (C->NumProtoSets * PROTOS_PER_PROTO_SET)
00161 #define SetForProto(P)    (P / PROTOS_PER_PROTO_SET)
00162 #define IndexForProto(P)  (P % PROTOS_PER_PROTO_SET)
00163 #define ProtoForProtoId(C,P)    (&((C->ProtoSets[SetForProto (P)])->    \
00164                                         Protos [IndexForProto (P)]))
00165 #define PPrunerWordIndexFor(I)  (((I) % PROTOS_PER_PROTO_SET) /         \
00166                                 PROTOS_PER_PP_WERD)
00167 #define PPrunerBitIndexFor(I) ((I) % PROTOS_PER_PP_WERD)
00168 #define PPrunerMaskFor(I) (1 << PPrunerBitIndexFor (I))
00169 
00170 #define MaxNumClassesIn(T)    (T->NumClassPruners * CLASSES_PER_CP)
00171 #define LegalClassId(c)   ((c) >= 0 && (c) <= MAX_CLASS_ID)
00172 #define UnusedClassIdIn(T,c)  ((T)->Class[c] == NULL)
00173 #define ClassForClassId(T,c) ((T)->Class[c])
00174 #define ClassPrunersFor(T)  ((T)->ClassPruner)
00175 #define CPrunerIdFor(c)   ((c) / CLASSES_PER_CP)
00176 #define CPrunerFor(T,c)   ((T)->ClassPruners[CPrunerIdFor(c)])
00177 #define CPrunerWordIndexFor(c)  (((c) % CLASSES_PER_CP) / CLASSES_PER_CP_WERD)
00178 #define CPrunerBitIndexFor(c) (((c) % CLASSES_PER_CP) % CLASSES_PER_CP_WERD)
00179 #define CPrunerMaskFor(L,c) (((L)+1) << CPrunerBitIndexFor (c) * NUM_BITS_PER_CLASS)
00180 
00181 /* DEBUG macros*/
00182 #define PRINT_MATCH_SUMMARY 0x001
00183 #define DISPLAY_FEATURE_MATCHES 0x002
00184 #define DISPLAY_PROTO_MATCHES 0x004
00185 #define PRINT_FEATURE_MATCHES 0x008
00186 #define PRINT_PROTO_MATCHES 0x010
00187 #define CLIP_MATCH_EVIDENCE 0x020
00188 
00189 #define MatchDebuggingOn(D)   (D)
00190 #define PrintMatchSummaryOn(D)    ((D) & PRINT_MATCH_SUMMARY)
00191 #define DisplayFeatureMatchesOn(D)  ((D) & DISPLAY_FEATURE_MATCHES)
00192 #define DisplayProtoMatchesOn(D)  ((D) & DISPLAY_PROTO_MATCHES)
00193 #define PrintFeatureMatchesOn(D)  ((D) & PRINT_FEATURE_MATCHES)
00194 #define PrintProtoMatchesOn(D)    ((D) & PRINT_PROTO_MATCHES)
00195 #define ClipMatchEvidenceOn(D)    ((D) & CLIP_MATCH_EVIDENCE)
00196 
00200 void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class);
00201 
00202 int AddIntConfig(INT_CLASS Class);
00203 
00204 int AddIntProto(INT_CLASS Class);
00205 
00206 void AddProtoToClassPruner(PROTO Proto,
00207                            CLASS_ID ClassId,
00208                            INT_TEMPLATES Templates);
00209 
00210 void AddProtoToProtoPruner(PROTO Proto, int ProtoId,
00211                            INT_CLASS Class, bool debug);
00212 
00213 int BucketFor(FLOAT32 Param, FLOAT32 Offset, int NumBuckets);
00214 
00215 int CircBucketFor(FLOAT32 Param, FLOAT32 Offset, int NumBuckets);
00216 
00217 void UpdateMatchDisplay();
00218 
00219 void ConvertConfig(BIT_VECTOR Config, int ConfigId, INT_CLASS Class);
00220 
00221 void DisplayIntFeature(const INT_FEATURE_STRUCT* Feature, FLOAT32 Evidence);
00222 
00223 void DisplayIntProto(INT_CLASS Class, PROTO_ID ProtoId, FLOAT32 Evidence);
00224 
00225 INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs);
00226 
00227 INT_TEMPLATES NewIntTemplates();
00228 
00229 void free_int_templates(INT_TEMPLATES templates);
00230 
00231 void ShowMatchDisplay();
00232 
00233 namespace tesseract {
00234 
00235 // Clears the given window and draws the featurespace guides for the
00236 // appropriate normalization method.
00237 void ClearFeatureSpaceWindow(NORM_METHOD norm_method, ScrollView* window);
00238 
00239 }  // namespace tesseract.
00240 
00241 /*----------------------------------------------------------------------------*/
00242 #ifndef GRAPHICS_DISABLED
00243 void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT* Feature,
00244                       ScrollView::Color color);
00245 
00246 void InitIntMatchWindowIfReqd();
00247 
00248 void InitProtoDisplayWindowIfReqd();
00249 
00250 void InitFeatureDisplayWindowIfReqd();
00251 
00252 // Creates a window of the appropriate size for displaying elements
00253 // in feature space.
00254 ScrollView* CreateFeatureSpaceWindow(const char* name, int xpos, int ypos);
00255 #endif  // GRAPHICS_DISABLED
00256 
00257 #endif