tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
ocrblock.h
1 /**********************************************************************
2  * File: ocrblock.h (Formerly block.h)
3  * Description: Page block class definition.
4  * Author: Ray Smith
5  * Created: Thu Mar 14 17:32:01 GMT 1991
6  *
7  * (C) Copyright 1991, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #ifndef OCRBLOCK_H
21 #define OCRBLOCK_H
22 
23 #include "ocrpara.h"
24 #include "ocrrow.h"
25 #include "pdblock.h"
26 
27 class BLOCK; //forward decl
28 
29 ELISTIZEH (BLOCK)
30 class BLOCK:public ELIST_LINK
31 //page block
32 {
33  friend class BLOCK_RECT_IT; //block iterator
34  public:
36  : re_rotation_(1.0f, 0.0f),
37  classify_rotation_(1.0f, 0.0f),
38  skew_(1.0f, 0.0f) {
39  right_to_left_ = false;
40  pdblk.hand_poly = nullptr;
41  }
42  BLOCK(const char *name, //< filename
43  BOOL8 prop, //< proportional
44  int16_t kern, //< kerning
45  int16_t space, //< spacing
46  int16_t xmin, //< bottom left
47  int16_t ymin,
48  int16_t xmax, //< top right
49  int16_t ymax);
50 
51  ~BLOCK () = default;
52 
60  void set_stats(BOOL8 prop,
61  int16_t kern,
62  int16_t space,
63  int16_t ch_pitch) {
64  proportional = prop;
65  kerning = (int8_t) kern;
66  spacing = space;
67  pitch = ch_pitch;
68  }
70  void set_xheight(int32_t height) {
71  xheight = height;
72  }
74  void set_font_class(int16_t font) {
75  font_class = font;
76  }
78  BOOL8 prop() const {
79  return proportional;
80  }
81  bool right_to_left() const {
82  return right_to_left_;
83  }
84  void set_right_to_left(bool value) {
85  right_to_left_ = value;
86  }
88  int32_t fixed_pitch() const {
89  return pitch;
90  }
92  int16_t kern() const {
93  return kerning;
94  }
96  int16_t font() const {
97  return font_class;
98  }
100  int16_t space() const {
101  return spacing;
102  }
104  const char *name() const {
105  return filename.string ();
106  }
108  int32_t x_height() const {
109  return xheight;
110  }
111  float cell_over_xheight() const {
112  return cell_over_xheight_;
113  }
114  void set_cell_over_xheight(float ratio) {
115  cell_over_xheight_ = ratio;
116  }
118  ROW_LIST *row_list() {
119  return &rows;
120  }
121  // Compute the margins between the edges of each row and this block's
122  // polyblock, and store the results in the rows.
123  void compute_row_margins();
124 
125  // get paragraphs
126  PARA_LIST *para_list() {
127  return &paras_;
128  }
130  C_BLOB_LIST *blob_list() {
131  return &c_blobs;
132  }
133  C_BLOB_LIST *reject_blobs() {
134  return &rej_blobs;
135  }
136  FCOORD re_rotation() const {
137  return re_rotation_; // How to transform coords back to image.
138  }
139  void set_re_rotation(const FCOORD& rotation) {
140  re_rotation_ = rotation;
141  }
143  return classify_rotation_; // Apply this before classifying.
144  }
145  void set_classify_rotation(const FCOORD& rotation) {
146  classify_rotation_ = rotation;
147  }
148  FCOORD skew() const {
149  return skew_; // Direction of true horizontal.
150  }
151  void set_skew(const FCOORD& skew) {
152  skew_ = skew;
153  }
154  const ICOORD& median_size() const {
155  return median_size_;
156  }
157  void set_median_size(int x, int y) {
158  median_size_.set_x(x);
159  median_size_.set_y(y);
160  }
161 
162  Pix* render_mask(TBOX* mask_box) {
163  return pdblk.render_mask(re_rotation_, mask_box);
164  }
165 
166  // Returns the bounding box including the desired combination of upper and
167  // lower noise/diacritic elements.
168  TBOX restricted_bounding_box(bool upper_dots, bool lower_dots) const;
169 
170  // Reflects the polygon in the y-axis and recomputes the bounding_box.
171  // Does nothing to any contained rows/words/blobs etc.
172  void reflect_polygon_in_y_axis();
173 
174  void rotate(const FCOORD& rotation);
175 
177  void sort_rows();
178 
180  void compress();
181 
183  void check_pitch();
184 
186  void compress(const ICOORD vec);
187 
189  void print(FILE* fp, bool dump);
190 
191  BLOCK& operator=(const BLOCK & source);
192  PDBLK pdblk; //< Page Description Block
193 
194  private:
195  BOOL8 proportional; //< proportional
196  bool right_to_left_; //< major script is right to left.
197  int8_t kerning; //< inter blob gap
198  int16_t spacing; //< inter word gap
199  int16_t pitch; //< pitch of non-props
200  int16_t font_class; //< correct font class
201  int32_t xheight; //< height of chars
202  float cell_over_xheight_; //< Ratio of cell height to xheight.
203  STRING filename; //< name of block
204  ROW_LIST rows; //< rows in block
205  PARA_LIST paras_; //< paragraphs of block
206  C_BLOB_LIST c_blobs; //< before textord
207  C_BLOB_LIST rej_blobs; //< duff stuff
208  FCOORD re_rotation_; //< How to transform coords back to image.
209  FCOORD classify_rotation_; //< Apply this before classifying.
210  FCOORD skew_; //< Direction of true horizontal.
211  ICOORD median_size_; //< Median size of blobs.
212 };
213 
214 // A function to print segmentation stats for the given block list.
215 void PrintSegmentationStats(BLOCK_LIST* block_list);
216 
217 // Extracts blobs fromo the given block list and adds them to the output list.
218 // The block list must have been created by performing a page segmentation.
219 void ExtractBlobsFromSegmentation(BLOCK_LIST* blocks,
220  C_BLOB_LIST* output_blob_list);
221 
222 // Refreshes the words in the block_list by using blobs in the
223 // new_blobs list.
224 // Block list must have word segmentation in it.
225 // It consumes the blobs provided in the new_blobs list. The blobs leftover in
226 // the new_blobs list after the call weren't matched to any blobs of the words
227 // in block list.
228 // The output not_found_blobs is a list of blobs from the original segmentation
229 // in the block_list for which no corresponding new blobs were found.
230 void RefreshWordBlobsFromNewBlobs(BLOCK_LIST* block_list,
231  C_BLOB_LIST* new_blobs,
232  C_BLOB_LIST* not_found_blobs);
233 
234 #endif
void set_median_size(int x, int y)
Definition: ocrblock.h:157
int16_t kern() const
return kerning
Definition: ocrblock.h:92
C_BLOB_LIST * reject_blobs()
Definition: ocrblock.h:133
FCOORD classify_rotation() const
Definition: ocrblock.h:142
FCOORD skew_
Definition: ocrblock.h:210
void set_xheight(int32_t height)
set char size
Definition: ocrblock.h:70
ROW_LIST * row_list()
get rows
Definition: ocrblock.h:118
void set_classify_rotation(const FCOORD &rotation)
Definition: ocrblock.h:145
int16_t font() const
return font class
Definition: ocrblock.h:96
void set_right_to_left(bool value)
Definition: ocrblock.h:84
PARA_LIST * para_list()
Definition: ocrblock.h:126
void set_re_rotation(const FCOORD &rotation)
Definition: ocrblock.h:139
Definition: rect.h:34
void set_skew(const FCOORD &skew)
Definition: ocrblock.h:151
page block
Definition: pdblock.h:32
int16_t space() const
return spacing
Definition: ocrblock.h:100
void set_stats(BOOL8 prop, int16_t kern, int16_t space, int16_t ch_pitch)
Definition: ocrblock.h:60
float cell_over_xheight() const
Definition: ocrblock.h:111
const char * name() const
return filename
Definition: ocrblock.h:104
STRING filename
Definition: ocrblock.h:203
Definition: pdblock.h:103
FCOORD classify_rotation_
Definition: ocrblock.h:209
ICOORD median_size_
Definition: ocrblock.h:211
const ICOORD & median_size() const
Definition: ocrblock.h:154
void set_cell_over_xheight(float ratio)
Definition: ocrblock.h:114
Definition: ocrblock.h:30
int32_t fixed_pitch() const
return pitch
Definition: ocrblock.h:88
BOOL8 proportional
Definition: ocrblock.h:195
int16_t spacing
Definition: ocrblock.h:198
C_BLOB_LIST rej_blobs
Definition: ocrblock.h:207
int16_t ymax
Definition: pdblock.h:138
bool right_to_left() const
Definition: ocrblock.h:81
BLOCK()
Definition: ocrblock.h:35
Definition: strngs.h:45
PDBLK pdblk
Definition: ocrblock.h:192
Pix * render_mask(TBOX *mask_box)
Definition: ocrblock.h:162
int32_t x_height() const
return xheight
Definition: ocrblock.h:108
PARA_LIST paras_
Definition: ocrblock.h:205
integer coordinate
Definition: points.h:32
float cell_over_xheight_
Definition: ocrblock.h:202
int8_t kerning
Definition: ocrblock.h:197
int16_t ymin
Definition: pdblock.h:137
FCOORD re_rotation() const
Definition: ocrblock.h:136
void set_font_class(int16_t font)
set font class
Definition: ocrblock.h:74
int16_t pitch
Definition: ocrblock.h:199
bool right_to_left_
Definition: ocrblock.h:196
int16_t font_class
Definition: ocrblock.h:200
C_BLOB_LIST c_blobs
Definition: ocrblock.h:206
C_BLOB_LIST * blob_list()
get blobs
Definition: ocrblock.h:130
FCOORD re_rotation_
Definition: ocrblock.h:208
ROW_LIST rows
Definition: ocrblock.h:204
FCOORD skew() const
Definition: ocrblock.h:148
Definition: points.h:189
int32_t xheight
Definition: ocrblock.h:201
BOOL8 prop() const
return proportional
Definition: ocrblock.h:78