mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2024-11-24 19:19:05 +08:00
Replace malloc / free by new / delete for ADAPT_CLASS_STRUCT
Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
parent
0a0a3e1946
commit
fdf4539769
@ -38,7 +38,7 @@ namespace tesseract {
|
||||
*
|
||||
* @note Globals: none
|
||||
*/
|
||||
void AddAdaptedClass(ADAPT_TEMPLATES_STRUCT *Templates, ADAPT_CLASS Class, CLASS_ID ClassId) {
|
||||
void AddAdaptedClass(ADAPT_TEMPLATES_STRUCT *Templates, ADAPT_CLASS_STRUCT *Class, CLASS_ID ClassId) {
|
||||
assert(Templates != nullptr);
|
||||
assert(Class != nullptr);
|
||||
assert(LegalClassId(ClassId));
|
||||
@ -76,55 +76,38 @@ static void FreePermConfig(PERM_CONFIG Config) {
|
||||
free(Config);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* This operation allocates and initializes a new adapted
|
||||
* class data structure and returns a ptr to it.
|
||||
*
|
||||
* @return Ptr to new class data structure.
|
||||
*
|
||||
* @note Globals: none
|
||||
*/
|
||||
ADAPT_CLASS NewAdaptedClass() {
|
||||
ADAPT_CLASS Class;
|
||||
ADAPT_CLASS_STRUCT::ADAPT_CLASS_STRUCT() {
|
||||
NumPermConfigs = 0;
|
||||
MaxNumTimesSeen = 0;
|
||||
TempProtos = NIL_LIST;
|
||||
|
||||
Class = static_cast<ADAPT_CLASS>(malloc(sizeof(ADAPT_CLASS_STRUCT)));
|
||||
Class->NumPermConfigs = 0;
|
||||
Class->MaxNumTimesSeen = 0;
|
||||
Class->TempProtos = NIL_LIST;
|
||||
|
||||
Class->PermProtos = NewBitVector(MAX_NUM_PROTOS);
|
||||
Class->PermConfigs = NewBitVector(MAX_NUM_CONFIGS);
|
||||
zero_all_bits(Class->PermProtos, WordsInVectorOfSize(MAX_NUM_PROTOS));
|
||||
zero_all_bits(Class->PermConfigs, WordsInVectorOfSize(MAX_NUM_CONFIGS));
|
||||
PermProtos = NewBitVector(MAX_NUM_PROTOS);
|
||||
PermConfigs = NewBitVector(MAX_NUM_CONFIGS);
|
||||
zero_all_bits(PermProtos, WordsInVectorOfSize(MAX_NUM_PROTOS));
|
||||
zero_all_bits(PermConfigs, WordsInVectorOfSize(MAX_NUM_CONFIGS));
|
||||
|
||||
for (int i = 0; i < MAX_NUM_CONFIGS; i++) {
|
||||
TempConfigFor(Class, i) = nullptr;
|
||||
TempConfigFor(this, i) = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
return (Class);
|
||||
|
||||
} /* NewAdaptedClass */
|
||||
|
||||
/*-------------------------------------------------------------------------*/
|
||||
void free_adapted_class(ADAPT_CLASS adapt_class) {
|
||||
ADAPT_CLASS_STRUCT::~ADAPT_CLASS_STRUCT() {
|
||||
for (int i = 0; i < MAX_NUM_CONFIGS; i++) {
|
||||
if (ConfigIsPermanent(adapt_class, i) && PermConfigFor(adapt_class, i) != nullptr) {
|
||||
FreePermConfig(PermConfigFor(adapt_class, i));
|
||||
} else if (!ConfigIsPermanent(adapt_class, i) && TempConfigFor(adapt_class, i) != nullptr) {
|
||||
FreeTempConfig(TempConfigFor(adapt_class, i));
|
||||
if (ConfigIsPermanent(this, i) && PermConfigFor(this, i) != nullptr) {
|
||||
FreePermConfig(PermConfigFor(this, i));
|
||||
} else if (!ConfigIsPermanent(this, i) && TempConfigFor(this, i) != nullptr) {
|
||||
FreeTempConfig(TempConfigFor(this, i));
|
||||
}
|
||||
}
|
||||
FreeBitVector(adapt_class->PermProtos);
|
||||
FreeBitVector(adapt_class->PermConfigs);
|
||||
auto list = adapt_class->TempProtos;
|
||||
FreeBitVector(PermProtos);
|
||||
FreeBitVector(PermConfigs);
|
||||
auto list = TempProtos;
|
||||
while (list != nullptr) {
|
||||
if (first_node(list) != nullptr) {
|
||||
delete first_node(list);
|
||||
}
|
||||
list = pop(list);
|
||||
}
|
||||
free(adapt_class);
|
||||
}
|
||||
|
||||
/// Constructor for adapted templates.
|
||||
@ -138,20 +121,20 @@ ADAPT_TEMPLATES_STRUCT::ADAPT_TEMPLATES_STRUCT(UNICHARSET &unicharset) {
|
||||
for (int i = 0; i < MAX_NUM_CLASSES; i++) {
|
||||
Class[i] = nullptr;
|
||||
if (i < unicharset.size()) {
|
||||
AddAdaptedClass(this, NewAdaptedClass(), i);
|
||||
AddAdaptedClass(this, new ADAPT_CLASS_STRUCT, i);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ADAPT_TEMPLATES_STRUCT::~ADAPT_TEMPLATES_STRUCT() {
|
||||
for (int i = 0; i < (Templates)->NumClasses; i++) {
|
||||
free_adapted_class(Class[i]);
|
||||
delete Class[i];
|
||||
}
|
||||
delete Templates;
|
||||
}
|
||||
|
||||
// Returns FontinfoId of the given config of the given adapted class.
|
||||
int Classify::GetFontinfoId(ADAPT_CLASS Class, uint8_t ConfigId) {
|
||||
int Classify::GetFontinfoId(ADAPT_CLASS_STRUCT *Class, uint8_t ConfigId) {
|
||||
return (ConfigIsPermanent(Class, ConfigId) ? PermConfigFor(Class, ConfigId)->FontinfoId
|
||||
: TempConfigFor(Class, ConfigId)->FontinfoId);
|
||||
}
|
||||
@ -194,7 +177,7 @@ TEMP_CONFIG NewTempConfig(int MaxProtoId, int FontinfoId) {
|
||||
*/
|
||||
void Classify::PrintAdaptedTemplates(FILE *File, ADAPT_TEMPLATES_STRUCT *Templates) {
|
||||
INT_CLASS_STRUCT *IClass;
|
||||
ADAPT_CLASS AClass;
|
||||
ADAPT_CLASS_STRUCT *AClass;
|
||||
|
||||
fprintf(File, "\n\nSUMMARY OF ADAPTED TEMPLATES:\n\n");
|
||||
fprintf(File, "Num classes = %d; Num permanent classes = %d\n\n", Templates->NumNonEmptyClasses,
|
||||
@ -225,14 +208,14 @@ void Classify::PrintAdaptedTemplates(FILE *File, ADAPT_TEMPLATES_STRUCT *Templat
|
||||
*
|
||||
* @note Globals: none
|
||||
*/
|
||||
ADAPT_CLASS ReadAdaptedClass(TFile *fp) {
|
||||
ADAPT_CLASS_STRUCT *ReadAdaptedClass(TFile *fp) {
|
||||
int NumTempProtos;
|
||||
int NumConfigs;
|
||||
int i;
|
||||
ADAPT_CLASS Class;
|
||||
ADAPT_CLASS_STRUCT *Class;
|
||||
|
||||
/* first read high level adapted class structure */
|
||||
Class = static_cast<ADAPT_CLASS>(malloc(sizeof(ADAPT_CLASS_STRUCT)));
|
||||
Class = new ADAPT_CLASS_STRUCT;
|
||||
fp->FRead(Class, sizeof(ADAPT_CLASS_STRUCT), 1);
|
||||
|
||||
/* then read in the definitions of the permanent protos and configs */
|
||||
@ -346,7 +329,7 @@ TEMP_CONFIG ReadTempConfig(TFile *fp) {
|
||||
*
|
||||
* @note Globals: none
|
||||
*/
|
||||
void WriteAdaptedClass(FILE *File, ADAPT_CLASS Class, int NumConfigs) {
|
||||
void WriteAdaptedClass(FILE *File, ADAPT_CLASS_STRUCT *Class, int NumConfigs) {
|
||||
int NumTempProtos;
|
||||
LIST TempProtos;
|
||||
int i;
|
||||
|
@ -50,6 +50,8 @@ union ADAPTED_CONFIG {
|
||||
};
|
||||
|
||||
struct ADAPT_CLASS_STRUCT {
|
||||
ADAPT_CLASS_STRUCT();
|
||||
~ADAPT_CLASS_STRUCT();
|
||||
uint8_t NumPermConfigs;
|
||||
uint8_t MaxNumTimesSeen; // maximum number of times any TEMP_CONFIG was seen
|
||||
// (cut at matcher_min_examples_for_prototyping)
|
||||
@ -58,7 +60,6 @@ struct ADAPT_CLASS_STRUCT {
|
||||
LIST TempProtos;
|
||||
ADAPTED_CONFIG Config[MAX_NUM_CONFIGS];
|
||||
};
|
||||
using ADAPT_CLASS = ADAPT_CLASS_STRUCT *;
|
||||
|
||||
class ADAPT_TEMPLATES_STRUCT {
|
||||
public:
|
||||
@ -68,7 +69,7 @@ public:
|
||||
INT_TEMPLATES_STRUCT *Templates;
|
||||
int NumNonEmptyClasses;
|
||||
uint8_t NumPermClasses;
|
||||
ADAPT_CLASS Class[MAX_NUM_CLASSES];
|
||||
ADAPT_CLASS_STRUCT *Class[MAX_NUM_CLASSES];
|
||||
};
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
@ -90,23 +91,19 @@ public:
|
||||
|
||||
#define IncreaseConfidence(TempConfig) ((TempConfig)->NumTimesSeen++)
|
||||
|
||||
void AddAdaptedClass(ADAPT_TEMPLATES_STRUCT *Templates, ADAPT_CLASS Class, CLASS_ID ClassId);
|
||||
void AddAdaptedClass(ADAPT_TEMPLATES_STRUCT *Templates, ADAPT_CLASS_STRUCT *Class, CLASS_ID ClassId);
|
||||
|
||||
void FreeTempConfig(TEMP_CONFIG Config);
|
||||
|
||||
ADAPT_CLASS NewAdaptedClass();
|
||||
|
||||
void free_adapted_class(ADAPT_CLASS adapt_class);
|
||||
|
||||
TEMP_CONFIG NewTempConfig(int MaxProtoId, int FontinfoId);
|
||||
|
||||
ADAPT_CLASS ReadAdaptedClass(tesseract::TFile *File);
|
||||
ADAPT_CLASS_STRUCT *ReadAdaptedClass(tesseract::TFile *File);
|
||||
|
||||
PERM_CONFIG ReadPermConfig(tesseract::TFile *File);
|
||||
|
||||
TEMP_CONFIG ReadTempConfig(tesseract::TFile *File);
|
||||
|
||||
void WriteAdaptedClass(FILE *File, ADAPT_CLASS Class, int NumConfigs);
|
||||
void WriteAdaptedClass(FILE *File, ADAPT_CLASS_STRUCT *Class, int NumConfigs);
|
||||
|
||||
void WritePermConfig(FILE *File, PERM_CONFIG Config);
|
||||
|
||||
|
@ -679,7 +679,7 @@ void Classify::SettupPass2() {
|
||||
* - BaselineCutoffs kludge needed to get cutoffs
|
||||
* - #PreTrainedTemplates kludge needed to get cutoffs
|
||||
*/
|
||||
void Classify::InitAdaptedClass(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, ADAPT_CLASS Class,
|
||||
void Classify::InitAdaptedClass(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, ADAPT_CLASS_STRUCT *Class,
|
||||
ADAPT_TEMPLATES_STRUCT *Templates) {
|
||||
FEATURE_SET Features;
|
||||
int Fid, Pid;
|
||||
@ -842,7 +842,7 @@ void Classify::AdaptToChar(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, float
|
||||
INT_FEATURE_ARRAY IntFeatures;
|
||||
UnicharRating int_result;
|
||||
INT_CLASS_STRUCT *IClass;
|
||||
ADAPT_CLASS Class;
|
||||
ADAPT_CLASS_STRUCT *Class;
|
||||
TEMP_CONFIG TempConfig;
|
||||
FEATURE_SET FloatFeatures;
|
||||
int NewTempConfigId;
|
||||
@ -1032,7 +1032,7 @@ void Classify::AddNewResult(const UnicharRating &new_result, ADAPT_RESULTS *resu
|
||||
*/
|
||||
void Classify::AmbigClassifier(const std::vector<INT_FEATURE_STRUCT> &int_features,
|
||||
const INT_FX_RESULT_STRUCT &fx_info, const TBLOB *blob,
|
||||
INT_TEMPLATES_STRUCT *templates, ADAPT_CLASS *classes,
|
||||
INT_TEMPLATES_STRUCT *templates, ADAPT_CLASS_STRUCT **classes,
|
||||
UNICHAR_ID *ambiguities, ADAPT_RESULTS *results) {
|
||||
if (int_features.empty()) {
|
||||
return;
|
||||
@ -1069,7 +1069,7 @@ void Classify::AmbigClassifier(const std::vector<INT_FEATURE_STRUCT> &int_featur
|
||||
/// Returns integer matcher results inside CLASS_PRUNER_RESULTS structure.
|
||||
void Classify::MasterMatcher(INT_TEMPLATES_STRUCT *templates, int16_t num_features,
|
||||
const INT_FEATURE_STRUCT *features, const uint8_t *norm_factors,
|
||||
ADAPT_CLASS *classes, int debug, int matcher_multiplier,
|
||||
ADAPT_CLASS_STRUCT **classes, int debug, int matcher_multiplier,
|
||||
const TBOX &blob_box, const std::vector<CP_RESULT_STRUCT> &results,
|
||||
ADAPT_RESULTS *final_results) {
|
||||
int top = blob_box.top();
|
||||
@ -1095,7 +1095,7 @@ void Classify::MasterMatcher(INT_TEMPLATES_STRUCT *templates, int16_t num_featur
|
||||
// unichar_ids represented, before applying a set of corrections to the
|
||||
// distance rating in int_result, (see ComputeCorrectedRating.)
|
||||
// The results are added to the final_results output.
|
||||
void Classify::ExpandShapesAndApplyCorrections(ADAPT_CLASS *classes, bool debug, int class_id,
|
||||
void Classify::ExpandShapesAndApplyCorrections(ADAPT_CLASS_STRUCT **classes, bool debug, int class_id,
|
||||
int bottom, int top, float cp_rating,
|
||||
int blob_length, int matcher_multiplier,
|
||||
const uint8_t *cn_factors, UnicharRating *int_result,
|
||||
@ -1669,7 +1669,7 @@ int Classify::MakeNewTemporaryConfig(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID
|
||||
int NumFeatures, INT_FEATURE_ARRAY Features,
|
||||
FEATURE_SET FloatFeatures) {
|
||||
INT_CLASS_STRUCT *IClass;
|
||||
ADAPT_CLASS Class;
|
||||
ADAPT_CLASS_STRUCT *Class;
|
||||
PROTO_ID OldProtos[MAX_NUM_PROTOS];
|
||||
FEATURE_ID BadFeatures[MAX_NUM_INT_FEATURES];
|
||||
int NumOldProtos;
|
||||
@ -1756,7 +1756,7 @@ int Classify::MakeNewTemporaryConfig(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID
|
||||
* @return Max proto id in class after all protos have been added.
|
||||
*/
|
||||
PROTO_ID Classify::MakeNewTempProtos(FEATURE_SET Features, int NumBadFeat, FEATURE_ID BadFeat[],
|
||||
INT_CLASS_STRUCT *IClass, ADAPT_CLASS Class,
|
||||
INT_CLASS_STRUCT *IClass, ADAPT_CLASS_STRUCT *Class,
|
||||
BIT_VECTOR TempProtoMask) {
|
||||
FEATURE_ID *ProtoStart;
|
||||
FEATURE_ID *ProtoEnd;
|
||||
@ -1840,7 +1840,7 @@ void Classify::MakePermanent(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID ClassId
|
||||
TBLOB *Blob) {
|
||||
UNICHAR_ID *Ambigs;
|
||||
TEMP_CONFIG Config;
|
||||
ADAPT_CLASS Class;
|
||||
ADAPT_CLASS_STRUCT *Class;
|
||||
PROTO_KEY ProtoKey;
|
||||
|
||||
Class = Templates->Class[ClassId];
|
||||
@ -1896,7 +1896,7 @@ void Classify::MakePermanent(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID ClassId
|
||||
* @return true if TempProto is converted, false otherwise
|
||||
*/
|
||||
int MakeTempProtoPerm(void *item1, void *item2) {
|
||||
ADAPT_CLASS Class;
|
||||
ADAPT_CLASS_STRUCT *Class;
|
||||
TEMP_CONFIG Config;
|
||||
TEMP_PROTO_STRUCT *TempProto;
|
||||
PROTO_KEY *ProtoKey;
|
||||
@ -2158,7 +2158,7 @@ bool Classify::TempConfigReliable(CLASS_ID class_id, const TEMP_CONFIG &config)
|
||||
const UnicharIdVector *ambigs = getDict().getUnicharAmbigs().AmbigsForAdaption(class_id);
|
||||
int ambigs_size = (ambigs == nullptr) ? 0 : ambigs->size();
|
||||
for (int ambig = 0; ambig < ambigs_size; ++ambig) {
|
||||
ADAPT_CLASS ambig_class = AdaptedTemplates->Class[(*ambigs)[ambig]];
|
||||
ADAPT_CLASS_STRUCT *ambig_class = AdaptedTemplates->Class[(*ambigs)[ambig]];
|
||||
assert(ambig_class != nullptr);
|
||||
if (ambig_class->NumPermConfigs == 0 &&
|
||||
ambig_class->MaxNumTimesSeen < matcher_min_examples_for_prototyping) {
|
||||
@ -2185,7 +2185,7 @@ void Classify::UpdateAmbigsGroup(CLASS_ID class_id, TBLOB *Blob) {
|
||||
}
|
||||
for (int ambig = 0; ambig < ambigs_size; ++ambig) {
|
||||
CLASS_ID ambig_class_id = (*ambigs)[ambig];
|
||||
const ADAPT_CLASS ambigs_class = AdaptedTemplates->Class[ambig_class_id];
|
||||
const ADAPT_CLASS_STRUCT *ambigs_class = AdaptedTemplates->Class[ambig_class_id];
|
||||
for (int cfg = 0; cfg < MAX_NUM_CONFIGS; ++cfg) {
|
||||
if (ConfigIsPermanent(ambigs_class, cfg)) {
|
||||
continue;
|
||||
|
@ -119,7 +119,7 @@ public:
|
||||
bool LargeSpeckle(const TBLOB &blob);
|
||||
|
||||
/* adaptive.cpp ************************************************************/
|
||||
int GetFontinfoId(ADAPT_CLASS Class, uint8_t ConfigId);
|
||||
int GetFontinfoId(ADAPT_CLASS_STRUCT *Class, uint8_t ConfigId);
|
||||
// Runs the class pruner from int_templates on the given features, returning
|
||||
// the number of classes output in results.
|
||||
// int_templates Class pruner tables
|
||||
@ -169,22 +169,22 @@ public:
|
||||
void LearnPieces(const char *fontname, int start, int length, float threshold,
|
||||
CharSegmentationType segmentation, const char *correct_text, WERD_RES *word);
|
||||
void InitAdaptiveClassifier(TessdataManager *mgr);
|
||||
void InitAdaptedClass(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, ADAPT_CLASS Class,
|
||||
void InitAdaptedClass(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, ADAPT_CLASS_STRUCT *Class,
|
||||
ADAPT_TEMPLATES_STRUCT *Templates);
|
||||
void AmbigClassifier(const std::vector<INT_FEATURE_STRUCT> &int_features,
|
||||
const INT_FX_RESULT_STRUCT &fx_info, const TBLOB *blob,
|
||||
INT_TEMPLATES_STRUCT *templates, ADAPT_CLASS *classes, UNICHAR_ID *ambiguities,
|
||||
INT_TEMPLATES_STRUCT *templates, ADAPT_CLASS_STRUCT **classes, UNICHAR_ID *ambiguities,
|
||||
ADAPT_RESULTS *results);
|
||||
void MasterMatcher(INT_TEMPLATES_STRUCT *templates, int16_t num_features,
|
||||
const INT_FEATURE_STRUCT *features, const uint8_t *norm_factors,
|
||||
ADAPT_CLASS *classes, int debug, int matcher_multiplier, const TBOX &blob_box,
|
||||
ADAPT_CLASS_STRUCT **classes, int debug, int matcher_multiplier, const TBOX &blob_box,
|
||||
const std::vector<CP_RESULT_STRUCT> &results, ADAPT_RESULTS *final_results);
|
||||
// Converts configs to fonts, and if the result is not adapted, and a
|
||||
// shape_table_ is present, the shape is expanded to include all
|
||||
// unichar_ids represented, before applying a set of corrections to the
|
||||
// distance rating in int_result, (see ComputeCorrectedRating.)
|
||||
// The results are added to the final_results output.
|
||||
void ExpandShapesAndApplyCorrections(ADAPT_CLASS *classes, bool debug, int class_id, int bottom,
|
||||
void ExpandShapesAndApplyCorrections(ADAPT_CLASS_STRUCT **classes, bool debug, int class_id, int bottom,
|
||||
int top, float cp_rating, int blob_length,
|
||||
int matcher_multiplier, const uint8_t *cn_factors,
|
||||
UnicharRating *int_result, ADAPT_RESULTS *final_results);
|
||||
@ -203,7 +203,7 @@ public:
|
||||
void DebugAdaptiveClassifier(TBLOB *Blob, ADAPT_RESULTS *Results);
|
||||
# endif
|
||||
PROTO_ID MakeNewTempProtos(FEATURE_SET Features, int NumBadFeat, FEATURE_ID BadFeat[],
|
||||
INT_CLASS_STRUCT *IClass, ADAPT_CLASS Class, BIT_VECTOR TempProtoMask);
|
||||
INT_CLASS_STRUCT *IClass, ADAPT_CLASS_STRUCT *Class, BIT_VECTOR TempProtoMask);
|
||||
int MakeNewTemporaryConfig(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID ClassId, int FontinfoId,
|
||||
int NumFeatures, INT_FEATURE_ARRAY Features,
|
||||
FEATURE_SET FloatFeatures);
|
||||
|
Loading…
Reference in New Issue
Block a user