tesseract  5.0.0-alpha-619-ge9db
featdefs.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: featdefs.cpp
3  ** Purpose: Definitions of currently defined feature types.
4  ** Author: Dan Johnson
5  **
6  ** (c) Copyright Hewlett-Packard Company, 1988.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  ******************************************************************************/
17 /*-----------------------------------------------------------------------------
18  Include Files and Type Defines
19 -----------------------------------------------------------------------------*/
20 #include "featdefs.h"
21 #include <cstring>
22 #include <cstdio>
23 #include "emalloc.h"
24 #include "picofeat.h" // for PicoFeatureLength
25 #include "scanutils.h"
26 
27 #define PICO_FEATURE_LENGTH 0.05
28 
29 /*-----------------------------------------------------------------------------
30  Global Data Definitions and Declarations
31 -----------------------------------------------------------------------------*/
32 const char* const kMicroFeatureType = "mf";
33 const char* const kCNFeatureType = "cn";
34 const char* const kIntFeatureType = "if";
35 const char* const kGeoFeatureType = "tb";
36 
37 // Define all of the parameters for the MicroFeature type.
38 StartParamDesc(MicroFeatureParams)
39 DefineParam(0, 0, -0.5, 0.5)
40 DefineParam(0, 0, -0.25, 0.75)
41 DefineParam(0, 1, 0.0, 1.0)
42 DefineParam(1, 0, 0.0, 1.0)
43 DefineParam (0, 1, -0.5, 0.5)
44 DefineParam (0, 1, -0.5, 0.5)
46 // Now define the feature type itself (see features.h for parameters).
47 DefineFeature(MicroFeatureDesc, 5, 1, kMicroFeatureType, MicroFeatureParams)
48 
49 // Define all of the parameters for the NormFeat type.
50 StartParamDesc (CharNormParams)
51 DefineParam(0, 0, -0.25, 0.75)
52 DefineParam(0, 1, 0.0, 1.0)
53 DefineParam(0, 0, 0.0, 1.0)
54 DefineParam(0, 0, 0.0, 1.0)
56 // Now define the feature type itself (see features.h for parameters).
57 DefineFeature(CharNormDesc, 4, 0, kCNFeatureType, CharNormParams)
58 
59 // Define all of the parameters for the IntFeature type
60 StartParamDesc(IntFeatParams)
61 DefineParam(0, 0, 0.0, 255.0)
62 DefineParam(0, 0, 0.0, 255.0)
63 DefineParam(1, 0, 0.0, 255.0)
65 // Now define the feature type itself (see features.h for parameters).
66 DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams)
67 
68 // Define all of the parameters for the GeoFeature type
69 StartParamDesc(GeoFeatParams)
70 DefineParam(0, 0, 0.0, 255.0)
71 DefineParam(0, 0, 0.0, 255.0)
72 DefineParam(0, 0, 0.0, 255.0)
74 // Now define the feature type itself (see features.h for parameters).
75 DefineFeature(GeoFeatDesc, 3, 0, kGeoFeatureType, GeoFeatParams)
76 
77 // Other features used for training the adaptive classifier, but not used
78 // during normal training, therefore not in the DescDefs array.
79 
80 // Define all of the parameters for the PicoFeature type
81 // define knob that can be used to adjust pico-feature length.
83 StartParamDesc(PicoFeatParams)
84 DefineParam(0, 0, -0.25, 0.75)
85 DefineParam(1, 0, 0.0, 1.0)
86 DefineParam(0, 0, -0.5, 0.5)
88 // Now define the feature type itself (see features.h for parameters).
89 DefineFeature(PicoFeatDesc, 2, 1, "pf", PicoFeatParams)
90 
91 // Define all of the parameters for the OutlineFeature type.
92 StartParamDesc(OutlineFeatParams)
93 DefineParam(0, 0, -0.5, 0.5)
94 DefineParam(0, 0, -0.25, 0.75)
95 DefineParam(0, 0, 0.0, 1.0)
96 DefineParam(1, 0, 0.0, 1.0)
98 // Now define the feature type itself (see features.h for parameters).
99 DefineFeature(OutlineFeatDesc, 3, 1, "of", OutlineFeatParams)
100 
101 // MUST be kept in-sync with ExtractorDefs in fxdefs.cpp.
102 static const FEATURE_DESC_STRUCT *DescDefs[NUM_FEATURE_TYPES] = {
104  &CharNormDesc,
105  &IntFeatDesc,
106  &GeoFeatDesc
107 };
108 
109 /*-----------------------------------------------------------------------------
110  Public Code
111 -----------------------------------------------------------------------------*/
112 void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs) {
113  featuredefs->NumFeatureTypes = NUM_FEATURE_TYPES;
114  for (int i = 0; i < NUM_FEATURE_TYPES; ++i) {
115  featuredefs->FeatureDesc[i] = DescDefs[i];
116  }
117 }
118 
119 /*---------------------------------------------------------------------------*/
129 void FreeCharDescription(CHAR_DESC CharDesc) {
130  if (CharDesc) {
131  for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
132  FreeFeatureSet (CharDesc->FeatureSets[i]);
133  Efree(CharDesc);
134  }
135 } /* FreeCharDescription */
136 
137 
138 /*---------------------------------------------------------------------------*/
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 */
158 
159 /*---------------------------------------------------------------------------*/
174 void WriteCharDescription(const FEATURE_DEFS_STRUCT& FeatureDefs,
175  CHAR_DESC CharDesc, STRING* str) {
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 */
192 
193 // Return whether all of the fields of the given feature set
194 // are well defined (not inf or nan).
195 bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
196  CHAR_DESC CharDesc) {
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 */
216 
217 /*---------------------------------------------------------------------------*/
237  FILE *File) {
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 }
256 
257 /*---------------------------------------------------------------------------*/
270 uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs,
271  const char *ShortName) {
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 }
emalloc.h
DefineParam
DefineParam(0, 0, -0.25, 0.75) DefineParam(0
picofeat.h
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
PicoFeatureLength
TESS_API float PicoFeatureLength
InitFeatureDefs
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:111
kGeoFeatureType
const char *const kGeoFeatureType
Definition: featdefs.cpp:34
tfscanf
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:181
ReadFeatureSet
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:139
Emalloc
void * Emalloc(int Size)
Definition: emalloc.cpp:31
ShortNameToFeatureType
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:269
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
FEATURE_DESC_STRUCT::ShortName
const char * ShortName
Definition: ocrfeatures.h:53
CHAR_DESC_STRUCT::FeatureSets
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:40
GeoFeatDesc
const FEATURE_DESC_STRUCT GeoFeatDesc
STRING
Definition: strngs.h:45
FEATURE_STRUCT
Definition: ocrfeatures.h:58
StartParamDesc
StartParamDesc(MicroFeatureParams) DefineParam(0
OutlineFeatDesc
const FEATURE_DESC_STRUCT OutlineFeatDesc
kIntFeatureType
const char *const kIntFeatureType
Definition: featdefs.cpp:33
FEATURE_DEFS_STRUCT::FeatureDesc
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:46
WriteCharDescription
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
Definition: featdefs.cpp:173
FEATURE_DEFS_STRUCT::NumFeatureTypes
int32_t NumFeatureTypes
Definition: featdefs.h:45
EndParamDesc
#define EndParamDesc
Definition: ocrfeatures.h:90
FEATURE_DESC_STRUCT
Definition: ocrfeatures.h:51
WriteFeatureSet
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
Definition: ocrfeatures.cpp:179
FEATURE_DEFS_STRUCT
Definition: featdefs.h:44
FEATURE_DESC_STRUCT::NumParams
uint16_t NumParams
Definition: ocrfeatures.h:52
FEATURE_SET_STRUCT::Features
FEATURE Features[1]
Definition: ocrfeatures.h:67
FEATURE_STRUCT::Params
float Params[1]
Definition: ocrfeatures.h:60
FEATURE_STRUCT::Type
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:59
MicroFeatureDesc
const FEATURE_DESC_STRUCT MicroFeatureDesc
kMicroFeatureType
const char *const kMicroFeatureType
Definition: featdefs.cpp:31
CHAR_DESC_STRUCT
Definition: featdefs.h:38
NUM_FEATURE_TYPES
#define NUM_FEATURE_TYPES
Definition: featdefs.h:26
PicoFeatDesc
const TESS_API FEATURE_DESC_STRUCT PicoFeatDesc
CharNormDesc
const FEATURE_DESC_STRUCT CharNormDesc
scanutils.h
featdefs.h
Efree
void Efree(void *ptr)
Definition: emalloc.cpp:45
FreeFeatureSet
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:61
of
EndParamDesc of
Definition: featdefs.cpp:98
kCNFeatureType
const char *const kCNFeatureType
Definition: featdefs.cpp:32
DefineFeature
MicroFeatureParams CharNormParams EndParamDesc DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams) StartParamDesc(GeoFeatParams) DefineParam(0
IntFeatDesc
const FEATURE_DESC_STRUCT IntFeatDesc
CHAR_DESC_STRUCT::NumFeatureSets
uint32_t NumFeatureSets
Definition: featdefs.h:39
ValidCharDescription
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
Definition: featdefs.cpp:194
NewCharDescription
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:147
PICO_FEATURE_LENGTH
#define PICO_FEATURE_LENGTH
Definition: featdefs.cpp:26
FEATURE_SET_STRUCT::NumFeatures
uint16_t NumFeatures
Definition: ocrfeatures.h:65
FEAT_NAME_SIZE
#define FEAT_NAME_SIZE
Definition: ocrfeatures.h:32
ReadCharDescription
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
Definition: featdefs.cpp:235
FreeCharDescription
void FreeCharDescription(CHAR_DESC CharDesc)
Definition: featdefs.cpp:128