comparison mupdf-source/thirdparty/tesseract/src/classify/mfx.cpp @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /******************************************************************************
2 ** Filename: mfx.c
3 ** Purpose: Micro feature extraction routines
4 ** Author: Dan Johnson
5 **
6 ** (c) Copyright Hewlett-Packard Company, 1988.
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 #include "mfx.h"
19
20 #include "clusttool.h" //NEEDED
21 #include "intfx.h"
22 #include "mfdefs.h"
23 #include "mfoutline.h"
24 #include "normalis.h"
25 #include "params.h"
26
27 namespace tesseract {
28
29 /* old numbers corresponded to 10.0 degrees and 80.0 degrees */
30 double_VAR(classify_min_slope, 0.414213562, "Slope below which lines are called horizontal");
31 double_VAR(classify_max_slope, 2.414213562, "Slope above which lines are called vertical");
32
33 /*----------------------------------------------------------------------------
34 Private Function Prototypes
35 -----------------------------------------------------------------------------*/
36
37 MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatures);
38
39 MicroFeature ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End);
40
41 /*----------------------------------------------------------------------------
42 Public Code
43 ----------------------------------------------------------------------------*/
44
45 /**
46 * This routine extracts micro-features from the specified
47 * blob and returns a list of the micro-features. All
48 * micro-features are normalized according to the specified
49 * line statistics.
50 * @param Blob blob to extract micro-features from
51 * @param cn_denorm control parameter to feature extractor
52 * @return List of micro-features extracted from the blob.
53 */
54 MICROFEATURES BlobMicroFeatures(TBLOB *Blob, const DENORM &cn_denorm) {
55 MICROFEATURES MicroFeatures;
56 LIST Outlines;
57 LIST RemainingOutlines;
58
59 if (Blob != nullptr) {
60 Outlines = ConvertBlob(Blob);
61
62 RemainingOutlines = Outlines;
63 iterate(RemainingOutlines) {
64 auto Outline = static_cast<MFOUTLINE>(RemainingOutlines->first_node());
65 CharNormalizeOutline(Outline, cn_denorm);
66 }
67
68 RemainingOutlines = Outlines;
69 iterate(RemainingOutlines) {
70 auto Outline = static_cast<MFOUTLINE>(RemainingOutlines->first_node());
71 FindDirectionChanges(Outline, classify_min_slope, classify_max_slope);
72 MarkDirectionChanges(Outline);
73 MicroFeatures = ConvertToMicroFeatures(Outline, MicroFeatures);
74 }
75 FreeOutlines(Outlines);
76 }
77 return MicroFeatures;
78 } /* BlobMicroFeatures */
79
80 /*---------------------------------------------------------------------------
81 Private Code
82 ---------------------------------------------------------------------------*/
83
84 /**
85 * Convert Outline to MicroFeatures
86 * @param Outline outline to extract micro-features from
87 * @param MicroFeatures list of micro-features to add to
88 * @return List of micro-features with new features added to front.
89 * @note Globals: none
90 */
91 MICROFEATURES ConvertToMicroFeatures(MFOUTLINE Outline, MICROFEATURES MicroFeatures) {
92 MFOUTLINE Current;
93 MFOUTLINE Last;
94 MFOUTLINE First;
95
96 if (DegenerateOutline(Outline)) {
97 return (MicroFeatures);
98 }
99
100 First = NextExtremity(Outline);
101 Last = First;
102 do {
103 Current = NextExtremity(Last);
104 if (!PointAt(Current)->Hidden) {
105 auto NewFeature = ExtractMicroFeature(Last, Current);
106 MicroFeatures.push_front(NewFeature);
107 }
108 Last = Current;
109 } while (Last != First);
110
111 return MicroFeatures;
112 } /* ConvertToMicroFeatures */
113
114 /**
115 * This routine computes the feature parameters which describe
116 * the micro-feature that starts and Start and ends at End.
117 * A new micro-feature is allocated, filled with the feature
118 * parameters, and returned. The routine assumes that
119 * Start and End are not the same point. If they are the
120 * same point, nullptr is returned, a warning message is
121 * printed, and the current outline is dumped to stdout.
122 * @param Start starting point of micro-feature
123 * @param End ending point of micro-feature
124 * @return New micro-feature or nullptr if the feature was rejected.
125 * @note Globals: none
126 */
127 MicroFeature ExtractMicroFeature(MFOUTLINE Start, MFOUTLINE End) {
128 MFEDGEPT *P1, *P2;
129
130 P1 = PointAt(Start);
131 P2 = PointAt(End);
132
133 MicroFeature NewFeature;
134 NewFeature[(int)MicroFeatureParameter::MFXPosition] = AverageOf(P1->Point.x, P2->Point.x);
135 NewFeature[(int)MicroFeatureParameter::MFYPosition] = AverageOf(P1->Point.y, P2->Point.y);
136 NewFeature[(int)MicroFeatureParameter::MFLength] = DistanceBetween(P1->Point, P2->Point);
137 NewFeature[(int)MicroFeatureParameter::MFDirection] = NormalizedAngleFrom(&P1->Point, &P2->Point, 1.0);
138 NewFeature[(int)MicroFeatureParameter::MFBulge1] = 0.0f; // deprecated
139 NewFeature[(int)MicroFeatureParameter::MFBulge2] = 0.0f; // deprecated
140
141 return NewFeature;
142 } /* ExtractMicroFeature */
143
144 } // namespace tesseract