classify/ocrfeatures.cpp

Go to the documentation of this file.
00001 
00020 /* =================
00021  Include Files and Type Defines
00022  ==================== */
00023 #include "ocrfeatures.h"
00024 #include "emalloc.h"
00025 #include "callcpp.h"
00026 #include "danerror.h"
00027 #include "freelist.h"
00028 #include "scanutils.h"
00029 
00030 /* =================
00031               Public Code
00032  ==================== */
00033 /* =============================== */
00047 BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) { 
00048   if (NumFeaturesIn (FeatureSet) >= MaxNumFeaturesIn (FeatureSet)) {
00049     FreeFeature(Feature);
00050     return (FALSE);
00051   }
00052 
00053   FeatureIn (FeatureSet, NumFeaturesIn (FeatureSet)) = Feature;
00054   NumFeaturesIn (FeatureSet)++;
00055   return (TRUE);
00056 
00057 }                                /* AddFeature */
00058 
00059 
00060 /* =============================== */
00070 void DefaultInitFXVars() { 
00071 }                                /* DefaultInitFXVars */
00072 
00073 
00074 /* =============================== */
00083 void FreeFeature(FEATURE Feature) { 
00084   if (Feature) {
00085     c_free_struct (Feature, sizeof (FEATURE_STRUCT)
00086       + sizeof (FLOAT32) * (NumParamsIn (Feature) - 1),
00087       "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
00088   }
00089 
00090 }                                /* FreeFeature */
00091 
00092 
00093 /* =============================== */
00103 void FreeFeatureSet(FEATURE_SET FeatureSet) { 
00104   int i;
00105 
00106   if (FeatureSet) {
00107     for (i = 0; i < NumFeaturesIn (FeatureSet); i++)
00108       FreeFeature (FeatureIn (FeatureSet, i));
00109     memfree(FeatureSet);
00110   }
00111 }                                /* FreeFeatureSet */
00112 
00113 
00114 /* =============================== */
00123 FEATURE NewFeature(FEATURE_DESC FeatureDesc) { 
00124   FEATURE Feature;
00125 
00126   Feature = (FEATURE) c_alloc_struct (sizeof (FEATURE_STRUCT) +
00127     (FeatureDesc->NumParams - 1) *
00128     sizeof (FLOAT32),
00129     "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
00130   TypeOf (Feature) = FeatureDesc;
00131   return (Feature);
00132 
00133 }                                /* NewFeature */
00134 
00135 
00136 /* =============================== */
00146 FEATURE_SET NewFeatureSet(int NumFeatures) { 
00147   FEATURE_SET FeatureSet;
00148 
00149   FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
00150     (NumFeatures - 1) * sizeof (FEATURE));
00151   MaxNumFeaturesIn (FeatureSet) = NumFeatures;
00152   NumFeaturesIn (FeatureSet) = 0;
00153   return (FeatureSet);
00154 
00155 }                                /* NewFeatureSet */
00156 
00157 
00158 /* =============================== */
00174 FEATURE ReadFeature(FILE *File, FEATURE_DESC FeatureDesc) { 
00175   FEATURE Feature;
00176   int i;
00177 
00178   Feature = NewFeature (FeatureDesc);
00179   for (i = 0; i < NumParamsIn (Feature); i++) {
00180     if (fscanf (File, "%f", &(ParamOf (Feature, i))) != 1)
00181       DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
00182   }
00183   return (Feature);
00184 
00185 }                                /* ReadFeature */
00186 
00187 
00188 /* =============================== */
00203 FEATURE_SET ReadFeatureSet(FILE *File, FEATURE_DESC FeatureDesc) { 
00204   FEATURE_SET FeatureSet;
00205   int NumFeatures;
00206   int i;
00207 
00208   if (fscanf (File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
00209     DoError (ILLEGAL_NUM_FEATURES, "Illegal number of features in set");
00210 
00211   FeatureSet = NewFeatureSet (NumFeatures);
00212   for (i = 0; i < NumFeatures; i++)
00213     AddFeature (FeatureSet, ReadFeature (File, FeatureDesc));
00214 
00215   return (FeatureSet);
00216 
00217 }                                /* ReadFeatureSet */
00218 
00219 
00220 /* =============================== */
00236 void WriteFeature(FILE *File, FEATURE Feature) { 
00237   int i;
00238 
00239   for (i = 0; i < NumParamsIn (Feature); i++)
00240     fprintf (File, " %12g", ParamOf (Feature, i));
00241   fprintf (File, "\n");
00242 
00243 }                                /* WriteFeature */
00244 
00245 
00246 /* =============================== */
00260 void WriteFeatureSet(FILE *File, FEATURE_SET FeatureSet) { 
00261   int i;
00262 
00263   if (FeatureSet) {
00264     fprintf (File, "%d\n", NumFeaturesIn (FeatureSet));
00265     for (i = 0; i < NumFeaturesIn (FeatureSet); i++)
00266       WriteFeature (File, FeatureIn (FeatureSet, i));
00267   }
00268 }                                /* WriteFeatureSet */
00269 
00270 
00271 /* =============================== */
00289 void WriteOldParamDesc(FILE *File, FEATURE_DESC FeatureDesc) { 
00290   int i;
00291 
00292   fprintf (File, "%d\n", FeatureDesc->NumParams);
00293   for (i = 0; i < FeatureDesc->NumParams; i++) {
00294     if (FeatureDesc->ParamDesc[i].Circular)
00295       fprintf (File, "circular ");
00296     else
00297       fprintf (File, "linear   ");
00298 
00299     if (FeatureDesc->ParamDesc[i].NonEssential)
00300       fprintf (File, "non-essential  ");
00301     else
00302       fprintf (File, "essential      ");
00303 
00304     fprintf (File, "%f  %f\n",
00305       FeatureDesc->ParamDesc[i].Min, FeatureDesc->ParamDesc[i].Max);
00306   }
00307 }                                /* WriteOldParamDesc */

Generated on Wed Feb 28 19:49:10 2007 for Tesseract by  doxygen 1.5.1