tesseract  4.00.00dev
protos.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: protos.c (Formerly protos.c)
5  * Description:
6  * Author: Mark Seaman, OCR Technology
7  * Created: Fri Oct 16 14:37:00 1987
8  * Modified: Mon Mar 4 14:51:24 1991 (Dan Johnson) danj@hpgrlj
9  * Language: C
10  * Package: N/A
11  * Status: Reusable Software Component
12  *
13  * (c) Copyright 1987, Hewlett-Packard Company.
14  ** Licensed under the Apache License, Version 2.0 (the "License");
15  ** you may not use this file except in compliance with the License.
16  ** You may obtain a copy of the License at
17  ** http://www.apache.org/licenses/LICENSE-2.0
18  ** Unless required by applicable law or agreed to in writing, software
19  ** distributed under the License is distributed on an "AS IS" BASIS,
20  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  ** See the License for the specific language governing permissions and
22  ** limitations under the License.
23  *
24  *********************************************************************************/
25 /*----------------------------------------------------------------------
26  I n c l u d e s
27 ----------------------------------------------------------------------*/
28 #include "protos.h"
29 #include "const.h"
30 #include "emalloc.h"
31 #include "callcpp.h"
32 #include "tprintf.h"
33 #include "scanutils.h"
34 #include "globals.h"
35 #include "classify.h"
36 #include "params.h"
37 
38 #include <stdio.h>
39 #include <math.h>
40 
41 #define PROTO_INCREMENT 32
42 #define CONFIG_INCREMENT 16
43 
44 /*----------------------------------------------------------------------
45  V a r i a b l e s
46 ----------------------------------------------------------------------*/
48 
49 STRING_VAR(classify_training_file, "MicroFeatures", "Training file");
50 
51 /*----------------------------------------------------------------------
52  F u n c t i o n s
53 ----------------------------------------------------------------------*/
63  int NewNumConfigs;
64  int NewConfig;
65  int MaxNumProtos;
67 
68  MaxNumProtos = Class->MaxNumProtos;
69 
70  if (Class->NumConfigs >= Class->MaxNumConfigs) {
71  /* add configs in CONFIG_INCREMENT chunks at a time */
72  NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) /
74 
75  Class->Configurations =
77  sizeof (BIT_VECTOR) * NewNumConfigs);
78 
79  Class->MaxNumConfigs = NewNumConfigs;
80  }
81  NewConfig = Class->NumConfigs++;
82  Config = NewBitVector (MaxNumProtos);
83  Class->Configurations[NewConfig] = Config;
84  zero_all_bits (Config, WordsInVectorOfSize (MaxNumProtos));
85 
86  return (NewConfig);
87 }
88 
89 
99  int i;
100  int Bit;
101  int NewNumProtos;
102  int NewProto;
104 
105  if (Class->NumProtos >= Class->MaxNumProtos) {
106  /* add protos in PROTO_INCREMENT chunks at a time */
107  NewNumProtos = (((Class->MaxNumProtos + PROTO_INCREMENT) /
109 
110  Class->Prototypes = (PROTO) Erealloc (Class->Prototypes,
111  sizeof (PROTO_STRUCT) *
112  NewNumProtos);
113 
114  Class->MaxNumProtos = NewNumProtos;
115 
116  for (i = 0; i < Class->NumConfigs; i++) {
117  Config = Class->Configurations[i];
118  Class->Configurations[i] = ExpandBitVector (Config, NewNumProtos);
119 
120  for (Bit = Class->NumProtos; Bit < NewNumProtos; Bit++)
121  reset_bit(Config, Bit);
122  }
123  }
124  NewProto = Class->NumProtos++;
125  if (Class->NumProtos > MAX_NUM_PROTOS) {
126  tprintf("Ouch! number of protos = %d, vs max of %d!",
127  Class->NumProtos, MAX_NUM_PROTOS);
128  }
129  return (NewProto);
130 }
131 
132 
142  inT16 Pid;
143  FLOAT32 TotalLength = 0;
144 
145  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
146  if (test_bit (Config, Pid)) {
147 
148  TotalLength += (ProtoIn (Class, Pid))->Length;
149  }
150  }
151  return (TotalLength);
152 }
153 
154 
163  inT16 Pid;
164  FLOAT32 TotalLength = 0;
165 
166  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
167  TotalLength += (ProtoIn (Class, Pid))->Length;
168  }
169  return (TotalLength);
170 }
171 
172 
181 void CopyProto(PROTO Src, PROTO Dest) {
182  Dest->X = Src->X;
183  Dest->Y = Src->Y;
184  Dest->Length = Src->Length;
185  Dest->Angle = Src->Angle;
186  Dest->A = Src->A;
187  Dest->B = Src->B;
188  Dest->C = Src->C;
189 }
190 
191 
192 /**********************************************************************
193  * FillABC
194  *
195  * Fill in Protos A, B, C fields based on the X, Y, Angle fields.
196  **********************************************************************/
197 void FillABC(PROTO Proto) {
198  FLOAT32 Slope, Intercept, Normalizer;
199 
200  Slope = tan (Proto->Angle * 2.0 * PI);
201  Intercept = Proto->Y - Slope * Proto->X;
202  Normalizer = 1.0 / sqrt (Slope * Slope + 1.0);
203  Proto->A = Slope * Normalizer;
204  Proto->B = -Normalizer;
205  Proto->C = Intercept * Normalizer;
206 }
207 
208 
209 /**********************************************************************
210  * FreeClass
211  *
212  * Deallocate the memory consumed by the specified class.
213  **********************************************************************/
214 void FreeClass(CLASS_TYPE Class) {
215  if (Class) {
216  FreeClassFields(Class);
217  delete Class;
218  }
219 }
220 
221 
222 /**********************************************************************
223  * FreeClassFields
224  *
225  * Deallocate the memory consumed by subfields of the specified class.
226  **********************************************************************/
228  int i;
229 
230  if (Class) {
231  if (Class->MaxNumProtos > 0)
232  free(Class->Prototypes);
233  if (Class->MaxNumConfigs > 0) {
234  for (i = 0; i < Class->NumConfigs; i++)
235  FreeBitVector (Class->Configurations[i]);
236  free(Class->Configurations);
237  }
238  }
239 }
240 
241 /**********************************************************************
242  * NewClass
243  *
244  * Allocate a new class with enough memory to hold the specified number
245  * of prototypes and configurations.
246  **********************************************************************/
247 CLASS_TYPE NewClass(int NumProtos, int NumConfigs) {
248  CLASS_TYPE Class;
249 
250  Class = new CLASS_STRUCT;
251 
252  if (NumProtos > 0)
253  Class->Prototypes = (PROTO) Emalloc (NumProtos * sizeof (PROTO_STRUCT));
254 
255  if (NumConfigs > 0)
256  Class->Configurations = (CONFIGS) Emalloc (NumConfigs *
257  sizeof (BIT_VECTOR));
258  Class->MaxNumProtos = NumProtos;
259  Class->MaxNumConfigs = NumConfigs;
260  Class->NumProtos = 0;
261  Class->NumConfigs = 0;
262  return (Class);
263 
264 }
265 
266 
267 /**********************************************************************
268  * PrintProtos
269  *
270  * Print the list of prototypes in this class type.
271  **********************************************************************/
272 void PrintProtos(CLASS_TYPE Class) {
273  inT16 Pid;
274 
275  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
276  cprintf ("Proto %d:\t", Pid);
277  PrintProto (ProtoIn (Class, Pid));
278  cprintf ("\t");
279  PrintProtoLine (ProtoIn (Class, Pid));
280  new_line();
281  }
282 }
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:54
#define MAX_NUM_PROTOS
Definition: intproto.h:47
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
FLOAT32 A
Definition: protos.h:44
BIT_VECTOR * CONFIGS
Definition: protos.h:40
void * Emalloc(int Size)
Definition: emalloc.cpp:47
inT16 MaxNumConfigs
Definition: protos.h:63
inT16 NumConfigs
Definition: protos.h:62
#define tprintf(...)
Definition: tprintf.h:31
void FillABC(PROTO Proto)
Definition: protos.cpp:197
CLASS_STRUCT TrainingData[NUMBER_OF_CLASSES]
Definition: protos.cpp:47
inT16 NumProtos
Definition: protos.h:59
#define PROTO_INCREMENT
Definition: protos.cpp:41
uinT32 * BIT_VECTOR
Definition: bitvec.h:28
CLUSTERCONFIG Config
int AddProtoToClass(CLASS_TYPE Class)
Definition: protos.cpp:98
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:89
CONFIGS Configurations
Definition: protos.h:64
int16_t inT16
Definition: host.h:36
char * classify_training_file
Definition: protos.cpp:49
FLOAT32 Length
Definition: protos.h:50
#define CONFIG_INCREMENT
Definition: protos.cpp:42
PROTO_STRUCT * PROTO
Definition: protos.h:52
#define PI
Definition: const.h:19
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:227
#define zero_all_bits(array, length)
Definition: bitvec.h:33
#define PrintProtoLine(Proto)
Definition: protos.h:148
PROTO Prototypes
Definition: protos.h:61
BIT_VECTOR ExpandBitVector(BIT_VECTOR Vector, int NewNumBits)
Definition: bitvec.cpp:47
void FreeClass(CLASS_TYPE Class)
Definition: protos.cpp:214
FLOAT32 Y
Definition: protos.h:48
FLOAT32 X
Definition: protos.h:47
FLOAT32 ClassProtoLength(CLASS_TYPE Class)
Definition: protos.cpp:162
CLASS_TYPE NewClass(int NumProtos, int NumConfigs)
Definition: protos.cpp:247
float FLOAT32
Definition: host.h:42
FLOAT32 Angle
Definition: protos.h:49
#define test_bit(array, bit)
Definition: bitvec.h:61
FLOAT32 ClassConfigLength(CLASS_TYPE Class, BIT_VECTOR Config)
Definition: protos.cpp:141
void CopyProto(PROTO Src, PROTO Dest)
Definition: protos.cpp:181
void * Erealloc(void *ptr, int size)
Definition: emalloc.cpp:64
int AddConfigToClass(CLASS_TYPE Class)
Definition: protos.cpp:62
void cprintf(const char *format,...)
Definition: callcpp.cpp:40
#define STRING_VAR(name, val, comment)
Definition: params.h:282
FLOAT32 C
Definition: protos.h:46
#define PrintProto(Proto)
Definition: protos.h:133
FLOAT32 B
Definition: protos.h:45
#define new_line()
Definition: cutil.h:83
void PrintProtos(CLASS_TYPE Class)
Definition: protos.cpp:272
inT16 MaxNumProtos
Definition: protos.h:60
#define ProtoIn(Class, Pid)
Definition: protos.h:123
#define reset_bit(array, bit)
Definition: bitvec.h:59
#define NUMBER_OF_CLASSES
Definition: protos.h:73