tesseract  3.05.02
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 
77 void FreeFeatureSet(FEATURE_SET FeatureSet) {
78  int i;
79 
80  if (FeatureSet) {
81  for (i = 0; i < FeatureSet->NumFeatures; i++)
82  FreeFeature(FeatureSet->Features[i]);
83  memfree(FeatureSet);
84  }
85 } /* FreeFeatureSet */
86 
94 FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
95  FEATURE Feature;
96 
97  Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) +
98  (FeatureDesc->NumParams - 1) *
99  sizeof (FLOAT32),
100  "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
101  Feature->Type = FeatureDesc;
102  return (Feature);
103 
104 } /* NewFeature */
105 
113 FEATURE_SET NewFeatureSet(int NumFeatures) {
114  FEATURE_SET FeatureSet;
115 
116  FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
117  (NumFeatures - 1) * sizeof (FEATURE));
118  FeatureSet->MaxNumFeatures = NumFeatures;
119  FeatureSet->NumFeatures = 0;
120  return (FeatureSet);
121 
122 } /* NewFeatureSet */
123 
138 FEATURE ReadFeature(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
139  FEATURE Feature;
140  int i;
141 
142  Feature = NewFeature (FeatureDesc);
143  for (i = 0; i < Feature->Type->NumParams; i++) {
144  if (tfscanf(File, "%f", &(Feature->Params[i])) != 1)
145  DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
146 #ifndef _WIN32
147  assert (!isnan(Feature->Params[i]));
148 #endif
149  }
150  return (Feature);
151 } /* ReadFeature */
152 
164 FEATURE_SET ReadFeatureSet(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
165  FEATURE_SET FeatureSet;
166  int NumFeatures;
167  int i;
168 
169  if (tfscanf(File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
170  DoError(ILLEGAL_NUM_FEATURES, "Illegal number of features in set");
171 
172  FeatureSet = NewFeatureSet(NumFeatures);
173  for (i = 0; i < NumFeatures; i++)
174  AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));
175 
176  return (FeatureSet);
177 } /* ReadFeatureSet */
178 
191 void WriteFeature(FEATURE Feature, STRING* str) {
192  for (int i = 0; i < Feature->Type->NumParams; i++) {
193 #ifndef WIN32
194  assert(!isnan(Feature->Params[i]));
195 #endif
196  str->add_str_double(" ", Feature->Params[i]);
197  }
198  *str += "\n";
199 } /* WriteFeature */
200 
211 void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
212  if (FeatureSet) {
213  str->add_str_int("", FeatureSet->NumFeatures);
214  *str += "\n";
215  for (int i = 0; i < FeatureSet->NumFeatures; i++) {
216  WriteFeature(FeatureSet->Features[i], str);
217  }
218  }
219 } /* WriteFeatureSet */
220 
236 void WriteOldParamDesc(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
237  int i;
238 
239  fprintf (File, "%d\n", FeatureDesc->NumParams);
240  for (i = 0; i < FeatureDesc->NumParams; i++) {
241  if (FeatureDesc->ParamDesc[i].Circular)
242  fprintf (File, "circular ");
243  else
244  fprintf (File, "linear ");
245 
246  if (FeatureDesc->ParamDesc[i].NonEssential)
247  fprintf (File, "non-essential ");
248  else
249  fprintf (File, "essential ");
250 
251  fprintf (File, "%f %f\n",
252  FeatureDesc->ParamDesc[i].Min, FeatureDesc->ParamDesc[i].Max);
253  }
254 } /* WriteOldParamDesc */
void WriteFeature(FEATURE Feature, STRING *str)
FEATURE_STRUCT * FEATURE
Definition: ocrfeatures.h:67
#define TRUE
Definition: capi.h:45
void free_struct(void *deadstruct, inT32, const char *)
Definition: memry.cpp:43
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:60
void memfree(void *element)
Definition: freelist.cpp:30
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:228
unsigned char BOOL8
Definition: host.h:46
void add_str_double(const char *str, double number)
Definition: strngs.cpp:394
FLOAT32 Params[1]
Definition: ocrfeatures.h:65
void add_str_int(const char *str, int number)
Definition: strngs.cpp:384
FEATURE_SET_STRUCT * FEATURE_SET
Definition: ocrfeatures.h:74
#define ILLEGAL_NUM_FEATURES
Definition: ocrfeatures.h:37
const PARAM_DESC * ParamDesc
Definition: ocrfeatures.h:59
#define FALSE
Definition: capi.h:46
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:44
float FLOAT32
Definition: host.h:44
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
inT8 Circular
Definition: ocrfeatures.h:47
Definition: strngs.h:44
inT8 NonEssential
Definition: ocrfeatures.h:48
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:94
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
FLOAT32 Min
Definition: ocrfeatures.h:49
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:64
FEATURE_SET NewFeatureSet(int NumFeatures)
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
void * alloc_struct(inT32 count, const char *)
Definition: memry.cpp:39
void * Emalloc(int Size)
Definition: emalloc.cpp:47
FEATURE Features[1]
Definition: ocrfeatures.h:72
#define ILLEGAL_FEATURE_PARAM
Definition: ocrfeatures.h:36
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:77
void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
FLOAT32 Max
Definition: ocrfeatures.h:50