classify/featdefs.cpp

Go to the documentation of this file.
00001 
00020 /* =================
00021  Include Files and Type Defines
00022  ==================== */
00023 #include "featdefs.h"
00024 #include "emalloc.h"
00025 #include "danerror.h"
00026 #include "scanutils.h"
00027 #include "variables.h"
00028 #include "sigmenu.h"
00029 
00030 #include <string.h>
00031 #include <stdio.h>
00032 
00033 /* Errors triggered by this module */
00034 #define ILLEGAL_NUM_SETS  3001
00035 
00036 #define PICO_FEATURE_LENGTH 0.05
00037 #define MAX_OUTLINE_FEATURES  100
00038 
00042 /* define all of the parameters for the MicroFeature type*/
00043 StartParamDesc (MicroFeatureParams)
00044 DefineParam (0, 0, -0.5, 0.5)
00045 DefineParam (0, 0, -0.25, 0.75)
00046 DefineParam (0, 0, 0.0, 1.0)
00047 DefineParam (1, 0, 0.0, 1.0)
00048 DefineParam (0, 1, -0.5, 0.5)
00049 DefineParam (0, 1, -0.5, 0.5)
00050 EndParamDesc
00051 /* now define the feature type itself (see features.h for info about each
00052   parameter).*/
00053 DefineFeature (MicroFeatureDesc, 5, 1, 4, 50, "Micro", "mf", MicroFeatureParams)
00054 
00055 // define all of the parameters for the PicoFeature type
00056 /* define knob that can be used to adjust pico-feature length */
00057 FLOAT32 PicoFeatureLength = PICO_FEATURE_LENGTH;
00058 StartParamDesc (PicoFeatParams)
00059 DefineParam (0, 0, -0.25, 0.75)
00060 DefineParam (1, 0, 0.0, 1.0)
00061 DefineParam (0, 0, -0.5, 0.5)
00062 EndParamDesc
00063 /* now define the feature type itself (see features.h for info about each
00064   parameter).*/
00065 DefineFeature (PicoFeatDesc, 2, 1, 1, MAX_UINT8, "Pico", "pf", PicoFeatParams)
00066 
00067 /* define all of the parameters for the NormFeat type*/
00068 StartParamDesc (CharNormParams)
00069 DefineParam (0, 0, -0.25, 0.75)
00070 DefineParam (0, 0, 0.0, 1.0)
00071 DefineParam (0, 0, 0.0, 1.0)
00072 DefineParam (0, 0, 0.0, 1.0)
00073 EndParamDesc
00074 /* now define the feature type itself (see features.h for info about each
00075   parameter).*/
00076 DefineFeature (CharNormDesc, 4, 0, 1, 1, "CharNorm", "cn", CharNormParams)
00077 
00078 // define all of the parameters for the OutlineFeature type
00079 StartParamDesc (OutlineFeatParams)
00080 DefineParam (0, 0, -0.5, 0.5)
00081 DefineParam (0, 0, -0.25, 0.75)
00082 DefineParam (0, 0, 0.0, 1.0)
00083 DefineParam (1, 0, 0.0, 1.0)
00084 EndParamDesc
00085 /* now define the feature type itself (see features.h for info about each
00086   parameter).*/
00087 DefineFeature (OutlineFeatDesc, 3, 1, 1, MAX_OUTLINE_FEATURES, "Outline",
00088                "of", OutlineFeatParams)
00089 
00090 /* =================
00091         Global Data Definitions and Declarations
00092  ==================== */
00093 FEATURE_DEFS_STRUCT FeatureDefs = {
00094   NUM_FEATURE_TYPES,
00095   {
00096     &MicroFeatureDesc,
00097       &PicoFeatDesc,
00098       &OutlineFeatDesc,
00099       &CharNormDesc
00100   }
00101 };
00102 
00103 /* =================
00104               Public Code
00105  ==================== */
00115 void FreeCharDescription(CHAR_DESC CharDesc) {
00116   int i;
00117 
00118   if (CharDesc) {
00119     for (i = 0; i < NumFeatureSetsIn (CharDesc); i++)
00120       FreeFeatureSet (FeaturesOfType (CharDesc, i));
00121     Efree(CharDesc);
00122   }
00123 }                                /* FreeCharDescription */
00124 
00125 
00135 CHAR_DESC NewCharDescription() {
00136   CHAR_DESC CharDesc;
00137   int i;
00138 
00139   CharDesc = (CHAR_DESC) Emalloc (sizeof (CHAR_DESC_STRUCT));
00140   NumFeatureSetsIn (CharDesc) = NumFeaturesDefined ();
00141 
00142   for (i = 0; i < NumFeatureSetsIn (CharDesc); i++)
00143     FeaturesOfType (CharDesc, i) = NULL;
00144 
00145   return (CharDesc);
00146 
00147 }                                /* NewCharDescription */
00148 
00149 
00167 void WriteCharDescription(FILE *File, CHAR_DESC CharDesc) {
00168   int Type;
00169   int NumSetsToWrite = 0;
00170 
00171   for (Type = 0; Type < NumFeatureSetsIn (CharDesc); Type++)
00172     if (FeaturesOfType (CharDesc, Type))
00173       NumSetsToWrite++;
00174 
00175   fprintf (File, " %d\n", NumSetsToWrite);
00176   for (Type = 0; Type < NumFeatureSetsIn (CharDesc); Type++)
00177   if (FeaturesOfType (CharDesc, Type)) {
00178     fprintf (File, "%s ", ShortNameOf (DefinitionOf (Type)));
00179     WriteFeatureSet (File, FeaturesOfType (CharDesc, Type));
00180   }
00181 }                                /* WriteCharDescription */
00182 
00183 
00201 CHAR_DESC ReadCharDescription(FILE *File) {
00202   int NumSetsToRead;
00203   char ShortName[FEAT_NAME_SIZE];
00204   CHAR_DESC CharDesc;
00205   int Type;
00206 
00207   if (fscanf (File, "%d", &NumSetsToRead) != 1 ||
00208     NumSetsToRead < 0 || NumSetsToRead > NumFeaturesDefined ())
00209     DoError (ILLEGAL_NUM_SETS, "Illegal number of feature sets");
00210 
00211   CharDesc = NewCharDescription ();
00212   for (; NumSetsToRead > 0; NumSetsToRead--) {
00213     fscanf (File, "%s", ShortName);
00214     Type = ShortNameToFeatureType (ShortName);
00215     FeaturesOfType (CharDesc, Type) =
00216       ReadFeatureSet (File, DefinitionOf (Type));
00217   }
00218   return (CharDesc);
00219 
00220 }                                // ReadCharDescription
00221 
00222 
00236 int ShortNameToFeatureType(const char *ShortName) {
00237   int i;
00238 
00239   for (i = 0; i < NumFeaturesDefined (); i++)
00240     if (!strcmp (ShortNameOf (DefinitionOf (i)), ShortName))
00241       return (i);
00242   DoError (ILLEGAL_SHORT_NAME, "Illegal short name for a feature");
00243   return 0;
00244 
00245 }                                // ShortNameToFeatureType

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