All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 (FEATURE Feature, STRING *str)
 
void WriteFeatureSet (FEATURE_SET FeatureSet, STRING *str)
 
void WriteOldParamDesc (FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
 

Function Documentation

BOOL8 AddFeature ( FEATURE_SET  FeatureSet,
FEATURE  Feature 
)

Add a feature to a feature set. If the feature set is already full, FALSE is returned to indicate that the feature could not be added to the set; otherwise, TRUE is returned.

Parameters
FeatureSetset of features to add Feature to
Featurefeature to be added to FeatureSet
Returns
TRUE if feature added to set, FALSE if set is already full.
Note
History: Tue May 22 17:22:23 1990, DSJ, Created.

Definition at line 44 of file ocrfeatures.cpp.

44  {
45  if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
46  FreeFeature(Feature);
47  return FALSE;
48  }
49 
50  FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
51  return TRUE;
52 } /* AddFeature */
FEATURE Features[1]
Definition: ocrfeatures.h:72
#define FALSE
Definition: capi.h:29
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:60
#define TRUE
Definition: capi.h:28
void FreeFeature ( FEATURE  Feature)

Release the memory consumed by the specified feature.

Parameters
Featurefeature to be deallocated.
Returns
none
Note
History: Mon May 21 13:33:27 1990, DSJ, Created.

Definition at line 60 of file ocrfeatures.cpp.

60  {
61  if (Feature) {
62  free_struct (Feature, sizeof (FEATURE_STRUCT)
63  + sizeof (FLOAT32) * (Feature->Type->NumParams - 1),
64  "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
65  }
66 
67 } /* FreeFeature */
float FLOAT32
Definition: host.h:111
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
void free_struct(void *deadstruct, inT32, const char *)
Definition: memry.cpp:43
void FreeFeatureSet ( FEATURE_SET  FeatureSet)

Release the memory consumed by the specified feature set. This routine also frees the memory consumed by the features contained in the set.

Parameters
FeatureSetset of features to be freed
Returns
none
Note
History: Mon May 21 13:59:46 1990, DSJ, Created.

Definition at line 78 of file ocrfeatures.cpp.

78  {
79  int i;
80 
81  if (FeatureSet) {
82  for (i = 0; i < FeatureSet->NumFeatures; i++)
83  FreeFeature(FeatureSet->Features[i]);
84  memfree(FeatureSet);
85  }
86 } /* FreeFeatureSet */
void memfree(void *element)
Definition: freelist.cpp:30
FEATURE Features[1]
Definition: ocrfeatures.h:72
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:60
FEATURE NewFeature ( const FEATURE_DESC_STRUCT FeatureDesc)

Allocate and return a new feature of the specified type.

Parameters
FeatureDescdescription of feature to be created.
Returns
New FEATURE.
Note
History: Mon May 21 14:06:42 1990, DSJ, Created.

Definition at line 96 of file ocrfeatures.cpp.

96  {
97  FEATURE Feature;
98 
99  Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) +
100  (FeatureDesc->NumParams - 1) *
101  sizeof (FLOAT32),
102  "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
103  Feature->Type = FeatureDesc;
104  return (Feature);
105 
106 } /* NewFeature */
float FLOAT32
Definition: host.h:111
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
void * alloc_struct(inT32 count, const char *)
Definition: memry.cpp:39
FEATURE_STRUCT * FEATURE
Definition: ocrfeatures.h:67
FEATURE_SET NewFeatureSet ( int  NumFeatures)

Allocate and return a new feature set large enough to hold the specified number of features.

Parameters
NumFeaturesmaximum # of features to be put in feature set
Returns
New FEATURE_SET.
Note
History: Mon May 21 14:22:40 1990, DSJ, Created.

Definition at line 116 of file ocrfeatures.cpp.

116  {
117  FEATURE_SET FeatureSet;
118 
119  FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
120  (NumFeatures - 1) * sizeof (FEATURE));
121  FeatureSet->MaxNumFeatures = NumFeatures;
122  FeatureSet->NumFeatures = 0;
123  return (FeatureSet);
124 
125 } /* NewFeatureSet */
void * Emalloc(int Size)
Definition: emalloc.cpp:47
FEATURE_SET_STRUCT * FEATURE_SET
Definition: ocrfeatures.h:74
FEATURE ReadFeature ( FILE *  File,
const FEATURE_DESC_STRUCT FeatureDesc 
)

Create a new feature of the specified type and read in the value of its parameters from File. The extra penalty for the feature is also computed by calling the appropriate function for the specified feature type. The correct text representation for a feature is a list of N floats where N is the number of parameters in the feature.

Parameters
Fileopen text file to read feature from
FeatureDescspecifies type of feature to read from File
Returns
New FEATURE read from File.
Note
Exceptions: ILLEGAL_FEATURE_PARAM if text file doesn't match expected format
History: Wed May 23 08:53:16 1990, DSJ, Created.

Definition at line 141 of file ocrfeatures.cpp.

141  {
142  FEATURE Feature;
143  int i;
144 
145  Feature = NewFeature (FeatureDesc);
146  for (i = 0; i < Feature->Type->NumParams; i++) {
147  if (tfscanf(File, "%f", &(Feature->Params[i])) != 1)
148  DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
149 #ifndef _WIN32
150  assert (!isnan(Feature->Params[i]));
151 #endif
152  }
153  return (Feature);
154 } /* ReadFeature */
#define isnan(x)
Definition: mathfix.h:31
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:96
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
#define ILLEGAL_FEATURE_PARAM
Definition: ocrfeatures.h:36
FEATURE_SET ReadFeatureSet ( FILE *  File,
const FEATURE_DESC_STRUCT FeatureDesc 
)

Create a new feature set of the specified type and read in the features from File. The correct text representation for a feature set is an integer which specifies the number (N) of features in a set followed by a list of N feature descriptions.

Parameters
Fileopen text file to read new feature set from
FeatureDescspecifies type of feature to read from File
Returns
New feature set read from File.
Note
History: Wed May 23 09:17:31 1990, DSJ, Created.

Definition at line 168 of file ocrfeatures.cpp.

168  {
169  FEATURE_SET FeatureSet;
170  int NumFeatures;
171  int i;
172 
173  if (tfscanf(File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
174  DoError(ILLEGAL_NUM_FEATURES, "Illegal number of features in set");
175 
176  FeatureSet = NewFeatureSet(NumFeatures);
177  for (i = 0; i < NumFeatures; i++)
178  AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));
179 
180  return (FeatureSet);
181 } /* ReadFeatureSet */
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
FEATURE_SET NewFeatureSet(int NumFeatures)
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
#define ILLEGAL_NUM_FEATURES
Definition: ocrfeatures.h:37
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:44
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
void WriteFeature ( FEATURE  Feature,
STRING str 
)

Appends a textual representation of Feature to str. This representation is simply a list of the N parameters of the feature, terminated with a newline. It is assumed that the ExtraPenalty field can be reconstructed from the parameters of the feature. It is also assumed that the feature type information is specified or assumed elsewhere.

Parameters
Featurefeature to write out to str
strstring to write Feature to
Returns
none
Note
History: Wed May 23 09:28:18 1990, DSJ, Created.

Definition at line 196 of file ocrfeatures.cpp.

196  {
197  for (int i = 0; i < Feature->Type->NumParams; i++) {
198 #ifndef WIN32
199  assert(!isnan(Feature->Params[i]));
200 #endif
201  str->add_str_double(" ", Feature->Params[i]);
202  }
203  *str += "\n";
204 } /* WriteFeature */
#define isnan(x)
Definition: mathfix.h:31
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
void add_str_double(const char *str, double number)
Definition: strngs.cpp:386
void WriteFeatureSet ( FEATURE_SET  FeatureSet,
STRING str 
)

Write a textual representation of FeatureSet to File. This representation is an integer specifying the number of features in the set, followed by a newline, followed by text representations for each feature in the set.

Parameters
FeatureSetfeature set to write to File
strstring to write Feature to
Returns
none
Note
History: Wed May 23 10:06:03 1990, DSJ, Created.

Definition at line 217 of file ocrfeatures.cpp.

217  {
218  if (FeatureSet) {
219  str->add_str_int("", FeatureSet->NumFeatures);
220  *str += "\n";
221  for (int i = 0; i < FeatureSet->NumFeatures; i++) {
222  WriteFeature(FeatureSet->Features[i], str);
223  }
224  }
225 } /* WriteFeatureSet */
void WriteFeature(FEATURE Feature, STRING *str)
FEATURE Features[1]
Definition: ocrfeatures.h:72
void add_str_int(const char *str, int number)
Definition: strngs.cpp:376
void WriteOldParamDesc ( FILE *  File,
const FEATURE_DESC_STRUCT FeatureDesc 
)

Write a textual representation of FeatureDesc to File in the old format (i.e. the format used by the clusterer).

This format is:

*  Number of Params
*  Description of Param 1
*  ...
* 
Parameters
Fileopen text file to write FeatureDesc to
FeatureDescfeature descriptor to write to File
Returns
none
Note
History: Fri May 25 15:27:18 1990, DSJ, Created.

Definition at line 243 of file ocrfeatures.cpp.

243  {
244  int i;
245 
246  fprintf (File, "%d\n", FeatureDesc->NumParams);
247  for (i = 0; i < FeatureDesc->NumParams; i++) {
248  if (FeatureDesc->ParamDesc[i].Circular)
249  fprintf (File, "circular ");
250  else
251  fprintf (File, "linear ");
252 
253  if (FeatureDesc->ParamDesc[i].NonEssential)
254  fprintf (File, "non-essential ");
255  else
256  fprintf (File, "essential ");
257 
258  fprintf (File, "%f %f\n",
259  FeatureDesc->ParamDesc[i].Min, FeatureDesc->ParamDesc[i].Max);
260  }
261 } /* WriteOldParamDesc */
FLOAT32 Min
Definition: ocrfeatures.h:49
inT8 NonEssential
Definition: ocrfeatures.h:48
inT8 Circular
Definition: ocrfeatures.h:47
const PARAM_DESC * ParamDesc
Definition: ocrfeatures.h:59
FLOAT32 Max
Definition: ocrfeatures.h:50