Skip to content

Commit

Permalink
Merge pull request #1734 from stweil/danerror
Browse files Browse the repository at this point in the history
Replace function DoError and remove danerror.cpp, danerror.h
  • Loading branch information
zdenop committed Jul 3, 2018
2 parents 412186b + 8728132 commit b0c9d5b
Show file tree
Hide file tree
Showing 19 changed files with 62 additions and 296 deletions.
1 change: 0 additions & 1 deletion src/ccmain/tessedit.cpp
Expand Up @@ -36,7 +36,6 @@
#include "stopper.h"
#include "intmatcher.h"
#include "chop.h"
#include "danerror.h"
#include "globals.h"
#ifndef ANDROID_BUILD
#include "lstmrecognizer.h"
Expand Down
9 changes: 2 additions & 7 deletions src/classify/cluster.cpp
Expand Up @@ -24,7 +24,6 @@
#include "kdpair.h"
#include "matrix.h"
#include "tprintf.h"
#include "danerror.h"
#include <cfloat> // for FLT_MAX
#include <cmath>

Expand Down Expand Up @@ -450,19 +449,15 @@ MakeClusterer (int16_t SampleSize, const PARAM_DESC ParamDesc[]) {
* @param CharID unique ident. of char that sample came from
*
* @return Pointer to the new sample data structure
* @note Exceptions: ALREADYCLUSTERED MakeSample can't be called after
* ClusterSamples has been called
* @note History: 5/29/89, DSJ, Created.
*/
SAMPLE* MakeSample(CLUSTERER * Clusterer, const float* Feature,
int32_t CharID) {
SAMPLE *Sample;
int i;

// see if the samples have already been clustered - if so trap an error
if (Clusterer->Root != nullptr)
DoError (ALREADYCLUSTERED,
"Can't add samples after they have been clustered");
// Can't add samples after they have been clustered.
ASSERT_HOST(Clusterer->Root == nullptr);

// allocate the new sample and initialize it
Sample = (SAMPLE *) Emalloc (sizeof (SAMPLE) +
Expand Down
3 changes: 0 additions & 3 deletions src/classify/cluster.h
Expand Up @@ -128,7 +128,4 @@ float StandardDeviation(PROTOTYPE *Proto, uint16_t Dimension);
int32_t MergeClusters(int16_t N, PARAM_DESC ParamDesc[], int32_t n1, int32_t n2,
float m[], float m1[], float m2[]);

//--------------Global Data Definitions and Declarations---------------------------
// define errors that can be trapped
#define ALREADYCLUSTERED 4000
#endif
46 changes: 13 additions & 33 deletions src/classify/clusttool.cpp
Expand Up @@ -17,7 +17,6 @@

//--------------------------Include Files----------------------------------
#include "clusttool.h"
#include "danerror.h"
#include "emalloc.h"
#include <cstdio>
#include <cmath>
Expand All @@ -38,34 +37,26 @@ using tesseract::TFile;
* @param fp open text file to read sample size from
* @return Sample size
* @note Globals: None
* @note Exceptions: ILLEGALSAMPLESIZE illegal format or range
* @note History: 6/6/89, DSJ, Created.
*/
uint16_t ReadSampleSize(TFile *fp) {
int SampleSize = 0;

const int kMaxLineSize = 100;
char line[kMaxLineSize];
if (fp->FGets(line, kMaxLineSize) == nullptr ||
sscanf(line, "%d", &SampleSize) != 1 || (SampleSize < 0) ||
(SampleSize > MAXSAMPLESIZE))
DoError (ILLEGALSAMPLESIZE, "Illegal sample size");
return (SampleSize);
ASSERT_HOST(fp->FGets(line, kMaxLineSize) != nullptr);
ASSERT_HOST(sscanf(line, "%d", &SampleSize) == 1);
ASSERT_HOST(SampleSize >= 0 && SampleSize <= MAXSAMPLESIZE);
return SampleSize;
}

/**
* This routine reads textual descriptions of sets of parameters
* which describe the characteristics of feature dimensions.
*
* Exceptions:
* - ILLEGALCIRCULARSPEC
* - ILLEGALESSENTIALSPEC
* - ILLEGALMINMAXSPEC
* @param fp open text file to read N parameter descriptions from
* @param N number of parameter descriptions to read
* @return Pointer to an array of parameter descriptors.
* @note Globals: None
* @note History: 6/6/89, DSJ, Created.
*/
PARAM_DESC *ReadParamDesc(TFile *fp, uint16_t N) {
PARAM_DESC *ParamDesc;
Expand All @@ -75,11 +66,11 @@ PARAM_DESC *ReadParamDesc(TFile *fp, uint16_t N) {
for (int i = 0; i < N; i++) {
const int kMaxLineSize = TOKENSIZE * 4;
char line[kMaxLineSize];
if (fp->FGets(line, kMaxLineSize) == nullptr ||
sscanf(line, "%" QUOTED_TOKENSIZE "s %" QUOTED_TOKENSIZE "s %f %f",
linear_token, essential_token, &ParamDesc[i].Min,
&ParamDesc[i].Max) != 4)
DoError(ILLEGALCIRCULARSPEC, "Illegal Parameter specification");
ASSERT_HOST(fp->FGets(line, kMaxLineSize) != nullptr);
ASSERT_HOST(sscanf(line,
"%" QUOTED_TOKENSIZE "s %" QUOTED_TOKENSIZE "s %f %f",
linear_token, essential_token, &ParamDesc[i].Min,
&ParamDesc[i].Max) == 4);
if (linear_token[0] == 'c')
ParamDesc[i].Circular = TRUE;
else
Expand All @@ -100,17 +91,10 @@ PARAM_DESC *ReadParamDesc(TFile *fp, uint16_t N) {
* This routine reads a textual description of a prototype from
* the specified file.
*
* Exceptions:
* - ILLEGALSIGNIFICANCESPEC
* - ILLEGALSAMPLECOUNT
* - ILLEGALMEANSPEC
* - ILLEGALVARIANCESPEC
* - ILLEGALDISTRIBUTION
* @param fp open text file to read prototype from
* @param N number of dimensions used in prototype
* @return List of prototypes
* @note Globals: None
* @note History: 6/6/89, DSJ, Created.
*/
PROTOTYPE *ReadPrototype(TFile *fp, uint16_t N) {
char sig_token[TOKENSIZE], shape_token[TOKENSIZE];
Expand Down Expand Up @@ -148,16 +132,15 @@ PROTOTYPE *ReadPrototype(TFile *fp, uint16_t N) {
Proto->Style = elliptical;
}

if (SampleCount < 0) DoError(ILLEGALSAMPLECOUNT, "Illegal sample count");
ASSERT_HOST(SampleCount >= 0);
Proto->NumSamples = SampleCount;

Proto->Mean = ReadNFloats(fp, N, nullptr);
if (Proto->Mean == nullptr) DoError(ILLEGALMEANSPEC, "Illegal prototype mean");
ASSERT_HOST(Proto->Mean != nullptr);

switch (Proto->Style) {
case spherical:
if (ReadNFloats(fp, 1, &(Proto->Variance.Spherical)) == nullptr)
DoError(ILLEGALVARIANCESPEC, "Illegal prototype variance");
ASSERT_HOST(ReadNFloats(fp, 1, &(Proto->Variance.Spherical)) != nullptr);
Proto->Magnitude.Spherical =
1.0 / sqrt(2.0 * M_PI * Proto->Variance.Spherical);
Proto->TotalMagnitude = pow(Proto->Magnitude.Spherical, (float)N);
Expand All @@ -167,8 +150,7 @@ PROTOTYPE *ReadPrototype(TFile *fp, uint16_t N) {
break;
case elliptical:
Proto->Variance.Elliptical = ReadNFloats(fp, N, nullptr);
if (Proto->Variance.Elliptical == nullptr)
DoError(ILLEGALVARIANCESPEC, "Illegal prototype variance");
ASSERT_HOST(Proto->Variance.Elliptical != nullptr);
Proto->Magnitude.Elliptical = (float *)Emalloc(N * sizeof(float));
Proto->Weight.Elliptical = (float *)Emalloc(N * sizeof(float));
Proto->TotalMagnitude = 1.0;
Expand Down Expand Up @@ -200,8 +182,6 @@ PROTOTYPE *ReadPrototype(TFile *fp, uint16_t N) {
* @param Buffer pointer to buffer to place floats into
* @return Pointer to buffer holding floats or nullptr if EOF
* @note Globals: None
* @note Exceptions: ILLEGALFLOAT
* @note History: 6/6/89, DSJ, Created.
*/
float *ReadNFloats(TFile *fp, uint16_t N, float Buffer[]) {
const int kMaxLineSize = 1024;
Expand Down
14 changes: 0 additions & 14 deletions src/classify/clusttool.h
Expand Up @@ -47,18 +47,4 @@ void WriteProtoList(FILE* File, uint16_t N, PARAM_DESC* ParamDesc,
LIST ProtoList, bool WriteSigProtos,
bool WriteInsigProtos);

//--------------Global Data Definitions and Declarations---------------------
// define errors that can be trapped
#define ILLEGALSAMPLESIZE 5000
#define ILLEGALCIRCULARSPEC 5001
#define ILLEGALMINMAXSPEC 5002
#define ILLEGALSIGNIFICANCESPEC 5003
#define ILLEGALSTYLESPEC 5004
#define ILLEGALSAMPLECOUNT 5005
#define ILLEGALMEANSPEC 5006
#define ILLEGALVARIANCESPEC 5007
#define ILLEGALDISTRIBUTION 5008
#define ILLEGALFLOAT 5009
#define ILLEGALESSENTIALSPEC 5013

#endif // TESSERACT_CLASSIFY_CLUSTTOOL_H_
41 changes: 8 additions & 33 deletions src/classify/featdefs.cpp
Expand Up @@ -19,15 +19,11 @@
-----------------------------------------------------------------------------*/
#include "featdefs.h"
#include "emalloc.h"
#include "danerror.h"
#include "scanutils.h"

#include <cstring>
#include <cstdio>

/** define errors triggered by this module */
#define ILLEGAL_NUM_SETS 3001

#define PICO_FEATURE_LENGTH 0.05

/*-----------------------------------------------------------------------------
Expand Down Expand Up @@ -129,9 +125,6 @@ void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs) {
*
* Globals:
* - none
*
* @note Exceptions: none
* @note History: Wed May 23 13:52:19 1990, DSJ, Created.
*/
void FreeCharDescription(CHAR_DESC CharDesc) {
if (CharDesc) {
Expand All @@ -151,8 +144,6 @@ void FreeCharDescription(CHAR_DESC CharDesc) {
* - none
*
* @return New character description structure.
* @note Exceptions: none
* @note History: Wed May 23 15:27:10 1990, DSJ, Created.
*/
CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
CHAR_DESC CharDesc;
Expand All @@ -163,10 +154,8 @@ CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
CharDesc->FeatureSets[i] = nullptr;

return (CharDesc);

} /* NewCharDescription */


/*---------------------------------------------------------------------------*/
/**
* Appends a textual representation of CharDesc to str.
Expand All @@ -181,9 +170,6 @@ CHAR_DESC NewCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs) {
* @param FeatureDefs definitions of feature types/extractors
* @param str string to append CharDesc to
* @param CharDesc character description to write to File
*
* @note Exceptions: none
* @note History: Wed May 23 17:21:18 1990, DSJ, Created.
*/
void WriteCharDescription(const FEATURE_DEFS_STRUCT& FeatureDefs,
CHAR_DESC CharDesc, STRING* str) {
Expand Down Expand Up @@ -246,9 +232,6 @@ bool ValidCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
* @param FeatureDefs definitions of feature types/extractors
* @param File open text file to read character description from
* @return Character description read from File.
* @note Exceptions:
* - ILLEGAL_NUM_SETS
* @note History: Wed May 23 17:32:48 1990, DSJ, Created.
*/
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
FILE *File) {
Expand All @@ -257,9 +240,9 @@ CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
CHAR_DESC CharDesc;
int Type;

if (tfscanf(File, "%d", &NumSetsToRead) != 1 ||
NumSetsToRead < 0 || NumSetsToRead > FeatureDefs.NumFeatureTypes)
DoError (ILLEGAL_NUM_SETS, "Illegal number of feature sets");
ASSERT_HOST(tfscanf(File, "%d", &NumSetsToRead) == 1);
ASSERT_HOST(NumSetsToRead >= 0);
ASSERT_HOST(NumSetsToRead <= FeatureDefs.NumFeatureTypes);

CharDesc = NewCharDescription(FeatureDefs);
for (; NumSetsToRead > 0; NumSetsToRead--) {
Expand All @@ -268,10 +251,8 @@ CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
CharDesc->FeatureSets[Type] =
ReadFeatureSet (File, FeatureDefs.FeatureDesc[Type]);
}
return (CharDesc);

} // ReadCharDescription

return CharDesc;
}

/*---------------------------------------------------------------------------*/
/**
Expand All @@ -285,18 +266,12 @@ CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs,
* @param FeatureDefs definitions of feature types/extractors
* @param ShortName short name of a feature type
* @return Feature type which corresponds to ShortName.
* @note Exceptions:
* - ILLEGAL_SHORT_NAME
* @note History: Wed May 23 15:36:05 1990, DSJ, Created.
*/
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs,
const char *ShortName) {
int i;

for (i = 0; i < FeatureDefs.NumFeatureTypes; i++)
for (int i = 0; i < FeatureDefs.NumFeatureTypes; i++)
if (!strcmp ((FeatureDefs.FeatureDesc[i]->ShortName), ShortName))
return static_cast<uint32_t>(i);
DoError (ILLEGAL_SHORT_NAME, "Illegal short name for a feature");
ASSERT_HOST(!"Illegal short name for a feature");
return 0;

} // ShortNameToFeatureType
}
3 changes: 0 additions & 3 deletions src/classify/featdefs.h
Expand Up @@ -30,9 +30,6 @@ extern const char* kCNFeatureType;
extern const char* kIntFeatureType;
extern const char* kGeoFeatureType;

/* define error traps which can be triggered by this module.*/
#define ILLEGAL_SHORT_NAME 2000

/* A character is described by multiple sets of extracted features. Each
set contains a number of features of a particular type, for example, a
set of bays, or a set of closures, or a set of microfeatures. Each
Expand Down
1 change: 1 addition & 0 deletions src/classify/intproto.cpp
Expand Up @@ -28,6 +28,7 @@
#endif

#include "classify.h"
#include "callcpp.h" // for cprintf
#include "emalloc.h"
#include "fontinfo.h"
#include "genericvector.h"
Expand Down
27 changes: 9 additions & 18 deletions src/classify/ocrfeatures.cpp
Expand Up @@ -21,7 +21,6 @@
#include "ocrfeatures.h"
#include "emalloc.h"
#include "callcpp.h"
#include "danerror.h"
#include "scanutils.h"

#include <cassert>
Expand Down Expand Up @@ -121,24 +120,20 @@ FEATURE_SET NewFeatureSet(int NumFeatures) {
* @param File open text file to read feature from
* @param FeatureDesc specifies type of feature to read from File
* @return New #FEATURE read from File.
* @note Exceptions: #ILLEGAL_FEATURE_PARAM if text file doesn't match expected
* format
* @note History: Wed May 23 08:53:16 1990, DSJ, Created.
*/
FEATURE ReadFeature(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
FEATURE Feature;
int i;

Feature = NewFeature (FeatureDesc);
for (i = 0; i < Feature->Type->NumParams; i++) {
if (tfscanf(File, "%f", &(Feature->Params[i])) != 1)
DoError (ILLEGAL_FEATURE_PARAM, "Illegal feature parameter spec");
ASSERT_HOST(tfscanf(File, "%f", &(Feature->Params[i])) == 1);
#ifndef _WIN32
assert (!std::isnan(Feature->Params[i]));
#endif
}
return (Feature);
} /* ReadFeature */
return Feature;
}

/**
* Create a new feature set of the specified type and read in
Expand All @@ -149,22 +144,18 @@ FEATURE ReadFeature(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
* @param File open text file to read new feature set from
* @param FeatureDesc specifies type of feature to read from File
* @return New feature set read from File.
* @note History: Wed May 23 09:17:31 1990, DSJ, Created.
*/
FEATURE_SET ReadFeatureSet(FILE* File, const FEATURE_DESC_STRUCT* FeatureDesc) {
FEATURE_SET FeatureSet;
int NumFeatures;
int i;
ASSERT_HOST(tfscanf(File, "%d", &NumFeatures) == 1);
ASSERT_HOST(NumFeatures >= 0);

if (tfscanf(File, "%d", &NumFeatures) != 1 || NumFeatures < 0)
DoError(ILLEGAL_NUM_FEATURES, "Illegal number of features in set");

FeatureSet = NewFeatureSet(NumFeatures);
for (i = 0; i < NumFeatures; i++)
FEATURE_SET FeatureSet = NewFeatureSet(NumFeatures);
for (int i = 0; i < NumFeatures; i++)
AddFeature(FeatureSet, ReadFeature (File, FeatureDesc));

return (FeatureSet);
} /* ReadFeatureSet */
return FeatureSet;
}

/**
* Appends a textual representation of Feature to str.
Expand Down
4 changes: 0 additions & 4 deletions src/classify/ocrfeatures.h
Expand Up @@ -33,10 +33,6 @@ struct INT_FX_RESULT_STRUCT;
#undef Max
#define FEAT_NAME_SIZE 80

// define trap errors which can be caused by this module
#define ILLEGAL_FEATURE_PARAM 1000
#define ILLEGAL_NUM_FEATURES 1001

// A character is described by multiple sets of extracted features. Each
// set contains a number of features of a particular type, for example, a
// set of bays, or a set of closures, or a set of microfeatures. Each
Expand Down

0 comments on commit b0c9d5b

Please sign in to comment.