107 #include <config_auto.h> 115 #include "allheaders.h" 149 if (newsev == L_SEVERITY_EXTERNAL) {
150 envsev = getenv(
"LEPT_MSG_SEVERITY");
154 L_INFO(
"message severity set to external\n",
"setMsgSeverity");
158 L_WARNING(
"environment var LEPT_MSG_SEVERITY not defined\n",
165 L_INFO(
"message severity set to %d\n",
"setMsgSeverity", newsev);
194 const char *procname,
212 const char *procname,
230 const char *procname,
256 static void lept_default_stderr_handler(
const char *formatted_msg)
259 fputs(formatted_msg, stderr);
264 void (*stderr_handler)(
const char *) = lept_default_stderr_handler;
283 stderr_handler = handler;
285 stderr_handler = lept_default_stderr_handler;
289 #define MAX_DEBUG_MESSAGE 2000 309 char msg[MAX_DEBUG_MESSAGE];
313 n = vsnprintf(msg,
sizeof(msg), fmt, args);
317 (*stderr_handler)(msg);
338 size_t nbytes1, nbytes2;
339 l_uint8 *array1, *array2;
341 PROCNAME(
"filesAreIdentical");
344 return ERROR_INT(
"&same not defined", procName, 1);
346 if (!fname1 || !fname2)
347 return ERROR_INT(
"both names not defined", procName, 1);
351 if (nbytes1 != nbytes2)
355 return ERROR_INT(
"array1 not read", procName, 1);
356 if ((array2 =
l_binaryRead(fname2, &nbytes2)) == NULL) {
358 return ERROR_INT(
"array2 not read", procName, 1);
361 for (i = 0; i < nbytes1; i++) {
362 if (array1[i] != array2[i]) {
390 convertOnBigEnd16(l_uint16 shortin)
392 return ((shortin << 8) | (shortin >> 8));
396 convertOnLittleEnd16(l_uint16 shortin)
404 convertOnLittleEnd16(l_uint16 shortin)
406 return ((shortin << 8) | (shortin >> 8));
410 convertOnBigEnd16(l_uint16 shortin)
424 convertOnBigEnd32(l_uint32 wordin)
426 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
427 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
431 convertOnLittleEnd32(l_uint32 wordin)
439 convertOnLittleEnd32(l_uint32 wordin)
441 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
442 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
446 convertOnBigEnd32(l_uint32 wordin)
483 l_int32 i, locb, sizeb, rembytes;
484 size_t inbytes, outbytes;
485 l_uint8 *datain, *dataout;
487 PROCNAME(
"fileCorruptByDeletion");
489 if (!filein || !fileout)
490 return ERROR_INT(
"filein and fileout not both specified", procName, 1);
491 if (loc < 0.0 || loc >= 1.0)
492 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", procName, 1);
494 return ERROR_INT(
"size must be > 0.0", procName, 1);
495 if (loc + size > 1.0)
499 locb = (l_int32)(loc * inbytes + 0.5);
500 locb = L_MIN(locb, inbytes - 1);
501 sizeb = (l_int32)(size * inbytes + 0.5);
502 sizeb = L_MAX(1, sizeb);
503 sizeb = L_MIN(sizeb, inbytes - locb);
504 L_INFO(
"Removed %d bytes at location %d\n", procName, sizeb, locb);
505 rembytes = inbytes - locb - sizeb;
507 outbytes = inbytes - sizeb;
508 dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1);
509 for (i = 0; i < locb; i++)
510 dataout[i] = datain[i];
511 for (i = 0; i < rembytes; i++)
512 dataout[locb + i] = datain[locb + sizeb + i];
547 l_int32 i, locb, sizeb;
551 PROCNAME(
"fileCorruptByMutation");
553 if (!filein || !fileout)
554 return ERROR_INT(
"filein and fileout not both specified", procName, 1);
555 if (loc < 0.0 || loc >= 1.0)
556 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", procName, 1);
558 return ERROR_INT(
"size must be > 0.0", procName, 1);
559 if (loc + size > 1.0)
563 locb = (l_int32)(loc * bytes + 0.5);
564 locb = L_MIN(locb, bytes - 1);
565 sizeb = (l_int32)(size * bytes + 0.5);
566 sizeb = L_MAX(1, sizeb);
567 sizeb = L_MIN(sizeb, bytes - locb);
568 L_INFO(
"Randomizing %d bytes at location %d\n", procName, sizeb, locb);
571 for (i = 0; i < sizeb; i++) {
573 (l_uint8)(255.9 * ((l_float64)rand() / (l_float64)RAND_MAX));
611 size_t inbytes, outbytes;
612 l_uint8 *datain, *dataout;
614 PROCNAME(
"fileReplaceBytes");
616 if (!filein || !fileout)
617 return ERROR_INT(
"filein and fileout not both specified", procName, 1);
620 if (start + nbytes > inbytes)
621 L_WARNING(
"start + nbytes > length(filein) = %zu\n", procName, inbytes);
623 if (!newdata) newsize = 0;
624 outbytes = inbytes - nbytes + newsize;
625 if ((dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1)) == NULL) {
627 return ERROR_INT(
"calloc fail for dataout", procName, 1);
630 for (i = 0; i < start; i++)
631 dataout[i] = datain[i];
632 for (i = start; i < start + newsize; i++)
633 dataout[i] = newdata[i - start];
634 index = start + nbytes;
636 for (i = start; i < outbytes; i++, index++)
637 dataout[i] = datain[index];
666 PROCNAME(
"genRandomIntOnInterval");
669 return ERROR_INT(
"&val not defined", procName, 1);
672 return ERROR_INT(
"invalid range", procName, 1);
674 if (seed > 0) srand(seed);
675 range = (l_float64)(end - start + 1);
676 *pval = start + (l_int32)((l_float64)range *
677 ((l_float64)rand() / (l_float64)RAND_MAX));
702 return (fval >= 0.0) ? (l_int32)(fval + 0.5) : (l_int32)(fval - 0.5);
737 PROCNAME(
"l_hashStringToUint64");
739 if (phash) *phash = 0;
740 if (!str || (str[0] ==
'\0'))
741 return ERROR_INT(
"str not defined or empty", procName, 1);
743 return ERROR_INT(
"&hash not defined", procName, 1);
745 mulp = 26544357894361247;
748 hash += (*str++ * mulp) ^ (hash >> 7);
750 *phash = hash ^ (hash << 37);
777 PROCNAME(
"l_hashStringToUint64Fast");
779 if (phash) *phash = 0;
780 if (!str || (str[0] ==
'\0'))
781 return ERROR_INT(
"str not defined or empty", procName, 1);
783 return ERROR_INT(
"&hash not defined", procName, 1);
786 for (p = (l_uint8 *)str; *p !=
'\0'; p++)
811 PROCNAME(
"l_hashPtToUint64");
814 return ERROR_INT(
"&hash not defined", procName, 1);
816 *phash = (l_uint64)(2173249142.3849 * x + 3763193258.6227 * y);
840 PROCNAME(
"l_hashFloatToUint64");
843 return ERROR_INT(
"&hash not defined", procName, 1);
844 val = (val >= 0.0) ? 847019.66701 * val : -217324.91613 * val;
845 *phash = (l_uint64)val;
866 PROCNAME(
"findNextLargerPrime");
869 return ERROR_INT(
"&prime not defined", procName, 1);
872 return ERROR_INT(
"start must be > 0", procName, 1);
874 for (i = start + 1; ; i++) {
882 return ERROR_INT(
"prime not found!", procName, 1);
901 l_uint64 limit, ratio;
903 PROCNAME(
"lept_isPrime");
905 if (pis_prime) *pis_prime = 0;
906 if (pfactor) *pfactor = 0;
908 return ERROR_INT(
"&is_prime not defined", procName, 1);
910 return ERROR_INT(
"n must be > 0", procName, 1);
913 if (pfactor) *pfactor = 2;
917 limit = (l_uint64)sqrt((l_float64)n);
918 for (div = 3; div < limit; div += 2) {
920 if (ratio * div == n) {
921 if (pfactor) *pfactor = div;
949 return (val >> 1) ^ val;
964 for (shift = 1; shift < 32; shift <<= 1)
984 size_t bufsize = 100;
986 char *version = (
char *)LEPT_CALLOC(bufsize,
sizeof(
char));
990 char dllStr[] =
"DLL";
992 char dllStr[] =
"LIB";
995 char debugStr[] =
"Debug";
997 char debugStr[] =
"Release";
1000 char bitStr[] =
" x86";
1002 char bitStr[] =
" x64";
1006 snprintf(version, bufsize,
"leptonica-%d.%d.%d (%s, %s) [MSC v.%d %s %s%s]",
1007 LIBLEPT_MAJOR_VERSION, LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION,
1008 __DATE__, __TIME__, _MSC_VER, dllStr, debugStr, bitStr);
1012 snprintf(version, bufsize,
"leptonica-%d.%d.%d", LIBLEPT_MAJOR_VERSION,
1013 LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION);
1023 #if !defined(_WIN32) && !defined(__Fuchsia__) 1025 #include <sys/time.h> 1026 #include <sys/resource.h> 1028 static struct rusage rusage_before;
1029 static struct rusage rusage_after;
1043 getrusage(RUSAGE_SELF, &rusage_before);
1049 l_int32 tsec, tusec;
1051 getrusage(RUSAGE_SELF, &rusage_after);
1053 tsec = rusage_after.ru_utime.tv_sec - rusage_before.ru_utime.tv_sec;
1054 tusec = rusage_after.ru_utime.tv_usec - rusage_before.ru_utime.tv_usec;
1055 return (tsec + ((l_float32)tusec) / 1000000.0);
1075 struct rusage *rusage_start;
1077 rusage_start = (
struct rusage *)LEPT_CALLOC(1,
sizeof(
struct rusage));
1078 getrusage(RUSAGE_SELF, rusage_start);
1079 return rusage_start;
1083 stopTimerNested(L_TIMER rusage_start)
1085 l_int32 tsec, tusec;
1086 struct rusage rusage_stop;
1088 getrusage(RUSAGE_SELF, &rusage_stop);
1090 tsec = rusage_stop.ru_utime.tv_sec -
1091 ((
struct rusage *)rusage_start)->ru_utime.tv_sec;
1092 tusec = rusage_stop.ru_utime.tv_usec -
1093 ((
struct rusage *)rusage_start)->ru_utime.tv_usec;
1094 LEPT_FREE(rusage_start);
1095 return (tsec + ((l_float32)tusec) / 1000000.0);
1112 gettimeofday(&tv, NULL);
1113 if (sec) *sec = (l_int32)tv.tv_sec;
1114 if (usec) *usec = (l_int32)tv.tv_usec;
1117 #elif defined(__Fuchsia__) 1141 stopTimerNested(L_TIMER rusage_start)
1158 static ULARGE_INTEGER utime_before;
1159 static ULARGE_INTEGER utime_after;
1164 HANDLE this_process;
1165 FILETIME start, stop, kernel, user;
1167 this_process = GetCurrentProcess();
1169 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
1171 utime_before.LowPart = user.dwLowDateTime;
1172 utime_before.HighPart = user.dwHighDateTime;
1178 HANDLE this_process;
1179 FILETIME start, stop, kernel, user;
1182 this_process = GetCurrentProcess();
1184 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
1186 utime_after.LowPart = user.dwLowDateTime;
1187 utime_after.HighPart = user.dwHighDateTime;
1188 hnsec = utime_after.QuadPart - utime_before.QuadPart;
1189 return (l_float32)(signed)hnsec / 10000000.0;
1195 HANDLE this_process;
1196 FILETIME start, stop, kernel, user;
1197 ULARGE_INTEGER *utime_start;
1199 this_process = GetCurrentProcess();
1201 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1203 utime_start = (ULARGE_INTEGER *)LEPT_CALLOC(1,
sizeof(ULARGE_INTEGER));
1204 utime_start->LowPart = user.dwLowDateTime;
1205 utime_start->HighPart = user.dwHighDateTime;
1210 stopTimerNested(L_TIMER utime_start)
1212 HANDLE this_process;
1213 FILETIME start, stop, kernel, user;
1214 ULARGE_INTEGER utime_stop;
1217 this_process = GetCurrentProcess ();
1219 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1221 utime_stop.LowPart = user.dwLowDateTime;
1222 utime_stop.HighPart = user.dwHighDateTime;
1223 hnsec = utime_stop.QuadPart - ((ULARGE_INTEGER *)utime_start)->QuadPart;
1224 LEPT_FREE(utime_start);
1225 return (l_float32)(signed)hnsec / 10000000.0;
1232 ULARGE_INTEGER utime, birthunix;
1233 FILETIME systemtime;
1234 LONGLONG birthunixhnsec = 116444736000000000;
1237 GetSystemTimeAsFileTime(&systemtime);
1238 utime.LowPart = systemtime.dwLowDateTime;
1239 utime.HighPart = systemtime.dwHighDateTime;
1241 birthunix.LowPart = (DWORD) birthunixhnsec;
1242 birthunix.HighPart = birthunixhnsec >> 32;
1244 usecs = (LONGLONG) ((utime.QuadPart - birthunix.QuadPart) / 10);
1246 if (sec) *sec = (l_int32) (usecs / 1000000);
1247 if (usec) *usec = (l_int32) (usecs % 1000000);
1286 l_int32 tsec, tusec;
1289 PROCNAME(
"stopWallTimer");
1292 return (l_float32)ERROR_FLOAT(
"&timer not defined", procName, 0.0);
1295 return (l_float32)ERROR_FLOAT(
"timer not defined", procName, 0.0);
1298 tsec = timer->stop_sec - timer->start_sec;
1299 tusec = timer->stop_usec - timer->start_usec;
1302 return (tsec + ((l_float32)tusec) / 1000000.0);
1321 char buf[128] =
"", sep =
'Z';
1322 l_int32 gmt_offset, relh, relm;
1325 struct tm *tptr = &Tm;
1340 gmtime_s(tptr, &ut);
1345 gmtime_r(&ut, tptr);
1347 tptr->tm_isdst = -1;
1352 gmt_offset = (l_int32) difftime(ut, lt);
1355 else if (gmt_offset < 0)
1357 relh = L_ABS(gmt_offset) / 3600;
1358 relm = (L_ABS(gmt_offset) % 3600) / 60;
1362 localtime_s(tptr, &ut);
1364 tptr = localtime(&ut);
1367 localtime_r(&ut, tptr);
1369 strftime(buf,
sizeof(buf),
"%Y%m%d%H%M%S", tptr);
1370 sprintf(buf + 14,
"%c%02d'%02d'", sep, relh, relm);
size_t nbytesInFile(const char *filename)
nbytesInFile()
l_ok genRandomIntOnInterval(l_int32 start, l_int32 end, l_int32 seed, l_int32 *pval)
genRandomIntOnInterval()
LEPT_DLL l_int32 LeptMsgSeverity
void leptSetStderrHandler(void(*handler)(const char *))
leptSetStderrHandler()
L_TIMER startTimerNested(void)
startTimerNested(), stopTimerNested()
l_ok l_hashPtToUint64(l_int32 x, l_int32 y, l_uint64 *phash)
l_hashPtToUint64()
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()
void lept_stderr(const char *fmt,...)
lept_stderr()
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()
l_uint32 convertIntToGrayCode(l_uint32 val)
convertIntToGrayCode()
l_ok fileReplaceBytes(const char *filein, l_int32 start, l_int32 nbytes, l_uint8 *newdata, size_t newsize, const char *fileout)
fileReplaceBytes()
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()
char * l_getFormattedDate(void)
l_getFormattedDate()
l_ok l_hashFloat64ToUint64(l_float64 val, l_uint64 *phash)
l_hashFloat64ToUint64()
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()
l_ok l_hashStringToUint64Fast(const char *str, l_uint64 *phash)
l_hashStringToUint64Fast()
l_int32 returnErrorInt(const char *msg, const char *procname, l_int32 ival)
returnErrorInt()
char * getLeptonicaVersion(void)
getLeptonicaVersion()