00001 00002 // File: colpartitionset.h 00003 // Description: Class to hold a list of ColPartitions of the page that 00004 // correspond roughly to columns. 00005 // Author: Ray Smith 00006 // Created: Thu Aug 14 10:50:01 PDT 2008 00007 // 00008 // (C) Copyright 2008, Google Inc. 00009 // Licensed under the Apache License, Version 2.0 (the "License"); 00010 // you may not use this file except in compliance with the License. 00011 // You may obtain a copy of the License at 00012 // http://www.apache.org/licenses/LICENSE-2.0 00013 // Unless required by applicable law or agreed to in writing, software 00014 // distributed under the License is distributed on an "AS IS" BASIS, 00015 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 // See the License for the specific language governing permissions and 00017 // limitations under the License. 00018 // 00020 00021 #ifndef TESSERACT_TEXTORD_COLPARTITIONSET_H__ 00022 #define TESSERACT_TEXTORD_COLPARTITIONSET_H__ 00023 00024 #include "colpartition.h" // For ColPartition_LIST. 00025 #include "genericvector.h" // For GenericVector. 00026 #include "rect.h" // For TBOX. 00027 #include "tabvector.h" // For BLOBNBOX_CLIST. 00028 00029 namespace tesseract { 00030 00031 class WorkingPartSet_LIST; 00032 class ColSegment_LIST; 00033 class ColPartitionSet; 00034 typedef GenericVector<ColPartitionSet*> PartSetVector; 00035 00036 // ColPartitionSet is a class that holds a list of ColPartitions. 00037 // Its main use is in holding a candidate partitioning of the width of the 00038 // image into columns, where each member ColPartition is a single column. 00039 // ColPartitionSets are used in building the column layout of a page. 00040 class ColPartitionSet : public ELIST_LINK { 00041 public: 00042 ColPartitionSet() { 00043 } 00044 explicit ColPartitionSet(ColPartition_LIST* partitions); 00045 explicit ColPartitionSet(ColPartition* partition); 00046 00047 ~ColPartitionSet(); 00048 00049 // Simple accessors. 00050 const TBOX& bounding_box() const { 00051 return bounding_box_; 00052 } 00053 bool Empty() { 00054 return parts_.empty(); 00055 } 00056 int ColumnCount() { 00057 return parts_.length(); 00058 } 00059 00060 // Return an element of the parts_ list from its index. 00061 ColPartition* GetColumnByIndex(int index); 00062 00063 // Return the ColPartition that contains the given coords, if any, else NULL. 00064 ColPartition* ColumnContaining(int x, int y); 00065 00066 // Return the bounding boxes of columns at the given y-range 00067 void GetColumnBoxes(int y_bottom, int y_top, ColSegment_LIST *segments); 00068 00069 // Move the parts to the output list, giving up ownership. 00070 void ReturnParts(ColPartition_LIST* parts); 00071 00072 // Merge any significantly overlapping partitions within the this and other, 00073 // and unique the boxes so that no two partitions use the same box. 00074 // Return true if any changes were made to either set. 00075 bool MergeOverlaps(ColPartitionSet* other, WidthCallback* cb); 00076 00077 // Attempt to improve this by adding partitions or expanding partitions. 00078 void ImproveColumnCandidate(WidthCallback* cb, PartSetVector* src_sets); 00079 00080 // If this set is good enough to represent a new partitioning into columns, 00081 // add it to the vector of sets, otherwise delete it. 00082 void AddToColumnSetsIfUnique(PartSetVector* column_sets, WidthCallback* cb); 00083 00084 // Return true if the partitions in other are all compatible with the columns 00085 // in this. 00086 bool CompatibleColumns(bool debug, ColPartitionSet* other, WidthCallback* cb); 00087 00088 // Return true if this ColPartitionSet makes a legal column candidate by 00089 // having legal individual partitions and non-overlapping adjacent pairs. 00090 bool LegalColumnCandidate(); 00091 00092 // Return a copy of this. If good_only will only copy the Good ColPartitions. 00093 ColPartitionSet* Copy(bool good_only); 00094 00095 // Display the edges of the columns at the given y coords. 00096 void DisplayColumnEdges(int y_bottom, int y_top, ScrollView* win); 00097 00098 // Return the PolyBlockType that best explains the columns overlapped 00099 // by the given coords(left,right,y), with the given margins. 00100 // Also return the first and last column index touched by the coords and 00101 // the leftmost and rightmost spanned columns. 00102 // Column indices are 2n + 1 for real colums (0 based) and even values 00103 // represent the gaps in between columns, with 0 being left of the leftmost. 00104 PolyBlockType SpanningType(BlobRegionType type, int left, int right, int y, 00105 int left_margin, int right_margin, 00106 int* first_col, int* last_col, 00107 int* first_spanned_col, int* last_spanned_col); 00108 00109 // The column_set has changed. Close down all in-progress WorkingPartSets in 00110 // columns that do not match and start new ones for the new columns in this. 00111 // As ColPartitions are turned into BLOCKs, the used ones are put in 00112 // used_parts, as they still need to be referenced in the grid. 00113 void ChangeWorkColumns(const ICOORD& bleft, const ICOORD& tright, 00114 int resolution, ColPartition_LIST* used_parts, 00115 WorkingPartSet_LIST* working_set); 00116 00117 // Accumulate the widths and gaps into the given variables. 00118 void AccumulateColumnWidthsAndGaps(int* total_width, int* width_samples, 00119 int* total_gap, int* gap_samples); 00120 00121 // Provide debug output for this ColPartitionSet and all the ColPartitions. 00122 void Print(); 00123 00124 private: 00125 // Add the given partition to the list in the appropriate place. 00126 void AddPartition(ColPartition* new_part, ColPartition_IT* it); 00127 00128 // Compute the coverage and good column count. 00129 void ComputeCoverage(); 00130 00131 // The partitions in this column candidate. 00132 ColPartition_LIST parts_; 00133 // The number of partitions that have a frequent column width. 00134 int good_column_count_; 00135 // Total width of all the ColPartitions. 00136 int total_coverage_; 00137 // Bounding box of all partitions in the set. 00138 TBOX bounding_box_; 00139 }; 00140 00141 ELISTIZEH(ColPartitionSet) 00142 00143 } // namespace tesseract. 00144 00145 #endif // TESSERACT_TEXTORD_COLPARTITION_H__