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 #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  if (CharDesc) {
143  for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
144  FreeFeatureSet (CharDesc->FeatureSets[i]);
145  Efree(CharDesc);
146  }
147 } /* FreeCharDescription */
148 
149 
150 /*---------------------------------------------------------------------------*/
163  CHAR_DESC CharDesc;
164  CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT));
165  CharDesc->NumFeatureSets = FeatureDefs.NumFeatureTypes;
166 
167  for (size_t i = 0; i < CharDesc->NumFeatureSets; i++)
168  CharDesc->FeatureSets[i] = NULL;
169 
170  return (CharDesc);
171 
172 } /* NewCharDescription */
173 
174 
175 /*---------------------------------------------------------------------------*/
194  CHAR_DESC CharDesc, STRING* str) {
195  int NumSetsToWrite = 0;
196 
197  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++)
198  if (CharDesc->FeatureSets[Type])
199  NumSetsToWrite++;
200 
201  str->add_str_int(" ", NumSetsToWrite);
202  *str += "\n";
203  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
204  if (CharDesc->FeatureSets[Type]) {
205  *str += FeatureDefs.FeatureDesc[Type]->ShortName;
206  *str += " ";
207  WriteFeatureSet(CharDesc->FeatureSets[Type], str);
208  }
209  }
210 } /* WriteCharDescription */
211 
212 // Return whether all of the fields of the given feature set
213 // are well defined (not inf or nan).
215  CHAR_DESC CharDesc) {
216  bool anything_written = false;
217  bool well_formed = true;
218  for (size_t Type = 0; Type < CharDesc->NumFeatureSets; Type++) {
219  if (CharDesc->FeatureSets[Type]) {
220  for (int i = 0; i < CharDesc->FeatureSets[Type]->NumFeatures; i++) {
221  FEATURE feat = CharDesc->FeatureSets[Type]->Features[i];
222  for (int p = 0; p < feat->Type->NumParams; p++) {
223  if (isnan(feat->Params[p]) || isinf(feat->Params[p]))
224  well_formed = false;
225  else
226  anything_written = true;
227  }
228  }
229  } else {
230  return false;
231  }
232  }
233  return anything_written && well_formed;
234 } /* ValidCharDescription */
235 
236 /*---------------------------------------------------------------------------*/
259  FILE *File) {
260  int NumSetsToRead;
261  char ShortName[FEAT_NAME_SIZE];
262  CHAR_DESC CharDesc;
263  int Type;
264 
265  if (tfscanf(File, "%d", &NumSetsToRead) != 1 ||
266  NumSetsToRead < 0 || NumSetsToRead > FeatureDefs.NumFeatureTypes)
267  DoError (ILLEGAL_NUM_SETS, "Illegal number of feature sets");
268 
269  CharDesc = NewCharDescription(FeatureDefs);
270  for (; NumSetsToRead > 0; NumSetsToRead--) {
271  tfscanf(File, "%s", ShortName);
272  Type = ShortNameToFeatureType(FeatureDefs, ShortName);
273  CharDesc->FeatureSets[Type] =
274  ReadFeatureSet (File, FeatureDefs.FeatureDesc[Type]);
275  }
276  return (CharDesc);
277 
278 } // ReadCharDescription
279 
280 
281 /*---------------------------------------------------------------------------*/
298  const char *ShortName) {
299  int i;
300 
301  for (i = 0; i < FeatureDefs.NumFeatureTypes; i++)
302  if (!strcmp ((FeatureDefs.FeatureDesc[i]->ShortName), ShortName))
303  return (i);
304  DoError (ILLEGAL_SHORT_NAME, "Illegal short name for a feature");
305  return 0;
306 
307 } // ShortNameToFeatureType
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:228
void add_str_int(const char *str, int number)
Definition: strngs.cpp:381
TESS_API FLOAT32 PicoFeatureLength
void WriteCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc, STRING *str)
Definition: featdefs.cpp:193
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
Definition: featdefs.cpp:121
FEATURE Features[1]
Definition: ocrfeatures.h:72
const FEATURE_DESC_STRUCT CharNormDesc
bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, CHAR_DESC CharDesc)
Definition: featdefs.cpp:214
EndParamDesc of
Definition: featdefs.cpp:108
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:71
const FEATURE_DESC_STRUCT IntFeatDesc
const char * kMicroFeatureType
Definition: featdefs.cpp:41
void * Emalloc(int Size)
Definition: emalloc.cpp:47
const char * ShortName
Definition: ocrfeatures.h:58
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
#define NUM_FEATURE_TYPES
Definition: featdefs.h:27
const FEATURE_DESC_STRUCT OutlineFeatDesc
#define ILLEGAL_SHORT_NAME
Definition: featdefs.h:34
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
Definition: featdefs.cpp:258
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
Definition: featdefs.h:44
const FEATURE_DESC_STRUCT MicroFeatureDesc
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs)
Definition: featdefs.cpp:162
#define isinf(x)
Definition: mathfix.h:32
DefineParam(0, 0, -0.25, 0.75) DefineParam(0
uinT32 NumFeatureSets
Definition: featdefs.h:43
#define PICO_FEATURE_LENGTH
Definition: featdefs.cpp:36
Definition: strngs.h:45
void FreeCharDescription(CHAR_DESC CharDesc)
Definition: featdefs.cpp:141
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
Definition: featdefs.h:50
#define isnan(x)
Definition: mathfix.h:31
int ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
Definition: featdefs.cpp:297
const char * kCNFeatureType
Definition: featdefs.cpp:42
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
float FLOAT32
Definition: host.h:42
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
const char * kIntFeatureType
Definition: featdefs.cpp:43
StartParamDesc(MicroFeatureParams) DefineParam(0
#define FEAT_NAME_SIZE
Definition: ocrfeatures.h:33
const FEATURE_DESC_STRUCT GeoFeatDesc
#define ILLEGAL_NUM_SETS
Definition: featdefs.cpp:34
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
void Efree(void *ptr)
Definition: emalloc.cpp:79
const char * kGeoFeatureType
Definition: featdefs.cpp:44
TESS_API const FEATURE_DESC_STRUCT PicoFeatDesc
inT32 NumFeatureTypes
Definition: featdefs.h:49
MicroFeatureParams CharNormParams EndParamDesc DefineFeature(IntFeatDesc, 2, 1, kIntFeatureType, IntFeatParams) StartParamDesc(GeoFeatParams) DefineParam(0
CHAR_DESC_STRUCT * CHAR_DESC
Definition: featdefs.h:46
#define EndParamDesc
Definition: ocrfeatures.h:92