tesseract  4.1.1
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  ASSERT_HOST(MaxNumProtos <= MAX_NUM_PROTOS);
55 
56  if (Class->NumConfigs >= Class->MaxNumConfigs) {
57  /* add configs in CONFIG_INCREMENT chunks at a time */
58  NewNumConfigs = (((Class->MaxNumConfigs + CONFIG_INCREMENT) /
60 
61  Class->Configurations =
62  static_cast<CONFIGS>(Erealloc (Class->Configurations,
63  sizeof (BIT_VECTOR) * NewNumConfigs));
64 
65  Class->MaxNumConfigs = NewNumConfigs;
66  }
67  NewConfig = Class->NumConfigs++;
68  Config = NewBitVector(MAX_NUM_PROTOS);
69  Class->Configurations[NewConfig] = Config;
70  zero_all_bits (Config, WordsInVectorOfSize(MAX_NUM_PROTOS));
71 
72  return (NewConfig);
73 }
74 
75 
85  if (Class->NumProtos >= Class->MaxNumProtos) {
86  /* add protos in PROTO_INCREMENT chunks at a time */
87  int NewNumProtos = (((Class->MaxNumProtos + PROTO_INCREMENT) /
89 
90  Class->Prototypes = static_cast<PROTO>(Erealloc (Class->Prototypes,
91  sizeof (PROTO_STRUCT) *
92  NewNumProtos));
93 
94  Class->MaxNumProtos = NewNumProtos;
95  ASSERT_HOST(NewNumProtos <= MAX_NUM_PROTOS);
96  }
97  int NewProto = Class->NumProtos++;
99  return (NewProto);
100 }
101 
102 
103 /**********************************************************************
104  * FillABC
105  *
106  * Fill in Protos A, B, C fields based on the X, Y, Angle fields.
107  **********************************************************************/
108 void FillABC(PROTO Proto) {
109  float Slope, Intercept, Normalizer;
110 
111  Slope = tan(Proto->Angle * 2.0 * M_PI);
112  Intercept = Proto->Y - Slope * Proto->X;
113  Normalizer = 1.0 / sqrt (Slope * Slope + 1.0);
114  Proto->A = Slope * Normalizer;
115  Proto->B = -Normalizer;
116  Proto->C = Intercept * Normalizer;
117 }
118 
119 
120 /**********************************************************************
121  * FreeClass
122  *
123  * Deallocate the memory consumed by the specified class.
124  **********************************************************************/
125 void FreeClass(CLASS_TYPE Class) {
126  if (Class) {
127  FreeClassFields(Class);
128  delete Class;
129  }
130 }
131 
132 
133 /**********************************************************************
134  * FreeClassFields
135  *
136  * Deallocate the memory consumed by subfields of the specified class.
137  **********************************************************************/
139  int i;
140 
141  if (Class) {
142  if (Class->MaxNumProtos > 0) free(Class->Prototypes);
143  if (Class->MaxNumConfigs > 0) {
144  for (i = 0; i < Class->NumConfigs; i++)
145  FreeBitVector (Class->Configurations[i]);
146  free(Class->Configurations);
147  }
148  }
149 }
150 
151 /**********************************************************************
152  * NewClass
153  *
154  * Allocate a new class with enough memory to hold the specified number
155  * of prototypes and configurations.
156  **********************************************************************/
157 CLASS_TYPE NewClass(int NumProtos, int NumConfigs) {
158  CLASS_TYPE Class;
159 
160  Class = new CLASS_STRUCT;
161 
162  if (NumProtos > 0)
163  Class->Prototypes = static_cast<PROTO>(Emalloc (NumProtos * sizeof (PROTO_STRUCT)));
164 
165  if (NumConfigs > 0)
166  Class->Configurations = static_cast<CONFIGS>(Emalloc (NumConfigs *
167  sizeof (BIT_VECTOR)));
168  Class->MaxNumProtos = NumProtos;
169  Class->MaxNumConfigs = NumConfigs;
170  Class->NumProtos = 0;
171  Class->NumConfigs = 0;
172  return (Class);
173 
174 }
#define ASSERT_HOST(x)
Definition: errcode.h:88
#define MAX_NUM_PROTOS
Definition: intproto.h:48
void FreeClass(CLASS_TYPE Class)
Definition: protos.cpp:125
#define PROTO_INCREMENT
Definition: protos.cpp:33
int AddProtoToClass(CLASS_TYPE Class)
Definition: protos.cpp:84
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:138
int AddConfigToClass(CLASS_TYPE Class)
Definition: protos.cpp:47
#define CONFIG_INCREMENT
Definition: protos.cpp:34
void FillABC(PROTO Proto)
Definition: protos.cpp:108
CLASS_TYPE NewClass(int NumProtos, int NumConfigs)
Definition: protos.cpp:157
BIT_VECTOR * CONFIGS
Definition: protos.h:34
uint32_t * BIT_VECTOR
Definition: bitvec.h:28
void * Erealloc(void *ptr, int size)
Definition: emalloc.cpp:38
void * Emalloc(int Size)
Definition: emalloc.cpp:31
CLUSTERCONFIG Config
float Angle
Definition: protos.h:42
float Y
Definition: protos.h:41
float B
Definition: protos.h:38
float A
Definition: protos.h:37
float C
Definition: protos.h:39
float X
Definition: protos.h:40
int16_t NumConfigs
Definition: protos.h:58
int16_t NumProtos
Definition: protos.h:55
CONFIGS Configurations
Definition: protos.h:60
int16_t MaxNumProtos
Definition: protos.h:56
int16_t MaxNumConfigs
Definition: protos.h:59
PROTO Prototypes
Definition: protos.h:57