tesseract  4.0.0-1-g2a2b
ocrfeatures.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  ** Filename: ocrfeatures.cpp
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 "scanutils.h"
25 
26 #include <cassert>
27 #include <cmath>
28 
29 /*----------------------------------------------------------------------------
30  Public Code
31 ----------------------------------------------------------------------------*/
41 bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
42  if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
43  FreeFeature(Feature);
44  return false;
45  }
46 
47  FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
48  return true;
49 } /* AddFeature */
50 
56 void FreeFeature(FEATURE Feature) { free(Feature); } /* FreeFeature */
57 
65 void FreeFeatureSet(FEATURE_SET FeatureSet) {
66  int i;
67 
68  if (FeatureSet) {
69  for (i = 0; i < FeatureSet->NumFeatures; i++)
70  FreeFeature(FeatureSet->Features[i]);
71  free(FeatureSet);
72  }
73 } /* FreeFeatureSet */
74 
81 FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
82  FEATURE Feature;
83 
84  Feature = (FEATURE)malloc(sizeof(FEATURE_STRUCT) +
85  (FeatureDesc->NumParams - 1) * sizeof(float));
86  Feature->Type = FeatureDesc;
87  return (Feature);
88 
89 } /* NewFeature */
90 
97 FEATURE_SET NewFeatureSet(int NumFeatures) {
98  FEATURE_SET FeatureSet;
99 
100  FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
101  (NumFeatures - 1) * sizeof (FEATURE));
102  FeatureSet->MaxNumFeatures = NumFeatures;
103  FeatureSet->NumFeatures = 0;
104  return (FeatureSet);
105 
106 } /* NewFeatureSet */
107 
119 FEATURE ReadFeature(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
120  FEATURE Feature;
121  int i;
122 
123  Feature = NewFeature (FeatureDesc);
124  for (i = 0; i < Feature->Type->NumParams; i++) {
125  ASSERT_HOST(tfscanf(File, "%f", &(Feature->Params[i])) == 1);
126 #ifndef _WIN32
127  assert (!std::isnan(Feature->Params[i]));
128 #endif
129  }
130  return Feature;
131 }
132 
143 FEATURE_SET ReadFeatureSet(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
144  int NumFeatures;
145  ASSERT_HOST(tfscanf(File, "%d", &NumFeatures) == 1);
146  ASSERT_HOST(NumFeatures >= 0);
147 
148  FEATURE_SET FeatureSet = NewFeatureSet(NumFeatures);
149  for (int i = 0; i < NumFeatures; i++)
150  AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));
151 
152  return FeatureSet;
153 }
154 
166 void WriteFeature(FEATURE Feature, STRING* str) {
167  for (int i = 0; i < Feature->Type->NumParams; i++) {
168 #ifndef WIN32
169  assert(!std::isnan(Feature->Params[i]));
170 #endif
171  str->add_str_double(" ", Feature->Params[i]);
172  }
173  *str += "\n";
174 } /* WriteFeature */
175 
185 void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
186  if (FeatureSet) {
187  str->add_str_int("", FeatureSet->NumFeatures);
188  *str += "\n";
189  for (int i = 0; i < FeatureSet->NumFeatures; i++) {
190  WriteFeature(FeatureSet->Features[i], str);
191  }
192  }
193 } /* WriteFeatureSet */
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:192
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:97
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:56
void * Emalloc(int Size)
Definition: emalloc.cpp:31
void WriteFeature(FEATURE Feature, STRING *str)
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
float Params[1]
Definition: ocrfeatures.h:62
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
uint16_t MaxNumFeatures
Definition: ocrfeatures.h:68
FEATURE Features[1]
Definition: ocrfeatures.h:69
uint16_t NumFeatures
Definition: ocrfeatures.h:67
void add_str_double(const char *str, double number)
Definition: strngs.cpp:389
FEATURE_STRUCT * FEATURE
Definition: ocrfeatures.h:64
FEATURE_SET_STRUCT * FEATURE_SET
Definition: ocrfeatures.h:71
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:81
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:65
Definition: strngs.h:45
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:41
#define ASSERT_HOST(x)
Definition: errcode.h:84
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:61