tesseract  4.1.0
featdefs.cpp File Reference
#include "featdefs.h"
#include <cstring>
#include <cstdio>
#include "emalloc.h"
#include "picofeat.h"
#include "scanutils.h"

Go to the source code of this file.

Macros

#define PICO_FEATURE_LENGTH   0.05
 

Functions

 StartParamDesc (MicroFeatureParams) DefineParam(0
 
 DefineParam (0, 0,-0.25, 0.75) DefineParam(0
 
 DefineParam (1, 0, 0.0, 1.0) DefineParam(0
 
 DefineParam (0, 1,-0.5, 0.5) EndParamDesc DefineFeature(MicroFeatureDesc
 
MicroFeatureParams StartParamDesc (CharNormParams) DefineParam(0
 
MicroFeatureParams DefineParam (0, 1, 0.0, 1.0) DefineParam(0
 
MicroFeatureParams DefineParam (0, 0, 0.0, 1.0) EndParamDesc DefineFeature(CharNormDesc
 
MicroFeatureParams CharNormParams StartParamDesc (IntFeatParams) DefineParam(0
 
MicroFeatureParams CharNormParams DefineParam (0, 0, 0.0, 255.0) DefineParam(1
 
MicroFeatureParams CharNormParams EndParamDesc DefineFeature (IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams) StartParamDesc(GeoFeatParams) DefineParam(0
 
MicroFeatureParams CharNormParams EndParamDesc EndParamDesc DefineFeature (GeoFeatDesc, 3, 0, kGeoFeatureType, GeoFeatParams) float PicoFeatureLength
 
 StartParamDesc (PicoFeatParams) DefineParam(0
 
EndParamDesc DefineFeature (PicoFeatDesc, 2, 1,"pf", PicoFeatParams) StartParamDesc(OutlineFeatParams) DefineParam(0
 
void InitFeatureDefs (FEATURE_DEFS_STRUCT *featuredefs)
 
void FreeCharDescription (CHAR_DESC CharDesc)
 
CHAR_DESC NewCharDescription (const FEATURE_DEFS_STRUCT &FeatureDefs)
 
void WriteCharDescription (const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
 
bool ValidCharDescription (const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
 
CHAR_DESC ReadCharDescription (const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
 
uint32_t ShortNameToFeatureType (const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
 

Variables

const char *const kMicroFeatureType = "mf"
 
const char *const kCNFeatureType = "cn"
 
const char *const kIntFeatureType = "if"
 
const char *const kGeoFeatureType = "tb"
 
EndParamDesc of
 

Macro Definition Documentation

#define PICO_FEATURE_LENGTH   0.05

Definition at line 27 of file featdefs.cpp.

Function Documentation

MicroFeatureParams CharNormParams EndParamDesc DefineFeature ( IntFeatDesc  ,
,
,
kIntFeatureType  ,
IntFeatParams   
)
MicroFeatureParams CharNormParams EndParamDesc EndParamDesc DefineFeature ( GeoFeatDesc  ,
,
,
kGeoFeatureType  ,
GeoFeatParams   
)
EndParamDesc DefineFeature ( PicoFeatDesc  ,
,
,
"pf"  ,
PicoFeatParams   
)
EndParamDesc DefineParam ( ,
,
-0.  25,
0.  75 
)
EndParamDesc DefineParam ( ,
,
0.  0,
1.  0 
)
DefineParam ( ,
,
-0.  5,
0.  5 
)
MicroFeatureParams DefineParam ( ,
,
0.  0,
1.  0 
)
MicroFeatureParams DefineParam ( ,
,
0.  0,
1.  0 
)
MicroFeatureParams CharNormParams EndParamDesc DefineParam ( ,
,
0.  0,
255.  0 
)
void FreeCharDescription ( CHAR_DESC  CharDesc)

Release the memory consumed by the specified character description and all of the features in that description.

Parameters
CharDesccharacter description to be deallocated

Globals:

  • none

Definition at line 129 of file featdefs.cpp.

129  {
130  if (CharDesc) {
131  for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
132  FreeFeatureSet (CharDesc->FeatureSets[i]);
133  Efree(CharDesc);
134  }
135 } /* FreeCharDescription */
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
void Efree(void *ptr)
Definition: emalloc.cpp:45
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:62
uint32_t NumFeatureSets
Definition: featdefs.h:41
void InitFeatureDefs ( FEATURE_DEFS_STRUCT featuredefs)

Definition at line 112 of file featdefs.cpp.

112  {
113  featuredefs->NumFeatureTypes = NUM_FEATURE_TYPES;
114  for (int i = 0; i < NUM_FEATURE_TYPES; ++i) {
115  featuredefs->FeatureDesc[i] = DescDefs[i];
116  }
117 }
int32_t NumFeatureTypes
Definition: featdefs.h:47
#define NUM_FEATURE_TYPES
Definition: featdefs.h:28
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:48
CHAR_DESC NewCharDescription ( const FEATURE_DEFS_STRUCT FeatureDefs)

Allocate a new character description, initialize its feature sets to be empty, and return it.

Globals:

  • none
Returns
New character description structure.

Definition at line 148 of file featdefs.cpp.

148  {
149  CHAR_DESC CharDesc;
150  CharDesc = static_cast<CHAR_DESC>(Emalloc (sizeof (CHAR_DESC_STRUCT)));
151  CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
152 
153  for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
154  CharDesc->FeatureSets[i] = nullptr;
155 
156  return (CharDesc);
157 } /* NewCharDescription */
int32_t NumFeatureTypes
Definition: featdefs.h:47
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
void * Emalloc(int Size)
Definition: emalloc.cpp:31
uint32_t NumFeatureSets
Definition: featdefs.h:41
CHAR_DESC ReadCharDescription ( const FEATURE_DEFS_STRUCT FeatureDefs,
FILE *  File 
)

Read a character description from File, and return a data structure containing this information. The data is formatted as follows:

  NumberOfSets
          ShortNameForSet1 Set1
          ShortNameForSet2 Set2
          ...

Globals:

  • none
Parameters
FeatureDefsdefinitions of feature types/extractors
Fileopen text file to read character description from
Returns
Character description read from File.

Definition at line 236 of file featdefs.cpp.

237  {
238  int NumSetsToRead;
239  char ShortName[FEAT_NAME_SIZE];
240  CHAR_DESC CharDesc;
241  int Type;
242 
243  ASSERT_HOST(tfscanf(File, "%d", &NumSetsToRead) == 1);
244  ASSERT_HOST(NumSetsToRead >= 0);
245  ASSERT_HOST(NumSetsToRead <= FeatureDefs.NumFeatureTypes);
246 
247  CharDesc = NewCharDescription(FeatureDefs);
248  for (; NumSetsToRead > 0; NumSetsToRead--) {
249  tfscanf(File, "%s", ShortName);
250  Type = ShortNameToFeatureType(FeatureDefs, ShortName);
251  CharDesc->FeatureSets[Type] =
252  ReadFeatureSet (File, FeatureDefs.FeatureDesc[Type]);
253  }
254  return CharDesc;
255 }
int32_t NumFeatureTypes
Definition: featdefs.h:47
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:270
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:181
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:148
#define ASSERT_HOST(x)
Definition: errcode.h:88
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:48
#define FEAT_NAME_SIZE
Definition: ocrfeatures.h:33
uint32_t ShortNameToFeatureType ( const FEATURE_DEFS_STRUCT FeatureDefs,
const char *  ShortName 
)

Search through all features currently defined and return the feature type for the feature with the specified short name. Trap an error if the specified name is not found.

Globals:

  • none
Parameters
FeatureDefsdefinitions of feature types/extractors
ShortNameshort name of a feature type
Returns
Feature type which corresponds to ShortName.

Definition at line 270 of file featdefs.cpp.

271  {
272  for (int i = 0; i < FeatureDefs.NumFeatureTypes; i++)
273  if (!strcmp ((FeatureDefs.FeatureDesc[i]->ShortName), ShortName))
274  return static_cast<uint32_t>(i);
275  ASSERT_HOST(!"Illegal short name for a feature");
276  return 0;
277 }
const char * ShortName
Definition: ocrfeatures.h:54
int32_t NumFeatureTypes
Definition: featdefs.h:47
#define ASSERT_HOST(x)
Definition: errcode.h:88
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:48
StartParamDesc ( MicroFeatureParams  )
MicroFeatureParams StartParamDesc ( CharNormParams  )
MicroFeatureParams CharNormParams StartParamDesc ( IntFeatParams  )
StartParamDesc ( PicoFeatParams  )
bool ValidCharDescription ( const FEATURE_DEFS_STRUCT FeatureDefs,
CHAR_DESC  CharDesc 
)

Definition at line 195 of file featdefs.cpp.

196  {
197  bool anything_written = false;
198  bool well_formed = true;
199  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
200  if (CharDesc->FeatureSets[Type]) {
201  for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
202  FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];
203  for (int p = 0; p < feat->Type->NumParams; p++) {
204  if (std::isnan(feat->Params[p]) || std::isinf(feat->Params[p]))
205  well_formed = false;
206  else
207  anything_written = true;
208  }
209  }
210  } else {
211  return false;
212  }
213  }
214  return anything_written && well_formed;
215 } /* ValidCharDescription */
uint16_t NumFeatures
Definition: ocrfeatures.h:66
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:60
FEATURE Features[1]
Definition: ocrfeatures.h:68
float Params[1]
Definition: ocrfeatures.h:61
uint32_t NumFeatureSets
Definition: featdefs.h:41
void WriteCharDescription ( const FEATURE_DEFS_STRUCT FeatureDefs,
CHAR_DESC  CharDesc,
STRING str 
)

Appends a textual representation of CharDesc to str. The format used is to write out the number of feature sets which will be written followed by a representation of each feature set.

Each set starts with the short name for that feature followed by a description of the feature set. Feature sets which are not present are not written.

Parameters
FeatureDefsdefinitions of feature types/extractors
strstring to append CharDesc to
CharDesccharacter description to write to File

Definition at line 174 of file featdefs.cpp.

175  {
176  int NumSetsToWrite = 0;
177 
178  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++)
179  if (CharDesc->FeatureSets[Type])
180  NumSetsToWrite++;
181 
182  str->add_str_int(" ", NumSetsToWrite);
183  *str += "\n";
184  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
185  if (CharDesc->FeatureSets[Type]) {
186  *str += FeatureDefs.FeatureDesc[Type]->ShortName;
187  *str += " ";
188  WriteFeatureSet(CharDesc->FeatureSets[Type], str);
189  }
190  }
191 } /* WriteCharDescription */
const char * ShortName
Definition: ocrfeatures.h:54
void add_str_int(const char *str, int number)
Definition: strngs.cpp:377
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:48
uint32_t NumFeatureSets
Definition: featdefs.h:41

Variable Documentation

MicroFeatureParams kCNFeatureType = "cn"

Definition at line 33 of file featdefs.cpp.

const char* const kGeoFeatureType = "tb"

Definition at line 35 of file featdefs.cpp.

const char* const kIntFeatureType = "if"

Definition at line 34 of file featdefs.cpp.

kMicroFeatureType = "mf"

Definition at line 32 of file featdefs.cpp.

Definition at line 99 of file featdefs.cpp.