tesseract  5.0.0-alpha-619-ge9db
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 39 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;

◆ FreeFeature()

void FreeFeature ( FEATURE  Feature)

Release the memory consumed by the specified feature.

Parameters
Featurefeature to be deallocated.

Definition at line 53 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 61 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  }

◆ 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 77 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 

◆ 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 93 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 

◆ 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 139 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;

◆ 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 179 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  }
AddFeature
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:39
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tfscanf
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:181
Emalloc
void * Emalloc(int Size)
Definition: emalloc.cpp:31
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
FEATURE_STRUCT
Definition: ocrfeatures.h:58
FreeFeature
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:53
FEATURE_SET_STRUCT::MaxNumFeatures
uint16_t MaxNumFeatures
Definition: ocrfeatures.h:66
FEATURE_DESC_STRUCT::NumParams
uint16_t NumParams
Definition: ocrfeatures.h:52
FEATURE_SET_STRUCT::Features
FEATURE Features[1]
Definition: ocrfeatures.h:67
FEATURE_STRUCT::Type
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:59
FEATURE_SET_STRUCT
Definition: ocrfeatures.h:64
NewFeatureSet
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:93
FEATURE_SET_STRUCT::NumFeatures
uint16_t NumFeatures
Definition: ocrfeatures.h:65