tesseract  5.0.0-alpha-619-ge9db
workingpartset.cpp
Go to the documentation of this file.
1 // 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 
24 namespace tesseract {
25 
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 != nullptr) {
34  ASSERT_HOST(partner->SingletonPartner(false) == part);
35  }
36  if (latest_part_ == nullptr || partner == nullptr) {
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.
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.
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.
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 = nullptr;
92  bool text_block = false;
93  do {
94  ColPartition* part = part_it_.extract();
95  if (part->blob_type() == BRT_UNKNOWN ||
96  (part->IsTextType() && part->type() != PT_TABLE))
97  text_block = true;
98  part->set_working_set(nullptr);
99  part_it_.forward();
100  block_it.add_after_then_move(part);
101  next_part = part->SingletonPartner(false);
102  if (part_it_.empty() || next_part != part_it_.data()) {
103  // Sequences of partitions can get split by titles.
104  next_part = nullptr;
105  }
106  // Merge adjacent blocks that are of the same type and let the
107  // linespacing determine the real boundaries.
108  if (next_part == nullptr && !part_it_.empty()) {
109  ColPartition* next_block_part = part_it_.data();
110  const TBOX& part_box = part->bounding_box();
111  const TBOX& next_box = next_block_part->bounding_box();
112 
113  // In addition to the same type, the next box must not be above the
114  // current box, nor (if image) too far below.
115  PolyBlockType type = part->type(), next_type = next_block_part->type();
116  if (ColPartition::TypesSimilar(type, next_type) &&
117  !part->IsLineType() && !next_block_part->IsLineType() &&
118  next_box.bottom() <= part_box.top() &&
119  (text_block || part_box.bottom() <= next_box.top()))
120  next_part = next_block_part;
121  }
122  } while (!part_it_.empty() && next_part != nullptr);
123  if (!text_block) {
124  TO_BLOCK* to_block = ColPartition::MakeBlock(bleft, tright,
125  &block_parts, used_parts);
126  if (to_block != nullptr) {
127  TO_BLOCK_IT to_block_it(&to_blocks_);
128  to_block_it.add_to_end(to_block);
129  BLOCK_IT block_it(&completed_blocks_);
130  block_it.add_to_end(to_block->block);
131  }
132  } else {
133  // Further sub-divide text blocks where linespacing changes.
134  ColPartition::LineSpacingBlocks(bleft, tright, resolution, &block_parts,
135  used_parts,
136  &completed_blocks_, &to_blocks_);
137  }
138  }
139  part_it_.set_to_list(&part_set_);
140  latest_part_ = nullptr;
141  ASSERT_HOST(completed_blocks_.length() == to_blocks_.length());
142 }
143 
144 } // namespace tesseract.
tesseract::ColPartition::set_working_set
void set_working_set(WorkingPartSet *working_set)
Definition: colpartition.h:202
tesseract::ColPartition::IsLineType
bool IsLineType() const
Definition: colpartition.h:425
tesseract::ColPartition::TypesSimilar
static bool TypesSimilar(PolyBlockType type1, PolyBlockType type2)
Definition: colpartition.h:418
tesseract::ColPartition::LineSpacingBlocks
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)
Definition: colpartition.cpp:1407
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
BRT_UNKNOWN
Definition: blobbox.h:77
ICOORD
integer coordinate
Definition: points.h:30
TBOX::top
int16_t top() const
Definition: rect.h:57
tesseract::ColPartition::type
PolyBlockType type() const
Definition: colpartition.h:181
TO_BLOCK
Definition: blobbox.h:691
tesseract::WorkingPartSet::ExtractCompletedBlocks
void ExtractCompletedBlocks(const ICOORD &bleft, const ICOORD &tright, int resolution, ColPartition_LIST *used_parts, BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
Definition: workingpartset.cpp:55
colpartition.h
PT_TABLE
Definition: capi.h:114
tesseract::ColPartition
Definition: colpartition.h:67
tesseract::ColPartition::SingletonPartner
ColPartition * SingletonPartner(bool upper)
Definition: colpartition.cpp:629
TO_BLOCK::block
BLOCK * block
Definition: blobbox.h:776
tesseract::ColPartition::blob_type
BlobRegionType blob_type() const
Definition: colpartition.h:148
TBOX::bottom
int16_t bottom() const
Definition: rect.h:64
tesseract
Definition: baseapi.h:65
tesseract::ColPartition::MakeBlock
static TO_BLOCK * MakeBlock(const ICOORD &bleft, const ICOORD &tright, ColPartition_LIST *block_parts, ColPartition_LIST *used_parts)
Definition: colpartition.cpp:1623
tesseract::WorkingPartSet
Definition: workingpartset.h:32
tesseract::ColPartition::bounding_box
const TBOX & bounding_box() const
Definition: colpartition.h:109
tesstrain_utils.type
type
Definition: tesstrain_utils.py:141
workingpartset.h
PolyBlockType
PolyBlockType
Definition: publictypes.h:52
tesseract::ColPartition::IsTextType
bool IsTextType() const
Definition: colpartition.h:433
ELISTIZE
#define ELISTIZE(CLASSNAME)
Definition: elst.h:919
tesseract::WorkingPartSet::InsertCompletedBlocks
void InsertCompletedBlocks(BLOCK_LIST *blocks, TO_BLOCK_LIST *to_blocks)
Definition: workingpartset.cpp:72
TBOX
Definition: rect.h:33