tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/classify/protos.cpp
Go to the documentation of this file.
00001 /* -*-C-*-
00002  ********************************************************************************
00003  *
00004  * File:        protos.c  (Formerly protos.c)
00005  * Description:
00006  * Author:       Mark Seaman, OCR Technology
00007  * Created:      Fri Oct 16 14:37:00 1987
00008  * Modified:     Mon Mar  4 14:51:24 1991 (Dan Johnson) danj@hpgrlj
00009  * Language:     C
00010  * Package:      N/A
00011  * Status:       Reusable Software Component
00012  *
00013  * (c) Copyright 1987, Hewlett-Packard Company.
00014  ** Licensed under the Apache License, Version 2.0 (the "License");
00015  ** you may not use this file except in compliance with the License.
00016  ** You may obtain a copy of the License at
00017  ** http://www.apache.org/licenses/LICENSE-2.0
00018  ** Unless required by applicable law or agreed to in writing, software
00019  ** distributed under the License is distributed on an "AS IS" BASIS,
00020  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  ** See the License for the specific language governing permissions and
00022  ** limitations under the License.
00023  *
00024  *********************************************************************************/
00025 /*----------------------------------------------------------------------
00026               I n c l u d e s
00027 ----------------------------------------------------------------------*/
00028 #include "protos.h"
00029 #include "const.h"
00030 #include "emalloc.h"
00031 #include "freelist.h"
00032 #include "callcpp.h"
00033 #include "tprintf.h"
00034 #include "scanutils.h"
00035 #include "globals.h"
00036 #include "classify.h"
00037 #include "params.h"
00038 
00039 #include <stdio.h>
00040 #include <math.h>
00041 
00042 #define PROTO_INCREMENT   32
00043 #define CONFIG_INCREMENT  16
00044 
00045 /*----------------------------------------------------------------------
00046               V a r i a b l e s
00047 ----------------------------------------------------------------------*/
00048 CLASS_STRUCT TrainingData[NUMBER_OF_CLASSES];
00049 
00050 STRING_VAR(classify_training_file, "MicroFeatures", "Training file");
00051 
00052 /*----------------------------------------------------------------------
00053               F u n c t i o n s
00054 ----------------------------------------------------------------------*/
00063 int AddConfigToClass(CLASS_TYPE Class) {
00064   int NewNumConfigs;
00065   int NewConfig;
00066   int MaxNumProtos;
00067   BIT_VECTOR Config;
00068 
00069   MaxNumProtos = Class->MaxNumProtos;
00070 
00071   if (Class->NumConfigs >= Class->MaxNumConfigs) {
00072     /* add configs in CONFIG_INCREMENT chunks at a time */
00073     NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) /
00074       CONFIG_INCREMENT) * CONFIG_INCREMENT);
00075 
00076     Class->Configurations =
00077       (CONFIGS) Erealloc (Class->Configurations,
00078       sizeof (BIT_VECTOR) * NewNumConfigs);
00079 
00080     Class->MaxNumConfigs = NewNumConfigs;
00081   }
00082   NewConfig = Class->NumConfigs++;
00083   Config = NewBitVector (MaxNumProtos);
00084   Class->Configurations[NewConfig] = Config;
00085   zero_all_bits (Config, WordsInVectorOfSize (MaxNumProtos));
00086 
00087   return (NewConfig);
00088 }
00089 
00090 
00099 int AddProtoToClass(CLASS_TYPE Class) {
00100   int i;
00101   int Bit;
00102   int NewNumProtos;
00103   int NewProto;
00104   BIT_VECTOR Config;
00105 
00106   if (Class->NumProtos >= Class->MaxNumProtos) {
00107     /* add protos in PROTO_INCREMENT chunks at a time */
00108     NewNumProtos = (((Class->MaxNumProtos + PROTO_INCREMENT) /
00109       PROTO_INCREMENT) * PROTO_INCREMENT);
00110 
00111     Class->Prototypes = (PROTO) Erealloc (Class->Prototypes,
00112       sizeof (PROTO_STRUCT) *
00113       NewNumProtos);
00114 
00115     Class->MaxNumProtos = NewNumProtos;
00116 
00117     for (i = 0; i < Class->NumConfigs; i++) {
00118       Config = Class->Configurations[i];
00119       Class->Configurations[i] = ExpandBitVector (Config, NewNumProtos);
00120 
00121       for (Bit = Class->NumProtos; Bit < NewNumProtos; Bit++)
00122         reset_bit(Config, Bit);
00123     }
00124   }
00125   NewProto = Class->NumProtos++;
00126   if (Class->NumProtos > MAX_NUM_PROTOS) {
00127     tprintf("Ouch! number of protos = %d, vs max of %d!",
00128             Class->NumProtos, MAX_NUM_PROTOS);
00129   }
00130   return (NewProto);
00131 }
00132 
00133 
00142 FLOAT32 ClassConfigLength(CLASS_TYPE Class, BIT_VECTOR Config) {
00143   inT16 Pid;
00144   FLOAT32 TotalLength = 0;
00145 
00146   for (Pid = 0; Pid < Class->NumProtos; Pid++) {
00147     if (test_bit (Config, Pid)) {
00148 
00149       TotalLength += (ProtoIn (Class, Pid))->Length;
00150     }
00151   }
00152   return (TotalLength);
00153 }
00154 
00155 
00163 FLOAT32 ClassProtoLength(CLASS_TYPE Class) {
00164   inT16 Pid;
00165   FLOAT32 TotalLength = 0;
00166 
00167   for (Pid = 0; Pid < Class->NumProtos; Pid++) {
00168     TotalLength += (ProtoIn (Class, Pid))->Length;
00169   }
00170   return (TotalLength);
00171 }
00172 
00173 
00182 void CopyProto(PROTO Src, PROTO Dest) {
00183   Dest->X = Src->X;
00184   Dest->Y = Src->Y;
00185   Dest->Length = Src->Length;
00186   Dest->Angle = Src->Angle;
00187   Dest->A = Src->A;
00188   Dest->B = Src->B;
00189   Dest->C = Src->C;
00190 }
00191 
00192 
00193 /**********************************************************************
00194  * FillABC
00195  *
00196  * Fill in Protos A, B, C fields based on the X, Y, Angle fields.
00197  **********************************************************************/
00198 void FillABC(PROTO Proto) {
00199   FLOAT32 Slope, Intercept, Normalizer;
00200 
00201   Slope = tan (Proto->Angle * 2.0 * PI);
00202   Intercept = Proto->Y - Slope * Proto->X;
00203   Normalizer = 1.0 / sqrt (Slope * Slope + 1.0);
00204   Proto->A = Slope * Normalizer;
00205   Proto->B = -Normalizer;
00206   Proto->C = Intercept * Normalizer;
00207 }
00208 
00209 
00210 /**********************************************************************
00211  * FreeClass
00212  *
00213  * Deallocate the memory consumed by the specified class.
00214  **********************************************************************/
00215 void FreeClass(CLASS_TYPE Class) {
00216   if (Class) {
00217     FreeClassFields(Class);
00218     delete Class;
00219   }
00220 }
00221 
00222 
00223 /**********************************************************************
00224  * FreeClassFields
00225  *
00226  * Deallocate the memory consumed by subfields of the specified class.
00227  **********************************************************************/
00228 void FreeClassFields(CLASS_TYPE Class) {
00229   int i;
00230 
00231   if (Class) {
00232     if (Class->MaxNumProtos > 0)
00233       memfree (Class->Prototypes);
00234     if (Class->MaxNumConfigs > 0) {
00235       for (i = 0; i < Class->NumConfigs; i++)
00236         FreeBitVector (Class->Configurations[i]);
00237       memfree (Class->Configurations);
00238     }
00239   }
00240 }
00241 
00242 /**********************************************************************
00243  * NewClass
00244  *
00245  * Allocate a new class with enough memory to hold the specified number
00246  * of prototypes and configurations.
00247  **********************************************************************/
00248 CLASS_TYPE NewClass(int NumProtos, int NumConfigs) {
00249   CLASS_TYPE Class;
00250 
00251   Class = new CLASS_STRUCT;
00252 
00253   if (NumProtos > 0)
00254     Class->Prototypes = (PROTO) Emalloc (NumProtos * sizeof (PROTO_STRUCT));
00255 
00256   if (NumConfigs > 0)
00257     Class->Configurations = (CONFIGS) Emalloc (NumConfigs *
00258       sizeof (BIT_VECTOR));
00259   Class->MaxNumProtos = NumProtos;
00260   Class->MaxNumConfigs = NumConfigs;
00261   Class->NumProtos = 0;
00262   Class->NumConfigs = 0;
00263   return (Class);
00264 
00265 }
00266 
00267 
00268 /**********************************************************************
00269  * PrintProtos
00270  *
00271  * Print the list of prototypes in this class type.
00272  **********************************************************************/
00273 void PrintProtos(CLASS_TYPE Class) {
00274   inT16 Pid;
00275 
00276   for (Pid = 0; Pid < Class->NumProtos; Pid++) {
00277     cprintf ("Proto %d:\t", Pid);
00278     PrintProto (ProtoIn (Class, Pid));
00279     cprintf ("\t");
00280     PrintProtoLine (ProtoIn (Class, Pid));
00281     new_line();
00282   }
00283 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines