Update Tesseract to version 4.
This commit is contained in:
56 files changed
+10765
-3462
No files matched your search
@@ -25,12 +25,63 @@
|
||||
#ifndef TESSERACT_CCUTIL_HELPERS_H_
|
||||
#define TESSERACT_CCUTIL_HELPERS_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <cassert>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
|
||||
#include "host.h"
|
||||
|
||||
// TODO(rays) Put the rest of the helpers in the namespace.
|
||||
namespace tesseract {
|
||||
|
||||
// A simple linear congruential random number generator, using Knuth's
|
||||
// constants from:
|
||||
// http://en.wikipedia.org/wiki/Linear_congruential_generator.
|
||||
class TRand {
|
||||
public:
|
||||
TRand() : seed_(1) {}
|
||||
// Sets the seed to the given value.
|
||||
void set_seed(uint64_t seed) {
|
||||
seed_ = seed;
|
||||
}
|
||||
// Sets the seed using a hash of a string.
|
||||
void set_seed(const std::string& str) {
|
||||
std::hash<std::string> hasher;
|
||||
set_seed(static_cast<uint64_t>(hasher(str)));
|
||||
}
|
||||
|
||||
// Returns an integer in the range 0 to INT32_MAX.
|
||||
int32_t IntRand() {
|
||||
Iterate();
|
||||
return seed_ >> 33;
|
||||
}
|
||||
// Returns a floating point value in the range [-range, range].
|
||||
double SignedRand(double range) {
|
||||
return range * 2.0 * IntRand() / INT32_MAX - range;
|
||||
}
|
||||
// Returns a floating point value in the range [0, range].
|
||||
double UnsignedRand(double range) {
|
||||
return range * IntRand() / INT32_MAX;
|
||||
}
|
||||
|
||||
private:
|
||||
// Steps the generator to the next value.
|
||||
void Iterate() {
|
||||
seed_ *= 6364136223846793005ULL;
|
||||
seed_ += 1442695040888963407ULL;
|
||||
}
|
||||
|
||||
// The current value of the seed.
|
||||
uint64_t seed_;
|
||||
};
|
||||
|
||||
} // namespace tesseract
|
||||
|
||||
// Remove newline (if any) at the end of the string.
|
||||
inline void chomp_string(char *str) {
|
||||
int last_index = strlen(str) - 1;
|
||||
int last_index = static_cast<int>(strlen(str)) - 1;
|
||||
while (last_index >= 0 &&
|
||||
(str[last_index] == '\n' || str[last_index] == '\r')) {
|
||||
str[last_index--] = '\0';
|
||||
@@ -42,16 +93,12 @@ inline void SkipNewline(FILE *file) {
|
||||
if (fgetc(file) != '\n') fseek(file, -1, SEEK_CUR);
|
||||
}
|
||||
|
||||
// qsort function to sort 2 floats.
|
||||
inline int sort_floats(const void *arg1, const void *arg2) {
|
||||
float diff = *((float *) arg1) - *((float *) arg2);
|
||||
if (diff > 0) {
|
||||
return 1;
|
||||
} else if (diff < 0) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
// Swaps the two args pointed to by the pointers.
|
||||
// Operator= and copy constructor must work on T.
|
||||
template<typename T> inline void Swap(T* p1, T* p2) {
|
||||
T tmp(*p2);
|
||||
*p2 = *p1;
|
||||
*p1 = tmp;
|
||||
}
|
||||
|
||||
// return the smallest multiple of block_size greater than or equal to n.
|
||||
@@ -122,9 +169,15 @@ inline int IntCastRounded(double x) {
|
||||
return x >= 0.0 ? static_cast<int>(x + 0.5) : -static_cast<int>(-x + 0.5);
|
||||
}
|
||||
|
||||
// Return a float cast to int with rounding.
|
||||
inline int IntCastRounded(float x) {
|
||||
return x >= 0.0f ? static_cast<int>(x + 0.5f) : -static_cast<int>(-x + 0.5f);
|
||||
}
|
||||
|
||||
// Reverse the order of bytes in a n byte quantity for big/little-endian switch.
|
||||
inline void ReverseN(void* ptr, int num_bytes) {
|
||||
char *cptr = reinterpret_cast<char *>(ptr);
|
||||
assert(num_bytes == 1 || num_bytes == 2 || num_bytes == 4 || num_bytes == 8);
|
||||
char* cptr = static_cast<char*>(ptr);
|
||||
int halfsize = num_bytes / 2;
|
||||
for (int i = 0; i < halfsize; ++i) {
|
||||
char tmp = cptr[i];
|
||||
|
||||
Reference in new issue
Block a user