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