tesseract  4.1.0
protos.cpp
Go to the documentation of this file.
1 /* -*-C-*-
2  ******************************************************************************
3  *
4  * File: protos.cpp (Formerly protos.c)
5  * Author: Mark Seaman, OCR Technology
6  *
7  * (c) Copyright 1987, Hewlett-Packard Company.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  *****************************************************************************/
19 /*----------------------------------------------------------------------
20  I n c l u d e s
21 ----------------------------------------------------------------------*/
22 #define _USE_MATH_DEFINES // for M_PI
23 #include "protos.h"
24 #include <cmath> // for M_PI
25 #include <cstdio>
26 #include "emalloc.h"
27 #include "callcpp.h"
28 #include "tprintf.h"
29 #include "classify.h"
30 #include "params.h"
31 #include "intproto.h"
32 
33 #define PROTO_INCREMENT 32
34 #define CONFIG_INCREMENT 16
35 
36 /*----------------------------------------------------------------------
37  F u n c t i o n s
38 ----------------------------------------------------------------------*/
48  int NewNumConfigs;
49  int NewConfig;
50  int MaxNumProtos;
52 
53  MaxNumProtos = Class->MaxNumProtos;
54 
55  if (Class->NumConfigs >= Class->MaxNumConfigs) {
56  /* add configs in CONFIG_INCREMENT chunks at a time */
57  NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) /
59 
60  Class->Configurations =
61  static_cast<CONFIGS>(Erealloc (Class->Configurations,
62  sizeof (BIT_VECTOR) * NewNumConfigs));
63 
64  Class->MaxNumConfigs = NewNumConfigs;
65  }
66  NewConfig = Class->NumConfigs++;
67  Config = NewBitVector (MaxNumProtos);
68  Class->Configurations[NewConfig] = Config;
69  zero_all_bits (Config, WordsInVectorOfSize (MaxNumProtos));
70 
71  return (NewConfig);
72 }
73 
74 
84  int i;
85  int Bit;
86  int NewNumProtos;
87  int NewProto;
89 
90  if (Class->NumProtos >= Class->MaxNumProtos) {
91  /* add protos in PROTO_INCREMENT chunks at a time */
92  NewNumProtos = (((Class->MaxNumProtos + PROTO_INCREMENT) /
94 
95  Class->Prototypes = static_cast<PROTO>(Erealloc (Class->Prototypes,
96  sizeof (PROTO_STRUCT) *
97  NewNumProtos));
98 
99  Class->MaxNumProtos = NewNumProtos;
100 
101  for (i = 0; i < Class->NumConfigs; i++) {
102  Config = Class->Configurations[i];
103  Class->Configurations[i] = ExpandBitVector (Config, NewNumProtos);
104 
105  for (Bit = Class->NumProtos; Bit < NewNumProtos; Bit++)
106  reset_bit(Config, Bit);
107  }
108  }
109  NewProto = Class->NumProtos++;
110  if (Class->NumProtos > MAX_NUM_PROTOS) {
111  tprintf("Ouch! number of protos = %d, vs max of %d!",
112  Class->NumProtos, MAX_NUM_PROTOS);
113  }
114  return (NewProto);
115 }
116 
117 
118 /**********************************************************************
119  * FillABC
120  *
121  * Fill in Protos A, B, C fields based on the X, Y, Angle fields.
122  **********************************************************************/
123 void FillABC(PROTO Proto) {
124  float Slope, Intercept, Normalizer;
125 
126  Slope = tan(Proto->Angle * 2.0 * M_PI);
127  Intercept = Proto->Y - Slope * Proto->X;
128  Normalizer = 1.0 / sqrt (Slope * Slope + 1.0);
129  Proto->A = Slope * Normalizer;
130  Proto->B = -Normalizer;
131  Proto->C = Intercept * Normalizer;
132 }
133 
134 
135 /**********************************************************************
136  * FreeClass
137  *
138  * Deallocate the memory consumed by the specified class.
139  **********************************************************************/
140 void FreeClass(CLASS_TYPE Class) {
141  if (Class) {
142  FreeClassFields(Class);
143  delete Class;
144  }
145 }
146 
147 
148 /**********************************************************************
149  * FreeClassFields
150  *
151  * Deallocate the memory consumed by subfields of the specified class.
152  **********************************************************************/
154  int i;
155 
156  if (Class) {
157  if (Class->MaxNumProtos > 0) free(Class->Prototypes);
158  if (Class->MaxNumConfigs > 0) {
159  for (i = 0; i < Class->NumConfigs; i++)
160  FreeBitVector (Class->Configurations[i]);
161  free(Class->Configurations);
162  }
163  }
164 }
165 
166 /**********************************************************************
167  * NewClass
168  *
169  * Allocate a new class with enough memory to hold the specified number
170  * of prototypes and configurations.
171  **********************************************************************/
172 CLASS_TYPE NewClass(int NumProtos, int NumConfigs) {
173  CLASS_TYPE Class;
174 
175  Class = new CLASS_STRUCT;
176 
177  if (NumProtos > 0)
178  Class->Prototypes = static_cast<PROTO>(Emalloc (NumProtos * sizeof (PROTO_STRUCT)));
179 
180  if (NumConfigs > 0)
181  Class->Configurations = static_cast<CONFIGS>(Emalloc (NumConfigs *
182  sizeof (BIT_VECTOR)));
183  Class->MaxNumProtos = NumProtos;
184  Class->MaxNumConfigs = NumConfigs;
185  Class->NumProtos = 0;
186  Class->NumConfigs = 0;
187  return (Class);
188 
189 }
#define WordsInVectorOfSize(NumBits)
Definition: bitvec.h:63
void FreeBitVector(BIT_VECTOR BitVector)
Definition: bitvec.cpp:50
int AddProtoToClass(CLASS_TYPE Class)
Definition: protos.cpp:83
#define reset_bit(array, bit)
Definition: bitvec.h:59
int AddConfigToClass(CLASS_TYPE Class)
Definition: protos.cpp:47
#define zero_all_bits(array, length)
Definition: bitvec.h:33
#define PROTO_INCREMENT
Definition: protos.cpp:33
int16_t MaxNumProtos
Definition: protos.h:56
float A
Definition: protos.h:37
float X
Definition: protos.h:40
BIT_VECTOR NewBitVector(int NumBits)
Definition: bitvec.cpp:81
#define MAX_NUM_PROTOS
Definition: intproto.h:48
int16_t MaxNumConfigs
Definition: protos.h:59
void * Erealloc(void *ptr, int size)
Definition: emalloc.cpp:38
uint32_t * BIT_VECTOR
Definition: bitvec.h:28
CONFIGS Configurations
Definition: protos.h:60
void FillABC(PROTO Proto)
Definition: protos.cpp:123
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
float Angle
Definition: protos.h:42
float Y
Definition: protos.h:41
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:153
float C
Definition: protos.h:39
CLASS_TYPE NewClass(int NumProtos, int NumConfigs)
Definition: protos.cpp:172
int16_t NumProtos
Definition: protos.h:55
void FreeClass(CLASS_TYPE Class)
Definition: protos.cpp:140
#define CONFIG_INCREMENT
Definition: protos.cpp:34
void * Emalloc(int Size)
Definition: emalloc.cpp:31
float B
Definition: protos.h:38
BIT_VECTOR * CONFIGS
Definition: protos.h:34
BIT_VECTOR ExpandBitVector(BIT_VECTOR Vector, int NewNumBits)
Definition: bitvec.cpp:43
int16_t NumConfigs
Definition: protos.h:58
PROTO Prototypes
Definition: protos.h:57
CLUSTERCONFIG Config