• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**********************************************************************
2  * File:        mod128.c  (Formerly dir128.c)
3  * Description: Code to convert a DIR128 to an ICOORD.
4  * Author:					Ray Smith
5  * Created:					Tue Oct 22 11:56:09 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 #include          "mfcpch.h"     //precompiled headers
21 #include          "mod128.h"
22 
23 static inT16 idirtab[] = {
24   1000, 0, 998, 49, 995, 98, 989, 146,
25   980, 195, 970, 242, 956, 290, 941, 336,
26   923, 382, 903, 427, 881, 471, 857, 514,
27   831, 555, 803, 595, 773, 634, 740, 671,
28   707, 707, 671, 740, 634, 773, 595, 803,
29   555, 831, 514, 857, 471, 881, 427, 903,
30   382, 923, 336, 941, 290, 956, 242, 970,
31   195, 980, 146, 989, 98, 995, 49, 998,
32   0, 1000, -49, 998, -98, 995, -146, 989,
33   -195, 980, -242, 970, -290, 956, -336, 941,
34   -382, 923, -427, 903, -471, 881, -514, 857,
35   -555, 831, -595, 803, -634, 773, -671, 740,
36   -707, 707, -740, 671, -773, 634, -803, 595,
37   -831, 555, -857, 514, -881, 471, -903, 427,
38   -923, 382, -941, 336, -956, 290, -970, 242,
39   -980, 195, -989, 146, -995, 98, -998, 49,
40   -1000, 0, -998, -49, -995, -98, -989, -146,
41   -980, -195, -970, -242, -956, -290, -941, -336,
42   -923, -382, -903, -427, -881, -471, -857, -514,
43   -831, -555, -803, -595, -773, -634, -740, -671,
44   -707, -707, -671, -740, -634, -773, -595, -803,
45   -555, -831, -514, -857, -471, -881, -427, -903,
46   -382, -923, -336, -941, -290, -956, -242, -970,
47   -195, -980, -146, -989, -98, -995, -49, -998,
48   0, -1000, 49, -998, 98, -995, 146, -989,
49   195, -980, 242, -970, 290, -956, 336, -941,
50   382, -923, 427, -903, 471, -881, 514, -857,
51   555, -831, 595, -803, 634, -773, 671, -740,
52   707, -707, 740, -671, 773, -634, 803, -595,
53   831, -555, 857, -514, 881, -471, 903, -427,
54   923, -382, 941, -336, 956, -290, 970, -242,
55   980, -195, 989, -146, 995, -98, 998, -49
56 };
57 
58 static ICOORD *dirtab = (ICOORD *) idirtab;
59 
60 /**********************************************************************
61  * DIR128::DIR128
62  *
63  * Quantize the direction of an FCOORD to make a DIR128.
64  **********************************************************************/
65 
DIR128(const FCOORD fc)66 DIR128::DIR128(                 //from fcoord
67                const FCOORD fc  //vector to quantize
68               ) {
69   int high, low, current;        //binary search
70 
71   low = 0;
72   if (fc.y () == 0) {
73     if (fc.x () >= 0)
74       dir = 0;
75     else
76       dir = MODULUS / 2;
77     return;
78   }
79   high = MODULUS;
80   do {
81     current = (high + low) / 2;
82     if (dirtab[current] * fc >= 0)
83       low = current;
84     else
85       high = current;
86   }
87   while (high - low > 1);
88   dir = low;
89 }
90 
91 
92 /**********************************************************************
93  * dir_to_gradient
94  *
95  * Convert a direction to a vector.
96  **********************************************************************/
97 
vector() const98 ICOORD DIR128::vector() const {  //convert to vector
99   return dirtab[dir];            //easy really
100 }
101