Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
helpers.h
Go to the documentation of this file.
1 /* -*-C-*-
2 ********************************************************************************
3 *
4 * File: helpers.h
5 * Description: General utility functions
6 * Author: Daria Antonova
7 * Created: Wed Apr 8 14:37:00 2009
8 * Language: C++
9 * Package: N/A
10 * Status: Reusable Software Component
11 *
12 * (c) Copyright 2009, Google Inc.
13 ** Licensed under the Apache License, Version 2.0 (the "License");
14 ** you may not use this file except in compliance with the License.
15 ** You may obtain a copy of the License at
16 ** http://www.apache.org/licenses/LICENSE-2.0
17 ** Unless required by applicable law or agreed to in writing, software
18 ** distributed under the License is distributed on an "AS IS" BASIS,
19 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 ** See the License for the specific language governing permissions and
21 ** limitations under the License.
22 *
23 ********************************************************************************/
24
25 #ifndef TESSERACT_CCUTIL_HELPERS_H_
26 #define TESSERACT_CCUTIL_HELPERS_H_
27
28 #include <stdio.h>
29 #include <string.h>
30
31 // Remove newline (if any) at the end of the string.
32 inline void chomp_string(char *str) {
33 int last_index = strlen(str) - 1;
34 while (last_index >= 0 &&
35 (str[last_index] == '\n' || str[last_index] == '\r')) {
36 str[last_index--] = '\0';
37 }
38 }
39
40 // Advance the current pointer of the file if it points to a newline character.
41 inline void SkipNewline(FILE *file) {
42 if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
43 }
44
45 // qsort function to sort 2 floats.
46 inline int sort_floats(const void *arg1, const void *arg2) {
47 float diff = *((float *) arg1) - *((float *) arg2);
48 if (diff > 0) {
49 return 1;
50 } else if (diff < 0) {
51 return -1;
52 } else {
53 return 0;
54 }
55 }
56
57 // return the smallest multiple of block_size greater than or equal to n.
58 inline int RoundUp(int n, int block_size) {
59 return block_size * ((n + block_size - 1) / block_size);
60 }
61
62 // Clip a numeric value to the interval [lower_bound, upper_bound].
63 template<typename T>
64 inline T ClipToRange(const T& x, const T& lower_bound, const T& upper_bound) {
65 if (x < lower_bound)
66 return lower_bound;
67 if (x > upper_bound)
68 return upper_bound;
69 return x;
70 }
71
72 // Extend the range [lower_bound, upper_bound] to include x.
73 template<typename T1, typename T2>
74 inline void UpdateRange(const T1& x, T2* lower_bound, T2* upper_bound) {
75 if (x < *lower_bound)
76 *lower_bound = x;
77 if (x > *upper_bound)
78 *upper_bound = x;
79 }
80
81 // Decrease lower_bound to be <= x_lo AND increase upper_bound to be >= x_hi.
82 template<typename T1, typename T2>
83 inline void UpdateRange(const T1& x_lo, const T1& x_hi,
84 T2* lower_bound, T2* upper_bound) {
85 if (x_lo < *lower_bound)
86 *lower_bound = x_lo;
87 if (x_hi > *upper_bound)
88 *upper_bound = x_hi;
89 }
90
91 // Intersect the range [*lower2, *upper2] with the range [lower1, upper1],
92 // putting the result back in [*lower2, *upper2].
93 // If non-intersecting ranges are given, we end up with *lower2 > *upper2.
94 template<typename T>
95 inline void IntersectRange(const T& lower1, const T& upper1,
96 T* lower2, T* upper2) {
97 if (lower1 > *lower2)
98 *lower2 = lower1;
99 if (upper1 < *upper2)
100 *upper2 = upper1;
101 }
102
103 // Proper modulo arithmetic operator. Returns a mod b that works for -ve a.
104 // For any integer a and positive b, returns r : 0<=r<b and a=n*b + r for
105 // some integer n.
106 inline int Modulo(int a, int b) {
107 return (a % b + b) % b;
108 }
109
110 // Integer division operator with rounding that works for negative input.
111 // Returns a divided by b, rounded to the nearest integer, without double
112 // counting at 0. With simple rounding 1/3 = 0, 0/3 = 0 -1/3 = 0, -2/3 = 0,
113 // -3/3 = 0 and -4/3 = -1.
114 // I want 1/3 = 0, 0/3 = 0, -1/3 = 0, -2/3 = -1, -3/3 = -1 and -4/3 = -1.
115 inline int DivRounded(int a, int b) {
116 if (b < 0) return -DivRounded(a, -b);
117 return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
118 }
119
120 // Return a double cast to int with rounding.
121 inline int IntCastRounded(double x) {
122 return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
123 }
124
125 // Reverse the order of bytes in a n byte quantity for big/little-endian switch.
126 inline void ReverseN(void* ptr, int num_bytes) {
127 char *cptr = reinterpret_cast<char *>(ptr);
128 int halfsize = num_bytes / 2;
129 for (int i = 0; i < halfsize; ++i) {
130 char tmp = cptr[i];
131 cptr[i] = cptr[num_bytes - 1 - i];
132 cptr[num_bytes - 1 - i] = tmp;
133 }
134 }
135
136 // Reverse the order of bytes in a 16 bit quantity for big/little-endian switch.
137 inline void Reverse16(void *ptr) {
138 ReverseN(ptr, 2);
139 }
140
141 // Reverse the order of bytes in a 32 bit quantity for big/little-endian switch.
142 inline void Reverse32(void *ptr) {
143 ReverseN(ptr, 4);
144 }
145
146 // Reverse the order of bytes in a 64 bit quantity for big/little-endian switch.
147 inline void Reverse64(void* ptr) {
148 ReverseN(ptr, 8);
149 }
150
151
152 #endif // TESSERACT_CCUTIL_HELPERS_H_