Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ocrfeatures.cpp File Reference
#include "ocrfeatures.h"
#include "emalloc.h"
#include "callcpp.h"
#include "danerror.h"
#include "freelist.h"
#include "scanutils.h"
#include <assert.h>
#include <math.h>

Go to the source code of this file.

Functions

BOOL8 AddFeature (FEATURE_SET FeatureSet, FEATURE Feature)
 
void FreeFeature (FEATURE Feature)
 
void FreeFeatureSet (FEATURE_SET FeatureSet)
 
FEATURE NewFeature (const FEATURE_DESC_STRUCT *FeatureDesc)
 
FEATURE_SET NewFeatureSet (int NumFeatures)
 
FEATURE ReadFeature (FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
 
FEATURE_SET ReadFeatureSet (FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
 
void WriteFeature (FILE *File, FEATURE Feature)
 
void WriteFeatureSet (FILE *File, FEATURE_SET FeatureSet)
 
void WriteOldParamDesc (FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
 

Function Documentation

BOOL8 AddFeature ( FEATURE_SET  FeatureSet,
FEATURE  Feature 
)

Include Files and Type Defines

Public Code

Definition at line 35 of file ocrfeatures.cpp.

35  {
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 */
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:59
#define FALSE
Definition: capi.h:28
FEATURE Features[1]
Definition: ocrfeatures.h:71
#define TRUE
Definition: capi.h:27
void FreeFeature ( FEATURE  Feature)

Definition at line 59 of file ocrfeatures.cpp.

59  {
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 */
void free_struct(void *deadstruct, inT32, const char *)
Definition: memry.cpp:44
float FLOAT32
Definition: host.h:111
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:63
void FreeFeatureSet ( FEATURE_SET  FeatureSet)

Definition at line 79 of file ocrfeatures.cpp.

79  {
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 */
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:59
void memfree(void *element)
Definition: freelist.cpp:30
FEATURE Features[1]
Definition: ocrfeatures.h:71
FEATURE NewFeature ( const FEATURE_DESC_STRUCT FeatureDesc)

Definition at line 102 of file ocrfeatures.cpp.

102  {
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 */
float FLOAT32
Definition: host.h:111
FEATURE_STRUCT * FEATURE
Definition: ocrfeatures.h:66
void * alloc_struct(inT32 count, const char *)
Definition: memry.cpp:40
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:63
FEATURE_SET NewFeatureSet ( int  NumFeatures)

Definition at line 126 of file ocrfeatures.cpp.

126  {
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 */
FEATURE_SET_STRUCT * FEATURE_SET
Definition: ocrfeatures.h:73
void * Emalloc(size_t Size)
Definition: emalloc.cpp:35
FEATURE ReadFeature ( FILE *  File,
const FEATURE_DESC_STRUCT FeatureDesc 
)

Definition at line 149 of file ocrfeatures.cpp.

149  {
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 */
void DoError(int Error, const char *Message)
Definition: danerror.cpp:32
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
FLOAT32 Params[1]
Definition: ocrfeatures.h:64
#define ILLEGAL_FEATURE_PARAM
Definition: ocrfeatures.h:35
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:63
FEATURE_SET ReadFeatureSet ( FILE *  File,
const FEATURE_DESC_STRUCT FeatureDesc 
)

Definition at line 182 of file ocrfeatures.cpp.

182  {
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 */
FEATURE_SET NewFeatureSet(int NumFeatures)
void DoError(int Error, const char *Message)
Definition: danerror.cpp:32
#define ILLEGAL_NUM_FEATURES
Definition: ocrfeatures.h:36
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:35
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
void WriteFeature ( FILE *  File,
FEATURE  Feature 
)

Definition at line 214 of file ocrfeatures.cpp.

214  {
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 */
FLOAT32 Params[1]
Definition: ocrfeatures.h:64
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:63
void WriteFeatureSet ( FILE *  File,
FEATURE_SET  FeatureSet 
)

Definition at line 243 of file ocrfeatures.cpp.

243  {
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 */
void WriteFeature(FILE *File, FEATURE Feature)
FEATURE Features[1]
Definition: ocrfeatures.h:71
void WriteOldParamDesc ( FILE *  File,
const FEATURE_DESC_STRUCT FeatureDesc 
)

Definition at line 268 of file ocrfeatures.cpp.

268  {
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 */
inT8 Circular
Definition: ocrfeatures.h:46
FLOAT32 Min
Definition: ocrfeatures.h:48
FLOAT32 Max
Definition: ocrfeatures.h:49
inT8 NonEssential
Definition: ocrfeatures.h:47
const PARAM_DESC * ParamDesc
Definition: ocrfeatures.h:58