tesseract  5.0.0
mod128.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: mod128.cpp (Formerly dir128.c)
3  * Description: Code to convert a DIR128 to an ICOORD.
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 #include "mod128.h"
20 
21 namespace tesseract {
22 
23 static const TDimension idirtab[] = {
24  1000, 0, 998, 49, 995, 98, 989, 146, 980, 195, 970, 242, 956, 290, 941,
25  336, 923, 382, 903, 427, 881, 471, 857, 514, 831, 555, 803, 595, 773, 634,
26  740, 671, 707, 707, 671, 740, 634, 773, 595, 803, 555, 831, 514, 857, 471,
27  881, 427, 903, 382, 923, 336, 941, 290, 956, 242, 970, 195, 980, 146, 989,
28  98, 995, 49, 998, 0, 1000, -49, 998, -98, 995, -146, 989, -195, 980, -242,
29  970, -290, 956, -336, 941, -382, 923, -427, 903, -471, 881, -514, 857, -555, 831,
30  -595, 803, -634, 773, -671, 740, -707, 707, -740, 671, -773, 634, -803, 595, -831,
31  555, -857, 514, -881, 471, -903, 427, -923, 382, -941, 336, -956, 290, -970, 242,
32  -980, 195, -989, 146, -995, 98, -998, 49, -1000, 0, -998, -49, -995, -98, -989,
33  -146, -980, -195, -970, -242, -956, -290, -941, -336, -923, -382, -903, -427, -881, -471,
34  -857, -514, -831, -555, -803, -595, -773, -634, -740, -671, -707, -707, -671, -740, -634,
35  -773, -595, -803, -555, -831, -514, -857, -471, -881, -427, -903, -382, -923, -336, -941,
36  -290, -956, -242, -970, -195, -980, -146, -989, -98, -995, -49, -998, 0, -1000, 49,
37  -998, 98, -995, 146, -989, 195, -980, 242, -970, 290, -956, 336, -941, 382, -923,
38  427, -903, 471, -881, 514, -857, 555, -831, 595, -803, 634, -773, 671, -740, 707,
39  -707, 740, -671, 773, -634, 803, -595, 831, -555, 857, -514, 881, -471, 903, -427,
40  923, -382, 941, -336, 956, -290, 970, -242, 980, -195, 989, -146, 995, -98, 998,
41  -49};
42 
43 static const ICOORD *dirtab = reinterpret_cast<const ICOORD *>(idirtab);
44 
45 /**********************************************************************
46  * DIR128::DIR128
47  *
48  * Quantize the direction of an FCOORD to make a DIR128.
49  **********************************************************************/
50 
51 DIR128::DIR128( // from fcoord
52  const FCOORD fc // vector to quantize
53 ) {
54  int high, low, current; // binary search
55 
56  low = 0;
57  if (fc.y() == 0) {
58  if (fc.x() >= 0) {
59  dir = 0;
60  } else {
61  dir = MODULUS / 2;
62  }
63  return;
64  }
65  high = MODULUS;
66  do {
67  current = (high + low) / 2;
68  if (dirtab[current] * fc >= 0) {
69  low = current;
70  } else {
71  high = current;
72  }
73  } while (high - low > 1);
74  dir = low;
75 }
76 
77 } // namespace tesseract
#define MODULUS
Definition: mod128.h:26
int16_t TDimension
Definition: tesstypes.h:32
int8_t dir
Definition: mod128.h:83
float y() const
Definition: points.h:209
float x() const
Definition: points.h:206