Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
blobs.h
Go to the documentation of this file.
1 /* -*-C-*-
2  ********************************************************************************
3  *
4  * File: blobs.h (Formerly blobs.h)
5  * Description: Blob definition
6  * Author: Mark Seaman, OCR Technology
7  * Created: Fri Oct 27 15:39:52 1989
8  * Modified: Thu Mar 28 15:33:38 1991 (Mark Seaman) marks@hpgrlt
9  * Language: C
10  * Package: N/A
11  * Status: Experimental (Do Not Distribute)
12  *
13  * (c) Copyright 1989, Hewlett-Packard Company.
14  ** Licensed under the Apache License, Version 2.0 (the "License");
15  ** you may not use this file except in compliance with the License.
16  ** You may obtain a copy of the License at
17  ** http://www.apache.org/licenses/LICENSE-2.0
18  ** Unless required by applicable law or agreed to in writing, software
19  ** distributed under the License is distributed on an "AS IS" BASIS,
20  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  ** See the License for the specific language governing permissions and
22  ** limitations under the License.
23  *
24  *********************************************************************************/
25 
26 #ifndef BLOBS_H
27 #define BLOBS_H
28 
29 /*----------------------------------------------------------------------
30  I n c l u d e s
31 ----------------------------------------------------------------------*/
32 #include "clst.h"
33 #include "rect.h"
34 #include "vecfuncs.h"
35 
36 class BLOCK;
37 class C_BLOB;
38 class DENORM;
39 class ROW;
40 class WERD;
41 
42 /*----------------------------------------------------------------------
43  T y p e s
44 ----------------------------------------------------------------------*/
45 #define EDGEPTFLAGS 4 /*concavity,length etc. */
46 
47 typedef struct
48 { /* Widths of pieces */
49  int num_chars;
50  int widths[1];
51 } WIDTH_RECORD;
52 
53 struct TPOINT {
54  TPOINT(): x(0), y(0) {}
55  TPOINT(inT16 vx, inT16 vy) : x(vx), y(vy) {}
56  TPOINT(const ICOORD &ic) : x(ic.x()), y(ic.y()) {}
57 
58  void operator+=(const TPOINT& other) {
59  x += other.x;
60  y += other.y;
61  }
62  void operator/=(int divisor) {
63  x /= divisor;
64  y /= divisor;
65  }
66 
67  inT16 x; // absolute x coord.
68  inT16 y; // absolute y coord.
69 };
70 typedef TPOINT VECTOR; // structure for coordinates.
71 
72 struct EDGEPT {
74  memset(flags, 0, EDGEPTFLAGS * sizeof(flags[0]));
75  }
76  EDGEPT(const EDGEPT& src) : next(NULL), prev(NULL) {
77  CopyFrom(src);
78  }
79  EDGEPT& operator=(const EDGEPT& src) {
80  CopyFrom(src);
81  return *this;
82  }
83  // Copies the data elements, but leaves the pointers untouched.
84  void CopyFrom(const EDGEPT& src) {
85  pos = src.pos;
86  vec = src.vec;
87  memcpy(flags, src.flags, EDGEPTFLAGS * sizeof(flags[0]));
88  }
89  // Accessors to hide or reveal a cut edge from feature extractors.
90  void Hide() {
91  flags[0] = true;
92  }
93  void Reveal() {
94  flags[0] = false;
95  }
96  bool IsHidden() const {
97  return flags[0] != 0;
98  }
99 
100  TPOINT pos; // position
101  VECTOR vec; // vector to next point
102  // TODO(rays) Remove flags and replace with
103  // is_hidden, runlength, dir, and fixed. The only use
104  // of the flags other than is_hidden is in polyaprx.cpp.
105  char flags[EDGEPTFLAGS]; // concavity, length etc
106  EDGEPT* next; // anticlockwise element
107  EDGEPT* prev; // clockwise element
108 };
109 
110 // For use in chop and findseam to keep a list of which EDGEPTs were inserted.
112 
113 struct TESSLINE {
114  TESSLINE() : is_hole(false), loop(NULL), next(NULL) {}
115  TESSLINE(const TESSLINE& src) : loop(NULL), next(NULL) {
116  CopyFrom(src);
117  }
119  Clear();
120  }
121  TESSLINE& operator=(const TESSLINE& src) {
122  CopyFrom(src);
123  return *this;
124  }
125  // Consume the circular list of EDGEPTs to make a TESSLINE.
126  static TESSLINE* BuildFromOutlineList(EDGEPT* outline);
127  // Copies the data and the outline, but leaves next untouched.
128  void CopyFrom(const TESSLINE& src);
129  // Deletes owned data.
130  void Clear();
131  // Normalize in-place using the DENORM.
132  void Normalize(const DENORM& denorm);
133  // Rotates by the given rotation in place.
134  void Rotate(const FCOORD rotation);
135  // Moves by the given vec in place.
136  void Move(const ICOORD vec);
137  // Scales by the given factor in place.
138  void Scale(float factor);
139  // Sets up the start and vec members of the loop from the pos members.
140  void SetupFromPos();
141  // Recomputes the bounding box from the points in the loop.
142  void ComputeBoundingBox();
143  // Computes the min and max cross product of the outline points with the
144  // given vec and returns the results in min_xp and max_xp. Geometrically
145  // this is the left and right edge of the outline perpendicular to the
146  // given direction, but to get the distance units correct, you would
147  // have to divide by the modulus of vec.
148  void MinMaxCrossProduct(const TPOINT vec, int* min_xp, int* max_xp) const;
149 
150  TBOX bounding_box() const;
151  // Returns true if the point is contained within the outline box.
152  bool Contains(const TPOINT& pt) {
153  return topleft.x <= pt.x && pt.x <= botright.x &&
154  botright.y <= pt.y && pt.y <= topleft.y;
155  }
156 
157  #ifndef GRAPHICS_DISABLED
158  void plot(ScrollView* window, ScrollView::Color color,
159  ScrollView::Color child_color);
160  #endif // GRAPHICS_DISABLED
161 
162  int BBArea() const {
163  return (botright.x - topleft.x) * (topleft.y - botright.y);
164  }
165 
166  TPOINT topleft; // Top left of loop.
167  TPOINT botright; // Bottom right of loop.
168  TPOINT start; // Start of loop.
169  bool is_hole; // True if this is a hole/child outline.
170  EDGEPT *loop; // Edgeloop.
171  TESSLINE *next; // Next outline in blob.
172 }; // Outline structure.
173 
174 struct TBLOB {
176  TBLOB(const TBLOB& src) : outlines(NULL), next(NULL) {
177  CopyFrom(src);
178  }
179  ~TBLOB() {
180  Clear();
181  }
182  TBLOB& operator=(const TBLOB& src) {
183  CopyFrom(src);
184  return *this;
185  }
186  // Factory to build a TBLOB from a C_BLOB with polygonal
187  // approximation along the way.
188  static TBLOB* PolygonalCopy(C_BLOB* src);
189  // Normalizes the blob for classification only if needed.
190  // (Normally this means a non-zero classify rotation.)
191  // If no Normalization is needed, then NULL is returned, and the denorm is
192  // unchanged. Otherwise a new TBLOB is returned and the denorm points to
193  // a new DENORM. In this case, both the TBLOB and DENORM must be deleted.
194  TBLOB* ClassifyNormalizeIfNeeded(const DENORM** denorm) const;
195  // Copies the data and the outlines, but leaves next untouched.
196  void CopyFrom(const TBLOB& src);
197  // Deletes owned data.
198  void Clear();
199  // Normalize in-place using the DENORM.
200  void Normalize(const DENORM& denorm);
201  // Rotates by the given rotation in place.
202  void Rotate(const FCOORD rotation);
203  // Moves by the given vec in place.
204  void Move(const ICOORD vec);
205  // Scales by the given factor in place.
206  void Scale(float factor);
207  // Recomputes the bounding boxes of the outlines.
208  void ComputeBoundingBoxes();
209 
210  // Returns the number of outlines.
211  int NumOutlines() const;
212 
213  TBOX bounding_box() const;
214 
215  #ifndef GRAPHICS_DISABLED
216  void plot(ScrollView* window, ScrollView::Color color,
217  ScrollView::Color child_color);
218  #endif // GRAPHICS_DISABLED
219 
220  int BBArea() const {
221  int total_area = 0;
222  for (TESSLINE* outline = outlines; outline != NULL; outline = outline->next)
223  total_area += outline->BBArea();
224  return total_area;
225  }
226 
227  TESSLINE *outlines; // List of outlines in blob.
228  TBLOB *next; // Next blob in block.
229 }; // Blob structure.
230 
231 int count_blobs(TBLOB *blobs);
232 
233 struct TWERD {
234  TWERD() : blobs(NULL), latin_script(false), next(NULL) {}
235  TWERD(const TWERD& src) : blobs(NULL), next(NULL) {
236  CopyFrom(src);
237  }
238  ~TWERD() {
239  Clear();
240  }
241  TWERD& operator=(const TWERD& src) {
242  CopyFrom(src);
243  return *this;
244  }
245  // Factory to build a TWERD from a (C_BLOB) WERD, with polygonal
246  // approximation along the way.
247  static TWERD* PolygonalCopy(WERD* src);
248  // Setup for Baseline normalization, recording the normalization in the
249  // DENORM, but doesn't do any normalization.
250  void SetupBLNormalize(const BLOCK* block, const ROW* row,
251  float x_height, bool numeric_mode,
252  DENORM* denorm) const;
253  // Normalize in-place using the DENORM.
254  void Normalize(const DENORM& denorm);
255  // Copies the data and the blobs, but leaves next untouched.
256  void CopyFrom(const TWERD& src);
257  // Deletes owned data.
258  void Clear();
259  // Recomputes the bounding boxes of the blobs.
260  void ComputeBoundingBoxes();
261 
262  // Returns the number of blobs in the word.
263  int NumBlobs() const {
264  return count_blobs(blobs);
265  }
266  TBOX bounding_box() const;
267 
268  // Merges the blobs from start to end, not including end, and deletes
269  // the blobs between start and end.
270  void MergeBlobs(int start, int end);
271 
272  void plot(ScrollView* window);
273 
274  TBLOB* blobs; // blobs in word.
275  bool latin_script; // This word is in a latin-based script.
276  TWERD* next; // next word.
277 };
278 
279 /*----------------------------------------------------------------------
280  M a c r o s
281 ----------------------------------------------------------------------*/
282 /**********************************************************************
283  * free_widths
284  *
285  * Free the memory taken up by a width array.
286  **********************************************************************/
287 #define free_widths(w) \
288 if (w) memfree (w)
289 
290 /*----------------------------------------------------------------------
291  F u n c t i o n s
292 ----------------------------------------------------------------------*/
293 // TODO(rays) This will become a member of TBLOB when TBLOB's definition
294 // moves to blobs.h
295 
296 // Returns the center of blob's bounding box in origin.
297 void blob_origin(TBLOB *blob, TPOINT *origin);
298 
299  /*blob to compute on */
301 
302 bool divisible_blob(TBLOB *blob, bool italic_blob, TPOINT* location);
303 
304 void divide_blobs(TBLOB *blob, TBLOB *other_blob, bool italic_blob,
305  const TPOINT& location);
306 
307 #endif