Mercurial > hgrepos > Python2 > PyMuPDF
comparison mupdf-source/thirdparty/tesseract/src/ccutil/params.h @ 2:b50eed0cc0ef upstream
ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4.
The directory name has changed: no version number in the expanded directory now.
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Mon, 15 Sep 2025 11:43:07 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:1d09e1dec1d9 | 2:b50eed0cc0ef |
|---|---|
| 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 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. | |
| 113 class Param { | |
| 114 public: | |
| 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 | |
| 136 protected: | |
| 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 | |
| 148 class IntParam : public Param { | |
| 149 public: | |
| 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 } | |
| 157 ~IntParam() { | |
| 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 } | |
| 169 void ResetToDefault() { | |
| 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 | |
| 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 : Param(name, comment, init) { | |
| 194 value_ = value; | |
| 195 default_ = value; | |
| 196 params_vec_ = &(vec->bool_params); | |
| 197 vec->bool_params.push_back(this); | |
| 198 } | |
| 199 ~BoolParam() { | |
| 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 } | |
| 211 void ResetToDefault() { | |
| 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 | |
| 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 : Param(name, comment, init) { | |
| 237 value_ = value; | |
| 238 default_ = value; | |
| 239 params_vec_ = &(vec->string_params); | |
| 240 vec->string_params.push_back(this); | |
| 241 } | |
| 242 ~StringParam() { | |
| 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 } | |
| 266 void ResetToDefault() { | |
| 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 | |
| 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 : Param(name, comment, init) { | |
| 291 value_ = value; | |
| 292 default_ = value; | |
| 293 params_vec_ = &(vec->double_params); | |
| 294 vec->double_params.push_back(this); | |
| 295 } | |
| 296 ~DoubleParam() { | |
| 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 } | |
| 308 void ResetToDefault() { | |
| 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 | |
| 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 #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 |
