All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 #ifdef _MSC_VER
22 #include <mathfix.h>
23 #endif
24 
25 #include "featdefs.h"
26 #include "emalloc.h"
27 #include "danerror.h"
28 #include "scanutils.h"
29 
30 #include <string.h>
31 #include <stdio.h>
32 
34 #define ILLEGAL_NUM_SETS 3001
35 
36 #define PICO_FEATURE_LENGTH 0.05
37 
38 /*-----------------------------------------------------------------------------
39  Global Data Definitions and Declarations
40 -----------------------------------------------------------------------------*/
41 const char* kMicroFeatureType = "mf";
42 const char* kCNFeatureType = "cn";
43 const char* kIntFeatureType = "if";
44 const char* kGeoFeatureType = "tb";
45 
46 // Define all of the parameters for the MicroFeature type.
47 StartParamDesc(MicroFeatureParams)
48 DefineParam(0, 0, -0.5, 0.5)
49 DefineParam(0, 0, -0.25, 0.75)
50 DefineParam(0, 1, 0.0, 1.0)
51 DefineParam(1, 0, 0.0, 1.0)
52 DefineParam (0, 1, -0.5, 0.5)
53 DefineParam (0, 1, -0.5, 0.5)
55 // Now define the feature type itself (see features.h for parameters).
56 DefineFeature(MicroFeatureDesc, 5, 1, kMicroFeatureType, MicroFeatureParams)
57 
58 // Define all of the parameters for the NormFeat type.
59 StartParamDesc (CharNormParams)
60 DefineParam(0, 0, -0.25, 0.75)
61 DefineParam(0, 1, 0.0, 1.0)
62 DefineParam(0, 0, 0.0, 1.0)
63 DefineParam(0, 0, 0.0, 1.0)
65 // Now define the feature type itself (see features.h for parameters).
66 DefineFeature(CharNormDesc, 4, 0, kCNFeatureType, CharNormParams)
67 
68 // Define all of the parameters for the IntFeature type
69 StartParamDesc(IntFeatParams)
70 DefineParam(0, 0, 0.0, 255.0)
71 DefineParam(0, 0, 0.0, 255.0)
72 DefineParam(1, 0, 0.0, 255.0)
74 // Now define the feature type itself (see features.h for parameters).
75 DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams)
76 
77 // Define all of the parameters for the GeoFeature type
78 StartParamDesc(GeoFeatParams)
79 DefineParam(0, 0, 0.0, 255.0)
80 DefineParam(0, 0, 0.0, 255.0)
81 DefineParam(0, 0, 0.0, 255.0)
83 // Now define the feature type itself (see features.h for parameters).
84 DefineFeature(GeoFeatDesc, 3, 0, kGeoFeatureType, GeoFeatParams)
85 
86 // Other features used for training the adaptive classifier, but not used
87 // during normal training, therefore not in the DescDefs array.
88 
89 // Define all of the parameters for the PicoFeature type
90 // define knob that can be used to adjust pico-feature length.
92 StartParamDesc(PicoFeatParams)
93 DefineParam(0, 0, -0.25, 0.75)
94 DefineParam(1, 0, 0.0, 1.0)
95 DefineParam(0, 0, -0.5, 0.5)
97 // Now define the feature type itself (see features.h for parameters).
98 DefineFeature(PicoFeatDesc, 2, 1, "pf", PicoFeatParams)
99 
100 // Define all of the parameters for the OutlineFeature type.
101 StartParamDesc(OutlineFeatParams)
102 DefineParam(0, 0, -0.5, 0.5)
103 DefineParam(0, 0, -0.25, 0.75)
104 DefineParam(0, 0, 0.0, 1.0)
105 DefineParam(1, 0, 0.0, 1.0)
107 // Now define the feature type itself (see features.h for parameters).
108 DefineFeature(OutlineFeatDesc, 3, 1, "of", OutlineFeatParams)
109 
110 // MUST be kept in-sync with ExtractorDefs in fxdefs.cpp.
111 static const FEATURE_DESC_STRUCT *DescDefs[NUM_FEATURE_TYPES] = {
113  &CharNormDesc,
114  &IntFeatDesc,
115  &GeoFeatDesc
116 };
117 
118 /*-----------------------------------------------------------------------------
119  Public Code
120 -----------------------------------------------------------------------------*/
122  featuredefs->NumFeatureTypes = NUM_FEATURE_TYPES;
123  for (int i = 0; i < NUM_FEATURE_TYPES; ++i) {
124  featuredefs->FeatureDesc[i] = DescDefs[i];
125  }
126 }
127 
128 /*---------------------------------------------------------------------------*/
142  int i;
143 
144  if (CharDesc) {
145  for (i = 0; i < CharDesc->NumFeatureSets; i++)
146  FreeFeatureSet (CharDesc->FeatureSets[i]);
147  Efree(CharDesc);
148  }
149 } /* FreeCharDescription */
150 
151 
152 /*---------------------------------------------------------------------------*/
165  CHAR_DESC CharDesc;
166  int i;
167 
168  CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT));
169  CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
170 
171  for (i = 0; i < CharDesc->NumFeatureSets; i++)
172  CharDesc->FeatureSets[i] = NULL;
173 
174  return (CharDesc);
175 
176 } /* NewCharDescription */
177 
178 
179 /*---------------------------------------------------------------------------*/
198  CHAR_DESC CharDesc, STRING* str) {
199  int Type;
200  int NumSetsToWrite = 0;
201 
202  for (Type = 0; Type < CharDesc->NumFeatureSets; Type++)
203  if (CharDesc->FeatureSets[Type])
204  NumSetsToWrite++;
205 
206  str->add_str_int(" ", NumSetsToWrite);
207  *str += "\n";
208  for (Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
209  if (CharDesc->FeatureSets[Type]) {
210  *str += FeatureDefs.FeatureDesc[Type]->ShortName;
211  *str += " ";
212  WriteFeatureSet(CharDesc->FeatureSets[Type], str);
213  }
214  }
215 } /* WriteCharDescription */
216 
217 // Return whether all of the fields of the given feature set
218 // are well defined (not inf or nan).
220  CHAR_DESC CharDesc) {
221  bool anything_written = false;
222  bool well_formed = true;
223  for (int Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
224  if (CharDesc->FeatureSets[Type]) {
225  for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
226  FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];
227  for (int p = 0; p < feat->Type->NumParams; p++) {
228  if (isnan(feat->Params[p]) || isinf(feat->Params[p]))
229  well_formed = false;
230  else
231  anything_written = true;
232  }
233  }
234  } else {
235  return false;
236  }
237  }
238  return anything_written && well_formed;
239 } /* ValidCharDescription */
240 
241 /*---------------------------------------------------------------------------*/
264  FILE *File) {
265  int NumSetsToRead;
266  char ShortName[FEAT_NAME_SIZE];
267  CHAR_DESC CharDesc;
268  int Type;
269 
270  if (tfscanf(File, "%d", &NumSetsToRead) != 1 ||
271  NumSetsToRead < 0 || NumSetsToRead > FeatureDefs.NumFeatureTypes)
272  DoError (ILLEGAL_NUM_SETS, "Illegal number of feature sets");
273 
274  CharDesc = NewCharDescription(FeatureDefs);
275  for (; NumSetsToRead > 0; NumSetsToRead--) {
276  tfscanf(File, "%s", ShortName);
277  Type = ShortNameToFeatureType(FeatureDefs, ShortName);
278  CharDesc->FeatureSets[Type] =
279  ReadFeatureSet (File, FeatureDefs.FeatureDesc[Type]);
280  }
281  return (CharDesc);
282 
283 } // ReadCharDescription
284 
285 
286 /*---------------------------------------------------------------------------*/
303  const char *ShortName) {
304  int i;
305 
306  for (i = 0; i < FeatureDefs.NumFeatureTypes; i++)
307  if (!strcmp ((FeatureDefs.FeatureDesc[i]->ShortName), ShortName))
308  return (i);
309  DoError (ILLEGAL_SHORT_NAME, "Illegal short name for a feature");
310  return 0;
311 
312 } // ShortNameToFeatureType
#define isnan(x)
Definition: mathfix.h:31
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
Definition: featdefs.cpp:263
#define isinf(x)
Definition: mathfix.h:32
#define EndParamDesc
Definition: ocrfeatures.h:92
float FLOAT32
Definition: host.h:111
#define ILLEGAL_NUM_SETS
Definition: featdefs.cpp:34
void FreeCharDescription(CHAR_DESC CharDesc)
Definition: featdefs.cpp:141
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:121
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
const char * kIntFeatureType
Definition: featdefs.cpp:43
#define PICO_FEATURE_LENGTH
Definition: featdefs.cpp:36
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:50
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:44
#define FEAT_NAME_SIZE
Definition: ocrfeatures.h:33
const FEATURE_DESC_STRUCT PicoFeatDesc
uinT32 NumFeatureSets
Definition: featdefs.h:43
StartParamDesc(MicroFeatureParams) DefineParam(0
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
Definition: featdefs.cpp:197
FEATURE Features[1]
Definition: ocrfeatures.h:72
EndParamDesc of
Definition: featdefs.cpp:108
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
const char * kCNFeatureType
Definition: featdefs.cpp:42
#define ILLEGAL_SHORT_NAME
Definition: featdefs.h:34
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
DefineParam(0, 0,-0.25, 0.75) DefineParam(0
MicroFeatureParams CharNormParams EndParamDesc DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams) StartParamDesc(GeoFeatParams) DefineParam(0
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
Definition: featdefs.cpp:219
const FEATURE_DESC_STRUCT CharNormDesc
const char * ShortName
Definition: ocrfeatures.h:58
void * Emalloc(int Size)
Definition: emalloc.cpp:47
const char * kGeoFeatureType
Definition: featdefs.cpp:44
const char * kMicroFeatureType
Definition: featdefs.cpp:41
#define NUM_FEATURE_TYPES
Definition: featdefs.h:27
const FEATURE_DESC_STRUCT IntFeatDesc
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
void add_str_int(const char *str, int number)
Definition: strngs.cpp:376
FLOAT32 PicoFeatureLength
void Efree(void *ptr)
Definition: emalloc.cpp:79
int ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:302
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
Definition: strngs.h:44
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:164
const FEATURE_DESC_STRUCT GeoFeatDesc
#define NULL
Definition: host.h:144
const FEATURE_DESC_STRUCT OutlineFeatDesc
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:78
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
CHAR_DESC_STRUCT * CHAR_DESC
Definition: featdefs.h:46
const FEATURE_DESC_STRUCT MicroFeatureDesc
inT32 NumFeatureTypes
Definition: featdefs.h:49