All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ocrfeatures.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: features.c
3  ** Purpose: Generic definition of a feature.
4  ** Author: Dan Johnson
5  ** History: Mon May 21 10:49:04 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 "ocrfeatures.h"
22 #include "emalloc.h"
23 #include "callcpp.h"
24 #include "danerror.h"
25 #include "freelist.h"
26 #include "scanutils.h"
27 
28 #include <assert.h>
29 #include <math.h>
30 
31 /*----------------------------------------------------------------------------
32  Public Code
33 ----------------------------------------------------------------------------*/
44 BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
45  if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
46  FreeFeature(Feature);
47  return FALSE;
48  }
49 
50  FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
51  return TRUE;
52 } /* AddFeature */
53 
60 void FreeFeature(FEATURE Feature) {
61  if (Feature) {
62  free_struct (Feature, sizeof (FEATURE_STRUCT)
63  + sizeof (FLOAT32) * (Feature->Type->NumParams - 1),
64  "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
65  }
66 
67 } /* FreeFeature */
68 
69 
78 void FreeFeatureSet(FEATURE_SET FeatureSet) {
79  int i;
80 
81  if (FeatureSet) {
82  for (i = 0; i < FeatureSet->NumFeatures; i++)
83  FreeFeature(FeatureSet->Features[i]);
84  memfree(FeatureSet);
85  }
86 } /* FreeFeatureSet */
87 
88 
96 FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
97  FEATURE Feature;
98 
99  Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) +
100  (FeatureDesc->NumParams - 1) *
101  sizeof (FLOAT32),
102  "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
103  Feature->Type = FeatureDesc;
104  return (Feature);
105 
106 } /* NewFeature */
107 
108 
116 FEATURE_SET NewFeatureSet(int NumFeatures) {
117  FEATURE_SET FeatureSet;
118 
119  FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
120  (NumFeatures - 1) * sizeof (FEATURE));
121  FeatureSet->MaxNumFeatures = NumFeatures;
122  FeatureSet->NumFeatures = 0;
123  return (FeatureSet);
124 
125 } /* NewFeatureSet */
126 
127 
141 FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
142  FEATURE Feature;
143  int i;
144 
145  Feature = NewFeature (FeatureDesc);
146  for (i = 0; i < Feature->Type->NumParams; i++) {
147  if (tfscanf(File, "%f", &(Feature->Params[i])) != 1)
148  DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
149 #ifndef _WIN32
150  assert (!isnan(Feature->Params[i]));
151 #endif
152  }
153  return (Feature);
154 } /* ReadFeature */
155 
156 
168 FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
169  FEATURE_SET FeatureSet;
170  int NumFeatures;
171  int i;
172 
173  if (tfscanf(File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
174  DoError(ILLEGAL_NUM_FEATURES, "Illegal number of features in set");
175 
176  FeatureSet = NewFeatureSet(NumFeatures);
177  for (i = 0; i < NumFeatures; i++)
178  AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));
179 
180  return (FeatureSet);
181 } /* ReadFeatureSet */
182 
183 
196 void WriteFeature(FEATURE Feature, STRING* str) {
197  for (int i = 0; i < Feature->Type->NumParams; i++) {
198 #ifndef WIN32
199  assert(!isnan(Feature->Params[i]));
200 #endif
201  str->add_str_double(" ", Feature->Params[i]);
202  }
203  *str += "\n";
204 } /* WriteFeature */
205 
206 
217 void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
218  if (FeatureSet) {
219  str->add_str_int("", FeatureSet->NumFeatures);
220  *str += "\n";
221  for (int i = 0; i < FeatureSet->NumFeatures; i++) {
222  WriteFeature(FeatureSet->Features[i], str);
223  }
224  }
225 } /* WriteFeatureSet */
226 
227 
243 void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
244  int i;
245 
246  fprintf (File, "%d\n", FeatureDesc->NumParams);
247  for (i = 0; i < FeatureDesc->NumParams; i++) {
248  if (FeatureDesc->ParamDesc[i].Circular)
249  fprintf (File, "circular ");
250  else
251  fprintf (File, "linear ");
252 
253  if (FeatureDesc->ParamDesc[i].NonEssential)
254  fprintf (File, "non-essential ");
255  else
256  fprintf (File, "essential ");
257 
258  fprintf (File, "%f %f\n",
259  FeatureDesc->ParamDesc[i].Min, FeatureDesc->ParamDesc[i].Max);
260  }
261 } /* WriteOldParamDesc */
#define isnan(x)
Definition: mathfix.h:31
void memfree(void *element)
Definition: freelist.cpp:30
FLOAT32 Min
Definition: ocrfeatures.h:49
void WriteFeature(FEATURE Feature, STRING *str)
float FLOAT32
Definition: host.h:111
void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:96
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:229
FEATURE_SET NewFeatureSet(int NumFeatures)
unsigned char BOOL8
Definition: host.h:113
FEATURE Features[1]
Definition: ocrfeatures.h:72
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
#define ILLEGAL_NUM_FEATURES
Definition: ocrfeatures.h:37
void * Emalloc(int Size)
Definition: emalloc.cpp:47
inT8 NonEssential
Definition: ocrfeatures.h:48
inT8 Circular
Definition: ocrfeatures.h:47
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
void add_str_int(const char *str, int number)
Definition: strngs.cpp:376
void * alloc_struct(inT32 count, const char *)
Definition: memry.cpp:39
FEATURE_SET_STRUCT * FEATURE_SET
Definition: ocrfeatures.h:74
#define FALSE
Definition: capi.h:29
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:44
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:60
#define TRUE
Definition: capi.h:28
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
Definition: strngs.h:44
void add_str_double(const char *str, double number)
Definition: strngs.cpp:386
#define ILLEGAL_FEATURE_PARAM
Definition: ocrfeatures.h:36
FEATURE_STRUCT * FEATURE
Definition: ocrfeatures.h:67
const PARAM_DESC * ParamDesc
Definition: ocrfeatures.h:59
void free_struct(void *deadstruct, inT32, const char *)
Definition: memry.cpp:43
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:78
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
FLOAT32 Max
Definition: ocrfeatures.h:50