/src/tesseract/src/classify/float2int.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /****************************************************************************** |
2 | | ** Filename: float2int.cpp |
3 | | ** Purpose: Routines for converting float features to int features |
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 "float2int.h" |
19 | | |
20 | | #include "classify.h" |
21 | | #include "mfoutline.h" |
22 | | #include "normmatch.h" |
23 | | #include "picofeat.h" |
24 | | |
25 | | #include "helpers.h" |
26 | | |
27 | 968M | #define MAX_INT_CHAR_NORM (INT_CHAR_NORM_RANGE - 1) |
28 | | |
29 | | /*---------------------------------------------------------------------------*/ |
30 | | namespace tesseract { |
31 | | |
32 | | /** |
33 | | * For each class in the unicharset, clears the corresponding |
34 | | * entry in char_norm_array. char_norm_array is indexed by unichar_id. |
35 | | * |
36 | | * Globals: |
37 | | * - none |
38 | | * |
39 | | * @param char_norm_array array to be cleared |
40 | | */ |
41 | 494k | void Classify::ClearCharNormArray(uint8_t *char_norm_array) { |
42 | 494k | memset(char_norm_array, 0, sizeof(*char_norm_array) * unicharset.size()); |
43 | 494k | } /* ClearCharNormArray */ |
44 | | |
45 | | /*---------------------------------------------------------------------------*/ |
46 | | /** |
47 | | * For each class in unicharset, computes the match between |
48 | | * norm_feature and the normalization protos for that class. |
49 | | * Converts this number to the range from 0 - 255 and stores it |
50 | | * into char_norm_array. CharNormArray is indexed by unichar_id. |
51 | | * |
52 | | * Globals: |
53 | | * - PreTrainedTemplates current set of built-in templates |
54 | | * |
55 | | * @param norm_feature character normalization feature |
56 | | * @param[out] char_norm_array place to put results of size unicharset.size() |
57 | | */ |
58 | | void Classify::ComputeIntCharNormArray(const FEATURE_STRUCT &norm_feature, |
59 | 2.01M | uint8_t *char_norm_array) { |
60 | 970M | for (unsigned i = 0; i < unicharset.size(); i++) { |
61 | 968M | if (i < PreTrainedTemplates->NumClasses) { |
62 | 228M | int norm_adjust = |
63 | 228M | static_cast<int>(INT_CHAR_NORM_RANGE * ComputeNormMatch(i, norm_feature, false)); |
64 | 228M | char_norm_array[i] = ClipToRange(norm_adjust, 0, MAX_INT_CHAR_NORM); |
65 | 740M | } else { |
66 | | // Classes with no templates (eg. ambigs & ligatures) default |
67 | | // to worst match. |
68 | 740M | char_norm_array[i] = MAX_INT_CHAR_NORM; |
69 | 740M | } |
70 | 968M | } |
71 | 2.01M | } /* ComputeIntCharNormArray */ |
72 | | |
73 | | /*---------------------------------------------------------------------------*/ |
74 | | /** |
75 | | * This routine converts each floating point pico-feature |
76 | | * in Features into integer format and saves it into |
77 | | * IntFeatures. |
78 | | * |
79 | | * Globals: |
80 | | * - none |
81 | | * |
82 | | * @param Features floating point pico-features to be converted |
83 | | * @param[out] IntFeatures array to put converted features into |
84 | | */ |
85 | 3.09k | void Classify::ComputeIntFeatures(FEATURE_SET Features, INT_FEATURE_ARRAY IntFeatures) { |
86 | 3.09k | float YShift; |
87 | | |
88 | 3.09k | if (classify_norm_method == baseline) { |
89 | 3.09k | YShift = BASELINE_Y_SHIFT; |
90 | 3.09k | } else { |
91 | 0 | YShift = Y_SHIFT; |
92 | 0 | } |
93 | | |
94 | 110k | for (int Fid = 0; Fid < Features->NumFeatures; Fid++) { |
95 | 107k | FEATURE Feature = Features->Features[Fid]; |
96 | | |
97 | 107k | IntFeatures[Fid].X = Bucket8For(Feature->Params[PicoFeatX], X_SHIFT, INT_FEAT_RANGE); |
98 | 107k | IntFeatures[Fid].Y = Bucket8For(Feature->Params[PicoFeatY], YShift, INT_FEAT_RANGE); |
99 | 107k | IntFeatures[Fid].Theta = |
100 | 107k | CircBucketFor(Feature->Params[PicoFeatDir], ANGLE_SHIFT, INT_FEAT_RANGE); |
101 | 107k | IntFeatures[Fid].CP_misses = 0; |
102 | 107k | } |
103 | 3.09k | } /* ComputeIntFeatures */ |
104 | | |
105 | | } // namespace tesseract |