mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2024-11-24 11:09:06 +08:00
Replace malloc / free by new / delete for TEMP_CONFIG_STRUCT
Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
parent
fdf4539769
commit
92359a4a11
@ -53,21 +53,6 @@ void AddAdaptedClass(ADAPT_TEMPLATES_STRUCT *Templates, ADAPT_CLASS_STRUCT *Clas
|
||||
|
||||
} /* AddAdaptedClass */
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* This routine frees all memory consumed by a temporary
|
||||
* configuration.
|
||||
*
|
||||
* @param Config config to be freed
|
||||
*
|
||||
* @note Globals: none
|
||||
*/
|
||||
void FreeTempConfig(TEMP_CONFIG Config) {
|
||||
assert(Config != nullptr);
|
||||
FreeBitVector(Config->Protos);
|
||||
free(Config);
|
||||
} /* FreeTempConfig */
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
|
||||
static void FreePermConfig(PERM_CONFIG Config) {
|
||||
@ -96,7 +81,7 @@ ADAPT_CLASS_STRUCT::~ADAPT_CLASS_STRUCT() {
|
||||
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));
|
||||
delete TempConfigFor(this, i);
|
||||
}
|
||||
}
|
||||
FreeBitVector(PermProtos);
|
||||
@ -139,31 +124,25 @@ int Classify::GetFontinfoId(ADAPT_CLASS_STRUCT *Class, uint8_t ConfigId) {
|
||||
: TempConfigFor(Class, ConfigId)->FontinfoId);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
* This routine allocates and returns a new temporary config.
|
||||
*
|
||||
* @param MaxProtoId max id of any proto in new config
|
||||
* @param FontinfoId font information from pre-trained templates
|
||||
* @return Ptr to new temp config.
|
||||
*
|
||||
* @note Globals: none
|
||||
*/
|
||||
TEMP_CONFIG NewTempConfig(int MaxProtoId, int FontinfoId) {
|
||||
int NumProtos = MaxProtoId + 1;
|
||||
/// This constructor allocates and returns a new temporary config.
|
||||
///
|
||||
/// @param MaxProtoId max id of any proto in new config
|
||||
/// @param FontinfoId font information from pre-trained templates
|
||||
TEMP_CONFIG_STRUCT::TEMP_CONFIG_STRUCT(int maxProtoId, int fontinfoId) {
|
||||
int NumProtos = maxProtoId + 1;
|
||||
|
||||
auto Config = static_cast<TEMP_CONFIG>(malloc(sizeof(TEMP_CONFIG_STRUCT)));
|
||||
Config->Protos = NewBitVector(NumProtos);
|
||||
Protos = NewBitVector(NumProtos);
|
||||
|
||||
Config->NumTimesSeen = 1;
|
||||
Config->MaxProtoId = MaxProtoId;
|
||||
Config->ProtoVectorSize = WordsInVectorOfSize(NumProtos);
|
||||
zero_all_bits(Config->Protos, Config->ProtoVectorSize);
|
||||
Config->FontinfoId = FontinfoId;
|
||||
NumTimesSeen = 1;
|
||||
MaxProtoId = maxProtoId;
|
||||
ProtoVectorSize = WordsInVectorOfSize(NumProtos);
|
||||
zero_all_bits(Protos, ProtoVectorSize);
|
||||
FontinfoId = fontinfoId;
|
||||
}
|
||||
|
||||
return (Config);
|
||||
|
||||
} /* NewTempConfig */
|
||||
TEMP_CONFIG_STRUCT::~TEMP_CONFIG_STRUCT() {
|
||||
FreeBitVector(Protos);
|
||||
}
|
||||
|
||||
/*---------------------------------------------------------------------------*/
|
||||
/**
|
||||
@ -307,8 +286,8 @@ PERM_CONFIG ReadPermConfig(TFile *fp) {
|
||||
*
|
||||
* @note Globals: none
|
||||
*/
|
||||
TEMP_CONFIG ReadTempConfig(TFile *fp) {
|
||||
auto Config = static_cast<TEMP_CONFIG>(malloc(sizeof(TEMP_CONFIG_STRUCT)));
|
||||
TEMP_CONFIG_STRUCT *ReadTempConfig(TFile *fp) {
|
||||
auto Config = new TEMP_CONFIG_STRUCT;
|
||||
fp->FRead(Config, sizeof(TEMP_CONFIG_STRUCT), 1);
|
||||
|
||||
Config->Protos = NewBitVector(Config->ProtoVectorSize * BITSINLONG);
|
||||
@ -419,7 +398,7 @@ void WritePermConfig(FILE *File, PERM_CONFIG Config) {
|
||||
*
|
||||
* @note Globals: none
|
||||
*/
|
||||
void WriteTempConfig(FILE *File, TEMP_CONFIG Config) {
|
||||
void WriteTempConfig(FILE *File, TEMP_CONFIG_STRUCT *Config) {
|
||||
assert(Config != nullptr);
|
||||
|
||||
fwrite(Config, sizeof(TEMP_CONFIG_STRUCT), 1, File);
|
||||
|
@ -30,13 +30,15 @@ struct TEMP_PROTO_STRUCT {
|
||||
};
|
||||
|
||||
struct TEMP_CONFIG_STRUCT {
|
||||
TEMP_CONFIG_STRUCT() = default;
|
||||
TEMP_CONFIG_STRUCT(int MaxProtoId, int FontinfoId);
|
||||
~TEMP_CONFIG_STRUCT();
|
||||
uint8_t NumTimesSeen;
|
||||
uint8_t ProtoVectorSize;
|
||||
PROTO_ID MaxProtoId;
|
||||
BIT_VECTOR Protos;
|
||||
int FontinfoId; // font information inferred from pre-trained templates
|
||||
};
|
||||
using TEMP_CONFIG = TEMP_CONFIG_STRUCT *;
|
||||
|
||||
struct PERM_CONFIG_STRUCT {
|
||||
UNICHAR_ID *Ambigs;
|
||||
@ -45,7 +47,7 @@ struct PERM_CONFIG_STRUCT {
|
||||
using PERM_CONFIG = PERM_CONFIG_STRUCT *;
|
||||
|
||||
union ADAPTED_CONFIG {
|
||||
TEMP_CONFIG Temp;
|
||||
TEMP_CONFIG_STRUCT *Temp;
|
||||
PERM_CONFIG Perm;
|
||||
};
|
||||
|
||||
@ -53,7 +55,7 @@ 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
|
||||
uint8_t MaxNumTimesSeen; // maximum number of times any TEMP_CONFIG_STRUCT was seen
|
||||
// (cut at matcher_min_examples_for_prototyping)
|
||||
BIT_VECTOR PermProtos;
|
||||
BIT_VECTOR PermConfigs;
|
||||
@ -93,21 +95,17 @@ public:
|
||||
|
||||
void AddAdaptedClass(ADAPT_TEMPLATES_STRUCT *Templates, ADAPT_CLASS_STRUCT *Class, CLASS_ID ClassId);
|
||||
|
||||
void FreeTempConfig(TEMP_CONFIG Config);
|
||||
|
||||
TEMP_CONFIG NewTempConfig(int MaxProtoId, int FontinfoId);
|
||||
|
||||
ADAPT_CLASS_STRUCT *ReadAdaptedClass(tesseract::TFile *File);
|
||||
|
||||
PERM_CONFIG ReadPermConfig(tesseract::TFile *File);
|
||||
|
||||
TEMP_CONFIG ReadTempConfig(tesseract::TFile *File);
|
||||
TEMP_CONFIG_STRUCT *ReadTempConfig(tesseract::TFile *File);
|
||||
|
||||
void WriteAdaptedClass(FILE *File, ADAPT_CLASS_STRUCT *Class, int NumConfigs);
|
||||
|
||||
void WritePermConfig(FILE *File, PERM_CONFIG Config);
|
||||
|
||||
void WriteTempConfig(FILE *File, TEMP_CONFIG Config);
|
||||
void WriteTempConfig(FILE *File, TEMP_CONFIG_STRUCT *Config);
|
||||
|
||||
} // namespace tesseract
|
||||
|
||||
|
@ -687,7 +687,7 @@ void Classify::InitAdaptedClass(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, A
|
||||
int NumFeatures;
|
||||
PROTO_STRUCT *Proto;
|
||||
INT_CLASS_STRUCT *IClass;
|
||||
TEMP_CONFIG Config;
|
||||
TEMP_CONFIG_STRUCT *Config;
|
||||
|
||||
classify_norm_method.set_value(baseline);
|
||||
Features = ExtractOutlineFeatures(Blob);
|
||||
@ -697,7 +697,7 @@ void Classify::InitAdaptedClass(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, A
|
||||
return;
|
||||
}
|
||||
|
||||
Config = NewTempConfig(NumFeatures - 1, FontinfoId);
|
||||
Config = new TEMP_CONFIG_STRUCT(NumFeatures - 1, FontinfoId);
|
||||
TempConfigFor(Class, 0) = Config;
|
||||
|
||||
/* this is a kludge to construct cutoffs for adapted templates */
|
||||
@ -843,7 +843,7 @@ void Classify::AdaptToChar(TBLOB *Blob, CLASS_ID ClassId, int FontinfoId, float
|
||||
UnicharRating int_result;
|
||||
INT_CLASS_STRUCT *IClass;
|
||||
ADAPT_CLASS_STRUCT *Class;
|
||||
TEMP_CONFIG TempConfig;
|
||||
TEMP_CONFIG_STRUCT *TempConfig;
|
||||
FEATURE_SET FloatFeatures;
|
||||
int NewTempConfigId;
|
||||
|
||||
@ -1677,7 +1677,6 @@ int Classify::MakeNewTemporaryConfig(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID
|
||||
int MaxProtoId, OldMaxProtoId;
|
||||
int MaskSize;
|
||||
int ConfigId;
|
||||
TEMP_CONFIG Config;
|
||||
int i;
|
||||
int debug_level = NO_DEBUG;
|
||||
|
||||
@ -1722,7 +1721,7 @@ int Classify::MakeNewTemporaryConfig(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID
|
||||
|
||||
ConfigId = AddIntConfig(IClass);
|
||||
ConvertConfig(TempProtoMask, ConfigId, IClass);
|
||||
Config = NewTempConfig(MaxProtoId, FontinfoId);
|
||||
auto Config = new TEMP_CONFIG_STRUCT(MaxProtoId, FontinfoId);
|
||||
TempConfigFor(Class, ConfigId) = Config;
|
||||
copy_all_bits(TempProtoMask, Config->Protos, Config->ProtoVectorSize);
|
||||
|
||||
@ -1839,12 +1838,10 @@ PROTO_ID Classify::MakeNewTempProtos(FEATURE_SET Features, int NumBadFeat, FEATU
|
||||
void Classify::MakePermanent(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID ClassId, int ConfigId,
|
||||
TBLOB *Blob) {
|
||||
UNICHAR_ID *Ambigs;
|
||||
TEMP_CONFIG Config;
|
||||
ADAPT_CLASS_STRUCT *Class;
|
||||
PROTO_KEY ProtoKey;
|
||||
|
||||
Class = Templates->Class[ClassId];
|
||||
Config = TempConfigFor(Class, ConfigId);
|
||||
auto Class = Templates->Class[ClassId];
|
||||
auto Config = TempConfigFor(Class, ConfigId);
|
||||
|
||||
MakeConfigPermanent(Class, ConfigId);
|
||||
if (Class->NumPermConfigs == 0) {
|
||||
@ -1864,7 +1861,7 @@ void Classify::MakePermanent(ADAPT_TEMPLATES_STRUCT *Templates, CLASS_ID ClassId
|
||||
ProtoKey.ClassId = ClassId;
|
||||
ProtoKey.ConfigId = ConfigId;
|
||||
Class->TempProtos = delete_d(Class->TempProtos, &ProtoKey, MakeTempProtoPerm);
|
||||
FreeTempConfig(Config);
|
||||
delete Config;
|
||||
|
||||
// Record permanent config.
|
||||
PermConfigFor(Class, ConfigId) = Perm;
|
||||
@ -1896,16 +1893,11 @@ 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_STRUCT *Class;
|
||||
TEMP_CONFIG Config;
|
||||
TEMP_PROTO_STRUCT *TempProto;
|
||||
PROTO_KEY *ProtoKey;
|
||||
auto TempProto = static_cast<TEMP_PROTO_STRUCT *>(item1);
|
||||
auto ProtoKey = static_cast<PROTO_KEY *>(item2);
|
||||
|
||||
TempProto = static_cast<TEMP_PROTO_STRUCT *>(item1);
|
||||
ProtoKey = static_cast<PROTO_KEY *>(item2);
|
||||
|
||||
Class = ProtoKey->Templates->Class[ProtoKey->ClassId];
|
||||
Config = TempConfigFor(Class, ProtoKey->ConfigId);
|
||||
auto Class = ProtoKey->Templates->Class[ProtoKey->ClassId];
|
||||
auto Config = TempConfigFor(Class, ProtoKey->ConfigId);
|
||||
|
||||
if (TempProto->ProtoId > Config->MaxProtoId || !test_bit(Config->Protos, TempProto->ProtoId)) {
|
||||
return false;
|
||||
@ -2141,9 +2133,9 @@ int Classify::ShapeIDToClassID(int shape_id) const {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Returns true if the given TEMP_CONFIG is good enough to make it
|
||||
// Returns true if the given TEMP_CONFIG_STRUCT is good enough to make it
|
||||
// a permanent config.
|
||||
bool Classify::TempConfigReliable(CLASS_ID class_id, const TEMP_CONFIG &config) {
|
||||
bool Classify::TempConfigReliable(CLASS_ID class_id, const TEMP_CONFIG_STRUCT *config) {
|
||||
if (classify_learning_debug_level >= 1) {
|
||||
tprintf("NumTimesSeen for config of %s is %d\n",
|
||||
getDict().getUnicharset().debug_str(class_id).c_str(), config->NumTimesSeen);
|
||||
@ -2190,7 +2182,7 @@ void Classify::UpdateAmbigsGroup(CLASS_ID class_id, TBLOB *Blob) {
|
||||
if (ConfigIsPermanent(ambigs_class, cfg)) {
|
||||
continue;
|
||||
}
|
||||
const TEMP_CONFIG config = TempConfigFor(AdaptedTemplates->Class[ambig_class_id], cfg);
|
||||
const TEMP_CONFIG_STRUCT *config = TempConfigFor(AdaptedTemplates->Class[ambig_class_id], cfg);
|
||||
if (config != nullptr && TempConfigReliable(ambig_class_id, config)) {
|
||||
if (classify_learning_debug_level >= 1) {
|
||||
tprintf("Making config %d of %s permanent\n", cfg,
|
||||
|
@ -263,7 +263,7 @@ public:
|
||||
void ComputeCharNormArrays(FEATURE_STRUCT *norm_feature, INT_TEMPLATES_STRUCT *templates,
|
||||
uint8_t *char_norm_array, uint8_t *pruner_array);
|
||||
|
||||
bool TempConfigReliable(CLASS_ID class_id, const TEMP_CONFIG &config);
|
||||
bool TempConfigReliable(CLASS_ID class_id, const TEMP_CONFIG_STRUCT *config);
|
||||
void UpdateAmbigsGroup(CLASS_ID class_id, TBLOB *Blob);
|
||||
|
||||
bool AdaptiveClassifierIsFull() const {
|
||||
|
Loading…
Reference in New Issue
Block a user