100 #include "config_auto.h" 108 #include "allheaders.h" 141 PROCNAME(
"setMsgSeverity");
144 if (newsev == L_SEVERITY_EXTERNAL) {
145 envsev = getenv(
"LEPT_MSG_SEVERITY");
149 L_INFO(
"message severity set to external\n", procName);
153 L_WARNING(
"environment var LEPT_MSG_SEVERITY not defined\n",
160 L_INFO(
"message severity set to %d\n", procName, newsev);
188 const char *procname,
191 fprintf(stderr,
"Error in %s: %s\n", procname, msg);
206 const char *procname,
209 fprintf(stderr,
"Error in %s: %s\n", procname, msg);
224 const char *procname,
227 fprintf(stderr,
"Error in %s: %s\n", procname, msg);
249 size_t nbytes1, nbytes2;
250 l_uint8 *array1, *array2;
252 PROCNAME(
"filesAreIdentical");
255 return ERROR_INT(
"&same not defined", procName, 1);
257 if (!fname1 || !fname2)
258 return ERROR_INT(
"both names not defined", procName, 1);
262 if (nbytes1 != nbytes2)
266 return ERROR_INT(
"array1 not read", procName, 1);
267 if ((array2 =
l_binaryRead(fname2, &nbytes2)) == NULL) {
269 return ERROR_INT(
"array2 not read", procName, 1);
272 for (i = 0; i < nbytes1; i++) {
273 if (array1[i] != array2[i]) {
300 convertOnBigEnd16(l_uint16 shortin)
302 return ((shortin << 8) | (shortin >> 8));
306 convertOnLittleEnd16(l_uint16 shortin)
314 convertOnLittleEnd16(l_uint16 shortin)
316 return ((shortin << 8) | (shortin >> 8));
320 convertOnBigEnd16(l_uint16 shortin)
334 convertOnBigEnd32(l_uint32 wordin)
336 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
337 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
341 convertOnLittleEnd32(l_uint32 wordin)
349 convertOnLittleEnd32(l_uint32 wordin)
351 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
352 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
356 convertOnBigEnd32(l_uint32 wordin)
393 l_int32 i, locb, sizeb, rembytes;
394 size_t inbytes, outbytes;
395 l_uint8 *datain, *dataout;
397 PROCNAME(
"fileCorruptByDeletion");
399 if (!filein || !fileout)
400 return ERROR_INT(
"filein and fileout not both specified", procName, 1);
401 if (loc < 0.0 || loc >= 1.0)
402 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", procName, 1);
404 return ERROR_INT(
"size must be > 0.0", procName, 1);
405 if (loc + size > 1.0)
409 locb = (l_int32)(loc * inbytes + 0.5);
410 locb = L_MIN(locb, inbytes - 1);
411 sizeb = (l_int32)(size * inbytes + 0.5);
412 sizeb = L_MAX(1, sizeb);
413 sizeb = L_MIN(sizeb, inbytes - locb);
414 L_INFO(
"Removed %d bytes at location %d\n", procName, sizeb, locb);
415 rembytes = inbytes - locb - sizeb;
417 outbytes = inbytes - sizeb;
418 dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1);
419 for (i = 0; i < locb; i++)
420 dataout[i] = datain[i];
421 for (i = 0; i < rembytes; i++)
422 dataout[locb + i] = datain[locb + sizeb + i];
457 l_int32 i, locb, sizeb;
461 PROCNAME(
"fileCorruptByMutation");
463 if (!filein || !fileout)
464 return ERROR_INT(
"filein and fileout not both specified", procName, 1);
465 if (loc < 0.0 || loc >= 1.0)
466 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", procName, 1);
468 return ERROR_INT(
"size must be > 0.0", procName, 1);
469 if (loc + size > 1.0)
473 locb = (l_int32)(loc * bytes + 0.5);
474 locb = L_MIN(locb, bytes - 1);
475 sizeb = (l_int32)(size * bytes + 0.5);
476 sizeb = L_MAX(1, sizeb);
477 sizeb = L_MIN(sizeb, bytes - locb);
478 L_INFO(
"Randomizing %d bytes at location %d\n", procName, sizeb, locb);
481 for (i = 0; i < sizeb; i++) {
483 (l_uint8)(255.9 * ((l_float64)rand() / (l_float64)RAND_MAX));
514 PROCNAME(
"genRandomIntegerInRange");
517 return ERROR_INT(
"&val not defined", procName, 1);
520 return ERROR_INT(
"range must be >= 2", procName, 1);
522 if (seed > 0) srand(seed);
523 *pval = (l_int32)((l_float64)range *
524 ((l_float64)rand() / (l_float64)RAND_MAX));
549 return (fval >= 0.0) ? (l_int32)(fval + 0.5) : (l_int32)(fval - 0.5);
584 PROCNAME(
"l_hashStringToUint64");
586 if (phash) *phash = 0;
587 if (!str || (str[0] ==
'\0'))
588 return ERROR_INT(
"str not defined or empty", procName, 1);
590 return ERROR_INT(
"&hash not defined", procName, 1);
592 mulp = 26544357894361247;
595 hash += (*str++ * mulp) ^ (hash >> 7);
597 *phash = hash ^ (hash << 37);
629 PROCNAME(
"l_hashPtToUint64");
632 return ERROR_INT(
"&hash not defined", procName, 1);
634 *phash = (l_uint64)(2173249142.3849 * x + 3763193258.6227 * y);
669 PROCNAME(
"l_hashFloatToUint64");
672 return ERROR_INT(
"&hash not defined", procName, 1);
673 *phash = (l_uint64)((21.732491 * nbuckets) * val);
694 PROCNAME(
"findNextLargerPrime");
697 return ERROR_INT(
"&prime not defined", procName, 1);
700 return ERROR_INT(
"start must be > 0", procName, 1);
702 for (i = start + 1; ; i++) {
710 return ERROR_INT(
"prime not found!", procName, 1);
729 l_uint64 limit, ratio;
731 PROCNAME(
"lept_isPrime");
733 if (pis_prime) *pis_prime = 0;
734 if (pfactor) *pfactor = 0;
736 return ERROR_INT(
"&is_prime not defined", procName, 1);
738 return ERROR_INT(
"n must be > 0", procName, 1);
741 if (pfactor) *pfactor = 2;
745 limit = (l_uint64)sqrt((l_float64)n);
746 for (div = 3; div < limit; div += 2) {
748 if (ratio * div == n) {
749 if (pfactor) *pfactor = div;
777 return (val >> 1) ^ val;
792 for (shift = 1; shift < 32; shift <<= 1)
812 size_t bufsize = 100;
814 char *version = (
char *)LEPT_CALLOC(bufsize,
sizeof(
char));
818 char dllStr[] =
"DLL";
820 char dllStr[] =
"LIB";
823 char debugStr[] =
"Debug";
825 char debugStr[] =
"Release";
828 char bitStr[] =
" x86";
830 char bitStr[] =
" x64";
834 snprintf(version, bufsize,
"leptonica-%d.%d.%d (%s, %s) [MSC v.%d %s %s%s]",
835 LIBLEPT_MAJOR_VERSION, LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION,
836 __DATE__, __TIME__, _MSC_VER, dllStr, debugStr, bitStr);
840 snprintf(version, bufsize,
"leptonica-%d.%d.%d", LIBLEPT_MAJOR_VERSION,
841 LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION);
853 #include <sys/time.h> 854 #include <sys/resource.h> 856 static struct rusage rusage_before;
857 static struct rusage rusage_after;
871 getrusage(RUSAGE_SELF, &rusage_before);
879 getrusage(RUSAGE_SELF, &rusage_after);
881 tsec = rusage_after.ru_utime.tv_sec - rusage_before.ru_utime.tv_sec;
882 tusec = rusage_after.ru_utime.tv_usec - rusage_before.ru_utime.tv_usec;
883 return (tsec + ((l_float32)tusec) / 1000000.0);
903 struct rusage *rusage_start;
905 rusage_start = (
struct rusage *)LEPT_CALLOC(1,
sizeof(
struct rusage));
906 getrusage(RUSAGE_SELF, rusage_start);
911 stopTimerNested(L_TIMER rusage_start)
914 struct rusage rusage_stop;
916 getrusage(RUSAGE_SELF, &rusage_stop);
918 tsec = rusage_stop.ru_utime.tv_sec -
919 ((
struct rusage *)rusage_start)->ru_utime.tv_sec;
920 tusec = rusage_stop.ru_utime.tv_usec -
921 ((
struct rusage *)rusage_start)->ru_utime.tv_usec;
922 LEPT_FREE(rusage_start);
923 return (tsec + ((l_float32)tusec) / 1000000.0);
940 gettimeofday(&tv, NULL);
941 if (sec) *sec = (l_int32)tv.tv_sec;
942 if (usec) *usec = (l_int32)tv.tv_usec;
953 static ULARGE_INTEGER utime_before;
954 static ULARGE_INTEGER utime_after;
960 FILETIME start, stop, kernel, user;
962 this_process = GetCurrentProcess();
964 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
966 utime_before.LowPart = user.dwLowDateTime;
967 utime_before.HighPart = user.dwHighDateTime;
974 FILETIME start, stop, kernel, user;
977 this_process = GetCurrentProcess();
979 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
981 utime_after.LowPart = user.dwLowDateTime;
982 utime_after.HighPart = user.dwHighDateTime;
983 hnsec = utime_after.QuadPart - utime_before.QuadPart;
984 return (l_float32)(signed)hnsec / 10000000.0;
991 FILETIME start, stop, kernel, user;
992 ULARGE_INTEGER *utime_start;
994 this_process = GetCurrentProcess();
996 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
998 utime_start = (ULARGE_INTEGER *)LEPT_CALLOC(1,
sizeof(ULARGE_INTEGER));
999 utime_start->LowPart = user.dwLowDateTime;
1000 utime_start->HighPart = user.dwHighDateTime;
1005 stopTimerNested(L_TIMER utime_start)
1007 HANDLE this_process;
1008 FILETIME start, stop, kernel, user;
1009 ULARGE_INTEGER utime_stop;
1012 this_process = GetCurrentProcess ();
1014 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1016 utime_stop.LowPart = user.dwLowDateTime;
1017 utime_stop.HighPart = user.dwHighDateTime;
1018 hnsec = utime_stop.QuadPart - ((ULARGE_INTEGER *)utime_start)->QuadPart;
1019 LEPT_FREE(utime_start);
1020 return (l_float32)(signed)hnsec / 10000000.0;
1027 ULARGE_INTEGER utime, birthunix;
1028 FILETIME systemtime;
1029 LONGLONG birthunixhnsec = 116444736000000000;
1032 GetSystemTimeAsFileTime(&systemtime);
1033 utime.LowPart = systemtime.dwLowDateTime;
1034 utime.HighPart = systemtime.dwHighDateTime;
1036 birthunix.LowPart = (DWORD) birthunixhnsec;
1037 birthunix.HighPart = birthunixhnsec >> 32;
1039 usecs = (LONGLONG) ((utime.QuadPart - birthunix.QuadPart) / 10);
1041 if (sec) *sec = (l_int32) (usecs / 1000000);
1042 if (usec) *usec = (l_int32) (usecs % 1000000);
1082 l_int32 tsec, tusec;
1085 PROCNAME(
"stopWallTimer");
1088 return (l_float32)ERROR_FLOAT(
"&timer not defined", procName, 0.0);
1091 return (l_float32)ERROR_FLOAT(
"timer not defined", procName, 0.0);
1094 tsec = timer->stop_sec - timer->start_sec;
1095 tusec = timer->stop_usec - timer->start_usec;
1098 return (tsec + ((l_float32)tusec) / 1000000.0);
1117 char buf[128] =
"", sep =
'Z';
1118 l_int32 gmt_offset, relh, relm;
1121 struct tm *tptr = &Tm;
1135 gmtime_s(tptr, &ut);
1137 gmtime_r(&ut, tptr);
1139 tptr->tm_isdst = -1;
1144 gmt_offset = (l_int32) difftime(ut, lt);
1148 else if (gmt_offset < 0)
1151 relh = L_ABS(gmt_offset) / 3600;
1152 relm = (L_ABS(gmt_offset) % 3600) / 60;
1154 strftime(buf,
sizeof(buf),
"%Y%m%d%H%M%S", localtime(&ut));
1155 sprintf(buf + 14,
"%c%02d'%02d'", sep, relh, relm);
size_t nbytesInFile(const char *filename)
nbytesInFile()
LEPT_DLL l_int32 LeptMsgSeverity
L_TIMER startTimerNested(void)
startTimerNested(), stopTimerNested()
l_ok l_hashPtToUint64(l_int32 x, l_int32 y, l_uint64 *phash)
l_hashPtToUint64()
l_ok genRandomIntegerInRange(l_int32 range, l_int32 seed, l_int32 *pval)
genRandomIntegerInRange()
l_int32 lept_roundftoi(l_float32 fval)
lept_roundftoi()
l_int32 setMsgSeverity(l_int32 newsev)
setMsgSeverity()
void startTimer(void)
startTimer(), stopTimer()
l_ok fileCorruptByMutation(const char *filein, l_float32 loc, l_float32 size, const char *fileout)
fileCorruptByMutation()
char * stringNew(const char *src)
stringNew()
l_float32 stopWallTimer(L_WALLTIMER **ptimer)
stopWallTimer()
l_ok l_hashFloat64ToUint64(l_int32 nbuckets, l_float64 val, l_uint64 *phash)
l_hashFloat64ToUint64()
void * returnErrorPtr(const char *msg, const char *procname, void *pval)
returnErrorPtr()
l_ok findNextLargerPrime(l_int32 start, l_uint32 *pprime)
findNextLargerPrime()
l_ok fileCorruptByDeletion(const char *filein, l_float32 loc, l_float32 size, const char *fileout)
fileCorruptByDeletion()
l_ok l_binaryWrite(const char *filename, const char *operation, const void *data, size_t nbytes)
l_binaryWrite()
l_uint8 * l_binaryRead(const char *filename, size_t *pnbytes)
l_binaryRead()
char * getLeptonicaVersion()
getLeptonicaVersion()
l_uint32 convertIntToGrayCode(l_uint32 val)
convertIntToGrayCode()
l_ok l_hashStringToUint64(const char *str, l_uint64 *phash)
l_hashStringToUint64()
l_float32 returnErrorFloat(const char *msg, const char *procname, l_float32 fval)
returnErrorFloat()
l_uint32 convertGrayCodeToInt(l_uint32 val)
convertGrayCodeToInt()
void l_getCurrentTime(l_int32 *sec, l_int32 *usec)
l_getCurrentTime()
l_ok filesAreIdentical(const char *fname1, const char *fname2, l_int32 *psame)
filesAreIdentical()
l_ok lept_isPrime(l_uint64 n, l_int32 *pis_prime, l_uint32 *pfactor)
lept_isPrime()
L_WALLTIMER * startWallTimer(void)
startWallTimer()
char * l_getFormattedDate()
l_getFormattedDate()
l_int32 returnErrorInt(const char *msg, const char *procname, l_int32 ival)
returnErrorInt()