Tesseract  3.02
tesseract-ocr/ccutil/params.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        params.h
00003  * Description: Class definitions of the *_VAR classes for tunable constants.
00004  * Author:      Ray Smith
00005  * Created:     Fri Feb 22 11:26:25 GMT 1991
00006  *
00007  * (C) Copyright 1991, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef           PARAMS_H
00021 #define           PARAMS_H
00022 
00023 #include          <stdio.h>
00024 
00025 #include          "genericvector.h"
00026 #include          "strngs.h"
00027 
00028 namespace tesseract {
00029 
00030 class IntParam;
00031 class BoolParam;
00032 class StringParam;
00033 class DoubleParam;
00034 
00035 // Enum for constraints on what kind of params should be set by SetParam().
00036 enum SetParamConstraint {
00037   SET_PARAM_CONSTRAINT_NONE,
00038   SET_PARAM_CONSTRAINT_DEBUG_ONLY,
00039   SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY,
00040   SET_PARAM_CONSTRAINT_NON_INIT_ONLY,
00041 };
00042 
00043 struct ParamsVectors {
00044   GenericVector<IntParam *> int_params;
00045   GenericVector<BoolParam *> bool_params;
00046   GenericVector<StringParam *> string_params;
00047   GenericVector<DoubleParam *> double_params;
00048 };
00049 
00050 // Utility functions for working with Tesseract parameters.
00051 class ParamUtils {
00052  public:
00053   // Reads a file of parameter definitions and set/modify the values therein.
00054   // If the filename begins with a + or -, the BoolVariables will be
00055   // ORed or ANDed with any current values.
00056   // Blank lines and lines beginning # are ignored.
00057   // Values may have any whitespace after the name and are the rest of line.
00058   static bool ReadParamsFile(
00059       const char *file,   // filename to read
00060       SetParamConstraint constraint,
00061       ParamsVectors *member_params);
00062 
00063   // Read parameters from the given file pointer (stop at end_offset).
00064   static bool ReadParamsFromFp(FILE *fp, inT64 end_offset,
00065                                SetParamConstraint constraint,
00066                                ParamsVectors *member_params);
00067 
00068   // Set a parameters to have the given value.
00069   static bool SetParam(const char *name, const char* value,
00070                        SetParamConstraint constraint,
00071                        ParamsVectors *member_params);
00072 
00073   // Returns the pointer to the parameter with the given name (of the
00074   // appropriate type) if it was found in the vector obtained from
00075   // GlobalParams() or in the given member_params.
00076   template<class T>
00077   static T *FindParam(const char *name,
00078                       const GenericVector<T *> &global_vec,
00079                       const GenericVector<T *> &member_vec) {
00080     int i;
00081     for (i = 0; i < global_vec.size(); ++i) {
00082       if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
00083     }
00084     for (i = 0; i < member_vec.size(); ++i) {
00085       if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
00086     }
00087     return NULL;
00088   }
00089   // Removes the given pointer to the param from the given vector.
00090   template<class T>
00091   static void RemoveParam(T *param_ptr, GenericVector<T *> *vec) {
00092     for (int i = 0; i < vec->size(); ++i) {
00093       if ((*vec)[i] == param_ptr) {
00094         vec->remove(i);
00095         return;
00096       }
00097     }
00098   }
00099   // Fetches the value of the named param as a STRING. Returns false if not
00100   // found.
00101   static bool GetParamAsString(const char *name,
00102                                const ParamsVectors* member_params,
00103                                STRING *value);
00104 
00105   // Print parameters to the given file.
00106   static void PrintParams(FILE *fp, const ParamsVectors *member_params);
00107 };
00108 
00109 // Definition of various parameter types.
00110 class Param {
00111  public:
00112   ~Param() {}
00113 
00114   const char *name_str() const { return name_; }
00115   const char *info_str() const { return info_; }
00116   bool is_init() const { return init_; }
00117   bool is_debug() const { return debug_; }
00118   bool constraint_ok(SetParamConstraint constraint) const {
00119     return (constraint == SET_PARAM_CONSTRAINT_NONE ||
00120             (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY &&
00121              this->is_debug()) ||
00122             (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY &&
00123              !this->is_debug()) ||
00124             (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY &&
00125              !this->is_init()));
00126   }
00127 
00128  protected:
00129   Param(const char *name, const char *comment, bool init) :
00130     name_(name), info_(comment), init_(init) {
00131     debug_ = (strstr(name, "debug") != NULL) || (strstr(name, "display"));
00132   }
00133 
00134   const char *name_;      // name of this parameter
00135   const char *info_;      // for menus
00136   bool init_;             // needs to be set before init
00137   bool debug_;
00138 };
00139 
00140 class IntParam : public Param {
00141   public:
00142    IntParam(inT32 value, const char *name, const char *comment, bool init,
00143             ParamsVectors *vec) : Param(name, comment, init) {
00144     value_ = value;
00145     params_vec_ = &(vec->int_params);
00146     vec->int_params.push_back(this);
00147   }
00148   ~IntParam() { ParamUtils::RemoveParam<IntParam>(this, params_vec_); }
00149   operator inT32() const { return value_; }
00150   void set_value(inT32 value) { value_ = value; }
00151 
00152  private:
00153   inT32 value_;
00154   // Pointer to the vector that contains this param (not owened by this class).
00155   GenericVector<IntParam *> *params_vec_;
00156 };
00157 
00158 class BoolParam : public Param {
00159  public:
00160   BoolParam(bool value, const char *name, const char *comment, bool init,
00161             ParamsVectors *vec) : Param(name, comment, init) {
00162     value_ = value;
00163     params_vec_ = &(vec->bool_params);
00164     vec->bool_params.push_back(this);
00165   }
00166   ~BoolParam() { ParamUtils::RemoveParam<BoolParam>(this, params_vec_); }
00167   operator BOOL8() const { return value_; }
00168   void set_value(BOOL8 value) { value_ = value; }
00169 
00170  private:
00171   BOOL8 value_;
00172   // Pointer to the vector that contains this param (not owned by this class).
00173   GenericVector<BoolParam *> *params_vec_;
00174 };
00175 
00176 class StringParam : public Param {
00177  public:
00178   StringParam(const char *value, const char *name,
00179               const char *comment, bool init,
00180               ParamsVectors *vec) : Param(name, comment, init) {
00181     value_ = value;
00182     params_vec_ = &(vec->string_params);
00183     vec->string_params.push_back(this);
00184   }
00185   ~StringParam() { ParamUtils::RemoveParam<StringParam>(this, params_vec_); }
00186   operator STRING &() { return value_; }
00187   const char *string() const { return value_.string(); }
00188   bool empty() { return value_.length() <= 0; }
00189   void set_value(const STRING &value) { value_ = value; }
00190 
00191  private:
00192   STRING value_;
00193   // Pointer to the vector that contains this param (not owened by this class).
00194   GenericVector<StringParam *> *params_vec_;
00195 };
00196 
00197 class DoubleParam : public Param {
00198  public:
00199   DoubleParam(double value, const char *name, const char *comment,
00200               bool init, ParamsVectors *vec) : Param(name, comment, init) {
00201     value_ = value;
00202     params_vec_ = &(vec->double_params);
00203     vec->double_params.push_back(this);
00204   }
00205   ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
00206   operator double() const { return value_; }
00207   void set_value(double value) { value_ = value; }
00208 
00209  private:
00210   double value_;
00211   // Pointer to the vector that contains this param (not owned by this class).
00212   GenericVector<DoubleParam *> *params_vec_;
00213 };
00214 
00215 }  // namespace tesseract
00216 
00217 // Global parameter lists.
00218 //
00219 // To avoid the problem of undetermined order of static initialization
00220 // global_params are accessed through the GlobalParams function that
00221 // initializes the static pointer to global_params only on the first
00222 // first time GlobalParams() is called.
00223 //
00224 // TODO(daria): remove GlobalParams() when all global Tesseract
00225 // parameters are converted to members.
00226 tesseract::ParamsVectors *GlobalParams();
00227 
00228 /*************************************************************************
00229  * Note on defining parameters.
00230  *
00231  * The values of the parameters defined with *_INIT_* macros are guaranteed
00232  * to be loaded from config files before Tesseract initialization is done
00233  * (there is no such guarantee for parameters defined with the other macros).
00234  *************************************************************************/
00235 
00236 #define INT_VAR_H(name,val,comment)\
00237   tesseract::IntParam      name
00238 
00239 #define BOOL_VAR_H(name,val,comment)\
00240   tesseract::BoolParam     name
00241 
00242 #define STRING_VAR_H(name,val,comment)\
00243   tesseract::StringParam     name
00244 
00245 #define double_VAR_H(name,val,comment)\
00246   tesseract::DoubleParam     name
00247 
00248 #define INT_VAR(name,val,comment)\
00249   tesseract::IntParam      name(val,#name,comment,false,GlobalParams())
00250 
00251 #define BOOL_VAR(name,val,comment)\
00252   tesseract::BoolParam     name(val,#name,comment,false,GlobalParams())
00253 
00254 #define STRING_VAR(name,val,comment)\
00255   tesseract::StringParam     name(val,#name,comment,false,GlobalParams())
00256 
00257 #define double_VAR(name,val,comment)\
00258   tesseract::DoubleParam     name(val,#name,comment,false,GlobalParams())
00259 
00260 #define INT_INIT_VAR(name,val,comment)\
00261   tesseract::IntParam      name(val,#name,comment,true,GlobalParams())
00262 
00263 #define BOOL_INIT_VAR(name,val,comment)\
00264   tesseract::BoolParam     name(val,#name,comment,true,GlobalParams())
00265 
00266 #define STRING_INIT_VAR(name,val,comment)\
00267   tesseract::StringParam     name(val,#name,comment,true,GlobalParams())
00268 
00269 #define double_INIT_VAR(name,val,comment)\
00270   tesseract::DoubleParam     name(val,#name,comment,true,GlobalParams())
00271 
00272 #define INT_MEMBER(name, val, comment, vec)\
00273   name(val, #name, comment, false, vec)
00274 
00275 #define BOOL_MEMBER(name, val, comment, vec)\
00276   name(val, #name, comment, false, vec)
00277 
00278 #define STRING_MEMBER(name, val, comment, vec)\
00279   name(val, #name, comment, false, vec)
00280 
00281 #define double_MEMBER(name, val, comment, vec)\
00282   name(val, #name, comment, false, vec)
00283 
00284 #define INT_INIT_MEMBER(name, val, comment, vec)\
00285   name(val, #name, comment, true, vec)
00286 
00287 #define BOOL_INIT_MEMBER(name, val, comment, vec)\
00288   name(val, #name, comment, true, vec)
00289 
00290 #define STRING_INIT_MEMBER(name, val, comment, vec)\
00291   name(val, #name, comment, true, vec)
00292 
00293 #define double_INIT_MEMBER(name, val, comment, vec)\
00294   name(val, #name, comment, true, vec)
00295 
00296 #endif