tesseract v5.3.3.20231005
workingpartset.cpp
Go to the documentation of this file.
1
2// File: workingpartset.cpp
3// Description: Class to hold a working set of partitions of the page
4// during construction of text/image regions.
5// Author: Ray Smith
6// Created: Tue Ocr 28 17:21:01 PDT 2008
7//
8// (C) Copyright 2008, Google Inc.
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12// http://www.apache.org/licenses/LICENSE-2.0
13// Unless required by applicable law or agreed to in writing, software
14// distributed under the License is distributed on an "AS IS" BASIS,
15// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16// See the License for the specific language governing permissions and
17// limitations under the License.
18//
20
21#include "workingpartset.h"
22#include "colpartition.h"
23
24namespace tesseract {
25
26// Add the partition to this WorkingPartSet. Unrelated partitions are
27// stored in the order in which they are received, but if the partition
28// has a SingletonPartner, make sure that it stays with its partner.
30 ColPartition *partner = part->SingletonPartner(true);
31 if (partner != nullptr) {
32 ASSERT_HOST(partner->SingletonPartner(false) == part);
33 }
34 if (latest_part_ == nullptr || partner == nullptr) {
35 // This partition goes at the end of the list
36 part_it_.move_to_last();
37 } else if (latest_part_->SingletonPartner(false) != part) {
38 // Reposition the iterator to the correct partner, or at the end.
39 for (part_it_.move_to_first(); !part_it_.at_last() && part_it_.data() != partner;
40 part_it_.forward()) {
41 ;
42 }
43 }
44 part_it_.add_after_then_move(part);
45 latest_part_ = part;
46}
47
48// Make blocks out of any partitions in this WorkingPartSet, and append
49// them to the end of the blocks list. bleft, tright and resolution give
50// the bounds and resolution of the source image, so that blocks can be
51// made to fit in the bounds.
52// All ColPartitions go in the used_parts list, as they need to be kept
53// around, but are no longer needed.
54void WorkingPartSet::ExtractCompletedBlocks(const ICOORD &bleft, const ICOORD &tright,
55 int resolution, ColPartition_LIST *used_parts,
56 BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks) {
57 MakeBlocks(bleft, tright, resolution, used_parts);
58 BLOCK_IT block_it(blocks);
59 block_it.move_to_last();
60 block_it.add_list_after(&completed_blocks_);
61 TO_BLOCK_IT to_block_it(to_blocks);
62 to_block_it.move_to_last();
63 to_block_it.add_list_after(&to_blocks_);
64}
65
66// Insert the given blocks at the front of the completed_blocks_ list so
67// they can be kept in the correct reading order.
68void WorkingPartSet::InsertCompletedBlocks(BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks) {
69 BLOCK_IT block_it(&completed_blocks_);
70 block_it.add_list_before(blocks);
71 TO_BLOCK_IT to_block_it(&to_blocks_);
72 to_block_it.add_list_before(to_blocks);
73}
74
75// Make a block using lines parallel to the given vector that fit between
76// the min and max coordinates specified by the ColPartitions.
77// Construct a block from the given list of partitions.
78void WorkingPartSet::MakeBlocks(const ICOORD &bleft, const ICOORD &tright, int resolution,
79 ColPartition_LIST *used_parts) {
80 part_it_.move_to_first();
81 while (!part_it_.empty()) {
82 // Gather a list of ColPartitions in block_parts that will be split
83 // by linespacing into smaller blocks.
84 ColPartition_LIST block_parts;
85 ColPartition_IT block_it(&block_parts);
86 ColPartition *next_part = nullptr;
87 bool text_block = false;
88 do {
89 ColPartition *part = part_it_.extract();
90 if (part->blob_type() == BRT_UNKNOWN || (part->IsTextType() && part->type() != PT_TABLE)) {
91 text_block = true;
92 }
93 part->set_working_set(nullptr);
94 part_it_.forward();
95 block_it.add_after_then_move(part);
96 next_part = part->SingletonPartner(false);
97 if (part_it_.empty() || next_part != part_it_.data()) {
98 // Sequences of partitions can get split by titles.
99 next_part = nullptr;
100 }
101 // Merge adjacent blocks that are of the same type and let the
102 // linespacing determine the real boundaries.
103 if (next_part == nullptr && !part_it_.empty()) {
104 ColPartition *next_block_part = part_it_.data();
105 const TBOX &part_box = part->bounding_box();
106 const TBOX &next_box = next_block_part->bounding_box();
107
108 // In addition to the same type, the next box must not be above the
109 // current box, nor (if image) too far below.
110 PolyBlockType type = part->type(), next_type = next_block_part->type();
111 if (ColPartition::TypesSimilar(type, next_type) && !part->IsLineType() &&
112 !next_block_part->IsLineType() && next_box.bottom() <= part_box.top() &&
113 (text_block || part_box.bottom() <= next_box.top())) {
114 next_part = next_block_part;
115 }
116 }
117 } while (!part_it_.empty() && next_part != nullptr);
118 if (!text_block) {
119 TO_BLOCK *to_block = ColPartition::MakeBlock(bleft, tright, &block_parts, used_parts);
120 if (to_block != nullptr) {
121 TO_BLOCK_IT to_block_it(&to_blocks_);
122 to_block_it.add_to_end(to_block);
123 BLOCK_IT block_it(&completed_blocks_);
124 block_it.add_to_end(to_block->block);
125 }
126 } else {
127 // Further sub-divide text blocks where linespacing changes.
128 ColPartition::LineSpacingBlocks(bleft, tright, resolution, &block_parts, used_parts,
129 &completed_blocks_, &to_blocks_);
130 }
131 }
132 part_it_.set_to_list(&part_set_);
133 latest_part_ = nullptr;
134 ASSERT_HOST(completed_blocks_.length() == to_blocks_.length());
135}
136
137} // namespace tesseract.
#define ASSERT_HOST(x)
Definition: errcode.h:54
@ TBOX
@ BRT_UNKNOWN
Definition: blobbox.h:80
type
Definition: upload.py:458
integer coordinate
Definition: points.h:36
static void LineSpacingBlocks(const ICOORD &bleft, const ICOORD &tright, int resolution, ColPartition_LIST *block_parts, ColPartition_LIST *used_parts, BLOCK_LIST *completed_blocks, TO_BLOCK_LIST *to_blocks)
static TO_BLOCK * MakeBlock(const ICOORD &bleft, const ICOORD &tright, ColPartition_LIST *block_parts, ColPartition_LIST *used_parts)
PolyBlockType type() const
Definition: colpartition.h:180
void set_working_set(WorkingPartSet *working_set)
Definition: colpartition.h:201
BlobRegionType blob_type() const
Definition: colpartition.h:147
const TBOX & bounding_box() const
Definition: colpartition.h:108
static bool TypesSimilar(PolyBlockType type1, PolyBlockType type2)
Definition: colpartition.h:418
ColPartition * SingletonPartner(bool upper)
void AddPartition(ColPartition *part)
void ExtractCompletedBlocks(const ICOORD &bleft, const ICOORD &tright, int resolution, ColPartition_LIST *used_parts, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
void InsertCompletedBlocks(BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)