tesseract 4.1.1
Loading...
Searching...
No Matches
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----------------------------------------------------------------------------*/
40bool 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
54void FreeFeature(FEATURE Feature) { free(Feature); } /* FreeFeature */
55
62void 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
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
94FEATURE_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
116static 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
140FEATURE_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
162static 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
180void 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 */
#define ASSERT_HOST(x)
Definition: errcode.h:88
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:181
FEATURE NewFeature(const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:78
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING *str)
void FreeFeatureSet(FEATURE_SET FeatureSet)
Definition: ocrfeatures.cpp:62
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:40
void FreeFeature(FEATURE Feature)
Definition: ocrfeatures.cpp:54
FEATURE_SET NewFeatureSet(int NumFeatures)
Definition: ocrfeatures.cpp:94
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
void * Emalloc(int Size)
Definition: emalloc.cpp:31
Definition: strngs.h:45
void add_str_int(const char *str, int number)
Definition: strngs.cpp:377
void add_str_double(const char *str, double number)
Definition: strngs.cpp:387
const FEATURE_DESC_STRUCT * Type
Definition: ocrfeatures.h:60
float Params[1]
Definition: ocrfeatures.h:61
FEATURE Features[1]
Definition: ocrfeatures.h:68
uint16_t MaxNumFeatures
Definition: ocrfeatures.h:67
uint16_t NumFeatures
Definition: ocrfeatures.h:66