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