From 47715e576acddd71d3d8a1d7c2bf900476bf7897 Mon Sep 17 00:00:00 2001 From: Egor Pugin Date: Wed, 7 Apr 2021 17:10:16 +0300 Subject: [PATCH] Replace microfeatures from oldlist to std::forward_list. --- src/classify/mf.cpp | 28 ++++++++++------------- src/classify/mfdefs.cpp | 50 ----------------------------------------- src/classify/mfdefs.h | 26 +++++---------------- src/classify/mfx.cpp | 18 ++++++--------- 4 files changed, 25 insertions(+), 97 deletions(-) delete mode 100644 src/classify/mfdefs.cpp diff --git a/src/classify/mf.cpp b/src/classify/mf.cpp index fcfff6cd0..ac7e14bbb 100644 --- a/src/classify/mf.cpp +++ b/src/classify/mf.cpp @@ -39,25 +39,22 @@ namespace tesseract { * @return Micro-features for Blob. */ FEATURE_SET ExtractMicros(TBLOB *Blob, const DENORM &cn_denorm) { - int NumFeatures; - MICROFEATURES Features, OldFeatures; - MICROFEATURE OldFeature; - - OldFeatures = BlobMicroFeatures(Blob, cn_denorm); - if (OldFeatures == nullptr) { + auto features = BlobMicroFeatures(Blob, cn_denorm); + if (features.empty()) { return nullptr; } - NumFeatures = count(OldFeatures); - auto FeatureSet = new FEATURE_SET_STRUCT(NumFeatures); + int n = 0; + for (auto &f : features) { + ++n; + } + auto FeatureSet = new FEATURE_SET_STRUCT(n); - Features = OldFeatures; - iterate(Features) { - OldFeature = reinterpret_cast first_node(Features); + for (auto &f : features) { auto Feature = new FEATURE_STRUCT(&MicroFeatureDesc); - Feature->Params[MFDirection] = OldFeature[ORIENTATION]; - Feature->Params[MFXPosition] = OldFeature[XPOSITION]; - Feature->Params[MFYPosition] = OldFeature[YPOSITION]; - Feature->Params[MFLength] = OldFeature[MFLENGTH]; + Feature->Params[MFDirection] = f[ORIENTATION]; + Feature->Params[MFXPosition] = f[XPOSITION]; + Feature->Params[MFYPosition] = f[YPOSITION]; + Feature->Params[MFLength] = f[MFLENGTH]; // Bulge features are deprecated and should not be used. Set to 0. Feature->Params[MFBulge1] = 0.0f; @@ -73,7 +70,6 @@ FEATURE_SET ExtractMicros(TBLOB *Blob, const DENORM &cn_denorm) { AddFeature(FeatureSet, Feature); } - FreeMicroFeatures(OldFeatures); return FeatureSet; } /* ExtractMicros */ diff --git a/src/classify/mfdefs.cpp b/src/classify/mfdefs.cpp deleted file mode 100644 index b2b46698a..000000000 --- a/src/classify/mfdefs.cpp +++ /dev/null @@ -1,50 +0,0 @@ -/****************************************************************************** - ** Filename: mfdefs.cpp - ** Purpose: Basic routines for manipulating micro-features - ** Author: Dan Johnson - ** - ** (c) Copyright Hewlett-Packard Company, 1988. - ** Licensed under the Apache License, Version 2.0 (the "License"); - ** you may not use this file except in compliance with the License. - ** You may obtain a copy of the License at - ** http://www.apache.org/licenses/LICENSE-2.0 - ** Unless required by applicable law or agreed to in writing, software - ** distributed under the License is distributed on an "AS IS" BASIS, - ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - ** See the License for the specific language governing permissions and - ** limitations under the License. - ******************************************************************************/ - -#include "mfdefs.h" - -#include - -namespace tesseract { - -/*---------------------------------------------------------------------------- - Public Code -----------------------------------------------------------------------------**/ - -/** - * This routine allocates and returns a new micro-feature - * data structure. - * @return New MICROFEATURE - */ -MICROFEATURE NewMicroFeature() { - return new MFBLOCK; -} /* NewMicroFeature */ - -/** - * This routine deallocates all of the memory consumed by - * a list of micro-features. - * @param MicroFeatures list of micro-features to be freed - */ -void FreeMicroFeatures(MICROFEATURES MicroFeatures) { - auto list = MicroFeatures; - while (list != NIL_LIST) { - delete first_node(list); - list = pop(list); - } -} /* FreeMicroFeatures */ - -} // namespace tesseract diff --git a/src/classify/mfdefs.h b/src/classify/mfdefs.h index 6897093a3..d6a7b3fdd 100644 --- a/src/classify/mfdefs.h +++ b/src/classify/mfdefs.h @@ -24,15 +24,15 @@ #include "matchdefs.h" #include "oldlist.h" +#include +#include + namespace tesseract { /* definition of a list of micro-features */ -using MICROFEATURES = LIST; - -/* definition of structure of micro-features */ -#define MFSIZE 6 -typedef float MFBLOCK[MFSIZE]; -using MICROFEATURE = float *; +using MICROFEATURE = float; +using MFBLOCK = std::array; +using MICROFEATURES = std::forward_list; /* definitions of individual micro-feature parameters */ #define XPOSITION 0 @@ -42,20 +42,6 @@ using MICROFEATURE = float *; #define FIRSTBULGE 4 #define SECONDBULGE 5 -/**---------------------------------------------------------------------------- - Macros -----------------------------------------------------------------------------**/ - -/* macros for accessing micro-feature lists */ -#define NextFeatureOf(L) ((MICROFEATURE)first_node(L)) - -/**---------------------------------------------------------------------------- - Public Function Prototypes -----------------------------------------------------------------------------**/ -MICROFEATURE NewMicroFeature(); - -void FreeMicroFeatures(MICROFEATURES MicroFeatures); - } // namespace tesseract #endif diff --git a/src/classify/mfx.cpp b/src/classify/mfx.cpp index b1fc8a4d3..39dd1df61 100644 --- a/src/classify/mfx.cpp +++ b/src/classify/mfx.cpp @@ -36,7 +36,7 @@ double_VAR(classify_max_slope, 2.414213562, "Slope above which lines are called MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatures); -MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End); +MFBLOCK ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End); /*---------------------------------------------------------------------------- Public Code @@ -52,7 +52,7 @@ MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End); * @return List of micro-features extracted from the blob. */ MICROFEATURES BlobMicroFeatures(TBLOB *Blob, const DENORM &cn_denorm) { - auto MicroFeatures = NIL_LIST; + MICROFEATURES MicroFeatures; LIST Outlines; LIST RemainingOutlines; MFOUTLINE Outline; @@ -93,7 +93,6 @@ MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatu MFOUTLINE Current; MFOUTLINE Last; MFOUTLINE First; - MICROFEATURE NewFeature; if (DegenerateOutline(Outline)) { return (MicroFeatures); @@ -104,15 +103,13 @@ MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatu do { Current = NextExtremity(Last); if (!PointAt(Current)->Hidden) { - NewFeature = ExtractMicroFeature(Last, Current); - if (NewFeature != nullptr) { - MicroFeatures = push(MicroFeatures, NewFeature); - } + auto NewFeature = ExtractMicroFeature(Last, Current); + MicroFeatures.push_front(NewFeature); } Last = Current; } while (Last != First); - return (MicroFeatures); + return MicroFeatures; } /* ConvertToMicroFeatures */ /** @@ -128,14 +125,13 @@ MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatu * @return New micro-feature or nullptr if the feature was rejected. * @note Globals: none */ -MICROFEATURE ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End) { - MICROFEATURE NewFeature; +MFBLOCK ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End) { MFEDGEPT *P1, *P2; P1 = PointAt(Start); P2 = PointAt(End); - NewFeature = NewMicroFeature(); + MFBLOCK NewFeature; NewFeature[XPOSITION] = AverageOf(P1->Point.x, P2->Point.x); NewFeature[YPOSITION] = AverageOf(P1->Point.y, P2->Point.y); NewFeature[MFLENGTH] = DistanceBetween(P1->Point, P2->Point);