/src/tesseract/src/ccstruct/mod128.h
Line | Count | Source |
1 | | /********************************************************************** |
2 | | * File: mod128.h (Formerly dir128.h) |
3 | | * Description: Header for class which implements modulo arithmetic. |
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 | | #ifndef MOD128_H |
20 | | #define MOD128_H |
21 | | |
22 | | #include "points.h" |
23 | | |
24 | | namespace tesseract { |
25 | | |
26 | 1.18G | #define MODULUS 128 /*range of directions */ |
27 | 436M | #define DIRBITS 7 // no of bits used |
28 | | #define DIRSCALE 1000 // length of vector |
29 | | |
30 | | class DIR128 { |
31 | | public: |
32 | | DIR128() = default; |
33 | | |
34 | | DIR128( // constructor |
35 | 468M | int16_t value) { // value to assign |
36 | 468M | value %= MODULUS; // modulo arithmetic |
37 | 468M | if (value < 0) { |
38 | 0 | value += MODULUS; // done properly |
39 | 0 | } |
40 | 468M | dir = static_cast<int8_t>(value); |
41 | 468M | } |
42 | | DIR128(const FCOORD fc); // quantize vector |
43 | | |
44 | | DIR128 &operator=( // assign of int16_t |
45 | 97.9M | int16_t value) { // value to assign |
46 | 97.9M | value %= MODULUS; // modulo arithmetic |
47 | 97.9M | if (value < 0) { |
48 | 3.03M | value += MODULUS; // done properly |
49 | 3.03M | } |
50 | 97.9M | dir = static_cast<int8_t>(value); |
51 | 97.9M | return *this; |
52 | 97.9M | } |
53 | | int8_t operator-( // subtraction |
54 | | const DIR128 &minus) const // for signed result |
55 | 285M | { |
56 | | // result |
57 | 285M | int16_t result = dir - minus.dir; |
58 | | |
59 | 285M | if (result > MODULUS / 2) { |
60 | 13.5M | result -= MODULUS; // get in range |
61 | 272M | } else if (result < -MODULUS / 2) { |
62 | 29.9M | result += MODULUS; |
63 | 29.9M | } |
64 | 285M | return static_cast<int8_t>(result); |
65 | 285M | } |
66 | | DIR128 operator+( // addition |
67 | | const DIR128 &add) const // of itself |
68 | 25.5M | { |
69 | 25.5M | DIR128 result; // sum |
70 | | |
71 | 25.5M | result = dir + add.dir; // let = do the work |
72 | 25.5M | return result; |
73 | 25.5M | } |
74 | | DIR128 &operator+=( // same as + |
75 | 69.1M | const DIR128 &add) { |
76 | 69.1M | *this = dir + add.dir; // let = do the work |
77 | 69.1M | return *this; |
78 | 69.1M | } |
79 | 257M | int8_t get_dir() const { // access function |
80 | 257M | return dir; |
81 | 257M | } |
82 | | |
83 | | int8_t dir; // a direction |
84 | | }; |
85 | | |
86 | | } // namespace tesseract |
87 | | |
88 | | #endif |