Tesseract 3.01
/data/source/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 struct ParamsVectors {
00036   GenericVector<IntParam *> int_params;
00037   GenericVector<BoolParam *> bool_params;
00038   GenericVector<StringParam *> string_params;
00039   GenericVector<DoubleParam *> double_params;
00040 };
00041 
00042 // Utility functions for working with Tesseract parameters.
00043 class ParamUtils {
00044  public:
00045   // Reads a file of parameter definitions and set/modify the values therein.
00046   // If the filename begins with a + or -, the BoolVariables will be
00047   // ORed or ANDed with any current values.
00048   // Blank lines and lines beginning # are ignored.
00049   // Values may have any whitespace after the name and are the rest of line.
00050   static bool ReadParamsFile(
00051       const char *file,   // filename to read
00052       bool init_only,     // only set parameters that need to be
00053                         // initialized when Init() is called
00054       ParamsVectors *member_params);
00055 
00056   // Read parameters from the given file pointer (stop at end_offset).
00057   static bool ReadParamsFromFp(FILE *fp, inT64 end_offset, bool init_only,
00058                                  ParamsVectors *member_params);
00059 
00060   // Set a parameters to have the given value.
00061   static bool SetParam(const char *name, const char* value,
00062                          bool init_only, ParamsVectors *member_params);
00063 
00064   // Returns the pointer to the parameter with the given name (of the
00065   // appropriate type) if it was found in the vector obtained from
00066   // GlobalParams() or in the given member_params.
00067   template<class T>
00068   static T *FindParam(const char *name,
00069                       const GenericVector<T *> &global_vec,
00070                       const GenericVector<T *> &member_vec) {
00071     int i;
00072     for (i = 0; i < global_vec.size(); ++i) {
00073       if (strcmp(global_vec[i]->name_str(), name) == 0) return global_vec[i];
00074     }
00075     for (i = 0; i < member_vec.size(); ++i) {
00076       if (strcmp(member_vec[i]->name_str(), name) == 0) return member_vec[i];
00077     }
00078     return NULL;
00079   }
00080   // Removes the given pointer to the param from the given vector.
00081   template<class T>
00082   static void RemoveParam(T *param_ptr, GenericVector<T *> *vec) {
00083     for (int i = 0; i < vec->size(); ++i) {
00084       if ((*vec)[i] == param_ptr) {
00085         vec->remove(i);
00086         return;
00087       }
00088     }
00089   }
00090   // Fetches the value of the named param as a STRING. Returns false if not
00091   // found.
00092   static bool GetParamAsString(const char *name,
00093                                const ParamsVectors* member_params,
00094                                STRING *value);
00095 
00096   // Print parameters to the given file.
00097   static void PrintParams(FILE *fp, const ParamsVectors *member_params);
00098 };
00099 
00100 // Definition of various parameter types.
00101 class Param {
00102  public:
00103   ~Param() {}
00104 
00105   const char *name_str() const { return name_; }
00106   const char *info_str() const { return info_; }
00107   bool is_init() const { return init_; }
00108 
00109  protected:
00110   Param(const char *name, const char *comment, bool init) :
00111     name_(name), info_(comment), init_(init) {}
00112 
00113     const char *name_;      // name of this parameter
00114     const char *info_;      // for menus
00115     bool init_;             // needs to be set before init
00116 };
00117 
00118 class IntParam : public Param {
00119   public:
00120    IntParam(inT32 value, const char *name, const char *comment, bool init,
00121             ParamsVectors *vec) : Param(name, comment, init) {
00122     value_ = value;
00123     params_vec_ = &(vec->int_params);
00124     vec->int_params.push_back(this);
00125   }
00126   ~IntParam() { ParamUtils::RemoveParam<IntParam>(this, params_vec_); }
00127   operator inT32() { return value_; }
00128   void set_value(inT32 value) { value_ = value; }
00129 
00130  private:
00131   inT32 value_;
00132   // Pointer to the vector that contains this param (not owened by this class).
00133   GenericVector<IntParam *> *params_vec_;
00134 };
00135 
00136 class BoolParam : public Param {
00137  public:
00138   BoolParam(bool value, const char *name, const char *comment, bool init,
00139             ParamsVectors *vec) : Param(name, comment, init) {
00140     value_ = value;
00141     params_vec_ = &(vec->bool_params);
00142     vec->bool_params.push_back(this);
00143   }
00144   ~BoolParam() { ParamUtils::RemoveParam<BoolParam>(this, params_vec_); }
00145   operator BOOL8() { return value_; }
00146   void set_value(BOOL8 value) { value_ = value; }
00147 
00148  private:
00149   BOOL8 value_;
00150   // Pointer to the vector that contains this param (not owened by this class).
00151   GenericVector<BoolParam *> *params_vec_;
00152 };
00153 
00154 class StringParam : public Param {
00155  public:
00156   StringParam(const char *value, const char *name,
00157               const char *comment, bool init,
00158               ParamsVectors *vec) : Param(name, comment, init) {
00159     value_ = value;
00160     params_vec_ = &(vec->string_params);
00161     vec->string_params.push_back(this);
00162   }
00163   ~StringParam() { ParamUtils::RemoveParam<StringParam>(this, params_vec_); }
00164   operator STRING &() { return value_; }
00165   const char *string() const { return value_.string(); }
00166   void set_value(const STRING &value) { value_ = value; }
00167 
00168  private:
00169   STRING value_;
00170   // Pointer to the vector that contains this param (not owened by this class).
00171   GenericVector<StringParam *> *params_vec_;
00172 };
00173 
00174 class DoubleParam : public Param {
00175  public:
00176   DoubleParam(double value, const char *name, const char *comment,
00177               bool init, ParamsVectors *vec) : Param(name, comment, init) {
00178     value_ = value;
00179     params_vec_ = &(vec->double_params);
00180     vec->double_params.push_back(this);
00181   }
00182   ~DoubleParam() { ParamUtils::RemoveParam<DoubleParam>(this, params_vec_); }
00183   operator double() { return value_; }
00184   void set_value(double value) { value_ = value; }
00185 
00186  private:
00187   double value_;
00188   // Pointer to the vector that contains this param (not owened by this class).
00189   GenericVector<DoubleParam *> *params_vec_;
00190 };
00191 
00192 }  // namespace tesseract
00193 
00194 // Global parameter lists.
00195 //
00196 // To avoid the problem of undetermined order of static initialization
00197 // global_params are accessed through the GlobalParams function that
00198 // initializes the static pointer to global_params only on the first
00199 // first time GlobalParams() is called.
00200 //
00201 // TODO(daria): remove GlobalParams() when all global Tesseract
00202 // parameters are converted to members.
00203 tesseract::ParamsVectors *GlobalParams();
00204 
00205 /*************************************************************************
00206  * Note on defining parameters.
00207  *
00208  * The values of the parameters defined with *_INIT_* macros are guaranteed
00209  * to be loaded from config files before Tesseract initialization is done
00210  * (there is no such guarantee for parameters defined with the other macros).
00211  *************************************************************************/
00212 
00213 #define INT_VAR_H(name,val,comment)\
00214   tesseract::IntParam      name
00215 
00216 #define BOOL_VAR_H(name,val,comment)\
00217   tesseract::BoolParam     name
00218 
00219 #define STRING_VAR_H(name,val,comment)\
00220   tesseract::StringParam     name
00221 
00222 #define double_VAR_H(name,val,comment)\
00223   tesseract::DoubleParam     name
00224 
00225 #define INT_VAR(name,val,comment)\
00226   tesseract::IntParam      name(val,#name,comment,false,GlobalParams())
00227 
00228 #define BOOL_VAR(name,val,comment)\
00229   tesseract::BoolParam     name(val,#name,comment,false,GlobalParams())
00230 
00231 #define STRING_VAR(name,val,comment)\
00232   tesseract::StringParam     name(val,#name,comment,false,GlobalParams())
00233 
00234 #define double_VAR(name,val,comment)\
00235   tesseract::DoubleParam     name(val,#name,comment,false,GlobalParams())
00236 
00237 #define INT_INIT_VAR(name,val,comment)\
00238   tesseract::IntParam      name(val,#name,comment,true,GlobalParams())
00239 
00240 #define BOOL_INIT_VAR(name,val,comment)\
00241   tesseract::BoolParam     name(val,#name,comment,true,GlobalParams())
00242 
00243 #define STRING_INIT_VAR(name,val,comment)\
00244   tesseract::StringParam     name(val,#name,comment,true,GlobalParams())
00245 
00246 #define double_INIT_VAR(name,val,comment)\
00247   tesseract::DoubleParam     name(val,#name,comment,true,GlobalParams())
00248 
00249 #define INT_MEMBER(name, val, comment, vec)\
00250   name(val, #name, comment, false, vec)
00251 
00252 #define BOOL_MEMBER(name, val, comment, vec)\
00253   name(val, #name, comment, false, vec)
00254 
00255 #define STRING_MEMBER(name, val, comment, vec)\
00256   name(val, #name, comment, false, vec)
00257 
00258 #define double_MEMBER(name, val, comment, vec)\
00259   name(val, #name, comment, false, vec)
00260 
00261 #define INT_INIT_MEMBER(name, val, comment, vec)\
00262   name(val, #name, comment, true, vec)
00263 
00264 #define BOOL_INIT_MEMBER(name, val, comment, vec)\
00265   name(val, #name, comment, true, vec)
00266 
00267 #define STRING_INIT_MEMBER(name, val, comment, vec)\
00268   name(val, #name, comment, true, vec)
00269 
00270 #define double_INIT_MEMBER(name, val, comment, vec)\
00271   name(val, #name, comment, true, vec)
00272 
00273 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines