tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/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 class FCOORD;
00032 
00033 /* define order of params in pruners */
00034 #define PRUNER_X      0
00035 #define PRUNER_Y      1
00036 #define PRUNER_ANGLE    2
00037 
00038 /* definition of coordinate system offsets for each table parameter */
00039 #define ANGLE_SHIFT (0.0)
00040 #define X_SHIFT   (0.5)
00041 #define Y_SHIFT   (0.5)
00042 
00043 #define MAX_PROTO_INDEX   24
00044 #define BITS_PER_WERD   static_cast<int>(8 * sizeof(uinT32))
00045 /* Script detection: increase this number to 128 */
00046 #define MAX_NUM_CONFIGS   64
00047 #define MAX_NUM_PROTOS    512
00048 #define PROTOS_PER_PROTO_SET  64
00049 #define MAX_NUM_PROTO_SETS  (MAX_NUM_PROTOS / PROTOS_PER_PROTO_SET)
00050 #define NUM_PP_PARAMS   3
00051 #define NUM_PP_BUCKETS    64
00052 #define NUM_CP_BUCKETS    24
00053 #define CLASSES_PER_CP    32
00054 #define NUM_BITS_PER_CLASS  2
00055 #define CLASS_PRUNER_CLASS_MASK (~(~0 << NUM_BITS_PER_CLASS))
00056 #define CLASSES_PER_CP_WERD (CLASSES_PER_CP / NUM_BITS_PER_CLASS)
00057 #define PROTOS_PER_PP_WERD  BITS_PER_WERD
00058 #define BITS_PER_CP_VECTOR  (CLASSES_PER_CP * NUM_BITS_PER_CLASS)
00059 #define MAX_NUM_CLASS_PRUNERS   ((MAX_NUM_CLASSES + CLASSES_PER_CP - 1) /   \
00060                                 CLASSES_PER_CP)
00061 #define WERDS_PER_CP_VECTOR (BITS_PER_CP_VECTOR / BITS_PER_WERD)
00062 #define WERDS_PER_PP_VECTOR     ((PROTOS_PER_PROTO_SET+BITS_PER_WERD-1)/    \
00063                                 BITS_PER_WERD)
00064 #define WERDS_PER_PP            (NUM_PP_PARAMS * NUM_PP_BUCKETS *               \
00065                                 WERDS_PER_PP_VECTOR)
00066 #define WERDS_PER_CP            (NUM_CP_BUCKETS * NUM_CP_BUCKETS *              \
00067                                 NUM_CP_BUCKETS * WERDS_PER_CP_VECTOR)
00068 #define WERDS_PER_CONFIG_VEC    ((MAX_NUM_CONFIGS + BITS_PER_WERD - 1) /    \
00069                                 BITS_PER_WERD)
00070 
00071 /* The first 3 dimensions of the CLASS_PRUNER_STRUCT are the
00072  * 3 axes of the quantized feature space.
00073  * The position of the the bits recorded for each class in the
00074  * 4th dimension is determined by using CPrunerWordIndexFor(c),
00075  * where c is the corresponding class id. */
00076 struct CLASS_PRUNER_STRUCT {
00077   uinT32 p[NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR];
00078 };
00079 
00080 typedef struct
00081 {
00082   inT8 A;
00083   uinT8 B;
00084   inT8 C;
00085   uinT8 Angle;
00086   uinT32 Configs[WERDS_PER_CONFIG_VEC];
00087 }
00088 
00089 
00090 INT_PROTO_STRUCT, *INT_PROTO;
00091 
00092 typedef uinT32 PROTO_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR];
00093 
00094 typedef struct
00095 {
00096   PROTO_PRUNER ProtoPruner;
00097   INT_PROTO_STRUCT Protos[PROTOS_PER_PROTO_SET];
00098 }
00099 
00100 
00101 PROTO_SET_STRUCT, *PROTO_SET;
00102 
00103 typedef uinT32 CONFIG_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][4];
00104 
00105 
00106 typedef struct
00107 {
00108   uinT16 NumProtos;
00109   uinT8 NumProtoSets;
00110   uinT8 NumConfigs;
00111   PROTO_SET ProtoSets[MAX_NUM_PROTO_SETS];
00112   uinT8 *ProtoLengths;
00113   uinT16 ConfigLengths[MAX_NUM_CONFIGS];
00114   int font_set_id;  // FontSet id, see above
00115 }
00116 
00117 
00118 INT_CLASS_STRUCT, *INT_CLASS;
00119 
00120 typedef struct
00121 {
00122   int NumClasses;
00123   int NumClassPruners;
00124   INT_CLASS Class[MAX_NUM_CLASSES];
00125   CLASS_PRUNER_STRUCT* ClassPruners[MAX_NUM_CLASS_PRUNERS];
00126 }
00127 
00128 
00129 INT_TEMPLATES_STRUCT, *INT_TEMPLATES;
00130 
00131 /* definitions of integer features*/
00132 #define MAX_NUM_INT_FEATURES 512
00133 #define INT_CHAR_NORM_RANGE  256
00134 
00135 struct INT_FEATURE_STRUCT {
00136   INT_FEATURE_STRUCT() : X(0), Y(0), Theta(0), CP_misses(0) { }
00137   // Builds a feature from an FCOORD for position with all the necessary
00138   // clipping and rounding.
00139   INT_FEATURE_STRUCT(const FCOORD& pos, uinT8 theta);
00140   // Builds a feature from ints with all the necessary clipping and casting.
00141   INT_FEATURE_STRUCT(int x, int y, int theta);
00142 
00143   uinT8 X;
00144   uinT8 Y;
00145   uinT8 Theta;
00146   inT8 CP_misses;
00147 
00148   void print() const {
00149     tprintf("(%d,%d):%d\n", X, Y, Theta);
00150   }
00151 };
00152 
00153 typedef INT_FEATURE_STRUCT *INT_FEATURE;
00154 
00155 typedef INT_FEATURE_STRUCT INT_FEATURE_ARRAY[MAX_NUM_INT_FEATURES];
00156 
00157 enum IntmatcherDebugAction {
00158   IDA_ADAPTIVE,
00159   IDA_STATIC,
00160   IDA_SHAPE_INDEX,
00161   IDA_BOTH
00162 };
00163 
00168 #define MaxNumIntProtosIn(C)  (C->NumProtoSets * PROTOS_PER_PROTO_SET)
00169 #define SetForProto(P)    (P / PROTOS_PER_PROTO_SET)
00170 #define IndexForProto(P)  (P % PROTOS_PER_PROTO_SET)
00171 #define ProtoForProtoId(C,P)    (&((C->ProtoSets[SetForProto (P)])->    \
00172                                         Protos [IndexForProto (P)]))
00173 #define PPrunerWordIndexFor(I)  (((I) % PROTOS_PER_PROTO_SET) /         \
00174                                 PROTOS_PER_PP_WERD)
00175 #define PPrunerBitIndexFor(I) ((I) % PROTOS_PER_PP_WERD)
00176 #define PPrunerMaskFor(I) (1 << PPrunerBitIndexFor (I))
00177 
00178 #define MaxNumClassesIn(T)    (T->NumClassPruners * CLASSES_PER_CP)
00179 #define LegalClassId(c)   ((c) >= 0 && (c) <= MAX_CLASS_ID)
00180 #define UnusedClassIdIn(T,c)  ((T)->Class[c] == NULL)
00181 #define ClassForClassId(T,c) ((T)->Class[c])
00182 #define ClassPrunersFor(T)  ((T)->ClassPruner)
00183 #define CPrunerIdFor(c)   ((c) / CLASSES_PER_CP)
00184 #define CPrunerFor(T,c)   ((T)->ClassPruners[CPrunerIdFor(c)])
00185 #define CPrunerWordIndexFor(c)  (((c) % CLASSES_PER_CP) / CLASSES_PER_CP_WERD)
00186 #define CPrunerBitIndexFor(c) (((c) % CLASSES_PER_CP) % CLASSES_PER_CP_WERD)
00187 #define CPrunerMaskFor(L,c) (((L)+1) << CPrunerBitIndexFor (c) * NUM_BITS_PER_CLASS)
00188 
00189 /* DEBUG macros*/
00190 #define PRINT_MATCH_SUMMARY 0x001
00191 #define DISPLAY_FEATURE_MATCHES 0x002
00192 #define DISPLAY_PROTO_MATCHES 0x004
00193 #define PRINT_FEATURE_MATCHES 0x008
00194 #define PRINT_PROTO_MATCHES 0x010
00195 #define CLIP_MATCH_EVIDENCE 0x020
00196 
00197 #define MatchDebuggingOn(D)   (D)
00198 #define PrintMatchSummaryOn(D)    ((D) & PRINT_MATCH_SUMMARY)
00199 #define DisplayFeatureMatchesOn(D)  ((D) & DISPLAY_FEATURE_MATCHES)
00200 #define DisplayProtoMatchesOn(D)  ((D) & DISPLAY_PROTO_MATCHES)
00201 #define PrintFeatureMatchesOn(D)  ((D) & PRINT_FEATURE_MATCHES)
00202 #define PrintProtoMatchesOn(D)    ((D) & PRINT_PROTO_MATCHES)
00203 #define ClipMatchEvidenceOn(D)    ((D) & CLIP_MATCH_EVIDENCE)
00204 
00208 void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class);
00209 
00210 int AddIntConfig(INT_CLASS Class);
00211 
00212 int AddIntProto(INT_CLASS Class);
00213 
00214 void AddProtoToClassPruner(PROTO Proto,
00215                            CLASS_ID ClassId,
00216                            INT_TEMPLATES Templates);
00217 
00218 void AddProtoToProtoPruner(PROTO Proto, int ProtoId,
00219                            INT_CLASS Class, bool debug);
00220 
00221 int BucketFor(FLOAT32 Param, FLOAT32 Offset, int NumBuckets);
00222 
00223 int CircBucketFor(FLOAT32 Param, FLOAT32 Offset, int NumBuckets);
00224 
00225 void UpdateMatchDisplay();
00226 
00227 void ConvertConfig(BIT_VECTOR Config, int ConfigId, INT_CLASS Class);
00228 
00229 void DisplayIntFeature(const INT_FEATURE_STRUCT* Feature, FLOAT32 Evidence);
00230 
00231 void DisplayIntProto(INT_CLASS Class, PROTO_ID ProtoId, FLOAT32 Evidence);
00232 
00233 INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs);
00234 
00235 INT_TEMPLATES NewIntTemplates();
00236 
00237 void free_int_templates(INT_TEMPLATES templates);
00238 
00239 void ShowMatchDisplay();
00240 
00241 namespace tesseract {
00242 
00243 // Clears the given window and draws the featurespace guides for the
00244 // appropriate normalization method.
00245 void ClearFeatureSpaceWindow(NORM_METHOD norm_method, ScrollView* window);
00246 
00247 }  // namespace tesseract.
00248 
00249 /*----------------------------------------------------------------------------*/
00250 #ifndef GRAPHICS_DISABLED
00251 void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT* Feature,
00252                       ScrollView::Color color);
00253 
00254 void InitIntMatchWindowIfReqd();
00255 
00256 void InitProtoDisplayWindowIfReqd();
00257 
00258 void InitFeatureDisplayWindowIfReqd();
00259 
00260 // Creates a window of the appropriate size for displaying elements
00261 // in feature space.
00262 ScrollView* CreateFeatureSpaceWindow(const char* name, int xpos, int ypos);
00263 #endif  // GRAPHICS_DISABLED
00264 
00265 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines