89 #include "config_auto.h" 98 #include "allheaders.h" 131 PROCNAME(
"setMsgSeverity");
134 if (newsev == L_SEVERITY_EXTERNAL) {
135 envsev = getenv(
"LEPT_MSG_SEVERITY");
139 L_INFO(
"message severity set to external\n", procName);
143 L_WARNING(
"environment var LEPT_MSG_SEVERITY not defined\n",
150 L_INFO(
"message severity set to %d\n", procName, newsev);
178 const char *procname,
181 fprintf(stderr,
"Error in %s: %s\n", procname, msg);
196 const char *procname,
199 fprintf(stderr,
"Error in %s: %s\n", procname, msg);
214 const char *procname,
217 fprintf(stderr,
"Error in %s: %s\n", procname, msg);
239 size_t nbytes1, nbytes2;
240 l_uint8 *array1, *array2;
242 PROCNAME(
"filesAreIdentical");
245 return ERROR_INT(
"&same not defined", procName, 1);
247 if (!fname1 || !fname2)
248 return ERROR_INT(
"both names not defined", procName, 1);
252 if (nbytes1 != nbytes2)
256 return ERROR_INT(
"array1 not read", procName, 1);
257 if ((array2 =
l_binaryRead(fname2, &nbytes2)) == NULL) {
259 return ERROR_INT(
"array2 not read", procName, 1);
262 for (i = 0; i < nbytes1; i++) {
263 if (array1[i] != array2[i]) {
290 convertOnBigEnd16(l_uint16 shortin)
292 return ((shortin << 8) | (shortin >> 8));
296 convertOnLittleEnd16(l_uint16 shortin)
304 convertOnLittleEnd16(l_uint16 shortin)
306 return ((shortin << 8) | (shortin >> 8));
310 convertOnBigEnd16(l_uint16 shortin)
324 convertOnBigEnd32(l_uint32 wordin)
326 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
327 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
331 convertOnLittleEnd32(l_uint32 wordin)
339 convertOnLittleEnd32(l_uint32 wordin)
341 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
342 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
346 convertOnBigEnd32(l_uint32 wordin)
383 l_int32 i, locb, sizeb, rembytes;
384 size_t inbytes, outbytes;
385 l_uint8 *datain, *dataout;
387 PROCNAME(
"fileCorruptByDeletion");
389 if (!filein || !fileout)
390 return ERROR_INT(
"filein and fileout not both specified", procName, 1);
391 if (loc < 0.0 || loc >= 1.0)
392 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", procName, 1);
394 return ERROR_INT(
"size must be > 0.0", procName, 1);
395 if (loc + size > 1.0)
399 locb = (l_int32)(loc * inbytes + 0.5);
400 locb = L_MIN(locb, inbytes - 1);
401 sizeb = (l_int32)(size * inbytes + 0.5);
402 sizeb = L_MAX(1, sizeb);
403 sizeb = L_MIN(sizeb, inbytes - locb);
404 L_INFO(
"Removed %d bytes at location %d\n", procName, sizeb, locb);
405 rembytes = inbytes - locb - sizeb;
407 outbytes = inbytes - sizeb;
408 dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1);
409 for (i = 0; i < locb; i++)
410 dataout[i] = datain[i];
411 for (i = 0; i < rembytes; i++)
412 dataout[locb + i] = datain[locb + sizeb + i];
447 l_int32 i, locb, sizeb;
451 PROCNAME(
"fileCorruptByMutation");
453 if (!filein || !fileout)
454 return ERROR_INT(
"filein and fileout not both specified", procName, 1);
455 if (loc < 0.0 || loc >= 1.0)
456 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", procName, 1);
458 return ERROR_INT(
"size must be > 0.0", procName, 1);
459 if (loc + size > 1.0)
463 locb = (l_int32)(loc * bytes + 0.5);
464 locb = L_MIN(locb, bytes - 1);
465 sizeb = (l_int32)(size * bytes + 0.5);
466 sizeb = L_MAX(1, sizeb);
467 sizeb = L_MIN(sizeb, bytes - locb);
468 L_INFO(
"Randomizing %d bytes at location %d\n", procName, sizeb, locb);
471 for (i = 0; i < sizeb; i++) {
473 (l_uint8)(255.9 * ((l_float64)rand() / (l_float64)RAND_MAX));
504 PROCNAME(
"genRandomIntegerInRange");
507 return ERROR_INT(
"&val not defined", procName, 1);
510 return ERROR_INT(
"range must be >= 2", procName, 1);
512 if (seed > 0) srand(seed);
513 *pval = (l_int32)((l_float64)range *
514 ((l_float64)rand() / (l_float64)RAND_MAX));
539 return (fval >= 0.0) ? (l_int32)(fval + 0.5) : (l_int32)(fval - 0.5);
574 PROCNAME(
"l_hashStringToUint64");
576 if (phash) *phash = 0;
577 if (!str || (str[0] ==
'\0'))
578 return ERROR_INT(
"str not defined or empty", procName, 1);
580 return ERROR_INT(
"&hash not defined", procName, 1);
582 mulp = 26544357894361247;
585 hash += (*str++ * mulp) ^ (hash >> 7);
587 *phash = hash ^ (hash << 37);
619 PROCNAME(
"l_hashPtToUint64");
622 return ERROR_INT(
"&hash not defined", procName, 1);
624 *phash = (l_uint64)(2173249142.3849 * x + 3763193258.6227 * y);
659 PROCNAME(
"l_hashFloatToUint64");
662 return ERROR_INT(
"&hash not defined", procName, 1);
663 *phash = (l_uint64)((21.732491 * nbuckets) * val);
684 PROCNAME(
"findNextLargerPrime");
687 return ERROR_INT(
"&prime not defined", procName, 1);
690 return ERROR_INT(
"start must be > 0", procName, 1);
692 for (i = start + 1; ; i++) {
700 return ERROR_INT(
"prime not found!", procName, 1);
719 l_uint64 limit, ratio;
721 PROCNAME(
"lept_isPrime");
723 if (pis_prime) *pis_prime = 0;
724 if (pfactor) *pfactor = 0;
726 return ERROR_INT(
"&is_prime not defined", procName, 1);
728 return ERROR_INT(
"n must be > 0", procName, 1);
731 if (pfactor) *pfactor = 2;
735 limit = (l_uint64)sqrt((l_float64)n);
736 for (div = 3; div < limit; div += 2) {
738 if (ratio * div == n) {
739 if (pfactor) *pfactor = div;
767 return (val >> 1) ^ val;
782 for (shift = 1; shift < 32; shift <<= 1)
802 size_t bufsize = 100;
804 char *version = (
char *)LEPT_CALLOC(bufsize,
sizeof(
char));
808 char dllStr[] =
"DLL";
810 char dllStr[] =
"LIB";
813 char debugStr[] =
"Debug";
815 char debugStr[] =
"Release";
818 char bitStr[] =
" x86";
820 char bitStr[] =
" x64";
824 snprintf(version, bufsize,
"leptonica-%d.%d.%d (%s, %s) [MSC v.%d %s %s%s]",
825 LIBLEPT_MAJOR_VERSION, LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION,
826 __DATE__, __TIME__, _MSC_VER, dllStr, debugStr, bitStr);
830 snprintf(version, bufsize,
"leptonica-%d.%d.%d", LIBLEPT_MAJOR_VERSION,
831 LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION);
843 #include <sys/time.h> 844 #include <sys/resource.h> 846 static struct rusage rusage_before;
847 static struct rusage rusage_after;
861 getrusage(RUSAGE_SELF, &rusage_before);
869 getrusage(RUSAGE_SELF, &rusage_after);
871 tsec = rusage_after.ru_utime.tv_sec - rusage_before.ru_utime.tv_sec;
872 tusec = rusage_after.ru_utime.tv_usec - rusage_before.ru_utime.tv_usec;
873 return (tsec + ((l_float32)tusec) / 1000000.0);
893 struct rusage *rusage_start;
895 rusage_start = (
struct rusage *)LEPT_CALLOC(1,
sizeof(
struct rusage));
896 getrusage(RUSAGE_SELF, rusage_start);
901 stopTimerNested(L_TIMER rusage_start)
904 struct rusage rusage_stop;
906 getrusage(RUSAGE_SELF, &rusage_stop);
908 tsec = rusage_stop.ru_utime.tv_sec -
909 ((
struct rusage *)rusage_start)->ru_utime.tv_sec;
910 tusec = rusage_stop.ru_utime.tv_usec -
911 ((
struct rusage *)rusage_start)->ru_utime.tv_usec;
912 LEPT_FREE(rusage_start);
913 return (tsec + ((l_float32)tusec) / 1000000.0);
930 gettimeofday(&tv, NULL);
931 if (sec) *sec = (l_int32)tv.tv_sec;
932 if (usec) *usec = (l_int32)tv.tv_usec;
943 static ULARGE_INTEGER utime_before;
944 static ULARGE_INTEGER utime_after;
950 FILETIME start, stop, kernel, user;
952 this_process = GetCurrentProcess();
954 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
956 utime_before.LowPart = user.dwLowDateTime;
957 utime_before.HighPart = user.dwHighDateTime;
964 FILETIME start, stop, kernel, user;
967 this_process = GetCurrentProcess();
969 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
971 utime_after.LowPart = user.dwLowDateTime;
972 utime_after.HighPart = user.dwHighDateTime;
973 hnsec = utime_after.QuadPart - utime_before.QuadPart;
974 return (l_float32)(signed)hnsec / 10000000.0;
981 FILETIME start, stop, kernel, user;
982 ULARGE_INTEGER *utime_start;
984 this_process = GetCurrentProcess();
986 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
988 utime_start = (ULARGE_INTEGER *)LEPT_CALLOC(1,
sizeof(ULARGE_INTEGER));
989 utime_start->LowPart = user.dwLowDateTime;
990 utime_start->HighPart = user.dwHighDateTime;
995 stopTimerNested(L_TIMER utime_start)
998 FILETIME start, stop, kernel, user;
999 ULARGE_INTEGER utime_stop;
1002 this_process = GetCurrentProcess ();
1004 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1006 utime_stop.LowPart = user.dwLowDateTime;
1007 utime_stop.HighPart = user.dwHighDateTime;
1008 hnsec = utime_stop.QuadPart - ((ULARGE_INTEGER *)utime_start)->QuadPart;
1009 LEPT_FREE(utime_start);
1010 return (l_float32)(signed)hnsec / 10000000.0;
1017 ULARGE_INTEGER utime, birthunix;
1018 FILETIME systemtime;
1019 LONGLONG birthunixhnsec = 116444736000000000;
1022 GetSystemTimeAsFileTime(&systemtime);
1023 utime.LowPart = systemtime.dwLowDateTime;
1024 utime.HighPart = systemtime.dwHighDateTime;
1026 birthunix.LowPart = (DWORD) birthunixhnsec;
1027 birthunix.HighPart = birthunixhnsec >> 32;
1029 usecs = (LONGLONG) ((utime.QuadPart - birthunix.QuadPart) / 10);
1031 if (sec) *sec = (l_int32) (usecs / 1000000);
1032 if (usec) *usec = (l_int32) (usecs % 1000000);
1072 l_int32 tsec, tusec;
1075 PROCNAME(
"stopWallTimer");
1078 return (l_float32)ERROR_FLOAT(
"&timer not defined", procName, 0.0);
1081 return (l_float32)ERROR_FLOAT(
"timer not defined", procName, 0.0);
1084 tsec = timer->stop_sec - timer->start_sec;
1085 tusec = timer->stop_usec - timer->start_usec;
1088 return (tsec + ((l_float32)tusec) / 1000000.0);
1107 char buf[
sizeof "199812231952SS-08'00'"] =
"", sep =
'Z';
1108 l_int32 gmt_offset, relh, relm;
1124 tptr->tm_isdst = -1;
1129 gmt_offset = (l_int32) difftime(ut, lt);
1133 else if (gmt_offset < 0)
1136 relh = L_ABS(gmt_offset) / 3600;
1137 relm = (L_ABS(gmt_offset) % 3600) / 60;
1139 strftime(buf,
sizeof(buf),
"%Y%m%d%H%M%S", localtime(&ut));
1140 sprintf(buf + 14,
"%c%02d'%02d'", sep, relh, relm);
size_t nbytesInFile(const char *filename)
nbytesInFile()
LEPT_DLL l_int32 LeptMsgSeverity
l_uint32 convertBinaryToGrayCode(l_uint32 val)
convertBinaryToGrayCode()
L_TIMER startTimerNested(void)
startTimerNested(), stopTimerNested()
l_int32 l_hashFloat64ToUint64(l_int32 nbuckets, l_float64 val, l_uint64 *phash)
l_hashFloat64ToUint64()
l_int32 lept_roundftoi(l_float32 fval)
lept_roundftoi()
l_int32 setMsgSeverity(l_int32 newsev)
setMsgSeverity()
void startTimer(void)
startTimer(), stopTimer()
char * stringNew(const char *src)
stringNew()
l_float32 stopWallTimer(L_WALLTIMER **ptimer)
stopWallTimer()
void * returnErrorPtr(const char *msg, const char *procname, void *pval)
returnErrorPtr()
l_int32 fileCorruptByDeletion(const char *filein, l_float32 loc, l_float32 size, const char *fileout)
fileCorruptByDeletion()
l_int32 filesAreIdentical(const char *fname1, const char *fname2, l_int32 *psame)
filesAreIdentical()
l_int32 l_hashPtToUint64(l_int32 x, l_int32 y, l_uint64 *phash)
l_hashPtToUint64()
l_int32 fileCorruptByMutation(const char *filein, l_float32 loc, l_float32 size, const char *fileout)
fileCorruptByMutation()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
l_int32 findNextLargerPrime(l_int32 start, l_uint32 *pprime)
findNextLargerPrime()
char * getLeptonicaVersion()
getLeptonicaVersion()
l_int32 l_hashStringToUint64(const char *str, l_uint64 *phash)
l_hashStringToUint64()
l_float32 returnErrorFloat(const char *msg, const char *procname, l_float32 fval)
returnErrorFloat()
l_int32 lept_isPrime(l_uint64 n, l_int32 *pis_prime, l_uint32 *pfactor)
lept_isPrime()
void l_getCurrentTime(l_int32 *sec, l_int32 *usec)
l_getCurrentTime()
L_WALLTIMER * startWallTimer(void)
startWallTimer()
l_int32 l_binaryWrite(const char *filename, const char *operation, void *data, size_t nbytes)
l_binaryWrite()
l_int32 genRandomIntegerInRange(l_int32 range, l_int32 seed, l_int32 *pval)
genRandomIntegerInRange()
char * l_getFormattedDate()
l_getFormattedDate()
l_uint32 convertGrayCodeToBinary(l_uint32 val)
convertGrayCodeToBinary()
l_int32 returnErrorInt(const char *msg, const char *procname, l_int32 ival)
returnErrorInt()