tesseract  4.0.0-1-g2a2b
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 "emalloc.h"
22 #include "scanutils.h"
23 
24 #include <cstring>
25 #include <cstdio>
26 
27 #define PICO_FEATURE_LENGTH 0.05
28 
29 /*-----------------------------------------------------------------------------
30  Global Data Definitions and Declarations
31 -----------------------------------------------------------------------------*/
32 const char* kMicroFeatureType = "mf";
33 const char* kCNFeatureType = "cn";
34 const char* kIntFeatureType = "if";
35 const char* 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 -----------------------------------------------------------------------------*/
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 /*---------------------------------------------------------------------------*/
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 = (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 /*---------------------------------------------------------------------------*/
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).
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 }
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:192
#define PICO_FEATURE_LENGTH
Definition: featdefs.cpp:27
void FreeCharDescription(CHAR_DESC CharDesc)
Definition: featdefs.cpp:129
const FEATURE_DESC_STRUCT MicroFeatureDesc
const char * ShortName
Definition: ocrfeatures.h:55
const FEATURE_DESC_STRUCT GeoFeatDesc
#define NUM_FEATURE_TYPES
Definition: featdefs.h:28
#define EndParamDesc
Definition: ocrfeatures.h:92
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
Definition: featdefs.cpp:236
void * Emalloc(int Size)
Definition: emalloc.cpp:31
EndParamDesc of
Definition: featdefs.cpp:99
StartParamDesc(MicroFeatureParams) DefineParam(0
void Efree(void *ptr)
Definition: emalloc.cpp:45
const FEATURE_DESC_STRUCT IntFeatDesc
DefineParam(0, 0, -0.25, 0.75) DefineParam(0
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
int32_t NumFeatureTypes
Definition: featdefs.h:47
float Params[1]
Definition: ocrfeatures.h:62
const char * kGeoFeatureType
Definition: featdefs.cpp:35
FEATURE Features[1]
Definition: ocrfeatures.h:69
const FEATURE_DESC_STRUCT OutlineFeatDesc
#define FEAT_NAME_SIZE
Definition: ocrfeatures.h:34
TESS_API float PicoFeatureLength
uint16_t NumFeatures
Definition: ocrfeatures.h:67
MicroFeatureParams CharNormParams EndParamDesc DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams) StartParamDesc(GeoFeatParams) DefineParam(0
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:112
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:42
const char * kMicroFeatureType
Definition: featdefs.cpp:32
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
Definition: featdefs.cpp:195
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
uint32_t NumFeatureSets
Definition: featdefs.h:41
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:65
Definition: strngs.h:45
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:48
const FEATURE_DESC_STRUCT CharNormDesc
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:270
const char * kCNFeatureType
Definition: featdefs.cpp:33
TESS_API const FEATURE_DESC_STRUCT PicoFeatDesc
CHAR_DESC_STRUCT * CHAR_DESC
Definition: featdefs.h:44
const char * kIntFeatureType
Definition: featdefs.cpp:34
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:148
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
Definition: featdefs.cpp:174
#define ASSERT_HOST(x)
Definition: errcode.h:84
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:61