/src/tesseract/src/ccstruct/stepblob.h
Line | Count | Source (jump to first uncovered line) |
1 | | /********************************************************************** |
2 | | * File: stepblob.h (Formerly cblob.h) |
3 | | * Description: Code for C_BLOB class. |
4 | | * Author: Ray Smith |
5 | | * Created: Tue Oct 08 10:41:13 BST 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 STEPBLOB_H |
21 | | #define STEPBLOB_H |
22 | | |
23 | | #include "coutln.h" // for C_OUTLINE_LIST, C_OUTLINE |
24 | | #include "elst.h" // for ELIST_ITERATOR, ELISTIZEH, ELIST_LINK |
25 | | #include "points.h" // for FCOORD, ICOORD (ptr only) |
26 | | #include "rect.h" // for TBOX |
27 | | #include "scrollview.h" // for ScrollView, ScrollView::Color |
28 | | |
29 | | #include <cstdint> // for int32_t, int16_t |
30 | | |
31 | | struct Pix; |
32 | | |
33 | | namespace tesseract { |
34 | | |
35 | | class C_BLOB; |
36 | | class DENORM; |
37 | | |
38 | | ELISTIZEH(C_BLOB) |
39 | | |
40 | | class TESS_API C_BLOB : public ELIST_LINK { |
41 | | public: |
42 | 757k | C_BLOB() = default; |
43 | | explicit C_BLOB(C_OUTLINE_LIST *outline_list); |
44 | | // Simpler constructor to build a blob from a single outline that has |
45 | | // already been fully initialized. |
46 | | explicit C_BLOB(C_OUTLINE *outline); |
47 | | |
48 | | // Builds a set of one or more blobs from a list of outlines. |
49 | | // Input: one outline on outline_list contains all the others, but the |
50 | | // nesting and order are undefined. |
51 | | // If good_blob is true, the blob is added to good_blobs_it, unless |
52 | | // an illegal (generation-skipping) parent-child relationship is found. |
53 | | // If so, the parent blob goes to bad_blobs_it, and the immediate children |
54 | | // are promoted to the top level, recursively being sent to good_blobs_it. |
55 | | // If good_blob is false, all created blobs will go to the bad_blobs_it. |
56 | | // Output: outline_list is empty. One or more blobs are added to |
57 | | // good_blobs_it and/or bad_blobs_it. |
58 | | static void ConstructBlobsFromOutlines(bool good_blob, C_OUTLINE_LIST *outline_list, |
59 | | C_BLOB_IT *good_blobs_it, C_BLOB_IT *bad_blobs_it); |
60 | | |
61 | | // Sets the COUT_INVERSE flag appropriately on the outlines and their |
62 | | // children recursively, reversing the outlines if needed so that |
63 | | // everything has an anticlockwise top-level. |
64 | | void CheckInverseFlagAndDirection(); |
65 | | |
66 | | // Build and return a fake blob containing a single fake outline with no |
67 | | // steps. |
68 | | static C_BLOB *FakeBlob(const TBOX &box); |
69 | | |
70 | 26.9M | C_OUTLINE_LIST *out_list() { // get outline list |
71 | 26.9M | return &outlines; |
72 | 26.9M | } |
73 | | |
74 | | TBOX bounding_box() const; // compute bounding box |
75 | | int32_t area(); // compute area |
76 | | int32_t perimeter(); // Total perimeter of outlines and 1st level children. |
77 | | int32_t outer_area(); // compute area |
78 | | int32_t count_transitions( // count maxima |
79 | | int32_t threshold); // size threshold |
80 | | |
81 | | void move(const ICOORD vec); // reposition blob by vector |
82 | | void rotate(const FCOORD &rotation); // Rotate by given vector. |
83 | | |
84 | | // Adds sub-pixel resolution EdgeOffsets for the outlines using greyscale |
85 | | // if the supplied pix is 8-bit or the binary edges if nullptr. |
86 | | void ComputeEdgeOffsets(int threshold, Image pix); |
87 | | |
88 | | // Estimates and returns the baseline position based on the shape of the |
89 | | // outlines. |
90 | | int16_t EstimateBaselinePosition(); |
91 | | |
92 | | // Returns a Pix rendering of the blob. pixDestroy after use. |
93 | | Image render(); |
94 | | // Returns a Pix rendering of the outline of the blob. (no fill). |
95 | | // pixDestroy after use. |
96 | | Image render_outline(); |
97 | | |
98 | | #ifndef GRAPHICS_DISABLED |
99 | | void plot( // draw one |
100 | | ScrollView *window, // window to draw in |
101 | | ScrollView::Color blob_colour, // for outer bits |
102 | | ScrollView::Color child_colour); // for holes |
103 | | // Draws the blob in the given colour, and child_colour, normalized |
104 | | // using the given denorm, making use of sub-pixel accurate information |
105 | | // if available. |
106 | | void plot_normed(const DENORM &denorm, ScrollView::Color blob_colour, |
107 | | ScrollView::Color child_colour, ScrollView *window); |
108 | | #endif // !GRAPHICS_DISABLED |
109 | | |
110 | 757k | C_BLOB &operator=(const C_BLOB &source) { |
111 | 757k | if (!outlines.empty()) { |
112 | 0 | outlines.clear(); |
113 | 0 | } |
114 | 757k | outlines.deep_copy(&source.outlines, &C_OUTLINE::deep_copy); |
115 | 757k | return *this; |
116 | 757k | } |
117 | | |
118 | 757k | static C_BLOB *deep_copy(const C_BLOB *src) { |
119 | 757k | auto *blob = new C_BLOB; |
120 | 757k | *blob = *src; |
121 | 757k | return blob; |
122 | 757k | } |
123 | | |
124 | 1.26M | static int SortByXMiddle(const void *v1, const void *v2) { |
125 | 1.26M | const C_BLOB *blob1 = *static_cast<const C_BLOB *const *>(v1); |
126 | 1.26M | const C_BLOB *blob2 = *static_cast<const C_BLOB *const *>(v2); |
127 | 1.26M | return blob1->bounding_box().x_middle() - blob2->bounding_box().x_middle(); |
128 | 1.26M | } |
129 | | |
130 | | private: |
131 | | C_OUTLINE_LIST outlines; // master elements |
132 | | }; |
133 | | |
134 | | } // namespace tesseract |
135 | | |
136 | | #endif |