tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/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   // Resets all parameters back to default values;
00109   static void ResetToDefaults(ParamsVectors* member_params);
00110 };
00111 
00112 // Definition of various parameter types.
00113 class Param {
00114  public:
00115   ~Param() {}
00116 
00117   const char *name_str() const { return name_; }
00118   const char *info_str() const { return info_; }
00119   bool is_init() const { return init_; }
00120   bool is_debug() const { return debug_; }
00121   bool constraint_ok(SetParamConstraint constraint) const {
00122     return (constraint == SET_PARAM_CONSTRAINT_NONE ||
00123             (constraint == SET_PARAM_CONSTRAINT_DEBUG_ONLY &&
00124              this->is_debug()) ||
00125             (constraint == SET_PARAM_CONSTRAINT_NON_DEBUG_ONLY &&
00126              !this->is_debug()) ||
00127             (constraint == SET_PARAM_CONSTRAINT_NON_INIT_ONLY &&
00128              !this->is_init()));
00129   }
00130 
00131  protected:
00132   Param(const char *name, const char *comment, bool init) :
00133     name_(name), info_(comment), init_(init) {
00134     debug_ = (strstr(name, "debug") != NULL) || (strstr(name, "display"));
00135   }
00136 
00137   const char *name_;      // name of this parameter
00138   const char *info_;      // for menus
00139   bool init_;             // needs to be set before init
00140   bool debug_;
00141 };
00142 
00143 class IntParam : public Param {
00144   public:
00145    IntParam(inT32 value, const char *name, const char *comment, bool init,
00146             ParamsVectors *vec) : Param(name, comment, init) {
00147     value_ = value;
00148     default_ = value;
00149     params_vec_ = &(vec->int_params);
00150     vec->int_params.push_back(this);
00151   }
00152   ~IntParam() { ParamUtils::RemoveParam<IntParam>(this, params_vec_); }
00153   operator inT32() const { return value_; }
00154   void operator=(inT32 value) { value_ = value; }
00155   void set_value(inT32 value) { value_ = value; }
00156   void ResetToDefault() {
00157     value_ = default_;
00158   }
00159 
00160  private:
00161   inT32 value_;
00162   inT32 default_;
00163   // Pointer to the vector that contains this param (not owened by this class).
00164   GenericVector<IntParam *> *params_vec_;
00165 };
00166 
00167 class BoolParam : public Param {
00168  public:
00169   BoolParam(bool value, const char *name, const char *comment, bool init,
00170             ParamsVectors *vec) : Param(name, comment, init) {
00171     value_ = value;
00172     default_ = value;
00173     params_vec_ = &(vec->bool_params);
00174     vec->bool_params.push_back(this);
00175   }
00176   ~BoolParam() { ParamUtils::RemoveParam<BoolParam>(this, params_vec_); }
00177   operator BOOL8() const { return value_; }
00178   void operator=(BOOL8 value) { value_ = value; }
00179   void set_value(BOOL8 value) { value_ = value; }
00180   void ResetToDefault() {
00181     value_ = default_;
00182   }
00183 
00184  private:
00185   BOOL8 value_;
00186   BOOL8 default_;
00187   // Pointer to the vector that contains this param (not owned by this class).
00188   GenericVector<BoolParam *> *params_vec_;
00189 };
00190 
00191 class StringParam : public Param {
00192  public:
00193   StringParam(const char *value, const char *name,
00194               const char *comment, bool init,
00195               ParamsVectors *vec) : Param(name, comment, init) {
00196     value_ = value;
00197     default_ = value;
00198     params_vec_ = &(vec->string_params);
00199     vec->string_params.push_back(this);
00200   }
00201   ~StringParam() { ParamUtils::RemoveParam<StringParam>(this, params_vec_); }
00202   operator STRING &() { return value_; }
00203   const char *string() const { return value_.string(); }
00204   const char *c_str() const { return value_.string(); }
00205   bool empty() { return value_.length() <= 0; }
00206   bool operator==(const STRING& other) { return value_ == other; }
00207   void operator=(const STRING& value) { value_ = value; }
00208   void set_value(const STRING& value) { value_ = value; }
00209   void ResetToDefault() {
00210     value_ = default_;
00211   }
00212 
00213  private:
00214   STRING value_;
00215   STRING default_;
00216   // Pointer to the vector that contains this param (not owened by this class).
00217   GenericVector<StringParam *> *params_vec_;
00218 };
00219 
00220 class DoubleParam : public Param {
00221  public:
00222   DoubleParam(double value, const char *name, const char *comment,
00223               bool init, ParamsVectors *vec) : Param(name, comment, init) {
00224     value_ = value;
00225     default_ = value;
00226     params_vec_ = &(vec->double_params);
00227     vec->double_params.push_back(this);
00228   }
00229   ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
00230   operator double() const { return value_; }
00231   void operator=(double value) { value_ = value; }
00232   void set_value(double value) { value_ = value; }
00233   void ResetToDefault() {
00234     value_ = default_;
00235   }
00236 
00237  private:
00238   double value_;
00239   double default_;
00240   // Pointer to the vector that contains this param (not owned by this class).
00241   GenericVector<DoubleParam *> *params_vec_;
00242 };
00243 
00244 }  // namespace tesseract
00245 
00246 // Global parameter lists.
00247 //
00248 // To avoid the problem of undetermined order of static initialization
00249 // global_params are accessed through the GlobalParams function that
00250 // initializes the static pointer to global_params only on the first
00251 // first time GlobalParams() is called.
00252 //
00253 // TODO(daria): remove GlobalParams() when all global Tesseract
00254 // parameters are converted to members.
00255 tesseract::ParamsVectors *GlobalParams();
00256 
00257 /*************************************************************************
00258  * Note on defining parameters.
00259  *
00260  * The values of the parameters defined with *_INIT_* macros are guaranteed
00261  * to be loaded from config files before Tesseract initialization is done
00262  * (there is no such guarantee for parameters defined with the other macros).
00263  *************************************************************************/
00264 
00265 #define INT_VAR_H(name,val,comment)\
00266   tesseract::IntParam      name
00267 
00268 #define BOOL_VAR_H(name,val,comment)\
00269   tesseract::BoolParam     name
00270 
00271 #define STRING_VAR_H(name,val,comment)\
00272   tesseract::StringParam     name
00273 
00274 #define double_VAR_H(name,val,comment)\
00275   tesseract::DoubleParam     name
00276 
00277 #define INT_VAR(name,val,comment)\
00278   tesseract::IntParam      name(val,#name,comment,false,GlobalParams())
00279 
00280 #define BOOL_VAR(name,val,comment)\
00281   tesseract::BoolParam     name(val,#name,comment,false,GlobalParams())
00282 
00283 #define STRING_VAR(name,val,comment)\
00284   tesseract::StringParam     name(val,#name,comment,false,GlobalParams())
00285 
00286 #define double_VAR(name,val,comment)\
00287   tesseract::DoubleParam     name(val,#name,comment,false,GlobalParams())
00288 
00289 #define INT_INIT_VAR(name,val,comment)\
00290   tesseract::IntParam      name(val,#name,comment,true,GlobalParams())
00291 
00292 #define BOOL_INIT_VAR(name,val,comment)\
00293   tesseract::BoolParam     name(val,#name,comment,true,GlobalParams())
00294 
00295 #define STRING_INIT_VAR(name,val,comment)\
00296   tesseract::StringParam     name(val,#name,comment,true,GlobalParams())
00297 
00298 #define double_INIT_VAR(name,val,comment)\
00299   tesseract::DoubleParam     name(val,#name,comment,true,GlobalParams())
00300 
00301 #define INT_MEMBER(name, val, comment, vec)\
00302   name(val, #name, comment, false, vec)
00303 
00304 #define BOOL_MEMBER(name, val, comment, vec)\
00305   name(val, #name, comment, false, vec)
00306 
00307 #define STRING_MEMBER(name, val, comment, vec)\
00308   name(val, #name, comment, false, vec)
00309 
00310 #define double_MEMBER(name, val, comment, vec)\
00311   name(val, #name, comment, false, vec)
00312 
00313 #define INT_INIT_MEMBER(name, val, comment, vec)\
00314   name(val, #name, comment, true, vec)
00315 
00316 #define BOOL_INIT_MEMBER(name, val, comment, vec)\
00317   name(val, #name, comment, true, vec)
00318 
00319 #define STRING_INIT_MEMBER(name, val, comment, vec)\
00320   name(val, #name, comment, true, vec)
00321 
00322 #define double_INIT_MEMBER(name, val, comment, vec)\
00323   name(val, #name, comment, true, vec)
00324 
00325 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines