tesseract v5.3.3.20231005
params.h
Go to the documentation of this file.
1/**********************************************************************
2 * File: params.h
3 * Description: Class definitions of the *_VAR classes for tunable constants.
4 * Author: Ray Smith
5 *
6 * (C) Copyright 1991, Hewlett-Packard Ltd.
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
19#ifndef PARAMS_H
20#define PARAMS_H
21
22#include <tesseract/export.h> // for TESS_API
23
24#include <cstdint>
25#include <cstdio>
26#include <cstring>
27#include <string>
28#include <vector>
29
30namespace tesseract {
31
32class IntParam;
33class BoolParam;
34class StringParam;
35class DoubleParam;
36class TFile;
37
38// Enum for constraints on what kind of params should be set by SetParam().
44};
45
47 std::vector<IntParam *> int_params;
48 std::vector<BoolParam *> bool_params;
49 std::vector<StringParam *> string_params;
50 std::vector<DoubleParam *> double_params;
51};
52
53// Utility functions for working with Tesseract parameters.
55public:
56 // Reads a file of parameter definitions and set/modify the values therein.
57 // If the filename begins with a + or -, the BoolVariables will be
58 // ORed or ANDed with any current values.
59 // Blank lines and lines beginning # are ignored.
60 // Values may have any whitespace after the name and are the rest of line.
61 static bool ReadParamsFile(const char *file, // filename to read
62 SetParamConstraint constraint, ParamsVectors *member_params);
63
64 // Read parameters from the given file pointer.
65 static bool ReadParamsFromFp(SetParamConstraint constraint, TFile *fp,
66 ParamsVectors *member_params);
67
68 // Set a parameters to have the given value.
69 static bool SetParam(const char *name, const char *value, SetParamConstraint constraint,
70 ParamsVectors *member_params);
71
72 // Returns the pointer to the parameter with the given name (of the
73 // appropriate type) if it was found in the vector obtained from
74 // GlobalParams() or in the given member_params.
75 template <class T>
76 static T *FindParam(const char *name, const std::vector<T *> &global_vec,
77 const std::vector<T *> &member_vec) {
78 for (auto *param : global_vec) {
79 if (strcmp(param->name_str(), name) == 0) {
80 return param;
81 }
82 }
83 for (auto *param : member_vec) {
84 if (strcmp(param->name_str(), name) == 0) {
85 return param;
86 }
87 }
88 return nullptr;
89 }
90 // Removes the given pointer to the param from the given vector.
91 template <class T>
92 static void RemoveParam(T *param_ptr, std::vector<T *> *vec) {
93 for (auto it = vec->begin(); it != vec->end(); ++it) {
94 if (*it == param_ptr) {
95 vec->erase(it);
96 break;
97 }
98 }
99 }
100 // Fetches the value of the named param as a string. Returns false if not
101 // found.
102 static bool GetParamAsString(const char *name, const ParamsVectors *member_params,
103 std::string *value);
104
105 // Print parameters to the given file.
106 static void PrintParams(FILE *fp, const ParamsVectors *member_params);
107
108 // Resets all parameters back to default values;
109 static void ResetToDefaults(ParamsVectors *member_params);
110};
111
112// Definition of various parameter types.
113class Param {
114public:
115 ~Param() = default;
116
117 const char *name_str() const {
118 return name_;
119 }
120 const char *info_str() const {
121 return info_;
122 }
123 bool is_init() const {
124 return init_;
125 }
126 bool is_debug() const {
127 return debug_;
128 }
129 bool constraint_ok(SetParamConstraint constraint) const {
130 return (constraint == SET_PARAM_CONSTRAINT_NONE ||
131 (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY && this->is_debug()) ||
132 (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY && !this->is_debug()) ||
133 (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY && !this->is_init()));
134 }
135
136protected:
137 Param(const char *name, const char *comment, bool init)
138 : name_(name), info_(comment), init_(init) {
139 debug_ = (strstr(name, "debug") != nullptr) || (strstr(name, "display"));
140 }
141
142 const char *name_; // name of this parameter
143 const char *info_; // for menus
144 bool init_; // needs to be set before init
145 bool debug_;
146};
147
148class IntParam : public Param {
149public:
150 IntParam(int32_t value, const char *name, const char *comment, bool init, ParamsVectors *vec)
151 : Param(name, comment, init) {
152 value_ = value;
153 default_ = value;
154 params_vec_ = &(vec->int_params);
155 vec->int_params.push_back(this);
156 }
158 ParamUtils::RemoveParam<IntParam>(this, params_vec_);
159 }
160 operator int32_t() const {
161 return value_;
162 }
163 void operator=(int32_t value) {
164 value_ = value;
165 }
166 void set_value(int32_t value) {
167 value_ = value;
168 }
170 value_ = default_;
171 }
172 void ResetFrom(const ParamsVectors *vec) {
173 for (auto *param : vec->int_params) {
174 if (strcmp(param->name_str(), name_) == 0) {
175 // printf("overriding param %s=%d by =%d\n", name_, value_,
176 // param);
177 value_ = *param;
178 break;
179 }
180 }
181 }
182
183private:
184 int32_t value_;
185 int32_t default_;
186 // Pointer to the vector that contains this param (not owned by this class).
187 std::vector<IntParam *> *params_vec_;
188};
189
190class BoolParam : public Param {
191public:
192 BoolParam(bool value, const char *name, const char *comment, bool init, ParamsVectors *vec)
193 : Param(name, comment, init) {
194 value_ = value;
195 default_ = value;
196 params_vec_ = &(vec->bool_params);
197 vec->bool_params.push_back(this);
198 }
200 ParamUtils::RemoveParam<BoolParam>(this, params_vec_);
201 }
202 operator bool() const {
203 return value_;
204 }
205 void operator=(bool value) {
206 value_ = value;
207 }
208 void set_value(bool value) {
209 value_ = value;
210 }
212 value_ = default_;
213 }
214 void ResetFrom(const ParamsVectors *vec) {
215 for (auto *param : vec->bool_params) {
216 if (strcmp(param->name_str(), name_) == 0) {
217 // printf("overriding param %s=%s by =%s\n", name_, value_ ? "true" :
218 // "false", *param ? "true" : "false");
219 value_ = *param;
220 break;
221 }
222 }
223 }
224
225private:
226 bool value_;
227 bool default_;
228 // Pointer to the vector that contains this param (not owned by this class).
229 std::vector<BoolParam *> *params_vec_;
230};
231
232class StringParam : public Param {
233public:
234 StringParam(const char *value, const char *name, const char *comment, bool init,
235 ParamsVectors *vec)
236 : Param(name, comment, init) {
237 value_ = value;
238 default_ = value;
239 params_vec_ = &(vec->string_params);
240 vec->string_params.push_back(this);
241 }
243 ParamUtils::RemoveParam<StringParam>(this, params_vec_);
244 }
245 operator std::string &() {
246 return value_;
247 }
248 const char *c_str() const {
249 return value_.c_str();
250 }
251 bool contains(char c) const {
252 return value_.find(c) != std::string::npos;
253 }
254 bool empty() const {
255 return value_.empty();
256 }
257 bool operator==(const std::string &other) const {
258 return value_ == other;
259 }
260 void operator=(const std::string &value) {
261 value_ = value;
262 }
263 void set_value(const std::string &value) {
264 value_ = value;
265 }
267 value_ = default_;
268 }
269 void ResetFrom(const ParamsVectors *vec) {
270 for (auto *param : vec->string_params) {
271 if (strcmp(param->name_str(), name_) == 0) {
272 // printf("overriding param %s=%s by =%s\n", name_, value_,
273 // param->c_str());
274 value_ = *param;
275 break;
276 }
277 }
278 }
279
280private:
281 std::string value_;
282 std::string default_;
283 // Pointer to the vector that contains this param (not owned by this class).
284 std::vector<StringParam *> *params_vec_;
285};
286
287class DoubleParam : public Param {
288public:
289 DoubleParam(double value, const char *name, const char *comment, bool init, ParamsVectors *vec)
290 : Param(name, comment, init) {
291 value_ = value;
292 default_ = value;
293 params_vec_ = &(vec->double_params);
294 vec->double_params.push_back(this);
295 }
297 ParamUtils::RemoveParam<DoubleParam>(this, params_vec_);
298 }
299 operator double() const {
300 return value_;
301 }
302 void operator=(double value) {
303 value_ = value;
304 }
305 void set_value(double value) {
306 value_ = value;
307 }
309 value_ = default_;
310 }
311 void ResetFrom(const ParamsVectors *vec) {
312 for (auto *param : vec->double_params) {
313 if (strcmp(param->name_str(), name_) == 0) {
314 // printf("overriding param %s=%f by =%f\n", name_, value_,
315 // *param);
316 value_ = *param;
317 break;
318 }
319 }
320 }
321
322private:
323 double value_;
324 double default_;
325 // Pointer to the vector that contains this param (not owned by this class).
326 std::vector<DoubleParam *> *params_vec_;
327};
328
329// Global parameter lists.
330//
331// To avoid the problem of undetermined order of static initialization
332// global_params are accessed through the GlobalParams function that
333// initializes the static pointer to global_params only on the first time
334// GlobalParams() is called.
335//
336// TODO(daria): remove GlobalParams() when all global Tesseract
337// parameters are converted to members.
339ParamsVectors *GlobalParams();
340
341/*************************************************************************
342 * Note on defining parameters.
343 *
344 * The values of the parameters defined with *_INIT_* macros are guaranteed
345 * to be loaded from config files before Tesseract initialization is done
346 * (there is no such guarantee for parameters defined with the other macros).
347 *************************************************************************/
348
349#define INT_VAR_H(name) ::tesseract::IntParam name
350
351#define BOOL_VAR_H(name) ::tesseract::BoolParam name
352
353#define STRING_VAR_H(name) ::tesseract::StringParam name
354
355#define double_VAR_H(name) ::tesseract::DoubleParam name
356
357#define INT_VAR(name, val, comment) \
358 ::tesseract::IntParam name(val, #name, comment, false, ::tesseract::GlobalParams())
359
360#define BOOL_VAR(name, val, comment) \
361 ::tesseract::BoolParam name(val, #name, comment, false, ::tesseract::GlobalParams())
362
363#define STRING_VAR(name, val, comment) \
364 ::tesseract::StringParam name(val, #name, comment, false, ::tesseract::GlobalParams())
365
366#define double_VAR(name, val, comment) \
367 ::tesseract::DoubleParam name(val, #name, comment, false, ::tesseract::GlobalParams())
368
369#define INT_MEMBER(name, val, comment, vec) name(val, #name, comment, false, vec)
370
371#define BOOL_MEMBER(name, val, comment, vec) name(val, #name, comment, false, vec)
372
373#define STRING_MEMBER(name, val, comment, vec) name(val, #name, comment, false, vec)
374
375#define double_MEMBER(name, val, comment, vec) name(val, #name, comment, false, vec)
376
377#define INT_INIT_MEMBER(name, val, comment, vec) name(val, #name, comment, true, vec)
378
379#define BOOL_INIT_MEMBER(name, val, comment, vec) name(val, #name, comment, true, vec)
380
381#define STRING_INIT_MEMBER(name, val, comment, vec) name(val, #name, comment, true, vec)
382
383#define double_INIT_MEMBER(name, val, comment, vec) name(val, #name, comment, true, vec)
384
385} // namespace tesseract
386
387#endif
int value
SetParamConstraint
Definition: params.h:39
@ SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY
Definition: params.h:42
@ SET_PARAM_CONSTRAINT_NONE
Definition: params.h:40
@ SET_PARAM_CONSTRAINT_NON_INIT_ONLY
Definition: params.h:43
@ SET_PARAM_CONSTRAINT_DEBUG_ONLY
Definition: params.h:41
tesseract::ParamsVectors * GlobalParams()
Definition: params.cpp:36
std::vector< BoolParam * > bool_params
Definition: params.h:48
std::vector< StringParam * > string_params
Definition: params.h:49
std::vector< IntParam * > int_params
Definition: params.h:47
std::vector< DoubleParam * > double_params
Definition: params.h:50
static void RemoveParam(T *param_ptr, std::vector< T * > *vec)
Definition: params.h:92
static T * FindParam(const char *name, const std::vector< T * > &global_vec, const std::vector< T * > &member_vec)
Definition: params.h:76
bool is_init() const
Definition: params.h:123
const char * name_
Definition: params.h:142
const char * name_str() const
Definition: params.h:117
const char * info_
Definition: params.h:143
Param(const char *name, const char *comment, bool init)
Definition: params.h:137
bool constraint_ok(SetParamConstraint constraint) const
Definition: params.h:129
const char * info_str() const
Definition: params.h:120
bool is_debug() const
Definition: params.h:126
~Param()=default
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:172
void ResetToDefault()
Definition: params.h:169
void operator=(int32_t value)
Definition: params.h:163
IntParam(int32_t value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:150
void set_value(int32_t value)
Definition: params.h:166
void ResetToDefault()
Definition: params.h:211
void set_value(bool value)
Definition: params.h:208
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:214
void operator=(bool value)
Definition: params.h:205
BoolParam(bool value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:192
bool operator==(const std::string &other) const
Definition: params.h:257
StringParam(const char *value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:234
void set_value(const std::string &value)
Definition: params.h:263
void operator=(const std::string &value)
Definition: params.h:260
bool empty() const
Definition: params.h:254
const char * c_str() const
Definition: params.h:248
bool contains(char c) const
Definition: params.h:251
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:269
void operator=(double value)
Definition: params.h:302
void ResetFrom(const ParamsVectors *vec)
Definition: params.h:311
void set_value(double value)
Definition: params.h:305
DoubleParam(double value, const char *name, const char *comment, bool init, ParamsVectors *vec)
Definition: params.h:289
#define TESS_API
Definition: export.h:32