Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ocrfeatures.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Filename: features.c
3 ** Purpose: Generic definition of a feature.
4 ** Author: Dan Johnson
5 ** History: Mon May 21 10:49:04 1990, DSJ, Created.
6 **
7 ** (c) Copyright Hewlett-Packard Company, 1988.
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 ******************************************************************************/
21 #include "ocrfeatures.h"
22 #include "emalloc.h"
23 #include "callcpp.h"
24 #include "danerror.h"
25 #include "freelist.h"
26 #include "scanutils.h"
27
28 #include <assert.h>
29 #include <math.h>
30
34 /*---------------------------------------------------------------------------*/
35 BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
36 /*
37 ** Parameters:
38 ** FeatureSet set of features to add Feature to
39 ** Feature feature to be added to FeatureSet
40 ** Globals: none
41 ** Operation: Add a feature to a feature set. If the feature set is
42 ** already full, FALSE is returned to indicate that the
43 ** feature could not be added to the set; otherwise, TRUE is
44 ** returned.
45 ** Return: TRUE if feature added to set, FALSE if set is already full.
46 ** Exceptions: none
47 ** History: Tue May 22 17:22:23 1990, DSJ, Created.
48 */
49 if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
50 FreeFeature(Feature);
51 return FALSE;
52 }
53
54 FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
55 return TRUE;
56 } /* AddFeature */
57
58 /*---------------------------------------------------------------------------*/
59 void FreeFeature(FEATURE Feature) {
60 /*
61 ** Parameters:
62 ** Feature feature to be deallocated.
63 ** Globals: none
64 ** Operation: Release the memory consumed by the specified feature.
65 ** Return: none
66 ** Exceptions: none
67 ** History: Mon May 21 13:33:27 1990, DSJ, Created.
68 */
69 if (Feature) {
70 free_struct (Feature, sizeof (FEATURE_STRUCT)
71 + sizeof (FLOAT32) * (Feature->Type->NumParams - 1),
72 "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
73 }
74
75 } /* FreeFeature */
76
77
78 /*---------------------------------------------------------------------------*/
79 void FreeFeatureSet(FEATURE_SET FeatureSet) {
80 /*
81 ** Parameters:
82 ** FeatureSet set of features to be freed
83 ** Globals: none
84 ** Operation: Release the memory consumed by the specified feature
85 ** set. This routine also frees the memory consumed by the
86 ** features contained in the set.
87 ** Return: none
88 ** Exceptions: none
89 ** History: Mon May 21 13:59:46 1990, DSJ, Created.
90 */
91 int i;
92
93 if (FeatureSet) {
94 for (i = 0; i < FeatureSet->NumFeatures; i++)
95 FreeFeature(FeatureSet->Features[i]);
96 memfree(FeatureSet);
97 }
98 } /* FreeFeatureSet */
99
100
101 /*---------------------------------------------------------------------------*/
102 FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
103 /*
104 ** Parameters:
105 ** FeatureDesc description of feature to be created.
106 ** Globals: none
107 ** Operation: Allocate and return a new feature of the specified
108 ** type.
109 ** Return: New feature.
110 ** Exceptions: none
111 ** History: Mon May 21 14:06:42 1990, DSJ, Created.
112 */
113 FEATURE Feature;
114
115 Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) +
116 (FeatureDesc->NumParams - 1) *
117 sizeof (FLOAT32),
118 "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
119 Feature->Type = FeatureDesc;
120 return (Feature);
121
122 } /* NewFeature */
123
124
125 /*---------------------------------------------------------------------------*/
126 FEATURE_SET NewFeatureSet(int NumFeatures) {
127 /*
128 ** Parameters:
129 ** NumFeatures maximum # of features to be put in feature set
130 ** Globals: none
131 ** Operation: Allocate and return a new feature set large enough to
132 ** hold the specified number of features.
133 ** Return: New feature set.
134 ** Exceptions: none
135 ** History: Mon May 21 14:22:40 1990, DSJ, Created.
136 */
137 FEATURE_SET FeatureSet;
138
139 FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
140 (NumFeatures - 1) * sizeof (FEATURE));
141 FeatureSet->MaxNumFeatures = NumFeatures;
142 FeatureSet->NumFeatures = 0;
143 return (FeatureSet);
144
145 } /* NewFeatureSet */
146
147
148 /*---------------------------------------------------------------------------*/
149 FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
150 /*
151 ** Parameters:
152 ** File open text file to read feature from
153 ** FeatureDesc specifies type of feature to read from File
154 ** Globals: none
155 ** Operation: Create a new feature of the specified type and read in
156 ** the value of its parameters from File. The extra penalty
157 ** for the feature is also computed by calling the appropriate
158 ** function for the specified feature type. The correct text
159 ** representation for a feature is a list of N floats where
160 ** N is the number of parameters in the feature.
161 ** Return: New feature read from File.
162 ** Exceptions: ILLEGAL_FEATURE_PARAM if text file doesn't match expected format
163 ** History: Wed May 23 08:53:16 1990, DSJ, Created.
164 */
165 FEATURE Feature;
166 int i;
167
168 Feature = NewFeature (FeatureDesc);
169 for (i = 0; i < Feature->Type->NumParams; i++) {
170 if (fscanf (File, "%f", &(Feature->Params[i])) != 1)
171 DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
172 #ifndef _WIN32
173 assert (!isnan(Feature->Params[i]));
174 #endif
175 }
176 return (Feature);
177
178 } /* ReadFeature */
179
180
181 /*---------------------------------------------------------------------------*/
182 FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
183 /*
184 ** Parameters:
185 ** File open text file to read new feature set from
186 ** FeatureDesc specifies type of feature to read from File
187 ** Globals: none
188 ** Operation: Create a new feature set of the specified type and read in
189 ** the features from File. The correct text representation
190 ** for a feature set is an integer which specifies the number (N)
191 ** of features in a set followed by a list of N feature
192 ** descriptions.
193 ** Return: New feature set read from File.
194 ** Exceptions: none
195 ** History: Wed May 23 09:17:31 1990, DSJ, Created.
196 */
197 FEATURE_SET FeatureSet;
198 int NumFeatures;
199 int i;
200
201 if (fscanf (File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
202 DoError (ILLEGAL_NUM_FEATURES, "Illegal number of features in set");
203
204 FeatureSet = NewFeatureSet (NumFeatures);
205 for (i = 0; i < NumFeatures; i++)
206 AddFeature (FeatureSet, ReadFeature (File, FeatureDesc));
207
208 return (FeatureSet);
209
210 } /* ReadFeatureSet */
211
212
213 /*---------------------------------------------------------------------------*/
214 void WriteFeature(FILE *File, FEATURE Feature) {
215 /*
216 ** Parameters:
217 ** File open text file to write Feature to
218 ** Feature feature to write out to File
219 ** Globals: none
220 ** Operation: Write a textual representation of Feature to File.
221 ** This representation is simply a list of the N parameters
222 ** of the feature, terminated with a newline. It is assumed
223 ** that the ExtraPenalty field can be reconstructed from the
224 ** parameters of the feature. It is also assumed that the
225 ** feature type information is specified or assumed elsewhere.
226 ** Return: none
227 ** Exceptions: none
228 ** History: Wed May 23 09:28:18 1990, DSJ, Created.
229 */
230 int i;
231
232 for (i = 0; i < Feature->Type->NumParams; i++) {
233 #ifndef _WIN32
234 assert(!isnan(Feature->Params[i]));
235 #endif
236 fprintf(File, " %g", Feature->Params[i]);
237 }
238 fprintf(File, "\n");
239 } /* WriteFeature */
240
241
242 /*---------------------------------------------------------------------------*/
243 void WriteFeatureSet(FILE *File, FEATURE_SET FeatureSet) {
244 /*
245 ** Parameters:
246 ** File open text file to write FeatureSet to
247 ** FeatureSet feature set to write to File
248 ** Globals: none
249 ** Operation: Write a textual representation of FeatureSet to File.
250 ** This representation is an integer specifying the number of
251 ** features in the set, followed by a newline, followed by
252 ** text representations for each feature in the set.
253 ** Return: none
254 ** Exceptions: none
255 ** History: Wed May 23 10:06:03 1990, DSJ, Created.
256 */
257 int i;
258
259 if (FeatureSet) {
260 fprintf (File, "%d\n", FeatureSet->NumFeatures);
261 for (i = 0; i < FeatureSet->NumFeatures; i++)
262 WriteFeature (File, FeatureSet->Features[i]);
263 }
264 } /* WriteFeatureSet */
265
266
267 /*---------------------------------------------------------------------------*/
268 void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
269 /*
270 ** Parameters:
271 ** File open text file to write FeatureDesc to
272 ** FeatureDesc feature descriptor to write to File
273 ** Globals: none
274 ** Operation: Write a textual representation of FeatureDesc to File
275 ** in the old format (i.e. the format used by the clusterer).
276 ** This format is:
277 ** Number of Params
278 ** Description of Param 1
279 ** ...
280 ** Return: none
281 ** Exceptions: none
282 ** History: Fri May 25 15:27:18 1990, DSJ, Created.
283 */
284 int i;
285
286 fprintf (File, "%d\n", FeatureDesc->NumParams);
287 for (i = 0; i < FeatureDesc->NumParams; i++) {
288 if (FeatureDesc->ParamDesc[i].Circular)
289 fprintf (File, "circular ");
290 else
291 fprintf (File, "linear ");
292
293 if (FeatureDesc->ParamDesc[i].NonEssential)
294 fprintf (File, "non-essential ");
295 else
296 fprintf (File, "essential ");
297
298 fprintf (File, "%f %f\n",
299 FeatureDesc->ParamDesc[i].Min, FeatureDesc->ParamDesc[i].Max);
300 }
301 } /* WriteOldParamDesc */