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 //
19 ///////////////////////////////////////////////////////////////////////
20
21 #include "workingpartset.h"
22 #include "colpartition.h"
23
24 namespace tesseract {
25
ELISTIZE(WorkingPartSet)26 ELISTIZE(WorkingPartSet)
27
28 // Add the partition to this WorkingPartSet. Unrelated partitions are
29 // stored in the order in which they are received, but if the partition
30 // has a SingletonPartner, make sure that it stays with its partner.
31 void WorkingPartSet::AddPartition(ColPartition* part) {
32 ColPartition* partner = part->SingletonPartner(true);
33 if (partner != NULL) {
34 ASSERT_HOST(partner->SingletonPartner(false) == part);
35 }
36 if (latest_part_ == NULL || partner == NULL) {
37 // This partition goes at the end of the list
38 part_it_.move_to_last();
39 } else if (latest_part_->SingletonPartner(false) != part) {
40 // Reposition the iterator to the correct partner, or at the end.
41 for (part_it_.move_to_first(); !part_it_.at_last() &&
42 part_it_.data() != partner;
43 part_it_.forward());
44 }
45 part_it_.add_after_then_move(part);
46 latest_part_ = part;
47 }
48
49 // Make blocks out of any partitions in this WorkingPartSet, and append
50 // them to the end of the blocks list. bleft, tright and resolution give
51 // the bounds and resolution of the source image, so that blocks can be
52 // made to fit in the bounds.
53 // All ColPartitions go in the used_parts list, as they need to be kept
54 // around, but are no longer needed.
ExtractCompletedBlocks(const ICOORD & bleft,const ICOORD & tright,int resolution,ColPartition_LIST * used_parts,BLOCK_LIST * blocks,TO_BLOCK_LIST * to_blocks)55 void WorkingPartSet::ExtractCompletedBlocks(const ICOORD& bleft,
56 const ICOORD& tright,
57 int resolution,
58 ColPartition_LIST* used_parts,
59 BLOCK_LIST* blocks,
60 TO_BLOCK_LIST* to_blocks) {
61 MakeBlocks(bleft, tright, resolution, used_parts);
62 BLOCK_IT block_it(blocks);
63 block_it.move_to_last();
64 block_it.add_list_after(&completed_blocks_);
65 TO_BLOCK_IT to_block_it(to_blocks);
66 to_block_it.move_to_last();
67 to_block_it.add_list_after(&to_blocks_);
68 }
69
70 // Insert the given blocks at the front of the completed_blocks_ list so
71 // they can be kept in the correct reading order.
InsertCompletedBlocks(BLOCK_LIST * blocks,TO_BLOCK_LIST * to_blocks)72 void WorkingPartSet::InsertCompletedBlocks(BLOCK_LIST* blocks,
73 TO_BLOCK_LIST* to_blocks) {
74 BLOCK_IT block_it(&completed_blocks_);
75 block_it.add_list_before(blocks);
76 TO_BLOCK_IT to_block_it(&to_blocks_);
77 to_block_it.add_list_before(to_blocks);
78 }
79
80 // Make a block using lines parallel to the given vector that fit between
81 // the min and max coordinates specified by the ColPartitions.
82 // Construct a block from the given list of partitions.
MakeBlocks(const ICOORD & bleft,const ICOORD & tright,int resolution,ColPartition_LIST * used_parts)83 void WorkingPartSet::MakeBlocks(const ICOORD& bleft, const ICOORD& tright,
84 int resolution, ColPartition_LIST* used_parts) {
85 part_it_.move_to_first();
86 while (!part_it_.empty()) {
87 // Gather a list of ColPartitions in block_parts that will be split
88 // by linespacing into smaller blocks.
89 ColPartition_LIST block_parts;
90 ColPartition_IT block_it(&block_parts);
91 ColPartition* next_part = NULL;
92 bool text_block = false;
93 do {
94 ColPartition* part = part_it_.extract();
95 if (part->blob_type() == BRT_UNKNOWN || part->blob_type() == BRT_TEXT)
96 text_block = true;
97 part->set_working_set(NULL);
98 part_it_.forward();
99 block_it.add_after_then_move(part);
100 next_part = part->SingletonPartner(false);
101 if (part_it_.empty() || next_part != part_it_.data()) {
102 // Sequences of partitions can get split by titles.
103 next_part = NULL;
104 }
105 // Merge adjacent blocks that are of the same type and let the
106 // linespacing determine the real boundaries.
107 if (next_part == NULL && !part_it_.empty()) {
108 ColPartition* next_block_part = part_it_.data();
109 const TBOX& part_box = part->bounding_box();
110 const TBOX& next_box = next_block_part->bounding_box();
111 // In addition to the same type, the next box must not be above the
112 // current box, nor (if image) too far below.
113 if (next_block_part->type() == part->type() &&
114 next_box.bottom() <= part_box.top() &&
115 (text_block ||
116 part_box.bottom() - next_box.top() < part_box.height()))
117 next_part = next_block_part;
118 }
119 } while (!part_it_.empty() && next_part != NULL);
120 if (!text_block) {
121 TO_BLOCK* to_block = ColPartition::MakeBlock(bleft, tright,
122 &block_parts, used_parts);
123 if (to_block != NULL) {
124 TO_BLOCK_IT to_block_it(&to_blocks_);
125 to_block_it.add_to_end(to_block);
126 BLOCK_IT block_it(&completed_blocks_);
127 block_it.add_to_end(to_block->block);
128 }
129 } else {
130 // Further sub-divide text blocks where linespacing changes.
131 ColPartition::LineSpacingBlocks(bleft, tright, resolution, &block_parts,
132 used_parts,
133 &completed_blocks_, &to_blocks_);
134 }
135 }
136 part_it_.set_to_list(&part_set_);
137 latest_part_ = NULL;
138 ASSERT_HOST(completed_blocks_.length() == to_blocks_.length());
139 }
140
141 } // namespace tesseract.
142
143