From 27dfc3354ebc4ddf291f191e577b079a45f5323b Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Tue, 29 Jun 2021 17:48:06 +0200 Subject: [PATCH] Fix vector resize with init for all elements (issue #3473) Fixes: 9710bc0465fc0df1a5bc0e71177151e8c5901204 Signed-off-by: Stefan Weil --- src/ccmain/control.cpp | 21 ++++++++++++++------- src/ccmain/linerec.cpp | 3 +-- src/ccmain/paragraphs.cpp | 4 ++-- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/ccmain/control.cpp b/src/ccmain/control.cpp index b439aa649a..6d4cf4827c 100644 --- a/src/ccmain/control.cpp +++ b/src/ccmain/control.cpp @@ -982,9 +982,12 @@ void Tesseract::AssignDiacriticsToOverlappingBlobs(const std::vector *overlapped_any_blob, std::vector *target_blobs) { std::vector blob_wanted; - word_wanted->resize(outlines.size(), false); - overlapped_any_blob->resize(outlines.size(), false); - target_blobs->resize(outlines.size(), nullptr); + word_wanted->clear(); + word_wanted->resize(outlines.size()); + overlapped_any_blob->clear(); + overlapped_any_blob->resize(outlines.size()); + target_blobs->clear(); + target_blobs->resize(outlines.size()); // For each real blob, find the outlines that seriously overlap it. // A single blob could be several merged characters, so there can be quite // a few outlines overlapping, and the full engine needs to be used to chop @@ -993,7 +996,8 @@ void Tesseract::AssignDiacriticsToOverlappingBlobs(const std::vectorbounding_box(); - blob_wanted.resize(outlines.size(), false); + blob_wanted.clear(); + blob_wanted.resize(outlines.size()); int num_blob_outlines = 0; for (unsigned i = 0; i < outlines.size(); ++i) { if (blob_box.major_x_overlap(outlines[i]->bounding_box()) && !(*word_wanted)[i]) { @@ -1032,15 +1036,18 @@ void Tesseract::AssignDiacriticsToNewBlobs(const std::vector &outli std::vector *word_wanted, std::vector *target_blobs) { std::vector blob_wanted; - word_wanted->resize(outlines.size(), false); - target_blobs->resize(outlines.size(), nullptr); + word_wanted->clear(); + word_wanted->resize(outlines.size()); + target_blobs->clear(); + target_blobs->resize(outlines.size()); // Check for outlines that need to be turned into stand-alone blobs. for (unsigned i = 0; i < outlines.size(); ++i) { if (outlines[i] == nullptr) { continue; } // Get a set of adjacent outlines that don't overlap any existing blob. - blob_wanted.resize(outlines.size(), false); + blob_wanted.clear(); + blob_wanted.resize(outlines.size()); int num_blob_outlines = 0; TBOX total_ol_box(outlines[i]->bounding_box()); while (i < outlines.size() && outlines[i] != nullptr) { diff --git a/src/ccmain/linerec.cpp b/src/ccmain/linerec.cpp index 4f85fd670e..f1deced486 100644 --- a/src/ccmain/linerec.cpp +++ b/src/ccmain/linerec.cpp @@ -151,8 +151,7 @@ ImageData *Tesseract::GetLineData(const TBOX &line_box, const std::vector line_boxes.push_back(box); line_texts.push_back(texts[b]); } - std::vector page_numbers; - page_numbers.resize(line_boxes.size(), applybox_page); + std::vector page_numbers(line_boxes.size(), applybox_page); image_data->AddBoxes(line_boxes, line_texts, page_numbers); return image_data; } diff --git a/src/ccmain/paragraphs.cpp b/src/ccmain/paragraphs.cpp index f817bafdd4..b9525b9950 100644 --- a/src/ccmain/paragraphs.cpp +++ b/src/ccmain/paragraphs.cpp @@ -2314,14 +2314,14 @@ void CanonicalizeDetectionResults(std::vector *row_owners, PARA_LIST *pa void DetectParagraphs(int debug_level, std::vector *row_infos, std::vector *row_owners, PARA_LIST *paragraphs, std::vector *models) { - std::vector rows; ParagraphTheory theory(models); // Initialize row_owners to be a bunch of nullptr pointers. + row_owners->clear(); row_owners->resize(row_infos->size()); // Set up row scratch registers for the main algorithm. - rows.resize(row_infos->size(), RowScratchRegisters()); + std::vector rows(row_infos->size()); for (unsigned i = 0; i < row_infos->size(); i++) { rows[i].Init((*row_infos)[i]); }