Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
colpartitionset.h
Go to the documentation of this file.
1 
2 // File: colpartitionset.h
3 // Description: Class to hold a list of ColPartitions of the page that
4 // correspond roughly to columns.
5 // Author: Ray Smith
6 // Created: Thu Aug 14 10:50: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 #ifndef TESSERACT_TEXTORD_COLPARTITIONSET_H__
22 #define TESSERACT_TEXTORD_COLPARTITIONSET_H__
23 
24 #include "colpartition.h" // For ColPartition_LIST.
25 #include "genericvector.h" // For GenericVector.
26 #include "rect.h" // For TBOX.
27 #include "tabvector.h" // For BLOBNBOX_CLIST.
28 
29 namespace tesseract {
30 
31 class WorkingPartSet_LIST;
32 class ColSegment_LIST;
35 
36 // ColPartitionSet is a class that holds a list of ColPartitions.
37 // Its main use is in holding a candidate partitioning of the width of the
38 // image into columns, where each member ColPartition is a single column.
39 // ColPartitionSets are used in building the column layout of a page.
40 class ColPartitionSet : public ELIST_LINK {
41  public:
43  }
44  explicit ColPartitionSet(ColPartition_LIST* partitions);
45  explicit ColPartitionSet(ColPartition* partition);
46 
48 
49  // Simple accessors.
50  const TBOX& bounding_box() const {
51  return bounding_box_;
52  }
53  bool Empty() {
54  return parts_.empty();
55  }
56  int ColumnCount() {
57  return parts_.length();
58  }
59 
60  // Return an element of the parts_ list from its index.
61  ColPartition* GetColumnByIndex(int index);
62 
63  // Return the ColPartition that contains the given coords, if any, else NULL.
64  ColPartition* ColumnContaining(int x, int y);
65 
66  // Return the bounding boxes of columns at the given y-range
67  void GetColumnBoxes(int y_bottom, int y_top, ColSegment_LIST *segments);
68 
69  // Extract all the parts from the list, relinquishing ownership.
70  void RelinquishParts();
71 
72  // Attempt to improve this by adding partitions or expanding partitions.
74 
75  // If this set is good enough to represent a new partitioning into columns,
76  // add it to the vector of sets, otherwise delete it.
77  void AddToColumnSetsIfUnique(PartSetVector* column_sets, WidthCallback* cb);
78 
79  // Return true if the partitions in other are all compatible with the columns
80  // in this.
81  bool CompatibleColumns(bool debug, ColPartitionSet* other, WidthCallback* cb);
82 
83  // Returns the total width of all blobs in the part_set that do not lie
84  // within an approved column. Used as a cost measure for using this
85  // column set over another that might be compatible.
86  int UnmatchedWidth(ColPartitionSet* part_set);
87 
88  // Return true if this ColPartitionSet makes a legal column candidate by
89  // having legal individual partitions and non-overlapping adjacent pairs.
90  bool LegalColumnCandidate();
91 
92  // Return a copy of this. If good_only will only copy the Good ColPartitions.
93  ColPartitionSet* Copy(bool good_only);
94 
95  // Display the edges of the columns at the given y coords.
96  void DisplayColumnEdges(int y_bottom, int y_top, ScrollView* win);
97 
98  // Return the ColumnSpanningType that best explains the columns overlapped
99  // by the given coords(left,right,y), with the given margins.
100  // Also return the first and last column index touched by the coords and
101  // the leftmost spanned column.
102  // Column indices are 2n + 1 for real colums (0 based) and even values
103  // represent the gaps in between columns, with 0 being left of the leftmost.
104  // resolution refers to the ppi resolution of the image. It may be 0 if only
105  // the first_col and last_col are required.
106  ColumnSpanningType SpanningType(int resolution,
107  int left, int right, int y,
108  int left_margin, int right_margin,
109  int* first_col, int* last_col,
110  int* first_spanned_col);
111 
112  // The column_set has changed. Close down all in-progress WorkingPartSets in
113  // columns that do not match and start new ones for the new columns in this.
114  // As ColPartitions are turned into BLOCKs, the used ones are put in
115  // used_parts, as they still need to be referenced in the grid.
116  void ChangeWorkColumns(const ICOORD& bleft, const ICOORD& tright,
117  int resolution, ColPartition_LIST* used_parts,
118  WorkingPartSet_LIST* working_set);
119 
120  // Accumulate the widths and gaps into the given variables.
121  void AccumulateColumnWidthsAndGaps(int* total_width, int* width_samples,
122  int* total_gap, int* gap_samples);
123 
124  // Provide debug output for this ColPartitionSet and all the ColPartitions.
125  void Print();
126 
127  private:
128  // Add the given partition to the list in the appropriate place.
129  void AddPartition(ColPartition* new_part, ColPartition_IT* it);
130 
131  // Compute the coverage and good column count. Coverage is the amount of the
132  // width of the page (in pixels) that is covered by ColPartitions, which are
133  // used to provide candidate column layouts.
134  // Coverage is split into good and bad. Good coverage is provided by
135  // ColPartitions of a frequent width (according to the callback function
136  // provided by TabFinder::WidthCB, which accesses stored statistics on the
137  // widths of ColParititions) and bad coverage is provided by all other
138  // ColPartitions, even if they have tab vectors at both sides. Thus:
139  // |-----------------------------------------------------------------|
140  // | Double width heading |
141  // |-----------------------------------------------------------------|
142  // |-------------------------------| |-------------------------------|
143  // | Common width ColParition | | Common width ColPartition |
144  // |-------------------------------| |-------------------------------|
145  // the layout with two common-width columns has better coverage than the
146  // double width heading, because the coverage is "good," even though less in
147  // total coverage than the heading, because the heading coverage is "bad."
148  void ComputeCoverage();
149 
150  // Adds the coverage, column count and box for a single partition,
151  // without adding it to the list. (Helper factored from ComputeCoverage.)
152  void AddPartitionCoverageAndBox(const ColPartition& part);
153 
154  // The partitions in this column candidate.
155  ColPartition_LIST parts_;
156  // The number of partitions that have a frequent column width.
157  int good_column_count_;
158  // Total width of all the good ColPartitions.
159  int good_coverage_;
160  // Total width of all the bad ColPartitions.
161  int bad_coverage_;
162  // Bounding box of all partitions in the set.
163  TBOX bounding_box_;
164 };
165 
166 ELISTIZEH(ColPartitionSet)
167 
168 } // namespace tesseract.
169 
170 #endif // TESSERACT_TEXTORD_COLPARTITION_H__