tesseract 5.2.0
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 "ocrfeatures.h"
19
20#include "scanutils.h"
21
22#include <cassert>
23#include <cmath>
24
25namespace tesseract {
26
27/*----------------------------------------------------------------------------
28 Public Code
29----------------------------------------------------------------------------*/
39bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
40 if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
41 delete Feature;
42 return false;
43 }
44
45 FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
46 return true;
47} /* AddFeature */
48
60static FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc) {
61 auto Feature = new FEATURE_STRUCT(FeatureDesc);
62 for (int i = 0; i < Feature->Type->NumParams; i++) {
63 ASSERT_HOST(tfscanf(File, "%f", &(Feature->Params[i])) == 1);
64#ifndef _WIN32
65 assert(!std::isnan(Feature->Params[i]));
66#endif
67 }
68 return Feature;
69}
70
82 int NumFeatures;
83 ASSERT_HOST(tfscanf(File, "%d", &NumFeatures) == 1);
84 ASSERT_HOST(NumFeatures >= 0);
85
86 auto FeatureSet = new FEATURE_SET_STRUCT(NumFeatures);
87 for (int i = 0; i < NumFeatures; i++) {
88 AddFeature(FeatureSet, ReadFeature(File, FeatureDesc));
89 }
90
91 return FeatureSet;
92}
93
104static void WriteFeature(FEATURE Feature, std::string &str) {
105 for (int i = 0; i < Feature->Type->NumParams; i++) {
106#ifndef WIN32
107 assert(!std::isnan(Feature->Params[i]));
108#endif
109 str += " " + std::to_string(Feature->Params[i]);
110 }
111 str += "\n";
112} /* WriteFeature */
113
122void WriteFeatureSet(FEATURE_SET FeatureSet, std::string &str) {
123 if (FeatureSet) {
124 str += "" + std::to_string(FeatureSet->NumFeatures);
125 str += "\n";
126 for (int i = 0; i < FeatureSet->NumFeatures; i++) {
127 WriteFeature(FeatureSet->Features[i], str);
128 }
129 }
130} /* WriteFeatureSet */
131
132} // namespace tesseract
int tfscanf(FILE *stream, const char *format,...)
Definition: scanutils.cpp:189
#define ASSERT_HOST(x)
Definition: errcode.h:54
FEATURE_STRUCT * FEATURE
Definition: ocrfeatures.h:68
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc)
Definition: ocrfeatures.cpp:81
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:39
void WriteFeatureSet(FEATURE_SET FeatureSet, std::string &str)
std::vector< FEATURE_STRUCT * > Features
Definition: ocrfeatures.h:85