tesseract  4.0.0-1-g2a2b
protos.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2  ******************************************************************************
3  *
4  * File: protos.cpp (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 "emalloc.h"
30 #include "callcpp.h"
31 #include "tprintf.h"
32 #include "globals.h"
33 #include "classify.h"
34 #include "params.h"
35 
36 #include <cstdio>
37 #include <cmath>
38 
39 #define PROTO_INCREMENT 32
40 #define CONFIG_INCREMENT 16
41 
42 /*----------------------------------------------------------------------
43  V a r i a b l e s
44 ----------------------------------------------------------------------*/
46 
47 STRING_VAR(classify_training_file, "MicroFeatures", "Training file");
48 
49 /*----------------------------------------------------------------------
50  F u n c t i o n s
51 ----------------------------------------------------------------------*/
61  int NewNumConfigs;
62  int NewConfig;
63  int MaxNumProtos;
65 
66  MaxNumProtos = Class->MaxNumProtos;
67 
68  if (Class->NumConfigs >= Class->MaxNumConfigs) {
69  /* add configs in CONFIG_INCREMENT chunks at a time */
70  NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) /
72 
73  Class->Configurations =
75  sizeof (BIT_VECTOR) * NewNumConfigs);
76 
77  Class->MaxNumConfigs = NewNumConfigs;
78  }
79  NewConfig = Class->NumConfigs++;
80  Config = NewBitVector (MaxNumProtos);
81  Class->Configurations[NewConfig] = Config;
82  zero_all_bits (Config, WordsInVectorOfSize (MaxNumProtos));
83 
84  return (NewConfig);
85 }
86 
87 
97  int i;
98  int Bit;
99  int NewNumProtos;
100  int NewProto;
102 
103  if (Class->NumProtos >= Class->MaxNumProtos) {
104  /* add protos in PROTO_INCREMENT chunks at a time */
105  NewNumProtos = (((Class->MaxNumProtos + PROTO_INCREMENT) /
107 
108  Class->Prototypes = (PROTO) Erealloc (Class->Prototypes,
109  sizeof (PROTO_STRUCT) *
110  NewNumProtos);
111 
112  Class->MaxNumProtos = NewNumProtos;
113 
114  for (i = 0; i < Class->NumConfigs; i++) {
115  Config = Class->Configurations[i];
116  Class->Configurations[i] = ExpandBitVector (Config, NewNumProtos);
117 
118  for (Bit = Class->NumProtos; Bit < NewNumProtos; Bit++)
119  reset_bit(Config, Bit);
120  }
121  }
122  NewProto = Class->NumProtos++;
123  if (Class->NumProtos > MAX_NUM_PROTOS) {
124  tprintf("Ouch! number of protos = %d, vs max of %d!",
125  Class->NumProtos, MAX_NUM_PROTOS);
126  }
127  return (NewProto);
128 }
129 
130 
140  int16_t Pid;
141  float TotalLength = 0;
142 
143  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
144  if (test_bit (Config, Pid)) {
145 
146  TotalLength += (ProtoIn (Class, Pid))->Length;
147  }
148  }
149  return (TotalLength);
150 }
151 
152 
161  int16_t Pid;
162  float TotalLength = 0;
163 
164  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
165  TotalLength += (ProtoIn (Class, Pid))->Length;
166  }
167  return (TotalLength);
168 }
169 
170 
179 void CopyProto(PROTO Src, PROTO Dest) {
180  Dest->X = Src->X;
181  Dest->Y = Src->Y;
182  Dest->Length = Src->Length;
183  Dest->Angle = Src->Angle;
184  Dest->A = Src->A;
185  Dest->B = Src->B;
186  Dest->C = Src->C;
187 }
188 
189 
190 /**********************************************************************
191  * FillABC
192  *
193  * Fill in Protos A, B, C fields based on the X, Y, Angle fields.
194  **********************************************************************/
195 void FillABC(PROTO Proto) {
196  float Slope, Intercept, Normalizer;
197 
198  Slope = tan(Proto->Angle * 2.0 * M_PI);
199  Intercept = Proto->Y - Slope * Proto->X;
200  Normalizer = 1.0 / sqrt (Slope * Slope + 1.0);
201  Proto->A = Slope * Normalizer;
202  Proto->B = -Normalizer;
203  Proto->C = Intercept * Normalizer;
204 }
205 
206 
207 /**********************************************************************
208  * FreeClass
209  *
210  * Deallocate the memory consumed by the specified class.
211  **********************************************************************/
212 void FreeClass(CLASS_TYPE Class) {
213  if (Class) {
214  FreeClassFields(Class);
215  delete Class;
216  }
217 }
218 
219 
220 /**********************************************************************
221  * FreeClassFields
222  *
223  * Deallocate the memory consumed by subfields of the specified class.
224  **********************************************************************/
226  int i;
227 
228  if (Class) {
229  if (Class->MaxNumProtos > 0) free(Class->Prototypes);
230  if (Class->MaxNumConfigs > 0) {
231  for (i = 0; i < Class->NumConfigs; i++)
232  FreeBitVector (Class->Configurations[i]);
233  free(Class->Configurations);
234  }
235  }
236 }
237 
238 /**********************************************************************
239  * NewClass
240  *
241  * Allocate a new class with enough memory to hold the specified number
242  * of prototypes and configurations.
243  **********************************************************************/
244 CLASS_TYPE NewClass(int NumProtos, int NumConfigs) {
245  CLASS_TYPE Class;
246 
247  Class = new CLASS_STRUCT;
248 
249  if (NumProtos > 0)
250  Class->Prototypes = (PROTO) Emalloc (NumProtos * sizeof (PROTO_STRUCT));
251 
252  if (NumConfigs > 0)
253  Class->Configurations = (CONFIGS) Emalloc (NumConfigs *
254  sizeof (BIT_VECTOR));
255  Class->MaxNumProtos = NumProtos;
256  Class->MaxNumConfigs = NumConfigs;
257  Class->NumProtos = 0;
258  Class->NumConfigs = 0;
259  return (Class);
260 
261 }
262 
263 
264 /**********************************************************************
265  * PrintProtos
266  *
267  * Print the list of prototypes in this class type.
268  **********************************************************************/
269 void PrintProtos(CLASS_TYPE Class) {
270  int16_t Pid;
271 
272  for (Pid = 0; Pid < Class->NumProtos; Pid++) {
273  cprintf ("Proto %d:\t", Pid);
274  PrintProto (ProtoIn (Class, Pid));
275  cprintf ("\t");
276  PrintProtoLine (ProtoIn (Class, Pid));
277  tprintf("\n");
278  }
279 }
#define CONFIG_INCREMENT
Definition: protos.cpp:40
int16_t NumProtos
Definition: protos.h:61
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:225
CLUSTERCONFIG Config
float ClassConfigLength(CLASS_TYPE Class, BIT_VECTOR Config)
Definition: protos.cpp:139
void cprintf(const char *format,...)
Definition: callcpp.cpp:33
#define STRING_VAR(name, val, comment)
Definition: params.h:282
float X
Definition: protos.h:46
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
float B
Definition: protos.h:44
PROTO_STRUCT * PROTO
Definition: protos.h:51
CLASS_STRUCT TrainingData[NUMBER_OF_CLASSES]
Definition: protos.cpp:45
BIT_VECTOR * CONFIGS
Definition: protos.h:40
void * Emalloc(int Size)
Definition: emalloc.cpp:31
uint32_t * BIT_VECTOR
Definition: bitvec.h:28
#define zero_all_bits(array, length)
Definition: bitvec.h:33
PROTO Prototypes
Definition: protos.h:63
void PrintProtos(CLASS_TYPE Class)
Definition: protos.cpp:269
int16_t MaxNumProtos
Definition: protos.h:62
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:51
#define ProtoIn(Class, Pid)
Definition: protos.h:121
int16_t NumConfigs
Definition: protos.h:64
float Y
Definition: protos.h:47
float Length
Definition: protos.h:49
void FreeClass(CLASS_TYPE Class)
Definition: protos.cpp:212
#define MAX_NUM_PROTOS
Definition: intproto.h:48
float ClassProtoLength(CLASS_TYPE Class)
Definition: protos.cpp:160
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:82
int16_t MaxNumConfigs
Definition: protos.h:65
int AddProtoToClass(CLASS_TYPE Class)
Definition: protos.cpp:96
float C
Definition: protos.h:45
#define NUMBER_OF_CLASSES
Definition: protos.h:75
int AddConfigToClass(CLASS_TYPE Class)
Definition: protos.cpp:60
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
void CopyProto(PROTO Src, PROTO Dest)
Definition: protos.cpp:179
BIT_VECTOR ExpandBitVector(BIT_VECTOR Vector, int NewNumBits)
Definition: bitvec.cpp:44
#define PrintProtoLine(Proto)
Definition: protos.h:141
void FillABC(PROTO Proto)
Definition: protos.cpp:195
#define reset_bit(array, bit)
Definition: bitvec.h:59
float Angle
Definition: protos.h:48
void * Erealloc(void *ptr, int size)
Definition: emalloc.cpp:38
CLASS_TYPE NewClass(int NumProtos, int NumConfigs)
Definition: protos.cpp:244
CONFIGS Configurations
Definition: protos.h:66
#define PrintProto(Proto)
Definition: protos.h:130
#define PROTO_INCREMENT
Definition: protos.cpp:39
float A
Definition: protos.h:43
#define test_bit(array, bit)
Definition: bitvec.h:61
char * classify_training_file
Definition: protos.cpp:47