Commit 3a260247 for tesseract
commit 3a260247375f091625378cd099713f7a4bf0e4d7
Author: İlkecan Bozdoğan <ilkecan@protonmail.com>
Date: Wed Nov 25 13:43:43 2026 +0000
Restart rows and paragraphs from the current block
PAGE_RES_IT::restart_row() and PageIterator::RestartParagraph() scanned
forward from the start of the page. Result iteration calls
restart_row() for almost every word and RestartParagraph() for every
paragraph, so iterating a page took time quadratic in its number of
words.
Keep a copy of the block iterator for the current word, like
wr_it_of_current_word, so that the new PAGE_RES_IT::start_block() can
move to the start of the current block directly. Rows and paragraphs do
not span blocks, so both scans can start there. This helps most on
pages with many small blocks, as in sparse segmentation. Within a
block, the scans are still linear, so a page with one large block, as
in PSM 4 or 6, still takes quadratic time.
RestartRow() and RestartParagraph() no longer leave a look-ahead that
skips an empty block after the current block, so a following Next()
visits it, as in normal iteration.
On the #4430 page in PSM 11, this reduces CPU time by 22%. The output
for the images in test/testing does not change.
Related: #4430
diff --git a/src/ccmain/pageiterator.cpp b/src/ccmain/pageiterator.cpp
index 87f646e7..7ccc4001 100644
--- a/src/ccmain/pageiterator.cpp
+++ b/src/ccmain/pageiterator.cpp
@@ -113,7 +113,9 @@ void PageIterator::RestartParagraph() {
}
const BLOCK_RES *target_block = it_->block();
const PARA *target_para = it_->row() == nullptr ? nullptr : it_->row()->row->para();
- PAGE_RES_IT para(page_res_);
+ // Paragraphs do not span blocks, so the scan can start at the current block.
+ PAGE_RES_IT para(*it_);
+ para.start_block(true);
// Paragraph and block identities are stable within PAGE_RES. Avoid cmp(),
// which searches the page and makes this forward scan quadratic.
while (para.block() != nullptr &&
diff --git a/src/ccstruct/pageres.cpp b/src/ccstruct/pageres.cpp
index 5f0c09d2..aa8db4d3 100644
--- a/src/ccstruct/pageres.cpp
+++ b/src/ccstruct/pageres.cpp
@@ -1576,6 +1576,23 @@ void PAGE_RES_IT::MakeCurrentWordFuzzy() {
WERD_RES *PAGE_RES_IT::start_page(bool empty_ok) {
block_res_it.set_to_list(&page_res->block_res_list);
block_res_it.mark_cycle_pt();
+ return start_at_block_res_it(empty_ok);
+}
+
+/*************************************************************************
+ * PAGE_RES_IT::start_block
+ *
+ * Move to the first word of the current block, as start_page() does for the
+ * first block.
+ *************************************************************************/
+
+WERD_RES *PAGE_RES_IT::start_block(bool empty_ok) {
+ ASSERT_HOST(block_res != nullptr);
+ block_res_it = bl_it_of_current_word;
+ return start_at_block_res_it(empty_ok);
+}
+
+WERD_RES *PAGE_RES_IT::start_at_block_res_it(bool empty_ok) {
prev_block_res = nullptr;
prev_row_res = nullptr;
prev_word_res = nullptr;
@@ -1652,6 +1669,7 @@ WERD_RES *PAGE_RES_IT::internal_forward(bool new_block, bool empty_ok) {
row_res = next_row_res;
word_res = next_word_res;
wr_it_of_current_word = wr_it_of_next_word;
+ bl_it_of_current_word = bl_it_of_next_word;
next_block_res = nullptr;
next_row_res = nullptr;
next_word_res = nullptr;
@@ -1662,6 +1680,7 @@ WERD_RES *PAGE_RES_IT::internal_forward(bool new_block, bool empty_ok) {
row_res_it.mark_cycle_pt();
if (row_res_it.empty() && empty_ok) {
next_block_res = block_res_it.data();
+ bl_it_of_next_word = block_res_it;
break;
}
new_row = true;
@@ -1678,6 +1697,7 @@ WERD_RES *PAGE_RES_IT::internal_forward(bool new_block, bool empty_ok) {
}
if (!word_res_it.cycled_list()) {
next_block_res = block_res_it.data();
+ bl_it_of_next_word = block_res_it;
next_row_res = row_res_it.data();
next_word_res = word_res_it.data();
wr_it_of_next_word = word_res_it;
@@ -1705,14 +1725,15 @@ foundword:
/*************************************************************************
* PAGE_RES_IT::restart_row()
*
- * Move to the beginning (leftmost word) of the current row.
+ * Move to the beginning (leftmost word) of the current row. As with
+ * forward_with_empties(), a following empty block is not skipped.
*************************************************************************/
WERD_RES *PAGE_RES_IT::restart_row() {
ROW_RES *row = this->row();
if (!row) {
return nullptr;
}
- for (restart_page(); this->row() != row; forward()) {
+ for (start_block(true); this->row() != row; forward_with_empties()) {
// pass
}
return word();
diff --git a/src/ccstruct/pageres.h b/src/ccstruct/pageres.h
index 0d19ef67..082fd9ec 100644
--- a/src/ccstruct/pageres.h
+++ b/src/ccstruct/pageres.h
@@ -717,6 +717,8 @@ public:
return start_page(true); // Allow empty blocks.
}
WERD_RES *start_page(bool empty_ok);
+ // Like start_page(), but from the current block, which must not be nullptr.
+ WERD_RES *start_block(bool empty_ok);
WERD_RES *restart_row();
@@ -786,6 +788,7 @@ public:
private:
WERD_RES *internal_forward(bool new_block, bool empty_ok);
+ WERD_RES *start_at_block_res_it(bool empty_ok);
WERD_RES *prev_word_res = nullptr; // previous word
ROW_RES *prev_row_res = nullptr; // row of prev word
@@ -806,6 +809,9 @@ private:
// Since word_res_it is 2 words further on, this is otherwise hard to do.
WERD_RES_IT wr_it_of_current_word;
WERD_RES_IT wr_it_of_next_word;
+ // The same for block_res_it, which may already be on a later block.
+ BLOCK_RES_IT bl_it_of_current_word;
+ BLOCK_RES_IT bl_it_of_next_word;
};
} // namespace tesseract
diff --git a/unittest/pageres_test.cc b/unittest/pageres_test.cc
index b0289ecf..060e3747 100644
--- a/unittest/pageres_test.cc
+++ b/unittest/pageres_test.cc
@@ -10,8 +10,10 @@
#include "pageres.h"
+#include <tesseract/pageiterator.h>
#include "boxword.h"
#include "ocrblock.h"
+#include "ocrpara.h"
#include "ocrrow.h"
#include "rect.h"
#include "stepblob.h"
@@ -37,21 +39,49 @@ WERD *MakeWord(const std::vector<TBOX> &boxes) {
return new WERD(&blobs, 1, nullptr);
}
-// Adds a block to blocks, with one row holding the given words.
-void AddRow(BLOCK_LIST *blocks, const std::vector<WERD *> &words) {
- int32_t xstarts[] = {0, 200};
- double coeffs[] = {0.0, 0.0, 10.0};
- auto *row = new ROW(1, xstarts, coeffs, 20.0f, 10.0f, -5.0f, 0, 10);
- WERD_IT w_it(row->word_list());
- for (auto *word : words) {
- w_it.add_to_end(word);
- }
- row->recalc_bounding_box();
+// Adds a block with one row per element of rows. Without rows it is empty.
+BLOCK *AddBlock(BLOCK_LIST *blocks,
+ const std::vector<std::vector<WERD *>> &rows) {
auto *block = new BLOCK("", true, 0, 0, 0, 0, 200, 50);
ROW_IT row_it(block->row_list());
- row_it.add_to_end(row);
+ for (const auto &words : rows) {
+ int32_t xstarts[] = {0, 200};
+ double coeffs[] = {0.0, 0.0, 10.0};
+ auto *row = new ROW(1, xstarts, coeffs, 20.0f, 10.0f, -5.0f, 0, 10);
+ WERD_IT w_it(row->word_list());
+ for (auto *word : words) {
+ w_it.add_to_end(word);
+ }
+ row->recalc_bounding_box();
+ row_it.add_to_end(row);
+ }
BLOCK_IT block_it(blocks);
block_it.add_to_end(block);
+ return block;
+}
+
+// Returns an iterator at the word made from werd.
+PAGE_RES_IT PageResItAt(PAGE_RES *page_res, const WERD *werd) {
+ PAGE_RES_IT it(page_res);
+ while (it.word()->word != werd) {
+ it.forward();
+ }
+ return it;
+}
+
+// Returns an iterator at the first empty block.
+PAGE_RES_IT PageResItAtEmptyBlock(PAGE_RES *page_res) {
+ PAGE_RES_IT it(page_res);
+ it.restart_page_with_empties();
+ while (it.word() != nullptr) {
+ it.forward_with_empties();
+ }
+ return it;
+}
+
+// Adds a block to blocks, with one row holding the given words.
+void AddRow(BLOCK_LIST *blocks, const std::vector<WERD *> &words) {
+ AddBlock(blocks, {words});
}
// Makes a word as the LSTM recognizer gives it: a combination that owns a
@@ -185,6 +215,148 @@ TEST(PageResTest, ReplaceCurrentWordCopiesSharedCombinationWord) {
EXPECT_EQ(nullptr, it.forward());
}
+// restart_row() starts from the current block, so use a block after the first.
+TEST(PageResTest, RestartRowInLaterBlock) {
+ BLOCK_LIST blocks;
+ WERD *a1 = MakeWord({kInputBlobs[0]});
+ WERD *b1 = MakeWord({kInputBlobs[0]});
+ WERD *b2 = MakeWord({kInputBlobs[1]});
+ WERD *b3 = MakeWord({kInputBlobs[0]});
+ WERD *b4 = MakeWord({kInputBlobs[1]});
+ WERD *c1 = MakeWord({kInputBlobs[0]});
+ AddBlock(&blocks, {{a1}});
+ AddBlock(&blocks, {{b1, b2}, {b3, b4}});
+ AddBlock(&blocks, {{c1}});
+ PAGE_RES page_res(false, &blocks, nullptr);
+ PAGE_RES_IT it(&page_res);
+ while (it.word()->word != b2) {
+ it.forward();
+ }
+ EXPECT_EQ(b1, it.restart_row()->word);
+ EXPECT_EQ(b2, it.forward()->word);
+ EXPECT_EQ(b3, it.forward()->word);
+ EXPECT_EQ(b4, it.forward()->word);
+ ROW_RES *row = it.row();
+ EXPECT_EQ(b3, it.restart_row()->word);
+ EXPECT_EQ(row, it.row());
+ EXPECT_EQ(b4, it.forward()->word);
+ EXPECT_EQ(c1, it.forward()->word);
+ EXPECT_EQ(nullptr, it.forward());
+}
+
+TEST(PageResTest, StartBlockWithEmptyBlock) {
+ BLOCK_LIST blocks;
+ WERD *a1 = MakeWord({kInputBlobs[0]});
+ WERD *b1 = MakeWord({kInputBlobs[0]});
+ WERD *b2 = MakeWord({kInputBlobs[1]});
+ AddBlock(&blocks, {{a1}});
+ AddBlock(&blocks, {});
+ AddBlock(&blocks, {{b1}, {b2}});
+ PAGE_RES page_res(false, &blocks, nullptr);
+ PAGE_RES_IT it(&page_res);
+ it.restart_page_with_empties();
+ EXPECT_EQ(a1, it.word()->word);
+
+ EXPECT_EQ(nullptr, it.forward_with_empties());
+ BLOCK_RES *empty_block = it.block();
+ ASSERT_NE(nullptr, empty_block);
+ EXPECT_EQ(nullptr, it.start_block(true));
+ EXPECT_EQ(empty_block, it.block());
+
+ EXPECT_EQ(b1, it.forward_with_empties()->word);
+ EXPECT_EQ(b2, it.forward_with_empties()->word);
+ BLOCK_RES *block = it.block();
+ EXPECT_EQ(b1, it.start_block(true)->word);
+ EXPECT_EQ(block, it.block());
+ // As at the start of the page, there is no previous word.
+ EXPECT_EQ(nullptr, it.prev_block());
+ EXPECT_EQ(nullptr, it.prev_word());
+ EXPECT_EQ(b2, it.forward_with_empties()->word);
+ EXPECT_EQ(nullptr, it.forward_with_empties());
+ EXPECT_EQ(nullptr, it.block());
+}
+
+// RestartParagraph() starts from the current block, so use a block after the
+// first.
+TEST(PageIteratorTest, RestartParagraphInLaterBlock) {
+ BLOCK_LIST blocks;
+ WERD *a1 = MakeWord({kInputBlobs[0]});
+ WERD *b1 = MakeWord({kInputBlobs[0]});
+ WERD *b2 = MakeWord({kInputBlobs[0]});
+ WERD *b3 = MakeWord({kInputBlobs[0]});
+ WERD *b4 = MakeWord({kInputBlobs[1]});
+ AddBlock(&blocks, {{a1}});
+ BLOCK *block = AddBlock(&blocks, {{b1}, {b2}, {b3, b4}});
+ auto *first = new PARA;
+ auto *second = new PARA;
+ PARA_IT para_it(block->para_list());
+ para_it.add_to_end(first);
+ para_it.add_to_end(second);
+ ROW_IT row_it(block->row_list());
+ row_it.data()->set_para(first);
+ row_it.forward();
+ row_it.data()->set_para(second);
+ row_it.forward();
+ row_it.data()->set_para(second);
+ PAGE_RES page_res(false, &blocks, nullptr);
+ // The Tesseract object is only needed for images.
+ PageIterator it(&page_res, nullptr, 1, 300, 0, 0, 200, 50);
+ PAGE_RES_IT b4_it = PageResItAt(&page_res, b4);
+ while (!it.PositionedAtSameWord(&b4_it)) {
+ ASSERT_TRUE(it.Next(RIL_WORD));
+ }
+
+ it.RestartParagraph();
+ PAGE_RES_IT b2_it = PageResItAt(&page_res, b2);
+ EXPECT_TRUE(it.PositionedAtSameWord(&b2_it));
+ EXPECT_TRUE(it.IsAtBeginningOf(RIL_PARA));
+ ASSERT_TRUE(it.Next(RIL_WORD));
+ PAGE_RES_IT b3_it = PageResItAt(&page_res, b3);
+ EXPECT_TRUE(it.PositionedAtSameWord(&b3_it));
+}
+
+// RestartParagraph() used to leave a look-ahead that skipped an empty block
+// after a block with a single word.
+TEST(PageIteratorTest, NextAfterRestartParagraphVisitsEmptyBlock) {
+ BLOCK_LIST blocks;
+ WERD *a1 = MakeWord({kInputBlobs[0]});
+ WERD *b1 = MakeWord({kInputBlobs[0]});
+ AddBlock(&blocks, {{a1}});
+ AddBlock(&blocks, {});
+ AddBlock(&blocks, {{b1}});
+ PAGE_RES page_res(false, &blocks, nullptr);
+ PageIterator it(&page_res, nullptr, 1, 300, 0, 0, 200, 50);
+ PAGE_RES_IT a1_it = PageResItAt(&page_res, a1);
+ ASSERT_TRUE(it.PositionedAtSameWord(&a1_it));
+
+ it.RestartParagraph();
+ ASSERT_TRUE(it.Next(RIL_WORD));
+ PAGE_RES_IT empty_it = PageResItAtEmptyBlock(&page_res);
+ EXPECT_TRUE(it.PositionedAtSameWord(&empty_it));
+}
+
+// The same for RestartRow() on a single-word last row of a block.
+TEST(PageIteratorTest, NextAfterRestartRowVisitsEmptyBlock) {
+ BLOCK_LIST blocks;
+ WERD *a1 = MakeWord({kInputBlobs[0]});
+ WERD *a2 = MakeWord({kInputBlobs[0]});
+ WERD *b1 = MakeWord({kInputBlobs[0]});
+ AddBlock(&blocks, {{a1}, {a2}});
+ AddBlock(&blocks, {});
+ AddBlock(&blocks, {{b1}});
+ PAGE_RES page_res(false, &blocks, nullptr);
+ PageIterator it(&page_res, nullptr, 1, 300, 0, 0, 200, 50);
+ ASSERT_TRUE(it.Next(RIL_WORD));
+ PAGE_RES_IT a2_it = PageResItAt(&page_res, a2);
+ ASSERT_TRUE(it.PositionedAtSameWord(&a2_it));
+
+ it.RestartRow();
+ EXPECT_TRUE(it.PositionedAtSameWord(&a2_it));
+ ASSERT_TRUE(it.Next(RIL_WORD));
+ PAGE_RES_IT empty_it = PageResItAtEmptyBlock(&page_res);
+ EXPECT_TRUE(it.PositionedAtSameWord(&empty_it));
+}
+
} // namespace
} // namespace tesseract