tesseract v5.3.3.20231005
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#include <sstream> // for std::stringstream
25
26namespace tesseract {
27
28/*----------------------------------------------------------------------------
29 Public Code
30----------------------------------------------------------------------------*/
40bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
41 if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
42 delete Feature;
43 return false;
44 }
45
46 FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
47 return true;
48} /* AddFeature */
49
61static FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT *FeatureDesc) {
62 auto Feature = new FEATURE_STRUCT(FeatureDesc);
63 for (int i = 0; i < Feature->Type->NumParams; i++) {
64 ASSERT_HOST(tfscanf(File, "%f", &(Feature->Params[i])) == 1);
65#ifndef _WIN32
66 assert(!std::isnan(Feature->Params[i]));
67#endif
68 }
69 return Feature;
70}
71
83 int NumFeatures;
84 ASSERT_HOST(tfscanf(File, "%d", &NumFeatures) == 1);
85 ASSERT_HOST(NumFeatures >= 0);
86
87 auto FeatureSet = new FEATURE_SET_STRUCT(NumFeatures);
88 for (int i = 0; i < NumFeatures; i++) {
89 AddFeature(FeatureSet, ReadFeature(File, FeatureDesc));
90 }
91
92 return FeatureSet;
93}
94
105static void WriteFeature(FEATURE Feature, std::string &str) {
106 for (int i = 0; i < Feature->Type->NumParams; i++) {
107#ifndef WIN32
108 assert(!std::isnan(Feature->Params[i]));
109#endif
110 std::stringstream stream;
111 // Use "C" locale (needed for double value).
112 stream.imbue(std::locale::classic());
113 // Use 8 digits for double value.
114 stream.precision(8);
115 stream << Feature->Params[i];
116 str += " " + stream.str();
117 }
118 str += "\n";
119} /* WriteFeature */
120
129void WriteFeatureSet(FEATURE_SET FeatureSet, std::string &str) {
130 if (FeatureSet) {
131 str += "" + std::to_string(FeatureSet->NumFeatures);
132 str += "\n";
133 for (int i = 0; i < FeatureSet->NumFeatures; i++) {
134 WriteFeature(FeatureSet->Features[i], str);
135 }
136 }
137} /* WriteFeatureSet */
138
139} // 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:82
bool AddFeature(FEATURE_SET FeatureSet, FEATURE Feature)
Definition: ocrfeatures.cpp:40
void WriteFeatureSet(FEATURE_SET FeatureSet, std::string &str)
std::vector< FEATURE_STRUCT * > Features
Definition: ocrfeatures.h:85