tesseract  4.0.0-1-g2a2b
ocrfeatures.cpp File Reference
#include "ocrfeatures.h"
#include "emalloc.h"
#include "callcpp.h"
#include "scanutils.h"
#include <cassert>
#include <cmath>

Go to the source code of this file.

Functions

bool 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)
 

Function Documentation

◆ AddFeature()

bool 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.

Definition at line 41 of file ocrfeatures.cpp.

41  {
42  if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
43  FreeFeature(Feature);
44  return false;
45  }
46 
47  FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
48  return true;
49 } /* AddFeature */
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:56
uint16_t MaxNumFeatures
Definition: ocrfeatures.h:68
FEATURE Features[1]
Definition: ocrfeatures.h:69
uint16_t NumFeatures
Definition: ocrfeatures.h:67

◆ FreeFeature()

void FreeFeature ( FEATURE  Feature)

Release the memory consumed by the specified feature.

Parameters
Featurefeature to be deallocated.
Returns
none

Definition at line 56 of file ocrfeatures.cpp.

56 { free(Feature); } /* FreeFeature */

◆ FreeFeatureSet()

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

Definition at line 65 of file ocrfeatures.cpp.

65  {
66  int i;
67 
68  if (FeatureSet) {
69  for (i = 0; i < FeatureSet->NumFeatures; i++)
70  FreeFeature(FeatureSet->Features[i]);
71  free(FeatureSet);
72  }
73 } /* FreeFeatureSet */
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:56
FEATURE Features[1]
Definition: ocrfeatures.h:69
uint16_t NumFeatures
Definition: ocrfeatures.h:67

◆ NewFeature()

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.

Definition at line 81 of file ocrfeatures.cpp.

81  {
82  FEATURE Feature;
83 
84  Feature = (FEATURE)malloc(sizeof(FEATURE_STRUCT) +
85  (FeatureDesc->NumParams - 1) * sizeof(float));
86  Feature->Type = FeatureDesc;
87  return (Feature);
88 
89 } /* NewFeature */
FEATURE_STRUCT * FEATURE
Definition: ocrfeatures.h:64
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:61

◆ NewFeatureSet()

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.

Definition at line 97 of file ocrfeatures.cpp.

97  {
98  FEATURE_SET FeatureSet;
99 
100  FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
101  (NumFeatures - 1) * sizeof (FEATURE));
102  FeatureSet->MaxNumFeatures = NumFeatures;
103  FeatureSet->NumFeatures = 0;
104  return (FeatureSet);
105 
106 } /* NewFeatureSet */
void * Emalloc(int Size)
Definition: emalloc.cpp:31
uint16_t MaxNumFeatures
Definition: ocrfeatures.h:68
uint16_t NumFeatures
Definition: ocrfeatures.h:67
FEATURE_SET_STRUCT * FEATURE_SET
Definition: ocrfeatures.h:71

◆ ReadFeature()

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.

Definition at line 119 of file ocrfeatures.cpp.

119  {
120  FEATURE Feature;
121  int i;
122 
123  Feature = NewFeature (FeatureDesc);
124  for (i = 0; i < Feature->Type->NumParams; i++) {
125  ASSERT_HOST(tfscanf(File, "%f", &(Feature->Params[i])) == 1);
126 #ifndef _WIN32
127  assert (!std::isnan(Feature->Params[i]));
128 #endif
129  }
130  return Feature;
131 }
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:192
float Params[1]
Definition: ocrfeatures.h:62
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:81
#define ASSERT_HOST(x)
Definition: errcode.h:84
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:61

◆ ReadFeatureSet()

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.

Definition at line 143 of file ocrfeatures.cpp.

143  {
144  int NumFeatures;
145  ASSERT_HOST(tfscanf(File, "%d", &NumFeatures) == 1);
146  ASSERT_HOST(NumFeatures >= 0);
147 
148  FEATURE_SET FeatureSet = NewFeatureSet(NumFeatures);
149  for (int i = 0; i < NumFeatures; i++)
150  AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));
151 
152  return FeatureSet;
153 }
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:192
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:97
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:41
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ WriteFeature()

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

Definition at line 166 of file ocrfeatures.cpp.

166  {
167  for (int i = 0; i < Feature->Type->NumParams; i++) {
168 #ifndef WIN32
169  assert(!std::isnan(Feature->Params[i]));
170 #endif
171  str->add_str_double(" ", Feature->Params[i]);
172  }
173  *str += "\n";
174 } /* WriteFeature */
float Params[1]
Definition: ocrfeatures.h:62
void add_str_double(const char *str, double number)
Definition: strngs.cpp:389
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:61

◆ WriteFeatureSet()

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

Definition at line 185 of file ocrfeatures.cpp.

185  {
186  if (FeatureSet) {
187  str->add_str_int("", FeatureSet->NumFeatures);
188  *str += "\n";
189  for (int i = 0; i < FeatureSet->NumFeatures; i++) {
190  WriteFeature(FeatureSet->Features[i], str);
191  }
192  }
193 } /* WriteFeatureSet */
void WriteFeature(FEATURE Feature, STRING *str)
FEATURE Features[1]
Definition: ocrfeatures.h:69
uint16_t NumFeatures
Definition: ocrfeatures.h:67
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379