Tesseract 3.01
/data/source/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 "callcpp.h"
00029 #include "scrollview.h"
00030 #include "unicharset.h"
00031 
00032 /* define order of params in pruners */
00033 #define PRUNER_X      0
00034 #define PRUNER_Y      1
00035 #define PRUNER_ANGLE    2
00036 
00037 /* definition of coordinate system offsets for each table parameter */
00038 #define ANGLE_SHIFT (0.0)
00039 #define X_SHIFT   (0.5)
00040 #define Y_SHIFT   (0.5)
00041 
00042 #define MAX_PROTO_INDEX   24
00043 #define BITS_PER_WERD   (8 * sizeof (uinT32))
00044 /* Script detection: increase this number to 128 */
00045 #define MAX_NUM_CONFIGS   64
00046 #define MAX_NUM_PROTOS    512
00047 #define PROTOS_PER_PROTO_SET  64
00048 #define MAX_NUM_PROTO_SETS  (MAX_NUM_PROTOS / PROTOS_PER_PROTO_SET)
00049 #define NUM_PP_PARAMS   3
00050 #define NUM_PP_BUCKETS    64
00051 #define NUM_CP_BUCKETS    24
00052 #define CLASSES_PER_CP    32
00053 #define NUM_BITS_PER_CLASS  2
00054 #define CLASS_PRUNER_CLASS_MASK (~(~0 << NUM_BITS_PER_CLASS))
00055 #define CLASSES_PER_CP_WERD (CLASSES_PER_CP / NUM_BITS_PER_CLASS)
00056 #define PROTOS_PER_PP_WERD  BITS_PER_WERD
00057 #define BITS_PER_CP_VECTOR  (CLASSES_PER_CP * NUM_BITS_PER_CLASS)
00058 #define MAX_NUM_CLASS_PRUNERS   ((MAX_NUM_CLASSES + CLASSES_PER_CP - 1) /   \
00059                                 CLASSES_PER_CP)
00060 #define WERDS_PER_CP_VECTOR (BITS_PER_CP_VECTOR / BITS_PER_WERD)
00061 #define WERDS_PER_PP_VECTOR     ((PROTOS_PER_PROTO_SET+BITS_PER_WERD-1)/    \
00062                                 BITS_PER_WERD)
00063 #define WERDS_PER_PP            (NUM_PP_PARAMS * NUM_PP_BUCKETS *               \
00064                                 WERDS_PER_PP_VECTOR)
00065 #define WERDS_PER_CP            (NUM_CP_BUCKETS * NUM_CP_BUCKETS *              \
00066                                 NUM_CP_BUCKETS * WERDS_PER_CP_VECTOR)
00067 #define WERDS_PER_CONFIG_VEC    ((MAX_NUM_CONFIGS + BITS_PER_WERD - 1) /    \
00068                                 BITS_PER_WERD)
00069 
00070 /* The first 3 dimensions of the CLASS_PRUNER_STRUCT are the
00071  * 3 axes of the quantized feature space.
00072  * The position of the the bits recorded for each class in the
00073  * 4th dimension is determined by using CPrunerWordIndexFor(c),
00074  * where c is the corresponding class id. */
00075 typedef uinT32 CLASS_PRUNER_STRUCT
00076 [NUM_CP_BUCKETS][NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR];
00077 
00078 typedef
00079 uinT32 (*CLASS_PRUNER)[NUM_CP_BUCKETS][NUM_CP_BUCKETS][WERDS_PER_CP_VECTOR];
00080 
00081 typedef struct
00082 {
00083   inT8 A;
00084   uinT8 B;
00085   inT8 C;
00086   uinT8 Angle;
00087   uinT32 Configs[WERDS_PER_CONFIG_VEC];
00088 }
00089 
00090 
00091 INT_PROTO_STRUCT, *INT_PROTO;
00092 
00093 typedef uinT32 PROTO_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][WERDS_PER_PP_VECTOR];
00094 
00095 typedef struct
00096 {
00097   PROTO_PRUNER ProtoPruner;
00098   INT_PROTO_STRUCT Protos[PROTOS_PER_PROTO_SET];
00099 }
00100 
00101 
00102 PROTO_SET_STRUCT, *PROTO_SET;
00103 
00104 typedef uinT32 CONFIG_PRUNER[NUM_PP_PARAMS][NUM_PP_BUCKETS][4];
00105 
00106 // Struct for information about spacing between characters in a particular font.
00107 struct FontSpacingInfo {
00108   inT16 x_gap_before;
00109   inT16 x_gap_after;
00110   GenericVector<UNICHAR_ID> kerned_unichar_ids;
00111   GenericVector<inT16> kerned_x_gaps;
00112 };
00113 
00114 /*
00115  * font_properties contains properties about boldness, italicness, fixed pitch,
00116  * serif, fraktur
00117  */
00118 struct FontInfo {
00119   FontInfo() : name(NULL), spacing_vec(NULL) {}
00120   ~FontInfo() {}
00121   // Reserves unicharset_size spots in spacing_vec.
00122   void init_spacing(int unicharset_size) {
00123     spacing_vec = new GenericVector<FontSpacingInfo *>();
00124     spacing_vec->init_to_size(unicharset_size, NULL);
00125   }
00126   // Adds the given pointer to FontSpacingInfo to spacing_vec member
00127   // (FontInfo class takes ownership of the pointer).
00128   // Note: init_spacing should be called before calling this function.
00129   void add_spacing(UNICHAR_ID uch_id, FontSpacingInfo *spacing_info) {
00130     ASSERT_HOST(spacing_vec != NULL && spacing_vec->size() > uch_id);
00131     (*spacing_vec)[uch_id] = spacing_info;
00132   }
00133 
00134   // Returns the pointer to FontSpacingInfo for the given UNICHAR_ID.
00135   const FontSpacingInfo *get_spacing(UNICHAR_ID uch_id) const {
00136     return (spacing_vec == NULL || spacing_vec->size() <= uch_id) ?
00137         NULL : (*spacing_vec)[uch_id];
00138   }
00139 
00140   // Fills spacing with the value of the x gap expected between the two given
00141   // UNICHAR_IDs. Returns true on success.
00142   bool get_spacing(UNICHAR_ID prev_uch_id,
00143                    UNICHAR_ID uch_id,
00144                    int *spacing) const {
00145     const FontSpacingInfo *prev_fsi = this->get_spacing(prev_uch_id);
00146     const FontSpacingInfo *fsi = this->get_spacing(uch_id);
00147     if (prev_fsi == NULL || fsi == NULL) return false;
00148     int i = 0;
00149     for (; i < prev_fsi->kerned_unichar_ids.size(); ++i) {
00150       if (prev_fsi->kerned_unichar_ids[i] == uch_id) break;
00151     }
00152     if (i < prev_fsi->kerned_unichar_ids.size()) {
00153       *spacing = prev_fsi->kerned_x_gaps[i];
00154     } else {
00155       *spacing = prev_fsi->x_gap_after + fsi->x_gap_before;
00156     }
00157     return true;
00158   }
00159 
00160   bool is_italic() const { return properties & 1; }
00161   bool is_bold() const { return (properties & 2) != 0; }
00162   bool is_fixed_pitch() const { return (properties & 4) != 0; }
00163   bool is_serif() const { return (properties & 8) != 0; }
00164   bool is_fraktur() const { return (properties & 16) != 0; }
00165 
00166   char* name;
00167   uinT32 properties;
00168   // Horizontal spacing between characters (indexed by UNICHAR_ID).
00169   GenericVector<FontSpacingInfo *> *spacing_vec;
00170 };
00171 
00172 // Every class (character) owns a FontSet that represents all the fonts that can
00173 // render this character.
00174 // Since almost all the characters from the same script share the same set of
00175 // fonts, the sets are shared over multiple classes (see
00176 // Classify::fontset_table_). Thus, a class only store an id to a set.
00177 // Because some fonts cannot render just one character of a set, there are a
00178 // lot of FontSet that differ only by one font. Rather than storing directly
00179 // the FontInfo in the FontSet structure, it's better to share FontInfos among
00180 // FontSets (Classify::fontinfo_table_).
00181 struct FontSet {
00182   int           size;
00183   int*          configs;  // FontInfo ids
00184 };
00185 
00186 
00187 typedef struct
00188 {
00189   uinT16 NumProtos;
00190   uinT8 NumProtoSets;
00191   uinT8 NumConfigs;
00192   PROTO_SET ProtoSets[MAX_NUM_PROTO_SETS];
00193   uinT8 *ProtoLengths;
00194   uinT16 ConfigLengths[MAX_NUM_CONFIGS];
00195   int font_set_id;  // FontSet id, see above
00196 }
00197 
00198 
00199 INT_CLASS_STRUCT, *INT_CLASS;
00200 
00201 typedef struct
00202 {
00203   int NumClasses;
00204   int NumClassPruners;
00205   INT_CLASS Class[MAX_NUM_CLASSES];
00206   CLASS_PRUNER ClassPruner[MAX_NUM_CLASS_PRUNERS];
00207 }
00208 
00209 
00210 INT_TEMPLATES_STRUCT, *INT_TEMPLATES;
00211 
00212 /* definitions of integer features*/
00213 #define MAX_NUM_INT_FEATURES 512
00214 #define INT_CHAR_NORM_RANGE  256
00215 
00216 struct INT_FEATURE_STRUCT
00217 {
00218   uinT8 X;
00219   uinT8 Y;
00220   uinT8 Theta;
00221   inT8 CP_misses;
00222 
00223   void print() const {
00224     tprintf("(%d,%d):%d\n", X, Y, Theta);
00225   }
00226 };
00227 
00228 typedef INT_FEATURE_STRUCT *INT_FEATURE;
00229 
00230 typedef INT_FEATURE_STRUCT INT_FEATURE_ARRAY[MAX_NUM_INT_FEATURES];
00231 
00232 enum IntmatcherDebugAction {
00233   IDA_ADAPTIVE,
00234   IDA_STATIC,
00235   IDA_BOTH
00236 };
00237 
00242 #define MaxNumIntProtosIn(C)  (C->NumProtoSets * PROTOS_PER_PROTO_SET)
00243 #define SetForProto(P)    (P / PROTOS_PER_PROTO_SET)
00244 #define IndexForProto(P)  (P % PROTOS_PER_PROTO_SET)
00245 #define ProtoForProtoId(C,P)    (&((C->ProtoSets[SetForProto (P)])->    \
00246                                         Protos [IndexForProto (P)]))
00247 #define PPrunerWordIndexFor(I)  (((I) % PROTOS_PER_PROTO_SET) /         \
00248                                 PROTOS_PER_PP_WERD)
00249 #define PPrunerBitIndexFor(I) ((I) % PROTOS_PER_PP_WERD)
00250 #define PPrunerMaskFor(I) (1 << PPrunerBitIndexFor (I))
00251 
00252 #define MaxNumClassesIn(T)    (T->NumClassPruners * CLASSES_PER_CP)
00253 #define LegalClassId(c)   ((c) >= 0 && (c) <= MAX_CLASS_ID)
00254 #define UnusedClassIdIn(T,c)  ((T)->Class[c] == NULL)
00255 #define ClassForClassId(T,c) ((T)->Class[c])
00256 #define ClassPrunersFor(T)  ((T)->ClassPruner)
00257 #define CPrunerIdFor(c)   ((c) / CLASSES_PER_CP)
00258 #define CPrunerFor(T,c)   ((T)->ClassPruner [CPrunerIdFor (c)])
00259 #define CPrunerWordIndexFor(c)  (((c) % CLASSES_PER_CP) / CLASSES_PER_CP_WERD)
00260 #define CPrunerBitIndexFor(c) (((c) % CLASSES_PER_CP) % CLASSES_PER_CP_WERD)
00261 #define CPrunerMaskFor(L,c) (((L)+1) << CPrunerBitIndexFor (c) * NUM_BITS_PER_CLASS)
00262 
00263 /* DEBUG macros*/
00264 #define PRINT_MATCH_SUMMARY 0x001
00265 #define DISPLAY_FEATURE_MATCHES 0x002
00266 #define DISPLAY_PROTO_MATCHES 0x004
00267 #define PRINT_FEATURE_MATCHES 0x008
00268 #define PRINT_PROTO_MATCHES 0x010
00269 #define CLIP_MATCH_EVIDENCE 0x020
00270 
00271 #define MatchDebuggingOn(D)   (D)
00272 #define PrintMatchSummaryOn(D)    ((D) & PRINT_MATCH_SUMMARY)
00273 #define DisplayFeatureMatchesOn(D)  ((D) & DISPLAY_FEATURE_MATCHES)
00274 #define DisplayProtoMatchesOn(D)  ((D) & DISPLAY_PROTO_MATCHES)
00275 #define PrintFeatureMatchesOn(D)  ((D) & PRINT_FEATURE_MATCHES)
00276 #define PrintProtoMatchesOn(D)    ((D) & PRINT_PROTO_MATCHES)
00277 #define ClipMatchEvidenceOn(D)    ((D) & CLIP_MATCH_EVIDENCE)
00278 
00282 void AddIntClass(INT_TEMPLATES Templates, CLASS_ID ClassId, INT_CLASS Class);
00283 
00284 int AddIntConfig(INT_CLASS Class);
00285 
00286 int AddIntProto(INT_CLASS Class);
00287 
00288 void AddProtoToClassPruner(PROTO Proto,
00289                            CLASS_ID ClassId,
00290                            INT_TEMPLATES Templates);
00291 
00292 void AddProtoToProtoPruner(PROTO Proto, int ProtoId,
00293                            INT_CLASS Class, bool debug);
00294 
00295 int BucketFor(FLOAT32 Param, FLOAT32 Offset, int NumBuckets);
00296 
00297 int CircBucketFor(FLOAT32 Param, FLOAT32 Offset, int NumBuckets);
00298 
00299 void UpdateMatchDisplay();
00300 
00301 void ConvertConfig(BIT_VECTOR Config, int ConfigId, INT_CLASS Class);
00302 
00303 void DisplayIntFeature(INT_FEATURE Feature, FLOAT32 Evidence);
00304 
00305 void DisplayIntProto(INT_CLASS Class, PROTO_ID ProtoId, FLOAT32 Evidence);
00306 
00307 INT_CLASS NewIntClass(int MaxNumProtos, int MaxNumConfigs);
00308 
00309 INT_TEMPLATES NewIntTemplates();
00310 
00311 void free_int_templates(INT_TEMPLATES templates);
00312 
00313 void ShowMatchDisplay();
00314 
00315 namespace tesseract {
00316 
00317 // Clears the given window and draws the featurespace guides for the
00318 // appropriate normalization method.
00319 void ClearFeatureSpaceWindow(NORM_METHOD norm_method, ScrollView* window);
00320 
00321 }  // namespace tesseract.
00322 
00323 /*----------------------------------------------------------------------------*/
00324 void RenderIntFeature(ScrollView *window, const INT_FEATURE_STRUCT* Feature,
00325                       ScrollView::Color color);
00326 
00327 void InitIntMatchWindowIfReqd();
00328 
00329 void InitProtoDisplayWindowIfReqd();
00330 
00331 void InitFeatureDisplayWindowIfReqd();
00332 
00333 // Creates a window of the appropriate size for displaying elements
00334 // in feature space.
00335 ScrollView* CreateFeatureSpaceWindow(const char* name, int xpos, int ypos);
00336 
00337 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines