tesseract  3.04.00
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 /*---------------------------------------------------------------------------*/
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 (tfscanf(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 } /* ReadFeature */
178 
179 
180 /*---------------------------------------------------------------------------*/
181 FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
182 /*
183  ** Parameters:
184  ** File open text file to read new feature set from
185  ** FeatureDesc specifies type of feature to read from File
186  ** Globals: none
187  ** Operation: Create a new feature set of the specified type and read in
188  ** the features from File. The correct text representation
189  ** for a feature set is an integer which specifies the number (N)
190  ** of features in a set followed by a list of N feature
191  ** descriptions.
192  ** Return: New feature set read from File.
193  ** Exceptions: none
194  ** History: Wed May 23 09:17:31 1990, DSJ, Created.
195  */
196  FEATURE_SET FeatureSet;
197  int NumFeatures;
198  int i;
199 
200  if (tfscanf(File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
201  DoError(ILLEGAL_NUM_FEATURES, "Illegal number of features in set");
202 
203  FeatureSet = NewFeatureSet(NumFeatures);
204  for (i = 0; i < NumFeatures; i++)
205  AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));
206 
207  return (FeatureSet);
208 } /* ReadFeatureSet */
209 
210 
211 /*---------------------------------------------------------------------------*/
212 /*
213  ** Parameters:
214  ** Feature: feature to write out to str
215  ** str: string to write Feature to
216  ** Operation: Appends a textual representation of Feature to str.
217  ** This representation is simply a list of the N parameters
218  ** of the feature, terminated with a newline. It is assumed
219  ** that the ExtraPenalty field can be reconstructed from the
220  ** parameters of the feature. It is also assumed that the
221  ** feature type information is specified or assumed elsewhere.
222  ** Return: none
223  ** Exceptions: none
224  ** History: Wed May 23 09:28:18 1990, DSJ, Created.
225  */
226 void WriteFeature(FEATURE Feature, STRING* str) {
227  for (int i = 0; i < Feature->Type->NumParams; i++) {
228 #ifndef WIN32
229  assert(!isnan(Feature->Params[i]));
230 #endif
231  str->add_str_double(" ", Feature->Params[i]);
232  }
233  *str += "\n";
234 } /* WriteFeature */
235 
236 
237 /*---------------------------------------------------------------------------*/
238 /*
239  ** Parameters:
240  ** FeatureSet: feature set to write to File
241  ** str: string to write Feature to
242  ** Globals: none
243  ** Operation: Write a textual representation of FeatureSet to File.
244  ** This representation is an integer specifying the number of
245  ** features in the set, followed by a newline, followed by
246  ** text representations for each feature in the set.
247  ** Return: none
248  ** Exceptions: none
249  ** History: Wed May 23 10:06:03 1990, DSJ, Created.
250  */
251 void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
252  if (FeatureSet) {
253  str->add_str_int("", FeatureSet->NumFeatures);
254  *str += "\n";
255  for (int i = 0; i < FeatureSet->NumFeatures; i++) {
256  WriteFeature(FeatureSet->Features[i], str);
257  }
258  }
259 } /* WriteFeatureSet */
260 
261 
262 /*---------------------------------------------------------------------------*/
263 void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
264 /*
265  ** Parameters:
266  ** File open text file to write FeatureDesc to
267  ** FeatureDesc feature descriptor to write to File
268  ** Globals: none
269  ** Operation: Write a textual representation of FeatureDesc to File
270  ** in the old format (i.e. the format used by the clusterer).
271  ** This format is:
272  ** Number of Params
273  ** Description of Param 1
274  ** ...
275  ** Return: none
276  ** Exceptions: none
277  ** History: Fri May 25 15:27:18 1990, DSJ, Created.
278  */
279  int i;
280 
281  fprintf (File, "%d\n", FeatureDesc->NumParams);
282  for (i = 0; i < FeatureDesc->NumParams; i++) {
283  if (FeatureDesc->ParamDesc[i].Circular)
284  fprintf (File, "circular ");
285  else
286  fprintf (File, "linear ");
287 
288  if (FeatureDesc->ParamDesc[i].NonEssential)
289  fprintf (File, "non-essential ");
290  else
291  fprintf (File, "essential ");
292 
293  fprintf (File, "%f %f\n",
294  FeatureDesc->ParamDesc[i].Min, FeatureDesc->ParamDesc[i].Max);
295  }
296 } /* WriteOldParamDesc */
FEATURE_STRUCT * FEATURE
Definition: ocrfeatures.h:67
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
#define ILLEGAL_FEATURE_PARAM
Definition: ocrfeatures.h:36
void free_struct(void *deadstruct, inT32, const char *)
Definition: memry.cpp:43
void add_str_double(const char *str, double number)
Definition: strngs.cpp:386
FEATURE Features[1]
Definition: ocrfeatures.h:72
#define FALSE
Definition: capi.h:29
#define isnan(x)
Definition: mathfix.h:31
void WriteFeature(FEATURE Feature, STRING *str)
void * alloc_struct(inT32 count, const char *)
Definition: memry.cpp:39
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:35
inT8 NonEssential
Definition: ocrfeatures.h:48
Definition: strngs.h:44
FEATURE_SET_STRUCT * FEATURE_SET
Definition: ocrfeatures.h:74
void DoError(int Error, const char *Message)
Definition: danerror.cpp:32
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
inT8 Circular
Definition: ocrfeatures.h:47
unsigned char BOOL8
Definition: host.h:113
#define TRUE
Definition: capi.h:28
void * Emalloc(int Size)
Definition: emalloc.cpp:35
#define ILLEGAL_NUM_FEATURES
Definition: ocrfeatures.h:37
FEATURE_SET NewFeatureSet(int NumFeatures)
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
void memfree(void *element)
Definition: freelist.cpp:30
FLOAT32 Min
Definition: ocrfeatures.h:49
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:59
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
void add_str_int(const char *str, int number)
Definition: strngs.cpp:376
const PARAM_DESC * ParamDesc
Definition: ocrfeatures.h:59
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
FLOAT32 Max
Definition: ocrfeatures.h:50
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:79
float FLOAT32
Definition: host.h:111