Coverage Report

Created: 2025-06-13 07:15

/src/tesseract/src/ccutil/params.h
Line
Count
Source (jump to first uncovered line)
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
30
namespace tesseract {
31
32
class IntParam;
33
class BoolParam;
34
class StringParam;
35
class DoubleParam;
36
class TFile;
37
38
// Enum for constraints on what kind of params should be set by SetParam().
39
enum SetParamConstraint {
40
  SET_PARAM_CONSTRAINT_NONE,
41
  SET_PARAM_CONSTRAINT_DEBUG_ONLY,
42
  SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY,
43
  SET_PARAM_CONSTRAINT_NON_INIT_ONLY,
44
};
45
46
struct ParamsVectors {
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.
54
class TESS_API ParamUtils {
55
public:
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
1.34M
                      const std::vector<T *> &member_vec) {
78
47.0M
    for (auto *param : global_vec) {
79
47.0M
      if (strcmp(param->name_str(), name) == 0) {
80
4
        return param;
81
4
      }
82
47.0M
    }
83
67.9M
    for (auto *param : member_vec) {
84
67.9M
      if (strcmp(param->name_str(), name) == 0) {
85
1.34M
        return param;
86
1.34M
      }
87
67.9M
    }
88
12
    return nullptr;
89
1.34M
  }
tesseract::IntParam* tesseract::ParamUtils::FindParam<tesseract::IntParam>(char const*, std::__1::vector<tesseract::IntParam*, std::__1::allocator<tesseract::IntParam*> > const&, std::__1::vector<tesseract::IntParam*, std::__1::allocator<tesseract::IntParam*> > const&)
Line
Count
Source
77
1.30M
                      const std::vector<T *> &member_vec) {
78
44.2M
    for (auto *param : global_vec) {
79
44.2M
      if (strcmp(param->name_str(), name) == 0) {
80
0
        return param;
81
0
      }
82
44.2M
    }
83
63.1M
    for (auto *param : member_vec) {
84
63.1M
      if (strcmp(param->name_str(), name) == 0) {
85
1.30M
        return param;
86
1.30M
      }
87
63.1M
    }
88
4
    return nullptr;
89
1.30M
  }
tesseract::BoolParam* tesseract::ParamUtils::FindParam<tesseract::BoolParam>(char const*, std::__1::vector<tesseract::BoolParam*, std::__1::allocator<tesseract::BoolParam*> > const&, std::__1::vector<tesseract::BoolParam*, std::__1::allocator<tesseract::BoolParam*> > const&)
Line
Count
Source
77
43.3k
                      const std::vector<T *> &member_vec) {
78
2.77M
    for (auto *param : global_vec) {
79
2.77M
      if (strcmp(param->name_str(), name) == 0) {
80
0
        return param;
81
0
      }
82
2.77M
    }
83
4.83M
    for (auto *param : member_vec) {
84
4.83M
      if (strcmp(param->name_str(), name) == 0) {
85
43.3k
        return param;
86
43.3k
      }
87
4.83M
    }
88
4
    return nullptr;
89
43.3k
  }
tesseract::StringParam* tesseract::ParamUtils::FindParam<tesseract::StringParam>(char const*, std::__1::vector<tesseract::StringParam*, std::__1::allocator<tesseract::StringParam*> > const&, std::__1::vector<tesseract::StringParam*, std::__1::allocator<tesseract::StringParam*> > const&)
Line
Count
Source
77
4
                      const std::vector<T *> &member_vec) {
78
12
    for (auto *param : global_vec) {
79
12
      if (strcmp(param->name_str(), name) == 0) {
80
4
        return param;
81
4
      }
82
12
    }
83
0
    for (auto *param : member_vec) {
84
0
      if (strcmp(param->name_str(), name) == 0) {
85
0
        return param;
86
0
      }
87
0
    }
88
0
    return nullptr;
89
0
  }
tesseract::DoubleParam* tesseract::ParamUtils::FindParam<tesseract::DoubleParam>(char const*, std::__1::vector<tesseract::DoubleParam*, std::__1::allocator<tesseract::DoubleParam*> > const&, std::__1::vector<tesseract::DoubleParam*, std::__1::allocator<tesseract::DoubleParam*> > const&)
Line
Count
Source
77
4
                      const std::vector<T *> &member_vec) {
78
300
    for (auto *param : global_vec) {
79
300
      if (strcmp(param->name_str(), name) == 0) {
80
0
        return param;
81
0
      }
82
300
    }
83
592
    for (auto *param : member_vec) {
84
592
      if (strcmp(param->name_str(), name) == 0) {
85
0
        return param;
86
0
      }
87
592
    }
88
4
    return nullptr;
89
4
  }
90
  // Removes the given pointer to the param from the given vector.
91
  template <class T>
92
0
  static void RemoveParam(T *param_ptr, std::vector<T *> *vec) {
93
0
    for (auto it = vec->begin(); it != vec->end(); ++it) {
94
0
      if (*it == param_ptr) {
95
0
        vec->erase(it);
96
0
        break;
97
0
      }
98
0
    }
99
0
  }
Unexecuted instantiation: void tesseract::ParamUtils::RemoveParam<tesseract::BoolParam>(tesseract::BoolParam*, std::__1::vector<tesseract::BoolParam*, std::__1::allocator<tesseract::BoolParam*> >*)
Unexecuted instantiation: void tesseract::ParamUtils::RemoveParam<tesseract::StringParam>(tesseract::StringParam*, std::__1::vector<tesseract::StringParam*, std::__1::allocator<tesseract::StringParam*> >*)
Unexecuted instantiation: void tesseract::ParamUtils::RemoveParam<tesseract::IntParam>(tesseract::IntParam*, std::__1::vector<tesseract::IntParam*, std::__1::allocator<tesseract::IntParam*> >*)
Unexecuted instantiation: void tesseract::ParamUtils::RemoveParam<tesseract::DoubleParam>(tesseract::DoubleParam*, std::__1::vector<tesseract::DoubleParam*, std::__1::allocator<tesseract::DoubleParam*> >*)
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.
113
class Param {
114
public:
115
  ~Param() = default;
116
117
114M
  const char *name_str() const {
118
114M
    return name_;
119
114M
  }
120
0
  const char *info_str() const {
121
0
    return info_;
122
0
  }
123
4
  bool is_init() const {
124
4
    return init_;
125
4
  }
126
0
  bool is_debug() const {
127
0
    return debug_;
128
0
  }
129
4
  bool constraint_ok(SetParamConstraint constraint) const {
130
4
    return (constraint == SET_PARAM_CONSTRAINT_NONE ||
131
4
            (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY && this->is_debug()) ||
132
4
            (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY && !this->is_debug()) ||
133
4
            (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY && !this->is_init()));
134
4
  }
135
136
protected:
137
  Param(const char *name, const char *comment, bool init)
138
2.58k
      : name_(name), info_(comment), init_(init) {
139
2.58k
    debug_ = (strstr(name, "debug") != nullptr) || (strstr(name, "display"));
140
2.58k
  }
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
148
class IntParam : public Param {
149
public:
150
  IntParam(int32_t value, const char *name, const char *comment, bool init, ParamsVectors *vec)
151
552
      : Param(name, comment, init) {
152
552
    value_ = value;
153
552
    default_ = value;
154
552
    params_vec_ = &(vec->int_params);
155
552
    vec->int_params.push_back(this);
156
552
  }
157
0
  ~IntParam() {
158
0
    ParamUtils::RemoveParam<IntParam>(this, params_vec_);
159
0
  }
160
2.51G
  operator int32_t() const {
161
2.51G
    return value_;
162
2.51G
  }
163
0
  void operator=(int32_t value) {
164
0
    value_ = value;
165
0
  }
166
51.0k
  void set_value(int32_t value) {
167
51.0k
    value_ = value;
168
51.0k
  }
169
0
  void ResetToDefault() {
170
0
    value_ = default_;
171
0
  }
172
0
  void ResetFrom(const ParamsVectors *vec) {
173
0
    for (auto *param : vec->int_params) {
174
0
      if (strcmp(param->name_str(), name_) == 0) {
175
0
        // printf("overriding param %s=%d by =%d\n", name_, value_,
176
0
        // param);
177
0
        value_ = *param;
178
0
        break;
179
0
      }
180
0
    }
181
0
  }
182
183
private:
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
190
class BoolParam : public Param {
191
public:
192
  BoolParam(bool value, const char *name, const char *comment, bool init, ParamsVectors *vec)
193
944
      : Param(name, comment, init) {
194
944
    value_ = value;
195
944
    default_ = value;
196
944
    params_vec_ = &(vec->bool_params);
197
944
    vec->bool_params.push_back(this);
198
944
  }
199
0
  ~BoolParam() {
200
0
    ParamUtils::RemoveParam<BoolParam>(this, params_vec_);
201
0
  }
202
363M
  operator bool() const {
203
363M
    return value_;
204
363M
  }
205
0
  void operator=(bool value) {
206
0
    value_ = value;
207
0
  }
208
12.4k
  void set_value(bool value) {
209
12.4k
    value_ = value;
210
12.4k
  }
211
0
  void ResetToDefault() {
212
0
    value_ = default_;
213
0
  }
214
0
  void ResetFrom(const ParamsVectors *vec) {
215
0
    for (auto *param : vec->bool_params) {
216
0
      if (strcmp(param->name_str(), name_) == 0) {
217
0
        // printf("overriding param %s=%s by =%s\n", name_, value_ ? "true" :
218
0
        // "false", *param ? "true" : "false");
219
0
        value_ = *param;
220
0
        break;
221
0
      }
222
0
    }
223
0
  }
224
225
private:
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
232
class StringParam : public Param {
233
public:
234
  StringParam(const char *value, const char *name, const char *comment, bool init,
235
              ParamsVectors *vec)
236
136
      : Param(name, comment, init) {
237
136
    value_ = value;
238
136
    default_ = value;
239
136
    params_vec_ = &(vec->string_params);
240
136
    vec->string_params.push_back(this);
241
136
  }
242
0
  ~StringParam() {
243
0
    ParamUtils::RemoveParam<StringParam>(this, params_vec_);
244
0
  }
245
20
  operator std::string &() {
246
20
    return value_;
247
20
  }
248
228k
  const char *c_str() const {
249
228k
    return value_.c_str();
250
228k
  }
251
2
  bool contains(char c) const {
252
2
    return value_.find(c) != std::string::npos;
253
2
  }
254
4.55k
  bool empty() const {
255
4.55k
    return value_.empty();
256
4.55k
  }
257
0
  bool operator==(const std::string &other) const {
258
0
    return value_ == other;
259
0
  }
260
0
  void operator=(const std::string &value) {
261
0
    value_ = value;
262
0
  }
263
4
  void set_value(const std::string &value) {
264
4
    value_ = value;
265
4
  }
266
0
  void ResetToDefault() {
267
0
    value_ = default_;
268
0
  }
269
16
  void ResetFrom(const ParamsVectors *vec) {
270
56
    for (auto *param : vec->string_params) {
271
56
      if (strcmp(param->name_str(), name_) == 0) {
272
        // printf("overriding param %s=%s by =%s\n", name_, value_,
273
        // param->c_str());
274
16
        value_ = *param;
275
16
        break;
276
16
      }
277
56
    }
278
16
  }
279
280
private:
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
287
class DoubleParam : public Param {
288
public:
289
  DoubleParam(double value, const char *name, const char *comment, bool init, ParamsVectors *vec)
290
948
      : Param(name, comment, init) {
291
948
    value_ = value;
292
948
    default_ = value;
293
948
    params_vec_ = &(vec->double_params);
294
948
    vec->double_params.push_back(this);
295
948
  }
296
0
  ~DoubleParam() {
297
0
    ParamUtils::RemoveParam<DoubleParam>(this, params_vec_);
298
0
  }
299
3.48G
  operator double() const {
300
3.48G
    return value_;
301
3.48G
  }
302
0
  void operator=(double value) {
303
0
    value_ = value;
304
0
  }
305
113k
  void set_value(double value) {
306
113k
    value_ = value;
307
113k
  }
308
0
  void ResetToDefault() {
309
0
    value_ = default_;
310
0
  }
311
0
  void ResetFrom(const ParamsVectors *vec) {
312
0
    for (auto *param : vec->double_params) {
313
0
      if (strcmp(param->name_str(), name_) == 0) {
314
0
        // printf("overriding param %s=%f by =%f\n", name_, value_,
315
0
        // *param);
316
0
        value_ = *param;
317
0
        break;
318
0
      }
319
0
    }
320
0
  }
321
322
private:
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.
338
TESS_API
339
ParamsVectors *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
404
#define INT_MEMBER(name, val, comment, vec) name(val, #name, comment, false, vec)
370
371
628
#define BOOL_MEMBER(name, val, comment, vec) name(val, #name, comment, false, vec)
372
373
104
#define STRING_MEMBER(name, val, comment, vec) name(val, #name, comment, false, vec)
374
375
648
#define double_MEMBER(name, val, comment, vec) name(val, #name, comment, false, vec)
376
377
12
#define INT_INIT_MEMBER(name, val, comment, vec) name(val, #name, comment, true, vec)
378
379
60
#define BOOL_INIT_MEMBER(name, val, comment, vec) name(val, #name, comment, true, vec)
380
381
16
#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