00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef TESSERACT_CCUTIL_HELPERS_H_
00026 #define TESSERACT_CCUTIL_HELPERS_H_
00027
00028 #include <stdio.h>
00029 #include <string.h>
00030
00031
00032 inline void chomp_string(char *str) {
00033 int last_index = strlen(str) - 1;
00034 while (last_index >= 0 &&
00035 (str[last_index] == '\n' || str[last_index] == '\r')) {
00036 str[last_index--] = '\0';
00037 }
00038 }
00039
00040
00041 inline void SkipNewline(FILE *file) {
00042 if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
00043 }
00044
00045
00046 inline int sort_floats(const void *arg1, const void *arg2) {
00047 float diff = *((float *) arg1) - *((float *) arg2);
00048 if (diff > 0) {
00049 return 1;
00050 } else if (diff < 0) {
00051 return -1;
00052 } else {
00053 return 0;
00054 }
00055 }
00056
00057
00058 inline int RoundUp(int n, int block_size) {
00059 return block_size * ((n + block_size - 1) / block_size);
00060 }
00061
00062
00063 template<typename T>
00064 inline T ClipToRange(const T& x, const T& lower_bound, const T& upper_bound) {
00065 if (x < lower_bound)
00066 return lower_bound;
00067 if (x > upper_bound)
00068 return upper_bound;
00069 return x;
00070 }
00071
00072
00073 template<typename T1, typename T2>
00074 inline void UpdateRange(const T1& x, T2* lower_bound, T2* upper_bound) {
00075 if (x < *lower_bound)
00076 *lower_bound = x;
00077 if (x > *upper_bound)
00078 *upper_bound = x;
00079 }
00080
00081
00082 template<typename T1, typename T2>
00083 inline void UpdateRange(const T1& x_lo, const T1& x_hi,
00084 T2* lower_bound, T2* upper_bound) {
00085 if (x_lo < *lower_bound)
00086 *lower_bound = x_lo;
00087 if (x_hi > *upper_bound)
00088 *upper_bound = x_hi;
00089 }
00090
00091
00092
00093
00094 template<typename T>
00095 inline void IntersectRange(const T& lower1, const T& upper1,
00096 T* lower2, T* upper2) {
00097 if (lower1 > *lower2)
00098 *lower2 = lower1;
00099 if (upper1 < *upper2)
00100 *upper2 = upper1;
00101 }
00102
00103
00104
00105
00106 inline int Modulo(int a, int b) {
00107 return (a % b + b) % b;
00108 }
00109
00110
00111
00112
00113
00114
00115 inline int DivRounded(int a, int b) {
00116 if (b < 0) return -DivRounded(a, -b);
00117 return a >= 0 ? (a + b / 2) / b : (a - b / 2) / b;
00118 }
00119
00120
00121 inline int IntCastRounded(double x) {
00122 return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
00123 }
00124
00125
00126 inline void ReverseN(void* ptr, int num_bytes) {
00127 char *cptr = reinterpret_cast<char *>(ptr);
00128 int halfsize = num_bytes / 2;
00129 for (int i = 0; i < halfsize; ++i) {
00130 char tmp = cptr[i];
00131 cptr[i] = cptr[num_bytes - 1 - i];
00132 cptr[num_bytes - 1 - i] = tmp;
00133 }
00134 }
00135
00136
00137 inline void Reverse16(void *ptr) {
00138 ReverseN(ptr, 2);
00139 }
00140
00141
00142 inline void Reverse32(void *ptr) {
00143 ReverseN(ptr, 4);
00144 }
00145
00146
00147 inline void Reverse64(void* ptr) {
00148 ReverseN(ptr, 8);
00149 }
00150
00151
00152 #endif // TESSERACT_CCUTIL_HELPERS_H_