107#include <config_auto.h>
115#include "allheaders.h"
119LEPT_DLL l_int32 LeptMsgSeverity = DEFAULT_SEVERITY;
148 oldsev = LeptMsgSeverity;
149 if (newsev == L_SEVERITY_EXTERNAL) {
150 envsev = getenv(
"LEPT_MSG_SEVERITY");
152 LeptMsgSeverity = atoi(envsev);
154 L_INFO(
"message severity set to external\n",
"setMsgSeverity");
158 L_WARNING(
"environment var LEPT_MSG_SEVERITY not defined\n",
163 LeptMsgSeverity = newsev;
165 L_INFO(
"message severity set to %d\n",
"setMsgSeverity", newsev);
194 const char *procname,
212 const char *procname,
230 const char *procname,
256static void lept_default_stderr_handler(
const char *formatted_msg)
259 fputs(formatted_msg, stderr);
264void (*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
309char msg[MAX_DEBUG_MESSAGE];
313 n = vsnprintf(msg,
sizeof(msg), fmt, args);
317 (*stderr_handler)(msg);
338size_t nbytes1, nbytes2;
339l_uint8 *array1, *array2;
342 return ERROR_INT(
"&same not defined", __func__, 1);
344 if (!fname1 || !fname2)
345 return ERROR_INT(
"both names not defined", __func__, 1);
347 nbytes1 = nbytesInFile(fname1);
348 nbytes2 = nbytesInFile(fname2);
349 if (nbytes1 != nbytes2)
352 if ((array1 = l_binaryRead(fname1, &nbytes1)) == NULL)
353 return ERROR_INT(
"array1 not read", __func__, 1);
354 if ((array2 = l_binaryRead(fname2, &nbytes2)) == NULL) {
356 return ERROR_INT(
"array2 not read", __func__, 1);
359 for (i = 0; i < nbytes1; i++) {
360 if (array1[i] != array2[i]) {
388convertOnBigEnd16(l_uint16 shortin)
390 return ((shortin << 8) | (shortin >> 8));
394convertOnLittleEnd16(l_uint16 shortin)
402convertOnLittleEnd16(l_uint16 shortin)
404 return ((shortin << 8) | (shortin >> 8));
408convertOnBigEnd16(l_uint16 shortin)
422convertOnBigEnd32(l_uint32 wordin)
424 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
425 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
429convertOnLittleEnd32(l_uint32 wordin)
437convertOnLittleEnd32(l_uint32 wordin)
439 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
440 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
444convertOnBigEnd32(l_uint32 wordin)
481l_int32 i, locb, sizeb, rembytes;
482size_t inbytes, outbytes;
483l_uint8 *datain, *dataout;
485 if (!filein || !fileout)
486 return ERROR_INT(
"filein and fileout not both specified", __func__, 1);
487 if (loc < 0.0 || loc >= 1.0)
488 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", __func__, 1);
490 return ERROR_INT(
"size must be > 0.0", __func__, 1);
491 if (loc + size > 1.0)
494 datain = l_binaryRead(filein, &inbytes);
495 locb = (l_int32)(loc * inbytes + 0.5);
496 locb = L_MIN(locb, inbytes - 1);
497 sizeb = (l_int32)(size * inbytes + 0.5);
498 sizeb = L_MAX(1, sizeb);
499 sizeb = L_MIN(sizeb, inbytes - locb);
500 L_INFO(
"Removed %d bytes at location %d\n", __func__, sizeb, locb);
501 rembytes = inbytes - locb - sizeb;
503 outbytes = inbytes - sizeb;
504 dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1);
505 for (i = 0; i < locb; i++)
506 dataout[i] = datain[i];
507 for (i = 0; i < rembytes; i++)
508 dataout[locb + i] = datain[locb + sizeb + i];
509 l_binaryWrite(fileout,
"w", dataout, outbytes);
543l_int32 i, locb, sizeb;
547 if (!filein || !fileout)
548 return ERROR_INT(
"filein and fileout not both specified", __func__, 1);
549 if (loc < 0.0 || loc >= 1.0)
550 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", __func__, 1);
552 return ERROR_INT(
"size must be > 0.0", __func__, 1);
553 if (loc + size > 1.0)
556 data = l_binaryRead(filein, &bytes);
557 locb = (l_int32)(loc * bytes + 0.5);
558 locb = L_MIN(locb, bytes - 1);
559 sizeb = (l_int32)(size * bytes + 0.5);
560 sizeb = L_MAX(1, sizeb);
561 sizeb = L_MIN(sizeb, bytes - locb);
562 L_INFO(
"Randomizing %d bytes at location %d\n", __func__, sizeb, locb);
565 for (i = 0; i < sizeb; i++) {
567 (l_uint8)(255.9 * ((l_float64)rand() / (l_float64)RAND_MAX));
570 l_binaryWrite(fileout,
"w", data, bytes);
605size_t inbytes, outbytes;
606l_uint8 *datain, *dataout;
608 if (!filein || !fileout)
609 return ERROR_INT(
"filein and fileout not both specified", __func__, 1);
611 datain = l_binaryRead(filein, &inbytes);
612 if (start + nbytes > inbytes)
613 L_WARNING(
"start + nbytes > length(filein) = %zu\n", __func__, inbytes);
615 if (!newdata) newsize = 0;
616 outbytes = inbytes - nbytes + newsize;
617 if ((dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1)) == NULL) {
619 return ERROR_INT(
"calloc fail for dataout", __func__, 1);
622 for (i = 0; i < start; i++)
623 dataout[i] = datain[i];
624 for (i = start; i < start + newsize; i++)
625 dataout[i] = newdata[i - start];
626 index = start + nbytes;
628 for (i = start; i < outbytes; i++, index++)
629 dataout[i] = datain[index];
630 l_binaryWrite(fileout,
"w", dataout, outbytes);
659 return ERROR_INT(
"&val not defined", __func__, 1);
662 return ERROR_INT(
"invalid range", __func__, 1);
664 if (seed > 0) srand(seed);
665 range = (l_float64)(end - start + 1);
666 *pval = start + (l_int32)((l_float64)range *
667 ((l_float64)rand() / (l_float64)RAND_MAX));
692 return (fval >= 0.0) ? (l_int32)(fval + 0.5) : (l_int32)(fval - 0.5);
727 if (phash) *phash = 0;
728 if (!str || (str[0] ==
'\0'))
729 return ERROR_INT(
"str not defined or empty", __func__, 1);
731 return ERROR_INT(
"&hash not defined", __func__, 1);
733 mulp = 26544357894361247;
736 hash += (*str++ * mulp) ^ (hash >> 7);
738 *phash = hash ^ (hash << 37);
765 if (phash) *phash = 0;
766 if (!str || (str[0] ==
'\0'))
767 return ERROR_INT(
"str not defined or empty", __func__, 1);
769 return ERROR_INT(
"&hash not defined", __func__, 1);
772 for (p = (l_uint8 *)str; *p !=
'\0'; p++)
798 return ERROR_INT(
"&hash not defined", __func__, 1);
800 *phash = (l_uint64)(2173249142.3849 * x + 3763193258.6227 * y);
825 return ERROR_INT(
"&hash not defined", __func__, 1);
826 val = (val >= 0.0) ? 847019.66701 * val : -217324.91613 * val;
827 *phash = (l_uint64)val;
849 return ERROR_INT(
"&prime not defined", __func__, 1);
852 return ERROR_INT(
"start must be > 0", __func__, 1);
854 for (i = start + 1; ; i++) {
862 return ERROR_INT(
"prime not found!", __func__, 1);
881l_uint64 limit, ratio;
883 if (pis_prime) *pis_prime = 0;
884 if (pfactor) *pfactor = 0;
886 return ERROR_INT(
"&is_prime not defined", __func__, 1);
888 return ERROR_INT(
"n must be > 0", __func__, 1);
891 if (pfactor) *pfactor = 2;
895 limit = (l_uint64)sqrt((l_float64)n);
896 for (div = 3; div < limit; div += 2) {
898 if (ratio * div == n) {
899 if (pfactor) *pfactor = div;
927 return (val >> 1) ^ val;
942 for (shift = 1; shift < 32; shift <<= 1)
964 char *version = (
char *)LEPT_CALLOC(bufsize,
sizeof(
char));
968 char dllStr[] =
"DLL";
970 char dllStr[] =
"LIB";
973 char debugStr[] =
"Debug";
975 char debugStr[] =
"Release";
978 char bitStr[] =
" x86";
980 char bitStr[] =
" x64";
984 snprintf(version, bufsize,
"leptonica-%d.%d.%d (%s, %s) [MSC v.%d %s %s%s]",
985 LIBLEPT_MAJOR_VERSION, LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION,
986 __DATE__, __TIME__, _MSC_VER, dllStr, debugStr, bitStr);
990 snprintf(version, bufsize,
"leptonica-%d.%d.%d", LIBLEPT_MAJOR_VERSION,
991 LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION);
1001#if !defined(_WIN32) && !defined(__Fuchsia__)
1003#include <sys/time.h>
1004#include <sys/resource.h>
1006static struct rusage rusage_before;
1007static struct rusage rusage_after;
1021 getrusage(RUSAGE_SELF, &rusage_before);
1029 getrusage(RUSAGE_SELF, &rusage_after);
1031 tsec = rusage_after.ru_utime.tv_sec - rusage_before.ru_utime.tv_sec;
1032 tusec = rusage_after.ru_utime.tv_usec - rusage_before.ru_utime.tv_usec;
1033 return (tsec + ((l_float32)tusec) / 1000000.0);
1053struct rusage *rusage_start;
1055 rusage_start = (
struct rusage *)LEPT_CALLOC(1,
sizeof(
struct rusage));
1056 getrusage(RUSAGE_SELF, rusage_start);
1057 return rusage_start;
1061stopTimerNested(L_TIMER rusage_start)
1064struct rusage rusage_stop;
1066 getrusage(RUSAGE_SELF, &rusage_stop);
1068 tsec = rusage_stop.ru_utime.tv_sec -
1069 ((
struct rusage *)rusage_start)->ru_utime.tv_sec;
1070 tusec = rusage_stop.ru_utime.tv_usec -
1071 ((
struct rusage *)rusage_start)->ru_utime.tv_usec;
1072 LEPT_FREE(rusage_start);
1073 return (tsec + ((l_float32)tusec) / 1000000.0);
1090 gettimeofday(&tv, NULL);
1091 if (sec) *sec = (l_int32)tv.tv_sec;
1092 if (usec) *usec = (l_int32)tv.tv_usec;
1095#elif defined(__Fuchsia__)
1119stopTimerNested(L_TIMER rusage_start)
1136static ULARGE_INTEGER utime_before;
1137static ULARGE_INTEGER utime_after;
1143FILETIME start, stop, kernel, user;
1145 this_process = GetCurrentProcess();
1147 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
1149 utime_before.LowPart = user.dwLowDateTime;
1150 utime_before.HighPart = user.dwHighDateTime;
1157FILETIME start, stop, kernel, user;
1160 this_process = GetCurrentProcess();
1162 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
1164 utime_after.LowPart = user.dwLowDateTime;
1165 utime_after.HighPart = user.dwHighDateTime;
1166 hnsec = utime_after.QuadPart - utime_before.QuadPart;
1167 return (l_float32)(signed)hnsec / 10000000.0;
1174FILETIME start, stop, kernel, user;
1175ULARGE_INTEGER *utime_start;
1177 this_process = GetCurrentProcess();
1179 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1181 utime_start = (ULARGE_INTEGER *)LEPT_CALLOC(1,
sizeof(ULARGE_INTEGER));
1182 utime_start->LowPart = user.dwLowDateTime;
1183 utime_start->HighPart = user.dwHighDateTime;
1188stopTimerNested(L_TIMER utime_start)
1191FILETIME start, stop, kernel, user;
1192ULARGE_INTEGER utime_stop;
1195 this_process = GetCurrentProcess ();
1197 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1199 utime_stop.LowPart = user.dwLowDateTime;
1200 utime_stop.HighPart = user.dwHighDateTime;
1201 hnsec = utime_stop.QuadPart - ((ULARGE_INTEGER *)utime_start)->QuadPart;
1202 LEPT_FREE(utime_start);
1203 return (l_float32)(signed)hnsec / 10000000.0;
1210ULARGE_INTEGER utime, birthunix;
1212LONGLONG birthunixhnsec = 116444736000000000;
1215 GetSystemTimeAsFileTime(&systemtime);
1216 utime.LowPart = systemtime.dwLowDateTime;
1217 utime.HighPart = systemtime.dwHighDateTime;
1219 birthunix.LowPart = (DWORD) birthunixhnsec;
1220 birthunix.HighPart = birthunixhnsec >> 32;
1222 usecs = (LONGLONG) ((utime.QuadPart - birthunix.QuadPart) / 10);
1224 if (sec) *sec = (l_int32) (usecs / 1000000);
1225 if (usec) *usec = (l_int32) (usecs % 1000000);
1250 timer = (L_WALLTIMER *)LEPT_CALLOC(1,
sizeof(L_WALLTIMER));
1268 return (l_float32)ERROR_FLOAT(
"&timer not defined", __func__, 0.0);
1271 return (l_float32)ERROR_FLOAT(
"timer not defined", __func__, 0.0);
1274 tsec = timer->stop_sec - timer->start_sec;
1275 tusec = timer->stop_usec - timer->start_usec;
1278 return (tsec + ((l_float32)tusec) / 1000000.0);
1297char buf[128] =
"", sep =
'Z';
1298l_int32 gmt_offset, relh, relm;
1301struct tm *tptr = &Tm;
1316 gmtime_s(tptr, &ut);
1321 gmtime_r(&ut, tptr);
1323 tptr->tm_isdst = -1;
1328 gmt_offset = (l_int32) difftime(ut, lt);
1331 else if (gmt_offset < 0)
1333 relh = L_ABS(gmt_offset) / 3600;
1334 relm = (L_ABS(gmt_offset) % 3600) / 60;
1338 localtime_s(tptr, &ut);
1340 tptr = localtime(&ut);
1343 localtime_r(&ut, tptr);
1345 strftime(buf,
sizeof(buf),
"%Y%m%d%H%M%S", tptr);
1346 sprintf(buf + 14,
"%c%02d'%02d'", sep, relh, relm);
1347 return stringNew(buf);
l_ok fileCorruptByMutation(const char *filein, l_float32 loc, l_float32 size, const char *fileout)
fileCorruptByMutation()
char * l_getFormattedDate(void)
l_getFormattedDate()
l_ok filesAreIdentical(const char *fname1, const char *fname2, l_int32 *psame)
filesAreIdentical()
l_ok fileReplaceBytes(const char *filein, l_int32 start, l_int32 nbytes, l_uint8 *newdata, size_t newsize, const char *fileout)
fileReplaceBytes()
l_float32 stopWallTimer(L_WALLTIMER **ptimer)
stopWallTimer()
void startTimer(void)
startTimer(), stopTimer()
l_ok l_hashStringToUint64Fast(const char *str, l_uint64 *phash)
l_hashStringToUint64Fast()
l_ok genRandomIntOnInterval(l_int32 start, l_int32 end, l_int32 seed, l_int32 *pval)
genRandomIntOnInterval()
void lept_stderr(const char *fmt,...)
lept_stderr()
l_ok lept_isPrime(l_uint64 n, l_int32 *pis_prime, l_uint32 *pfactor)
lept_isPrime()
char * getLeptonicaVersion(void)
getLeptonicaVersion()
void leptSetStderrHandler(void(*handler)(const char *))
leptSetStderrHandler()
l_float32 returnErrorFloat(const char *msg, const char *procname, l_float32 fval)
returnErrorFloat()
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_hashStringToUint64(const char *str, l_uint64 *phash)
l_hashStringToUint64()
void l_getCurrentTime(l_int32 *sec, l_int32 *usec)
l_getCurrentTime()
L_TIMER startTimerNested(void)
startTimerNested(), stopTimerNested()
l_int32 setMsgSeverity(l_int32 newsev)
setMsgSeverity()
l_int32 lept_roundftoi(l_float32 fval)
lept_roundftoi()
void * returnErrorPtr(const char *msg, const char *procname, void *pval)
returnErrorPtr()
l_uint32 convertGrayCodeToInt(l_uint32 val)
convertGrayCodeToInt()
l_int32 returnErrorInt(const char *msg, const char *procname, l_int32 ival)
returnErrorInt()
L_WALLTIMER * startWallTimer(void)
startWallTimer()
l_uint32 convertIntToGrayCode(l_uint32 val)
convertIntToGrayCode()
l_ok l_hashFloat64ToUint64(l_float64 val, l_uint64 *phash)
l_hashFloat64ToUint64()
l_ok l_hashPtToUint64(l_int32 x, l_int32 y, l_uint64 *phash)
l_hashPtToUint64()