Coverage Report

Created: 2025-06-13 07:15

/src/tesseract/src/classify/protos.cpp
Line
Count
Source (jump to first uncovered line)
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
29
#include <cmath> // for M_PI
30
#include <cstdio>
31
32
namespace tesseract {
33
34
0
#define PROTO_INCREMENT 32
35
0
#define CONFIG_INCREMENT 16
36
37
/*----------------------------------------------------------------------
38
              F u n c t i o n s
39
----------------------------------------------------------------------*/
40
/**
41
 * @name AddConfigToClass
42
 *
43
 * Add a new config to this class.  Malloc new space and copy the
44
 * old configs if necessary.  Return the config id for the new config.
45
 *
46
 * @param Class The class to add to
47
 */
48
0
int AddConfigToClass(CLASS_TYPE Class) {
49
0
  int NewNumConfigs;
50
0
  int NewConfig;
51
0
  int MaxNumProtos;
52
0
  BIT_VECTOR Config;
53
54
0
  MaxNumProtos = Class->MaxNumProtos;
55
0
  ASSERT_HOST(MaxNumProtos <= MAX_NUM_PROTOS);
56
57
0
  if (Class->NumConfigs >= Class->MaxNumConfigs) {
58
    /* add configs in CONFIG_INCREMENT chunks at a time */
59
0
    NewNumConfigs =
60
0
        (((Class->MaxNumConfigs + CONFIG_INCREMENT) / CONFIG_INCREMENT) * CONFIG_INCREMENT);
61
62
0
    Class->Configurations.resize(NewNumConfigs);
63
0
    Class->MaxNumConfigs = NewNumConfigs;
64
0
  }
65
0
  NewConfig = Class->NumConfigs++;
66
0
  Config = NewBitVector(MAX_NUM_PROTOS);
67
0
  Class->Configurations[NewConfig] = Config;
68
0
  zero_all_bits(Config, WordsInVectorOfSize(MAX_NUM_PROTOS));
69
70
0
  return (NewConfig);
71
0
}
72
73
/**
74
 * @name AddProtoToClass
75
 *
76
 * Add a new proto to this class.  Malloc new space and copy the
77
 * old protos if necessary.  Return the proto id for the new proto.
78
 *
79
 * @param Class The class to add to
80
 */
81
0
int AddProtoToClass(CLASS_TYPE Class) {
82
0
  if (Class->NumProtos >= Class->MaxNumProtos) {
83
    /* add protos in PROTO_INCREMENT chunks at a time */
84
0
    int NewNumProtos =
85
0
        (((Class->MaxNumProtos + PROTO_INCREMENT) / PROTO_INCREMENT) * PROTO_INCREMENT);
86
87
0
    Class->Prototypes.resize(NewNumProtos);
88
89
0
    Class->MaxNumProtos = NewNumProtos;
90
0
    ASSERT_HOST(NewNumProtos <= MAX_NUM_PROTOS);
91
0
  }
92
0
  int NewProto = Class->NumProtos++;
93
0
  ASSERT_HOST(Class->NumProtos <= MAX_NUM_PROTOS);
94
0
  return (NewProto);
95
0
}
96
97
/**********************************************************************
98
 * FillABC
99
 *
100
 * Fill in Protos A, B, C fields based on the X, Y, Angle fields.
101
 **********************************************************************/
102
13.2k
void FillABC(PROTO_STRUCT *Proto) {
103
13.2k
  float Slope, Intercept, Normalizer;
104
105
13.2k
  Slope = tan(Proto->Angle * 2.0 * M_PI);
106
13.2k
  Intercept = Proto->Y - Slope * Proto->X;
107
13.2k
  Normalizer = 1.0 / sqrt(Slope * Slope + 1.0);
108
13.2k
  Proto->A = Slope * Normalizer;
109
13.2k
  Proto->B = -Normalizer;
110
13.2k
  Proto->C = Intercept * Normalizer;
111
13.2k
}
112
113
/**********************************************************************
114
 * FreeClass
115
 *
116
 * Deallocate the memory consumed by the specified class.
117
 **********************************************************************/
118
0
void FreeClass(CLASS_TYPE Class) {
119
0
  if (Class) {
120
0
    FreeClassFields(Class);
121
0
    delete Class;
122
0
  }
123
0
}
124
125
/**********************************************************************
126
 * FreeClassFields
127
 *
128
 * Deallocate the memory consumed by subfields of the specified class.
129
 **********************************************************************/
130
0
void FreeClassFields(CLASS_TYPE Class) {
131
0
  if (Class) {
132
0
    for (int i = 0; i < Class->NumConfigs; i++) {
133
0
      FreeBitVector(Class->Configurations[i]);
134
0
    }
135
0
  }
136
0
}
137
138
/**********************************************************************
139
 * NewClass
140
 *
141
 * Allocate a new class with enough memory to hold the specified number
142
 * of prototypes and configurations.
143
 **********************************************************************/
144
0
CLASS_TYPE NewClass(int NumProtos, int NumConfigs) {
145
0
  CLASS_TYPE Class;
146
147
0
  Class = new CLASS_STRUCT;
148
149
0
  Class->Prototypes.resize(NumProtos);
150
0
  Class->Configurations.resize(NumConfigs);
151
0
  Class->MaxNumProtos = NumProtos;
152
0
  Class->MaxNumConfigs = NumConfigs;
153
0
  Class->NumProtos = 0;
154
0
  Class->NumConfigs = 0;
155
0
  return (Class);
156
0
}
157
158
} // namespace tesseract