tesseract 3.04.01

classify/ocrfeatures.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002  **     Filename:    features.c
00003  **     Purpose:     Generic definition of a feature.
00004  **     Author:      Dan Johnson
00005  **     History:     Mon May 21 10:49:04 1990, DSJ, Created.
00006  **
00007  **     (c) Copyright Hewlett-Packard Company, 1988.
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           Include Files and Type Defines
00020 ----------------------------------------------------------------------------*/
00021 #include "ocrfeatures.h"
00022 #include "emalloc.h"
00023 #include "callcpp.h"
00024 #include "danerror.h"
00025 #include "freelist.h"
00026 #include "scanutils.h"
00027 
00028 #include <assert.h>
00029 #include <math.h>
00030 
00031 /*----------------------------------------------------------------------------
00032               Public Code
00033 ----------------------------------------------------------------------------*/
00044 BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
00045   if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
00046     FreeFeature(Feature);
00047     return FALSE;
00048   }
00049 
00050   FeatureSet->Features[FeatureSet->NumFeatures++] = Feature;
00051   return TRUE;
00052 }                                /* AddFeature */
00053 
00060 void FreeFeature(FEATURE Feature) {
00061   if (Feature) {
00062     free_struct (Feature, sizeof (FEATURE_STRUCT)
00063       + sizeof (FLOAT32) * (Feature->Type->NumParams - 1),
00064       "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
00065   }
00066 
00067 }                                /* FreeFeature */
00068 
00069 
00078 void FreeFeatureSet(FEATURE_SET FeatureSet) {
00079   int i;
00080 
00081   if (FeatureSet) {
00082     for (i = 0; i < FeatureSet->NumFeatures; i++)
00083       FreeFeature(FeatureSet->Features[i]);
00084     memfree(FeatureSet);
00085   }
00086 }                                /* FreeFeatureSet */
00087 
00088 
00096 FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
00097   FEATURE Feature;
00098 
00099   Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) +
00100     (FeatureDesc->NumParams - 1) *
00101     sizeof (FLOAT32),
00102     "sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
00103   Feature->Type = FeatureDesc;
00104   return (Feature);
00105 
00106 }                                /* NewFeature */
00107 
00108 
00116 FEATURE_SET NewFeatureSet(int NumFeatures) {
00117   FEATURE_SET FeatureSet;
00118 
00119   FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
00120     (NumFeatures - 1) * sizeof (FEATURE));
00121   FeatureSet->MaxNumFeatures = NumFeatures;
00122   FeatureSet->NumFeatures = 0;
00123   return (FeatureSet);
00124 
00125 }                                /* NewFeatureSet */
00126 
00127 
00141 FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
00142   FEATURE Feature;
00143   int i;
00144 
00145   Feature = NewFeature (FeatureDesc);
00146   for (i = 0; i < Feature->Type->NumParams; i++) {
00147     if (tfscanf(File, "%f", &(Feature->Params[i])) != 1)
00148       DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
00149 #ifndef _WIN32
00150     assert (!isnan(Feature->Params[i]));
00151 #endif
00152   }
00153   return (Feature);
00154 }                                /* ReadFeature */
00155 
00156 
00168 FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
00169   FEATURE_SET FeatureSet;
00170   int NumFeatures;
00171   int i;
00172 
00173   if (tfscanf(File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
00174     DoError(ILLEGAL_NUM_FEATURES, "Illegal number of features in set");
00175 
00176   FeatureSet = NewFeatureSet(NumFeatures);
00177   for (i = 0; i < NumFeatures; i++)
00178     AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));
00179 
00180   return (FeatureSet);
00181 }                                /* ReadFeatureSet */
00182 
00183 
00196 void WriteFeature(FEATURE Feature, STRING* str) {
00197   for (int i = 0; i < Feature->Type->NumParams; i++) {
00198 #ifndef WIN32
00199     assert(!isnan(Feature->Params[i]));
00200 #endif
00201     str->add_str_double(" ", Feature->Params[i]);
00202   }
00203   *str += "\n";
00204 }                                /* WriteFeature */
00205 
00206 
00217 void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
00218   if (FeatureSet) {
00219     str->add_str_int("", FeatureSet->NumFeatures);
00220     *str += "\n";
00221     for (int i = 0; i < FeatureSet->NumFeatures; i++) {
00222       WriteFeature(FeatureSet->Features[i], str);
00223     }
00224   }
00225 }                                /* WriteFeatureSet */
00226 
00227 
00243 void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
00244   int i;
00245 
00246   fprintf (File, "%d\n", FeatureDesc->NumParams);
00247   for (i = 0; i < FeatureDesc->NumParams; i++) {
00248     if (FeatureDesc->ParamDesc[i].Circular)
00249       fprintf (File, "circular ");
00250     else
00251       fprintf (File, "linear   ");
00252 
00253     if (FeatureDesc->ParamDesc[i].NonEssential)
00254       fprintf (File, "non-essential  ");
00255     else
00256       fprintf (File, "essential      ");
00257 
00258     fprintf (File, "%f  %f\n",
00259       FeatureDesc->ParamDesc[i].Min, FeatureDesc->ParamDesc[i].Max);
00260   }
00261 }                                /* WriteOldParamDesc */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines