[edgblob] Replace unique ptr with vector. Fix possible index issues.

Closes #1921.
This commit is contained in:
Egor Pugin 2021-04-12 01:17:57 +03:00
parent cca46e6b29
commit 57c90eee02
2 changed files with 35 additions and 27 deletions

View File

@ -23,11 +23,13 @@
#include "edgblob.h"
#include <memory>
#include "edgloop.h"
#include "scanedg.h"
#include <memory>
#define BUCKETSIZE 16
namespace tesseract {
// Control parameters used in outline_complexity(), which rejects an outline
@ -58,12 +60,11 @@ static double_VAR(edges_boxarea, 0.875, "Min area fraction of grandchild for box
OL_BUCKETS::OL_BUCKETS(ICOORD bleft, // corners
ICOORD tright)
: bl(bleft), tr(tright) {
bxdim = (tright.x() - bleft.x()) / BUCKETSIZE + 1;
bydim = (tright.y() - bleft.y()) / BUCKETSIZE + 1;
// make array
buckets = std::make_unique<C_OUTLINE_LIST[]>(bxdim * bydim);
index = 0;
: bxdim((tright.x() - bleft.x()) / BUCKETSIZE + 1),
bydim((tright.y() - bleft.y()) / BUCKETSIZE + 1),
buckets(bxdim * bydim),
bl(bleft),
tr(tright) {
}
/**
@ -79,6 +80,21 @@ C_OUTLINE_LIST *OL_BUCKETS::operator()( // array access
return &buckets[(y - bl.y()) / BUCKETSIZE * bxdim + (x - bl.x()) / BUCKETSIZE];
}
C_OUTLINE_LIST *OL_BUCKETS::start_scan() {
return scan_next(buckets.begin());
}
C_OUTLINE_LIST *OL_BUCKETS::scan_next() {
return scan_next(it);
}
C_OUTLINE_LIST *OL_BUCKETS::scan_next(decltype(buckets)::iterator in_it) {
it = std::find_if(in_it, buckets.end(), [](auto &&b) { return !b.empty(); });
if (it == buckets.end())
return nullptr;
return &*it;
}
/**
* @name OL_BUCKETS::outline_complexity
*
@ -408,7 +424,10 @@ void empty_buckets( // find blobs
good_blob = capture_children(buckets, &junk_blobs, &out_it);
C_BLOB::ConstructBlobsFromOutlines(good_blob, &outlines, &good_blobs, &junk_blobs);
bucket_it.set_to_list(buckets->scan_next());
if (auto l = buckets->scan_next())
bucket_it.set_to_list(l);
else
break;
}
}

View File

@ -26,36 +26,23 @@
#include "scrollview.h"
#include <memory>
#include <vector>
namespace tesseract {
#define BUCKETSIZE 16
class OL_BUCKETS {
public:
OL_BUCKETS( // constructor
ICOORD bleft, // corners
ICOORD tright);
~OL_BUCKETS() = default;
C_OUTLINE_LIST *operator()( // array access
int16_t x, // image coords
int16_t y);
// first non-empty bucket
C_OUTLINE_LIST *start_scan() {
for (index = 0; buckets[index].empty() && index < bxdim * bydim - 1; index++) {
;
}
return &buckets[index];
}
C_OUTLINE_LIST *start_scan();
// next non-empty bucket
C_OUTLINE_LIST *scan_next() {
for (; buckets[index].empty() && index < bxdim * bydim - 1; index++) {
;
}
return &buckets[index];
}
C_OUTLINE_LIST *scan_next();
int32_t count_children( // recursive sum
C_OUTLINE *outline, // parent outline
int32_t max_count); // max output
@ -68,12 +55,14 @@ public:
C_OUTLINE_IT *it); // destination iterator
private:
std::unique_ptr<C_OUTLINE_LIST[]> buckets; // array of buckets
int16_t bxdim; // size of array
int16_t bydim;
std::vector<C_OUTLINE_LIST> buckets; // array of buckets
ICOORD bl; // corners
ICOORD tr;
int32_t index; // for extraction scan
decltype(buckets)::iterator it; // for extraction scan
C_OUTLINE_LIST *scan_next(decltype(buckets)::iterator it);
};
void extract_edges(Image pix, // thresholded image