Coverage Report

Created: 2026-01-13 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tesseract/src/ccutil/params.cpp
Line
Count
Source
1
/**********************************************************************
2
 * File:        params.cpp
3
 * Description: Initialization and setting of Tesseract parameters.
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
#include "params.h"
20
21
#include "helpers.h"  // for chomp_string
22
#include "host.h"     // tesseract/export.h, windows.h for MAX_PATH
23
#include "serialis.h" // for TFile
24
#include "tprintf.h"
25
26
#include <climits> // for INT_MIN, INT_MAX
27
#include <cmath>   // for NAN, std::isnan
28
#include <cstdio>
29
#include <cstdlib>
30
#include <cstring>
31
#include <locale>  // for std::locale::classic
32
#include <sstream> // for std::stringstream
33
34
namespace tesseract {
35
36
1.33M
tesseract::ParamsVectors *GlobalParams() {
37
1.33M
  static tesseract::ParamsVectors global_params = tesseract::ParamsVectors();
38
1.33M
  return &global_params;
39
1.33M
}
40
41
bool ParamUtils::ReadParamsFile(const char *file, SetParamConstraint constraint,
42
0
                                ParamsVectors *member_params) {
43
0
  TFile fp;
44
0
  if (!fp.Open(file, nullptr)) {
45
0
    tprintf("read_params_file: Can't open %s\n", file);
46
0
    return true;
47
0
  }
48
0
  return ReadParamsFromFp(constraint, &fp, member_params);
49
0
}
50
51
bool ParamUtils::ReadParamsFromFp(SetParamConstraint constraint, TFile *fp,
52
0
                                  ParamsVectors *member_params) {
53
0
  char line[MAX_PATH]; // input line
54
0
  bool anyerr = false; // true if any error
55
0
  bool foundit;        // found parameter
56
0
  char *valptr;        // value field
57
58
0
  while (fp->FGets(line, MAX_PATH) != nullptr) {
59
0
    if (line[0] != '\r' && line[0] != '\n' && line[0] != '#') {
60
0
      chomp_string(line); // remove newline
61
0
      for (valptr = line; *valptr && *valptr != ' ' && *valptr != '\t'; valptr++) {
62
0
        ;
63
0
      }
64
0
      if (*valptr) {    // found blank
65
0
        *valptr = '\0'; // make name a string
66
0
        do {
67
0
          valptr++; // find end of blanks
68
0
        } while (*valptr == ' ' || *valptr == '\t');
69
0
      }
70
0
      foundit = SetParam(line, valptr, constraint, member_params);
71
72
0
      if (!foundit) {
73
0
        anyerr = true; // had an error
74
0
        tprintf("Warning: Parameter not found: %s\n", line);
75
0
      }
76
0
    }
77
0
  }
78
0
  return anyerr;
79
0
}
80
81
bool ParamUtils::SetParam(const char *name, const char *value, SetParamConstraint constraint,
82
4
                          ParamsVectors *member_params) {
83
  // Look for the parameter among string parameters.
84
4
  auto *sp =
85
4
      FindParam<StringParam>(name, GlobalParams()->string_params, member_params->string_params);
86
4
  if (sp != nullptr && sp->constraint_ok(constraint)) {
87
4
    sp->set_value(value);
88
4
  }
89
4
  if (*value == '\0') {
90
0
    return (sp != nullptr);
91
0
  }
92
93
  // Look for the parameter among int parameters.
94
4
  auto *ip = FindParam<IntParam>(name, GlobalParams()->int_params, member_params->int_params);
95
4
  if (ip && ip->constraint_ok(constraint)) {
96
0
    int intval = INT_MIN;
97
0
    std::stringstream stream(value);
98
0
    stream.imbue(std::locale::classic());
99
0
    stream >> intval;
100
0
    if (intval != INT_MIN) {
101
0
      ip->set_value(intval);
102
0
    }
103
0
  }
104
105
  // Look for the parameter among bool parameters.
106
4
  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params, member_params->bool_params);
107
4
  if (bp != nullptr && bp->constraint_ok(constraint)) {
108
0
    if (*value == 'T' || *value == 't' || *value == 'Y' || *value == 'y' || *value == '1') {
109
0
      bp->set_value(true);
110
0
    } else if (*value == 'F' || *value == 'f' || *value == 'N' || *value == 'n' || *value == '0') {
111
0
      bp->set_value(false);
112
0
    }
113
0
  }
114
115
  // Look for the parameter among double parameters.
116
4
  auto *dp =
117
4
      FindParam<DoubleParam>(name, GlobalParams()->double_params, member_params->double_params);
118
4
  if (dp != nullptr && dp->constraint_ok(constraint)) {
119
0
    double doubleval = NAN;
120
0
    std::stringstream stream(value);
121
0
    stream.imbue(std::locale::classic());
122
0
    stream >> doubleval;
123
0
    if (!std::isnan(doubleval)) {
124
0
      dp->set_value(doubleval);
125
0
    }
126
0
  }
127
4
  return (sp || ip || bp || dp);
128
4
}
129
130
bool ParamUtils::GetParamAsString(const char *name, const ParamsVectors *member_params,
131
0
                                  std::string *value) {
132
  // Look for the parameter among string parameters.
133
0
  auto *sp =
134
0
      FindParam<StringParam>(name, GlobalParams()->string_params, member_params->string_params);
135
0
  if (sp) {
136
0
    *value = sp->c_str();
137
0
    return true;
138
0
  }
139
  // Look for the parameter among int parameters.
140
0
  auto *ip = FindParam<IntParam>(name, GlobalParams()->int_params, member_params->int_params);
141
0
  if (ip) {
142
0
    *value = std::to_string(int32_t(*ip));
143
0
    return true;
144
0
  }
145
  // Look for the parameter among bool parameters.
146
0
  auto *bp = FindParam<BoolParam>(name, GlobalParams()->bool_params, member_params->bool_params);
147
0
  if (bp != nullptr) {
148
0
    *value = bool(*bp) ? "1" : "0";
149
0
    return true;
150
0
  }
151
  // Look for the parameter among double parameters.
152
0
  auto *dp =
153
0
      FindParam<DoubleParam>(name, GlobalParams()->double_params, member_params->double_params);
154
0
  if (dp != nullptr) {
155
0
    std::ostringstream stream;
156
0
    stream.imbue(std::locale::classic());
157
0
    stream << double(*dp);
158
0
    *value = stream.str();
159
0
    return true;
160
0
  }
161
0
  return false;
162
0
}
163
164
0
void ParamUtils::PrintParams(FILE *fp, const ParamsVectors *member_params) {
165
0
  int num_iterations = (member_params == nullptr) ? 1 : 2;
166
0
  std::ostringstream stream;
167
0
  stream.imbue(std::locale::classic());
168
0
  for (int v = 0; v < num_iterations; ++v) {
169
0
    const ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
170
0
    for (auto int_param : vec->int_params) {
171
0
      stream << int_param->name_str() << '\t' << (int32_t)(*int_param) << '\t'
172
0
             << int_param->info_str() << '\n';
173
0
    }
174
0
    for (auto bool_param : vec->bool_params) {
175
0
      stream << bool_param->name_str() << '\t' << bool(*bool_param) << '\t'
176
0
             << bool_param->info_str() << '\n';
177
0
    }
178
0
    for (auto string_param : vec->string_params) {
179
0
      stream << string_param->name_str() << '\t' << string_param->c_str() << '\t'
180
0
             << string_param->info_str() << '\n';
181
0
    }
182
0
    for (auto double_param : vec->double_params) {
183
0
      stream << double_param->name_str() << '\t' << (double)(*double_param) << '\t'
184
0
             << double_param->info_str() << '\n';
185
0
    }
186
0
  }
187
0
  fprintf(fp, "%s", stream.str().c_str());
188
0
}
189
190
// Resets all parameters back to default values;
191
0
void ParamUtils::ResetToDefaults(ParamsVectors *member_params) {
192
0
  int num_iterations = (member_params == nullptr) ? 1 : 2;
193
0
  for (int v = 0; v < num_iterations; ++v) {
194
0
    ParamsVectors *vec = (v == 0) ? GlobalParams() : member_params;
195
0
    for (auto &param : vec->int_params) {
196
0
      param->ResetToDefault();
197
0
    }
198
0
    for (auto &param : vec->bool_params) {
199
0
      param->ResetToDefault();
200
0
    }
201
0
    for (auto &param : vec->string_params) {
202
0
      param->ResetToDefault();
203
0
    }
204
0
    for (auto &param : vec->double_params) {
205
0
      param->ResetToDefault();
206
0
    }
207
0
  }
208
0
}
209
210
} // namespace tesseract