tesseract 4.1.1
Loading...
Searching...
No Matches
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_SET ReadFeatureSet (FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
 
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 40 of file ocrfeatures.cpp.

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

◆ FreeFeature()

void FreeFeature ( FEATURE  Feature)

Release the memory consumed by the specified feature.

Parameters
Featurefeature to be deallocated.

Definition at line 54 of file ocrfeatures.cpp.

54{ 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

Definition at line 62 of file ocrfeatures.cpp.

62 {
63 int i;
64
65 if (FeatureSet) {
66 for (i = 0; i < FeatureSet->NumFeatures; i++)
67 FreeFeature(FeatureSet->Features[i]);
68 free(FeatureSet);
69 }
70} /* FreeFeatureSet */

◆ 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 78 of file ocrfeatures.cpp.

78 {
79 FEATURE Feature;
80
81 Feature = static_cast<FEATURE>(malloc(sizeof(FEATURE_STRUCT) +
82 (FeatureDesc->NumParams - 1) * sizeof(float)));
83 Feature->Type = FeatureDesc;
84 return (Feature);
85
86} /* NewFeature */
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:60

◆ 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 94 of file ocrfeatures.cpp.

94 {
95 FEATURE_SET FeatureSet;
96
97 FeatureSet = static_cast<FEATURE_SET>(Emalloc (sizeof (FEATURE_SET_STRUCT) +
98 (NumFeatures - 1) * sizeof (FEATURE)));
99 FeatureSet->MaxNumFeatures = NumFeatures;
100 FeatureSet->NumFeatures = 0;
101 return (FeatureSet);
102
103} /* NewFeatureSet */
void * Emalloc(int Size)
Definition: emalloc.cpp:31

◆ 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 140 of file ocrfeatures.cpp.

140 {
141 int NumFeatures;
142 ASSERT_HOST(tfscanf(File, "%d", &NumFeatures) == 1);
143 ASSERT_HOST(NumFeatures >= 0);
144
145 FEATURE_SET FeatureSet = NewFeatureSet(NumFeatures);
146 for (int i = 0; i < NumFeatures; i++)
147 AddFeature(FeatureSet, ReadFeature(File, FeatureDesc));
148
149 return FeatureSet;
150}
#define ASSERT_HOST(x)
Definition: errcode.h:88
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:181
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:40
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:94

◆ 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

Definition at line 180 of file ocrfeatures.cpp.

180 {
181 if (FeatureSet) {
182 str->add_str_int("", FeatureSet->NumFeatures);
183 *str += "\n";
184 for (int i = 0; i < FeatureSet->NumFeatures; i++) {
185 WriteFeature(FeatureSet->Features[i], str);
186 }
187 }
188} /* WriteFeatureSet */
void add_str_int(const char *str, int number)
Definition: strngs.cpp:377