tesseract  5.0.0-alpha-619-ge9db
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  **
6  ** (c) Copyright Hewlett-Packard Company, 1988.
7  ** Licensed under the Apache License, Version 2.0 (the "License");
8  ** you may not use this file except in compliance with the License.
9  ** You may obtain a copy of the License at
10  ** http://www.apache.org/licenses/LICENSE-2.0
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  ******************************************************************************/
17 /*----------------------------------------------------------------------------
18  Include Files and Type Defines
19 ----------------------------------------------------------------------------*/
20 #include "ocrfeatures.h"
21 #include "emalloc.h"
22 #include "callcpp.h"
23 #include "scanutils.h"
24 
25 #include <cassert>
26 #include <cmath>
27 
28 /*----------------------------------------------------------------------------
29  Public Code
30 ----------------------------------------------------------------------------*/
40 bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
41  if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
42  FreeFeature(Feature);
43  return false;
44  }
45 
46  FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
47  return true;
48 } /* AddFeature */
49 
54 void FreeFeature(FEATURE Feature) { free(Feature); } /* FreeFeature */
55 
62 void FreeFeatureSet(FEATURE_SET FeatureSet) {
63  int i;
64 
65  if (FeatureSet) {
66  for (i = 0; i < FeatureSet->NumFeatures; i++)
67  FreeFeature(FeatureSet->Features[i]);
68  free(FeatureSet);
69  }
70 } /* FreeFeatureSet */
71 
78 FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
79  FEATURE Feature;
80 
81  Feature = static_cast<FEATURE>(malloc(sizeof(FEATURE_STRUCT) +
82  (FeatureDesc->NumParams - 1) * sizeof(float)));
83  Feature->Type = FeatureDesc;
84  return (Feature);
85 
86 } /* NewFeature */
87 
94 FEATURE_SET NewFeatureSet(int NumFeatures) {
95  FEATURE_SET FeatureSet;
96 
97  FeatureSet = static_cast<FEATURE_SET>(Emalloc (sizeof (FEATURE_SET_STRUCT) +
98  (NumFeatures - 1) * sizeof (FEATURE)));
99  FeatureSet->MaxNumFeatures = NumFeatures;
100  FeatureSet->NumFeatures = 0;
101  return (FeatureSet);
102 
103 } /* NewFeatureSet */
104 
116 static FEATURE ReadFeature(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
117  FEATURE Feature;
118  int i;
119 
120  Feature = NewFeature (FeatureDesc);
121  for (i = 0; i < Feature->Type->NumParams; i++) {
122  ASSERT_HOST(tfscanf(File, "%f", &(Feature->Params[i])) == 1);
123 #ifndef _WIN32
124  assert (!std::isnan(Feature->Params[i]));
125 #endif
126  }
127  return Feature;
128 }
129 
140 FEATURE_SET ReadFeatureSet(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
141  int NumFeatures;
142  ASSERT_HOST(tfscanf(File, "%d", &NumFeatures) == 1);
143  ASSERT_HOST(NumFeatures >= 0);
144 
145  FEATURE_SET FeatureSet = NewFeatureSet(NumFeatures);
146  for (int i = 0; i < NumFeatures; i++)
147  AddFeature(FeatureSet, ReadFeature(File, FeatureDesc));
148 
149  return FeatureSet;
150 }
151 
162 static void WriteFeature(FEATURE Feature, STRING* str) {
163  for (int i = 0; i < Feature->Type->NumParams; i++) {
164 #ifndef WIN32
165  assert(!std::isnan(Feature->Params[i]));
166 #endif
167  str->add_str_double(" ", Feature->Params[i]);
168  }
169  *str += "\n";
170 } /* WriteFeature */
171 
180 void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
181  if (FeatureSet) {
182  str->add_str_int("", FeatureSet->NumFeatures);
183  *str += "\n";
184  for (int i = 0; i < FeatureSet->NumFeatures; i++) {
185  WriteFeature(FeatureSet->Features[i], str);
186  }
187  }
188 } /* WriteFeatureSet */
emalloc.h
AddFeature
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:39
STRING::add_str_int
void add_str_int(const char *str, int number)
Definition: strngs.cpp:370
tfscanf
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:181
ReadFeatureSet
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:139
Emalloc
void * Emalloc(int Size)
Definition: emalloc.cpp:31
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
STRING
Definition: strngs.h:45
FEATURE_STRUCT
Definition: ocrfeatures.h:58
ocrfeatures.h
FreeFeature
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:53
FEATURE_DESC_STRUCT
Definition: ocrfeatures.h:51
WriteFeatureSet
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
Definition: ocrfeatures.cpp:179
FEATURE_SET_STRUCT::MaxNumFeatures
uint16_t MaxNumFeatures
Definition: ocrfeatures.h:66
FEATURE_DESC_STRUCT::NumParams
uint16_t NumParams
Definition: ocrfeatures.h:52
FEATURE_SET_STRUCT::Features
FEATURE Features[1]
Definition: ocrfeatures.h:67
FEATURE_STRUCT::Params
float Params[1]
Definition: ocrfeatures.h:60
FEATURE_STRUCT::Type
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:59
callcpp.h
NewFeature
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:77
FEATURE_SET_STRUCT
Definition: ocrfeatures.h:64
NewFeatureSet
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:93
scanutils.h
STRING::add_str_double
void add_str_double(const char *str, double number)
Definition: strngs.cpp:380
FreeFeatureSet
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:61
FEATURE_SET_STRUCT::NumFeatures
uint16_t NumFeatures
Definition: ocrfeatures.h:65