comparison mupdf-source/thirdparty/tesseract/src/classify/protos.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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
33 namespace 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 ----------------------------------------------------------------------*/
41 /**
42 * @name AddConfigToClass
43 *
44 * Add a new config to this class. Malloc new space and copy the
45 * old configs if necessary. Return the config id for the new config.
46 *
47 * @param Class The class to add to
48 */
49 int AddConfigToClass(CLASS_TYPE Class) {
50 int NewNumConfigs;
51 int NewConfig;
52 int MaxNumProtos;
53 BIT_VECTOR Config;
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 =
61 (((Class->MaxNumConfigs + CONFIG_INCREMENT) / CONFIG_INCREMENT) * CONFIG_INCREMENT);
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
74 /**
75 * @name AddProtoToClass
76 *
77 * Add a new proto to this class. Malloc new space and copy the
78 * old protos if necessary. Return the proto id for the new proto.
79 *
80 * @param Class The class to add to
81 */
82 int AddProtoToClass(CLASS_TYPE Class) {
83 if (Class->NumProtos >= Class->MaxNumProtos) {
84 /* add protos in PROTO_INCREMENT chunks at a time */
85 int NewNumProtos =
86 (((Class->MaxNumProtos + PROTO_INCREMENT) / PROTO_INCREMENT) * PROTO_INCREMENT);
87
88 Class->Prototypes.resize(NewNumProtos);
89
90 Class->MaxNumProtos = NewNumProtos;
91 ASSERT_HOST(NewNumProtos <= MAX_NUM_PROTOS);
92 }
93 int NewProto = Class->NumProtos++;
94 ASSERT_HOST(Class->NumProtos <= MAX_NUM_PROTOS);
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 **********************************************************************/
103 void 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 **********************************************************************/
119 void FreeClass(CLASS_TYPE Class) {
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 **********************************************************************/
131 void FreeClassFields(CLASS_TYPE Class) {
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 **********************************************************************/
145 CLASS_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