tesseract v5.3.3.20231005
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
23#include "protos.h"
24
25#include "classify.h"
26#include "intproto.h"
27#include "params.h"
28#include "tprintf.h"
29
30#include <cmath> // for M_PI
31#include <cstdio>
32
33namespace tesseract {
34
35#define PROTO_INCREMENT 32
36#define CONFIG_INCREMENT 16
37
38/*----------------------------------------------------------------------
39 F u n c t i o n s
40----------------------------------------------------------------------*/
50 int NewNumConfigs;
51 int NewConfig;
52 int MaxNumProtos;
54
55 MaxNumProtos = Class->MaxNumProtos;
56 ASSERT_HOST(MaxNumProtos <= MAX_NUM_PROTOS);
57
58 if (Class->NumConfigs >= Class->MaxNumConfigs) {
59 /* add configs in CONFIG_INCREMENT chunks at a time */
60 NewNumConfigs =
62
63 Class->Configurations.resize(NewNumConfigs);
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
83 if (Class->NumProtos >= Class->MaxNumProtos) {
84 /* add protos in PROTO_INCREMENT chunks at a time */
85 int NewNumProtos =
87
88 Class->Prototypes.resize(NewNumProtos);
89
90 Class->MaxNumProtos = NewNumProtos;
91 ASSERT_HOST(NewNumProtos <= MAX_NUM_PROTOS);
92 }
93 int NewProto = Class->NumProtos++;
95 return (NewProto);
96}
97
98/**********************************************************************
99 * FillABC
100 *
101 * Fill in Protos A, B, C fields based on the X, Y, Angle fields.
102 **********************************************************************/
103void FillABC(PROTO_STRUCT *Proto) {
104 float Slope, Intercept, Normalizer;
105
106 Slope = tan(Proto->Angle * 2.0 * M_PI);
107 Intercept = Proto->Y - Slope * Proto->X;
108 Normalizer = 1.0 / sqrt(Slope * Slope + 1.0);
109 Proto->A = Slope * Normalizer;
110 Proto->B = -Normalizer;
111 Proto->C = Intercept * Normalizer;
112}
113
114/**********************************************************************
115 * FreeClass
116 *
117 * Deallocate the memory consumed by the specified class.
118 **********************************************************************/
120 if (Class) {
121 FreeClassFields(Class);
122 delete Class;
123 }
124}
125
126/**********************************************************************
127 * FreeClassFields
128 *
129 * Deallocate the memory consumed by subfields of the specified class.
130 **********************************************************************/
132 if (Class) {
133 for (int i = 0; i < Class->NumConfigs; i++) {
134 FreeBitVector(Class->Configurations[i]);
135 }
136 }
137}
138
139/**********************************************************************
140 * NewClass
141 *
142 * Allocate a new class with enough memory to hold the specified number
143 * of prototypes and configurations.
144 **********************************************************************/
145CLASS_TYPE NewClass(int NumProtos, int NumConfigs) {
146 CLASS_TYPE Class;
147
148 Class = new CLASS_STRUCT;
149
150 Class->Prototypes.resize(NumProtos);
151 Class->Configurations.resize(NumConfigs);
152 Class->MaxNumProtos = NumProtos;
153 Class->MaxNumConfigs = NumConfigs;
154 Class->NumProtos = 0;
155 Class->NumConfigs = 0;
156 return (Class);
157}
158
159} // namespace tesseract
#define ASSERT_HOST(x)
Definition: errcode.h:54
uint32_t * BIT_VECTOR
Definition: bitvec.h:28
#define MAX_NUM_PROTOS
Definition: intproto.h:48
#define PROTO_INCREMENT
Definition: protos.cpp:35
#define CONFIG_INCREMENT
Definition: protos.cpp:36
int AddConfigToClass(CLASS_TYPE Class)
Definition: protos.cpp:49
int AddProtoToClass(CLASS_TYPE Class)
Definition: protos.cpp:82
CLUSTERCONFIG Config
void FreeClassFields(CLASS_TYPE Class)
Definition: protos.cpp:131
CLASS_TYPE NewClass(int NumProtos, int NumConfigs)
Definition: protos.cpp:145
void FreeClass(CLASS_TYPE Class)
Definition: protos.cpp:119
void FillABC(PROTO_STRUCT *Proto)
Definition: protos.cpp:103
std::vector< BIT_VECTOR > Configurations
Definition: protos.h:46
int16_t MaxNumConfigs
Definition: protos.h:44
int16_t MaxNumProtos
Definition: protos.h:42
std::vector< PROTO_STRUCT > Prototypes
Definition: protos.h:45