tesseract  4.00.00dev
featdefs.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: featdefs.c
3  ** Purpose: Definitions of currently defined feature types.
4  ** Author: Dan Johnson
5  ** History: Mon May 21 10:26:21 1990, DSJ, Created.
6  **
7  ** (c) Copyright Hewlett-Packard Company, 1988.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  ******************************************************************************/
18 /*-----------------------------------------------------------------------------
19  Include Files and Type Defines
20 -----------------------------------------------------------------------------*/
21 #include "featdefs.h"
22 #include "emalloc.h"
23 #include "danerror.h"
24 #include "scanutils.h"
25 
26 #include <string.h>
27 #include <stdio.h>
28 
30 #define ILLEGAL_NUM_SETS 3001
31 
32 #define PICO_FEATURE_LENGTH 0.05
33 
34 /*-----------------------------------------------------------------------------
35  Global Data Definitions and Declarations
36 -----------------------------------------------------------------------------*/
37 const char* kMicroFeatureType = "mf";
38 const char* kCNFeatureType = "cn";
39 const char* kIntFeatureType = "if";
40 const char* kGeoFeatureType = "tb";
41 
42 // Define all of the parameters for the MicroFeature type.
43 StartParamDesc(MicroFeatureParams)
44 DefineParam(0, 0, -0.5, 0.5)
45 DefineParam(0, 0, -0.25, 0.75)
46 DefineParam(0, 1, 0.0, 1.0)
47 DefineParam(1, 0, 0.0, 1.0)
48 DefineParam (0, 1, -0.5, 0.5)
49 DefineParam (0, 1, -0.5, 0.5)
51 // Now define the feature type itself (see features.h for parameters).
52 DefineFeature(MicroFeatureDesc, 5, 1, kMicroFeatureType, MicroFeatureParams)
53 
54 // Define all of the parameters for the NormFeat type.
55 StartParamDesc (CharNormParams)
56 DefineParam(0, 0, -0.25, 0.75)
57 DefineParam(0, 1, 0.0, 1.0)
58 DefineParam(0, 0, 0.0, 1.0)
59 DefineParam(0, 0, 0.0, 1.0)
61 // Now define the feature type itself (see features.h for parameters).
62 DefineFeature(CharNormDesc, 4, 0, kCNFeatureType, CharNormParams)
63 
64 // Define all of the parameters for the IntFeature type
65 StartParamDesc(IntFeatParams)
66 DefineParam(0, 0, 0.0, 255.0)
67 DefineParam(0, 0, 0.0, 255.0)
68 DefineParam(1, 0, 0.0, 255.0)
70 // Now define the feature type itself (see features.h for parameters).
71 DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams)
72 
73 // Define all of the parameters for the GeoFeature type
74 StartParamDesc(GeoFeatParams)
75 DefineParam(0, 0, 0.0, 255.0)
76 DefineParam(0, 0, 0.0, 255.0)
77 DefineParam(0, 0, 0.0, 255.0)
79 // Now define the feature type itself (see features.h for parameters).
80 DefineFeature(GeoFeatDesc, 3, 0, kGeoFeatureType, GeoFeatParams)
81 
82 // Other features used for training the adaptive classifier, but not used
83 // during normal training, therefore not in the DescDefs array.
84 
85 // Define all of the parameters for the PicoFeature type
86 // define knob that can be used to adjust pico-feature length.
88 StartParamDesc(PicoFeatParams)
89 DefineParam(0, 0, -0.25, 0.75)
90 DefineParam(1, 0, 0.0, 1.0)
91 DefineParam(0, 0, -0.5, 0.5)
93 // Now define the feature type itself (see features.h for parameters).
94 DefineFeature(PicoFeatDesc, 2, 1, "pf", PicoFeatParams)
95 
96 // Define all of the parameters for the OutlineFeature type.
97 StartParamDesc(OutlineFeatParams)
98 DefineParam(0, 0, -0.5, 0.5)
99 DefineParam(0, 0, -0.25, 0.75)
100 DefineParam(0, 0, 0.0, 1.0)
101 DefineParam(1, 0, 0.0, 1.0)
103 // Now define the feature type itself (see features.h for parameters).
104 DefineFeature(OutlineFeatDesc, 3, 1, "of", OutlineFeatParams)
105 
106 // MUST be kept in-sync with ExtractorDefs in fxdefs.cpp.
107 static const FEATURE_DESC_STRUCT *DescDefs[NUM_FEATURE_TYPES] = {
109  &CharNormDesc,
110  &IntFeatDesc,
111  &GeoFeatDesc
112 };
113 
114 /*-----------------------------------------------------------------------------
115  Public Code
116 -----------------------------------------------------------------------------*/
118  featuredefs->NumFeatureTypes = NUM_FEATURE_TYPES;
119  for (int i = 0; i < NUM_FEATURE_TYPES; ++i) {
120  featuredefs->FeatureDesc[i] = DescDefs[i];
121  }
122 }
123 
124 /*---------------------------------------------------------------------------*/
138  if (CharDesc) {
139  for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
140  FreeFeatureSet (CharDesc->FeatureSets[i]);
141  Efree(CharDesc);
142  }
143 } /* FreeCharDescription */
144 
145 
146 /*---------------------------------------------------------------------------*/
159  CHAR_DESC CharDesc;
160  CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT));
161  CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
162 
163  for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
164  CharDesc->FeatureSets[i] = NULL;
165 
166  return (CharDesc);
167 
168 } /* NewCharDescription */
169 
170 
171 /*---------------------------------------------------------------------------*/
190  CHAR_DESC CharDesc, STRING* str) {
191  int NumSetsToWrite = 0;
192 
193  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++)
194  if (CharDesc->FeatureSets[Type])
195  NumSetsToWrite++;
196 
197  str->add_str_int(" ", NumSetsToWrite);
198  *str += "\n";
199  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
200  if (CharDesc->FeatureSets[Type]) {
201  *str += FeatureDefs.FeatureDesc[Type]->ShortName;
202  *str += " ";
203  WriteFeatureSet(CharDesc->FeatureSets[Type], str);
204  }
205  }
206 } /* WriteCharDescription */
207 
208 // Return whether all of the fields of the given feature set
209 // are well defined (not inf or nan).
211  CHAR_DESC CharDesc) {
212  bool anything_written = false;
213  bool well_formed = true;
214  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
215  if (CharDesc->FeatureSets[Type]) {
216  for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
217  FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];
218  for (int p = 0; p < feat->Type->NumParams; p++) {
219  if (isnan(feat->Params[p]) || isinf(feat->Params[p]))
220  well_formed = false;
221  else
222  anything_written = true;
223  }
224  }
225  } else {
226  return false;
227  }
228  }
229  return anything_written && well_formed;
230 } /* ValidCharDescription */
231 
232 /*---------------------------------------------------------------------------*/
255  FILE *File) {
256  int NumSetsToRead;
257  char ShortName[FEAT_NAME_SIZE];
258  CHAR_DESC CharDesc;
259  int Type;
260 
261  if (tfscanf(File, "%d", &NumSetsToRead) != 1 ||
262  NumSetsToRead < 0 || NumSetsToRead > FeatureDefs.NumFeatureTypes)
263  DoError (ILLEGAL_NUM_SETS, "Illegal number of feature sets");
264 
265  CharDesc = NewCharDescription(FeatureDefs);
266  for (; NumSetsToRead > 0; NumSetsToRead--) {
267  tfscanf(File, "%s", ShortName);
268  Type = ShortNameToFeatureType(FeatureDefs, ShortName);
269  CharDesc->FeatureSets[Type] =
270  ReadFeatureSet (File, FeatureDefs.FeatureDesc[Type]);
271  }
272  return (CharDesc);
273 
274 } // ReadCharDescription
275 
276 
277 /*---------------------------------------------------------------------------*/
293 uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs,
294  const char *ShortName) {
295  int i;
296 
297  for (i = 0; i < FeatureDefs.NumFeatureTypes; i++)
298  if (!strcmp ((FeatureDefs.FeatureDesc[i]->ShortName), ShortName))
299  return static_cast<uint32_t>(i);
300  DoError (ILLEGAL_SHORT_NAME, "Illegal short name for a feature");
301  return 0;
302 
303 } // ShortNameToFeatureType
uinT32 NumFeatureSets
Definition: featdefs.h:43
void FreeCharDescription(CHAR_DESC CharDesc)
Definition: featdefs.cpp:137
CHAR_DESC_STRUCT * CHAR_DESC
Definition: featdefs.h:46
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
const char * ShortName
Definition: ocrfeatures.h:58
EndParamDesc of
Definition: featdefs.cpp:104
const char * kCNFeatureType
Definition: featdefs.cpp:38
void add_str_int(const char *str, int number)
Definition: strngs.cpp:381
#define ILLEGAL_SHORT_NAME
Definition: featdefs.h:34
const FEATURE_DESC_STRUCT IntFeatDesc
void Efree(void *ptr)
Definition: emalloc.cpp:79
const FEATURE_DESC_STRUCT GeoFeatDesc
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:228
const FEATURE_DESC_STRUCT OutlineFeatDesc
#define NUM_FEATURE_TYPES
Definition: featdefs.h:27
StartParamDesc(MicroFeatureParams) DefineParam(0
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
Definition: featdefs.cpp:189
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
inT32 NumFeatureTypes
Definition: featdefs.h:49
const char * kGeoFeatureType
Definition: featdefs.cpp:40
Definition: strngs.h:45
TESS_API const FEATURE_DESC_STRUCT PicoFeatDesc
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
const char * kMicroFeatureType
Definition: featdefs.cpp:37
#define ILLEGAL_NUM_SETS
Definition: featdefs.cpp:30
#define EndParamDesc
Definition: ocrfeatures.h:92
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:117
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:50
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
Definition: featdefs.cpp:254
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:69
const char * kIntFeatureType
Definition: featdefs.cpp:39
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
FEATURE Features[1]
Definition: ocrfeatures.h:72
#define PICO_FEATURE_LENGTH
Definition: featdefs.cpp:32
#define FEAT_NAME_SIZE
Definition: ocrfeatures.h:33
TESS_API FLOAT32 PicoFeatureLength
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
void * Emalloc(int Size)
Definition: emalloc.cpp:47
float FLOAT32
Definition: host.h:42
MicroFeatureParams CharNormParams EndParamDesc DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams) StartParamDesc(GeoFeatParams) DefineParam(0
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:158
DefineParam(0, 0, -0.25, 0.75) DefineParam(0
const FEATURE_DESC_STRUCT MicroFeatureDesc
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
Definition: featdefs.cpp:210
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:44
const FEATURE_DESC_STRUCT CharNormDesc
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:293