modernise classify/ocrfeatures.cpp

This commit is contained in:
Jim O'Regan 2015-07-20 12:03:48 +01:00
parent 8eeb053542
commit 52e4c77df8

View File

@ -15,9 +15,9 @@
** See the License for the specific language governing permissions and ** See the License for the specific language governing permissions and
** limitations under the License. ** limitations under the License.
******************************************************************************/ ******************************************************************************/
/**---------------------------------------------------------------------------- /*----------------------------------------------------------------------------
Include Files and Type Defines Include Files and Type Defines
----------------------------------------------------------------------------**/ ----------------------------------------------------------------------------*/
#include "ocrfeatures.h" #include "ocrfeatures.h"
#include "emalloc.h" #include "emalloc.h"
#include "callcpp.h" #include "callcpp.h"
@ -28,24 +28,20 @@
#include <assert.h> #include <assert.h>
#include <math.h> #include <math.h>
/**---------------------------------------------------------------------------- /*----------------------------------------------------------------------------
Public Code Public Code
----------------------------------------------------------------------------**/ ----------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/ /**
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) { * Add a feature to a feature set. If the feature set is
/* * already full, FALSE is returned to indicate that the
** Parameters: * feature could not be added to the set; otherwise, TRUE is
** FeatureSet set of features to add Feature to * returned.
** Feature feature to be added to FeatureSet * @param FeatureSet set of features to add Feature to
** Globals: none * @param Feature feature to be added to FeatureSet
** Operation: Add a feature to a feature set. If the feature set is * @return TRUE if feature added to set, FALSE if set is already full.
** already full, FALSE is returned to indicate that the * @note History: Tue May 22 17:22:23 1990, DSJ, Created.
** feature could not be added to the set; otherwise, TRUE is
** returned.
** Return: TRUE if feature added to set, FALSE if set is already full.
** Exceptions: none
** History: Tue May 22 17:22:23 1990, DSJ, Created.
*/ */
BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) { if (FeatureSet->NumFeatures >= FeatureSet->MaxNumFeatures) {
FreeFeature(Feature); FreeFeature(Feature);
return FALSE; return FALSE;
@ -55,17 +51,13 @@ BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
return TRUE; return TRUE;
} /* AddFeature */ } /* AddFeature */
/*---------------------------------------------------------------------------*/ /**
void FreeFeature(FEATURE Feature) { * Release the memory consumed by the specified feature.
/* * @param Feature feature to be deallocated.
** Parameters: * @return none
** Feature feature to be deallocated. * @note History: Mon May 21 13:33:27 1990, DSJ, Created.
** Globals: none
** Operation: Release the memory consumed by the specified feature.
** Return: none
** Exceptions: none
** History: Mon May 21 13:33:27 1990, DSJ, Created.
*/ */
void FreeFeature(FEATURE Feature) {
if (Feature) { if (Feature) {
free_struct (Feature, sizeof (FEATURE_STRUCT) free_struct (Feature, sizeof (FEATURE_STRUCT)
+ sizeof (FLOAT32) * (Feature->Type->NumParams - 1), + sizeof (FLOAT32) * (Feature->Type->NumParams - 1),
@ -75,19 +67,15 @@ void FreeFeature(FEATURE Feature) {
} /* FreeFeature */ } /* FreeFeature */
/*---------------------------------------------------------------------------*/ /**
void FreeFeatureSet(FEATURE_SET FeatureSet) { * Release the memory consumed by the specified feature
/* * set. This routine also frees the memory consumed by the
** Parameters: * features contained in the set.
** FeatureSet set of features to be freed * @param FeatureSet set of features to be freed
** Globals: none * @return none
** Operation: Release the memory consumed by the specified feature * @note History: Mon May 21 13:59:46 1990, DSJ, Created.
** set. This routine also frees the memory consumed by the
** features contained in the set.
** Return: none
** Exceptions: none
** History: Mon May 21 13:59:46 1990, DSJ, Created.
*/ */
void FreeFeatureSet(FEATURE_SET FeatureSet) {
int i; int i;
if (FeatureSet) { if (FeatureSet) {
@ -98,18 +86,14 @@ void FreeFeatureSet(FEATURE_SET FeatureSet) {
} /* FreeFeatureSet */ } /* FreeFeatureSet */
/*---------------------------------------------------------------------------*/ /**
FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) { * Allocate and return a new feature of the specified
/* * type.
** Parameters: * @param FeatureDesc description of feature to be created.
** FeatureDesc description of feature to be created. * @return New #FEATURE.
** Globals: none * @note History: Mon May 21 14:06:42 1990, DSJ, Created.
** Operation: Allocate and return a new feature of the specified
** type.
** Return: New feature.
** Exceptions: none
** History: Mon May 21 14:06:42 1990, DSJ, Created.
*/ */
FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
FEATURE Feature; FEATURE Feature;
Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) + Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) +
@ -122,18 +106,14 @@ FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
} /* NewFeature */ } /* NewFeature */
/*---------------------------------------------------------------------------*/ /**
FEATURE_SET NewFeatureSet(int NumFeatures) { * Allocate and return a new feature set large enough to
/* * hold the specified number of features.
** Parameters: * @param NumFeatures maximum # of features to be put in feature set
** NumFeatures maximum # of features to be put in feature set * @return New #FEATURE_SET.
** Globals: none * @note History: Mon May 21 14:22:40 1990, DSJ, Created.
** Operation: Allocate and return a new feature set large enough to
** hold the specified number of features.
** Return: New feature set.
** Exceptions: none
** History: Mon May 21 14:22:40 1990, DSJ, Created.
*/ */
FEATURE_SET NewFeatureSet(int NumFeatures) {
FEATURE_SET FeatureSet; FEATURE_SET FeatureSet;
FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) + FeatureSet = (FEATURE_SET) Emalloc (sizeof (FEATURE_SET_STRUCT) +
@ -145,23 +125,20 @@ FEATURE_SET NewFeatureSet(int NumFeatures) {
} /* NewFeatureSet */ } /* NewFeatureSet */
/*---------------------------------------------------------------------------*/ /**
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { * Create a new feature of the specified type and read in
/* * the value of its parameters from File. The extra penalty
** Parameters: * for the feature is also computed by calling the appropriate
** File open text file to read feature from * function for the specified feature type. The correct text
** FeatureDesc specifies type of feature to read from File * representation for a feature is a list of N floats where
** Globals: none * N is the number of parameters in the feature.
** Operation: Create a new feature of the specified type and read in * @param File open text file to read feature from
** the value of its parameters from File. The extra penalty * @param FeatureDesc specifies type of feature to read from File
** for the feature is also computed by calling the appropriate * @return New #FEATURE read from File.
** function for the specified feature type. The correct text * @note Exceptions: #ILLEGAL_FEATURE_PARAM if text file doesn't match expected format
** representation for a feature is a list of N floats where * @note History: Wed May 23 08:53:16 1990, DSJ, Created.
** N is the number of parameters in the feature.
** Return: New feature read from File.
** Exceptions: ILLEGAL_FEATURE_PARAM if text file doesn't match expected format
** History: Wed May 23 08:53:16 1990, DSJ, Created.
*/ */
FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
FEATURE Feature; FEATURE Feature;
int i; int i;
@ -177,22 +154,18 @@ FEATURE ReadFeature(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
} /* ReadFeature */ } /* ReadFeature */
/*---------------------------------------------------------------------------*/ /**
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { * Create a new feature set of the specified type and read in
/* * the features from File. The correct text representation
** Parameters: * for a feature set is an integer which specifies the number (N)
** File open text file to read new feature set from * of features in a set followed by a list of N feature
** FeatureDesc specifies type of feature to read from File * descriptions.
** Globals: none * @param File open text file to read new feature set from
** Operation: Create a new feature set of the specified type and read in * @param FeatureDesc specifies type of feature to read from File
** the features from File. The correct text representation * @return New feature set read from File.
** for a feature set is an integer which specifies the number (N) * @note History: Wed May 23 09:17:31 1990, DSJ, Created.
** of features in a set followed by a list of N feature
** descriptions.
** Return: New feature set read from File.
** Exceptions: none
** History: Wed May 23 09:17:31 1990, DSJ, Created.
*/ */
FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
FEATURE_SET FeatureSet; FEATURE_SET FeatureSet;
int NumFeatures; int NumFeatures;
int i; int i;
@ -208,20 +181,17 @@ FEATURE_SET ReadFeatureSet(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
} /* ReadFeatureSet */ } /* ReadFeatureSet */
/*---------------------------------------------------------------------------*/ /**
/* * Appends a textual representation of Feature to str.
** Parameters: * This representation is simply a list of the N parameters
** Feature: feature to write out to str * of the feature, terminated with a newline. It is assumed
** str: string to write Feature to * that the ExtraPenalty field can be reconstructed from the
** Operation: Appends a textual representation of Feature to str. * parameters of the feature. It is also assumed that the
** This representation is simply a list of the N parameters * feature type information is specified or assumed elsewhere.
** of the feature, terminated with a newline. It is assumed * @param Feature feature to write out to str
** that the ExtraPenalty field can be reconstructed from the * @param str string to write Feature to
** parameters of the feature. It is also assumed that the * @return none
** feature type information is specified or assumed elsewhere. * @note History: Wed May 23 09:28:18 1990, DSJ, Created.
** Return: none
** Exceptions: none
** History: Wed May 23 09:28:18 1990, DSJ, Created.
*/ */
void WriteFeature(FEATURE Feature, STRING* str) { void WriteFeature(FEATURE Feature, STRING* str) {
for (int i = 0; i < Feature->Type->NumParams; i++) { for (int i = 0; i < Feature->Type->NumParams; i++) {
@ -234,19 +204,15 @@ void WriteFeature(FEATURE Feature, STRING* str) {
} /* WriteFeature */ } /* WriteFeature */
/*---------------------------------------------------------------------------*/ /**
/* * Write a textual representation of FeatureSet to File.
** Parameters: * This representation is an integer specifying the number of
** FeatureSet: feature set to write to File * features in the set, followed by a newline, followed by
** str: string to write Feature to * text representations for each feature in the set.
** Globals: none * @param FeatureSet feature set to write to File
** Operation: Write a textual representation of FeatureSet to File. * @param str string to write Feature to
** This representation is an integer specifying the number of * @return none
** features in the set, followed by a newline, followed by * @note History: Wed May 23 10:06:03 1990, DSJ, Created.
** text representations for each feature in the set.
** Return: none
** Exceptions: none
** History: Wed May 23 10:06:03 1990, DSJ, Created.
*/ */
void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) { void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
if (FeatureSet) { if (FeatureSet) {
@ -259,23 +225,22 @@ void WriteFeatureSet(FEATURE_SET FeatureSet, STRING* str) {
} /* WriteFeatureSet */ } /* WriteFeatureSet */
/*---------------------------------------------------------------------------*/ /**
void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) { * Write a textual representation of FeatureDesc to File
/* * in the old format (i.e. the format used by the clusterer).
** Parameters: *
** File open text file to write FeatureDesc to * This format is:
** FeatureDesc feature descriptor to write to File * @verbatim
** Globals: none * Number of Params
** Operation: Write a textual representation of FeatureDesc to File * Description of Param 1
** in the old format (i.e. the format used by the clusterer). * ...
** This format is: * @endverbatim
** Number of Params * @param File open text file to write FeatureDesc to
** Description of Param 1 * @param FeatureDesc feature descriptor to write to File
** ... * @return none
** Return: none * @note History: Fri May 25 15:27:18 1990, DSJ, Created.
** Exceptions: none
** History: Fri May 25 15:27:18 1990, DSJ, Created.
*/ */
void WriteOldParamDesc(FILE *File, const FEATURE_DESC_STRUCT* FeatureDesc) {
int i; int i;
fprintf (File, "%d\n", FeatureDesc->NumParams); fprintf (File, "%d\n", FeatureDesc->NumParams);