Coverage Report

Created: 2025-06-13 07:15

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