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