112#include <config_auto.h>
120#include "allheaders.h"
154 if (newsev == L_SEVERITY_EXTERNAL) {
155 envsev = getenv(
"LEPT_MSG_SEVERITY");
159 L_INFO(
"message severity set to external\n",
"setMsgSeverity");
163 L_WARNING(
"environment var LEPT_MSG_SEVERITY not defined\n",
170 L_INFO(
"message severity set to %d\n",
"setMsgSeverity", newsev);
202 const char *procname,
220 const char *procname,
238 const char *procname,
259 const char *procname,
262 lept_stderr(
"Leptonica Error in %s: %s: %s\n", procname, msg, arg);
280 const char *procname,
283 lept_stderr(
"Leptonica Error in %s: %s: %s\n", procname, msg, arg);
301 const char *procname,
304 lept_stderr(
"Leptonica Error in %s: %s: %s\n", procname, msg, arg);
327static void lept_default_stderr_handler(
const char *formatted_msg)
330 fputs(formatted_msg, stderr);
335void (*stderr_handler)(
const char *) = lept_default_stderr_handler;
354 stderr_handler = handler;
356 stderr_handler = lept_default_stderr_handler;
360#define MAX_DEBUG_MESSAGE 2000
380char msg[MAX_DEBUG_MESSAGE];
384 n = vsnprintf(msg,
sizeof(msg), fmt, args);
388 (*stderr_handler)(msg);
409size_t nbytes1, nbytes2;
410l_uint8 *array1, *array2;
413 return ERROR_INT(
"&same not defined", __func__, 1);
415 if (!fname1 || !fname2)
416 return ERROR_INT(
"both names not defined", __func__, 1);
418 nbytes1 = nbytesInFile(fname1);
419 nbytes2 = nbytesInFile(fname2);
420 if (nbytes1 != nbytes2)
423 if ((array1 = l_binaryRead(fname1, &nbytes1)) == NULL)
424 return ERROR_INT(
"array1 not read", __func__, 1);
425 if ((array2 = l_binaryRead(fname2, &nbytes2)) == NULL) {
427 return ERROR_INT(
"array2 not read", __func__, 1);
430 for (i = 0; i < nbytes1; i++) {
431 if (array1[i] != array2[i]) {
459convertOnBigEnd16(l_uint16 shortin)
461 return ((shortin << 8) | (shortin >> 8));
465convertOnLittleEnd16(l_uint16 shortin)
473convertOnLittleEnd16(l_uint16 shortin)
475 return ((shortin << 8) | (shortin >> 8));
479convertOnBigEnd16(l_uint16 shortin)
493convertOnBigEnd32(l_uint32 wordin)
495 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
496 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
500convertOnLittleEnd32(l_uint32 wordin)
508convertOnLittleEnd32(l_uint32 wordin)
510 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
511 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
515convertOnBigEnd32(l_uint32 wordin)
552l_int32 i, locb, sizeb, rembytes;
553size_t inbytes, outbytes;
554l_uint8 *datain, *dataout;
556 if (!filein || !fileout)
557 return ERROR_INT(
"filein and fileout not both specified", __func__, 1);
558 if (loc < 0.0 || loc >= 1.0)
559 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", __func__, 1);
561 return ERROR_INT(
"size must be > 0.0", __func__, 1);
562 if (loc + size > 1.0)
565 datain = l_binaryRead(filein, &inbytes);
566 locb = (l_int32)(loc * inbytes + 0.5);
567 locb = L_MIN(locb, inbytes - 1);
568 sizeb = (l_int32)(size * inbytes + 0.5);
569 sizeb = L_MAX(1, sizeb);
570 sizeb = L_MIN(sizeb, inbytes - locb);
571 L_INFO(
"Removed %d bytes at location %d\n", __func__, sizeb, locb);
572 rembytes = inbytes - locb - sizeb;
574 outbytes = inbytes - sizeb;
575 dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1);
576 for (i = 0; i < locb; i++)
577 dataout[i] = datain[i];
578 for (i = 0; i < rembytes; i++)
579 dataout[locb + i] = datain[locb + sizeb + i];
580 l_binaryWrite(fileout,
"w", dataout, outbytes);
614l_int32 i, locb, sizeb;
618 if (!filein || !fileout)
619 return ERROR_INT(
"filein and fileout not both specified", __func__, 1);
620 if (loc < 0.0 || loc >= 1.0)
621 return ERROR_INT(
"loc must be in [0.0 ... 1.0)", __func__, 1);
623 return ERROR_INT(
"size must be > 0.0", __func__, 1);
624 if (loc + size > 1.0)
627 data = l_binaryRead(filein, &bytes);
628 locb = (l_int32)(loc * bytes + 0.5);
629 locb = L_MIN(locb, bytes - 1);
630 sizeb = (l_int32)(size * bytes + 0.5);
631 sizeb = L_MAX(1, sizeb);
632 sizeb = L_MIN(sizeb, bytes - locb);
633 L_INFO(
"Randomizing %d bytes at location %d\n", __func__, sizeb, locb);
636 for (i = 0; i < sizeb; i++) {
638 (l_uint8)(255.9 * ((l_float64)rand() / (l_float64)RAND_MAX));
641 l_binaryWrite(fileout,
"w", data, bytes);
676size_t inbytes, outbytes;
677l_uint8 *datain, *dataout;
679 if (!filein || !fileout)
680 return ERROR_INT(
"filein and fileout not both specified", __func__, 1);
682 datain = l_binaryRead(filein, &inbytes);
683 if (start + nbytes > inbytes)
684 L_WARNING(
"start + nbytes > length(filein) = %zu\n", __func__, inbytes);
686 if (!newdata) newsize = 0;
687 outbytes = inbytes - nbytes + newsize;
688 if ((dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1)) == NULL) {
690 return ERROR_INT(
"calloc fail for dataout", __func__, 1);
693 for (i = 0; i < start; i++)
694 dataout[i] = datain[i];
695 for (i = start; i < start + newsize; i++)
696 dataout[i] = newdata[i - start];
697 index = start + nbytes;
699 for (i = start; i < outbytes; i++, index++)
700 dataout[i] = datain[index];
701 l_binaryWrite(fileout,
"w", dataout, outbytes);
730 return ERROR_INT(
"&val not defined", __func__, 1);
733 return ERROR_INT(
"invalid range", __func__, 1);
735 if (seed > 0) srand(seed);
736 range = (l_float64)(end - start + 1);
737 *pval = start + (l_int32)((l_float64)range *
738 ((l_float64)rand() / (l_float64)RAND_MAX));
763 return (fval >= 0.0) ? (l_int32)(fval + 0.5) : (l_int32)(fval - 0.5);
776 return (fval >= 0.0) ? (l_int32)(fval) : -(l_int32)(-fval);
797 return (fval == (l_int32)fval ? (l_int32)fval :
798 fval > 0.0 ? 1 + (l_int32)(fval) : -(1 + (l_int32)(-fval)));
833 if (phash) *phash = 0;
834 if (!str || (str[0] ==
'\0'))
835 return ERROR_INT(
"str not defined or empty", __func__, 1);
837 return ERROR_INT(
"&hash not defined", __func__, 1);
839 mulp = 26544357894361247;
842 hash += (*str++ * mulp) ^ (hash >> 7);
844 *phash = hash ^ (hash << 37);
871 if (phash) *phash = 0;
872 if (!str || (str[0] ==
'\0'))
873 return ERROR_INT(
"str not defined or empty", __func__, 1);
875 return ERROR_INT(
"&hash not defined", __func__, 1);
878 for (p = (l_uint8 *)str; *p !=
'\0'; p++)
904 return ERROR_INT(
"&hash not defined", __func__, 1);
906 *phash = (l_uint64)(2173249142.3849 * x + 3763193258.6227 * y);
931 return ERROR_INT(
"&hash not defined", __func__, 1);
932 val = (val >= 0.0) ? 847019.66701 * val : -217324.91613 * val;
933 *phash = (l_uint64)val;
955 return ERROR_INT(
"&prime not defined", __func__, 1);
958 return ERROR_INT(
"start must be > 0", __func__, 1);
960 for (i = start + 1; ; i++) {
968 return ERROR_INT(
"prime not found!", __func__, 1);
987l_uint64 limit, ratio;
989 if (pis_prime) *pis_prime = 0;
990 if (pfactor) *pfactor = 0;
992 return ERROR_INT(
"&is_prime not defined", __func__, 1);
994 return ERROR_INT(
"n must be > 0", __func__, 1);
997 if (pfactor) *pfactor = 2;
1001 limit = (l_uint64)sqrt((l_float64)n);
1002 for (div = 3; div < limit; div += 2) {
1004 if (ratio * div == n) {
1005 if (pfactor) *pfactor = div;
1033 return (val >> 1) ^ val;
1048 for (shift = 1; shift < 32; shift <<= 1)
1049 val ^= val >> shift;
1068size_t bufsize = 100;
1070 char *version = (
char *)LEPT_CALLOC(bufsize,
sizeof(
char));
1074 char dllStr[] =
"DLL";
1076 char dllStr[] =
"LIB";
1079 char debugStr[] =
"Debug";
1081 char debugStr[] =
"Release";
1084 char bitStr[] =
" x86";
1086 char bitStr[] =
" x64";
1090 snprintf(version, bufsize,
"leptonica-%d.%d.%d (%s, %s) [MSC v.%d %s %s%s]",
1091 LIBLEPT_MAJOR_VERSION, LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION,
1092 __DATE__, __TIME__, _MSC_VER, dllStr, debugStr, bitStr);
1096 snprintf(version, bufsize,
"leptonica-%d.%d.%d", LIBLEPT_MAJOR_VERSION,
1097 LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION);
1107#if !defined(_WIN32) && !defined(__Fuchsia__)
1109#include <sys/time.h>
1110#include <sys/resource.h>
1112static struct rusage rusage_before;
1113static struct rusage rusage_after;
1127 getrusage(RUSAGE_SELF, &rusage_before);
1135 getrusage(RUSAGE_SELF, &rusage_after);
1137 tsec = rusage_after.ru_utime.tv_sec - rusage_before.ru_utime.tv_sec;
1138 tusec = rusage_after.ru_utime.tv_usec - rusage_before.ru_utime.tv_usec;
1139 return (tsec + ((l_float32)tusec) / 1000000.0);
1159struct rusage *rusage_start;
1161 rusage_start = (
struct rusage *)LEPT_CALLOC(1,
sizeof(
struct rusage));
1162 getrusage(RUSAGE_SELF, rusage_start);
1163 return rusage_start;
1167stopTimerNested(L_TIMER rusage_start)
1170struct rusage rusage_stop;
1172 getrusage(RUSAGE_SELF, &rusage_stop);
1174 tsec = rusage_stop.ru_utime.tv_sec -
1175 ((
struct rusage *)rusage_start)->ru_utime.tv_sec;
1176 tusec = rusage_stop.ru_utime.tv_usec -
1177 ((
struct rusage *)rusage_start)->ru_utime.tv_usec;
1178 LEPT_FREE(rusage_start);
1179 return (tsec + ((l_float32)tusec) / 1000000.0);
1196 gettimeofday(&tv, NULL);
1197 if (sec) *sec = (l_int32)tv.tv_sec;
1198 if (usec) *usec = (l_int32)tv.tv_usec;
1201#elif defined(__Fuchsia__)
1225stopTimerNested(L_TIMER rusage_start)
1242static ULARGE_INTEGER utime_before;
1243static ULARGE_INTEGER utime_after;
1249FILETIME start, stop, kernel, user;
1251 this_process = GetCurrentProcess();
1253 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
1255 utime_before.LowPart = user.dwLowDateTime;
1256 utime_before.HighPart = user.dwHighDateTime;
1263FILETIME start, stop, kernel, user;
1266 this_process = GetCurrentProcess();
1268 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
1270 utime_after.LowPart = user.dwLowDateTime;
1271 utime_after.HighPart = user.dwHighDateTime;
1272 hnsec = utime_after.QuadPart - utime_before.QuadPart;
1273 return (l_float32)(signed)hnsec / 10000000.0f;
1280FILETIME start, stop, kernel, user;
1281ULARGE_INTEGER *utime_start;
1283 this_process = GetCurrentProcess();
1285 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1287 utime_start = (ULARGE_INTEGER *)LEPT_CALLOC(1,
sizeof(ULARGE_INTEGER));
1288 utime_start->LowPart = user.dwLowDateTime;
1289 utime_start->HighPart = user.dwHighDateTime;
1294stopTimerNested(L_TIMER utime_start)
1297FILETIME start, stop, kernel, user;
1298ULARGE_INTEGER utime_stop;
1301 this_process = GetCurrentProcess ();
1303 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1305 utime_stop.LowPart = user.dwLowDateTime;
1306 utime_stop.HighPart = user.dwHighDateTime;
1307 hnsec = utime_stop.QuadPart - ((ULARGE_INTEGER *)utime_start)->QuadPart;
1308 LEPT_FREE(utime_start);
1309 return (l_float32)(signed)hnsec / 10000000.0f;
1316ULARGE_INTEGER utime, birthunix;
1318LONGLONG birthunixhnsec = 116444736000000000;
1321 GetSystemTimeAsFileTime(&systemtime);
1322 utime.LowPart = systemtime.dwLowDateTime;
1323 utime.HighPart = systemtime.dwHighDateTime;
1325 birthunix.LowPart = (DWORD) birthunixhnsec;
1326 birthunix.HighPart = birthunixhnsec >> 32;
1328 usecs = (LONGLONG) ((utime.QuadPart - birthunix.QuadPart) / 10);
1330 if (sec) *sec = (l_int32) (usecs / 1000000);
1331 if (usec) *usec = (l_int32) (usecs % 1000000);
1374 return (l_float32)ERROR_FLOAT(
"&timer not defined", __func__, 0.0);
1377 return (l_float32)ERROR_FLOAT(
"timer not defined", __func__, 0.0);
1380 tsec = timer->stop_sec - timer->start_sec;
1381 tusec = timer->stop_usec - timer->start_usec;
1384 return (tsec + ((l_float32)tusec) / 1000000.0f);
1403char buf[128] =
"", sep =
'Z';
1404l_int32 gmt_offset, relh, relm;
1407struct tm *tptr = &Tm;
1422 gmtime_s(tptr, &ut);
1427 gmtime_r(&ut, tptr);
1429 tptr->tm_isdst = -1;
1434 gmt_offset = (l_int32) difftime(ut, lt);
1437 else if (gmt_offset < 0)
1439 relh = L_ABS(gmt_offset) / 3600;
1440 relm = (L_ABS(gmt_offset) % 3600) / 60;
1444 localtime_s(tptr, &ut);
1446 tptr = localtime(&ut);
1449 localtime_r(&ut, tptr);
1451 strftime(buf,
sizeof(buf),
"%Y%m%d%H%M%S", tptr);
1452 sprintf(buf + 14,
"%c%02d'%02d'", sep, relh, relm);
1453 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()
void * returnErrorPtr1(const char *msg, const char *arg, const char *procname, void *pval)
returnErrorPtr1()
l_int32 returnErrorInt1(const char *msg, const char *arg, const char *procname, l_int32 ival)
returnErrorInt1()
l_float32 returnErrorFloat(const char *msg, const char *procname, l_float32 fval)
returnErrorFloat()
l_int32 lept_floor(l_float32 fval)
lept_floor()
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()
LEPT_DLL l_int32 LeptMsgSeverity
l_ok l_hashStringToUint64(const char *str, l_uint64 *phash)
l_hashStringToUint64()
void l_getCurrentTime(l_int32 *sec, l_int32 *usec)
l_getCurrentTime()
l_float32 returnErrorFloat1(const char *msg, const char *arg, const char *procname, l_float32 fval)
returnErrorFloat1()
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 lept_ceiling(l_float32 fval)
lept_ceiling()
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()