Leptonica 1.85.0
Image processing and image analysis suite
Loading...
Searching...
No Matches
utils1.c
Go to the documentation of this file.
1/*====================================================================*
2 - Copyright (C) 2001 Leptonica. All rights reserved.
3 -
4 - Redistribution and use in source and binary forms, with or without
5 - modification, are permitted provided that the following conditions
6 - are met:
7 - 1. Redistributions of source code must retain the above copyright
8 - notice, this list of conditions and the following disclaimer.
9 - 2. Redistributions in binary form must reproduce the above
10 - copyright notice, this list of conditions and the following
11 - disclaimer in the documentation and/or other materials
12 - provided with the distribution.
13 -
14 - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 - ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 - LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 - A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ANY
18 - CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 - EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 - PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 - PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 - OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
23 - NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *====================================================================*/
26
111#ifdef HAVE_CONFIG_H
112#include <config_auto.h>
113#endif /* HAVE_CONFIG_H */
114
115#ifdef _WIN32
116#include <windows.h>
117#endif /* _WIN32 */
118
119#include <time.h>
120#include "allheaders.h"
121#include <math.h>
122
123 /* Global for controlling message output at runtime */
124LEPT_DLL l_int32 LeptMsgSeverity = DEFAULT_SEVERITY;
125
126#define DEBUG_SEV 0
127
128/*----------------------------------------------------------------------*
129 * Control of error, warning and info messages *
130 *----------------------------------------------------------------------*/
147l_int32
148setMsgSeverity(l_int32 newsev)
149{
150l_int32 oldsev;
151char *envsev;
152
153 oldsev = LeptMsgSeverity;
154 if (newsev == L_SEVERITY_EXTERNAL) {
155 envsev = getenv("LEPT_MSG_SEVERITY");
156 if (envsev) {
157 LeptMsgSeverity = atoi(envsev);
158#if DEBUG_SEV
159 L_INFO("message severity set to external\n", "setMsgSeverity");
160#endif /* DEBUG_SEV */
161 } else {
162#if DEBUG_SEV
163 L_WARNING("environment var LEPT_MSG_SEVERITY not defined\n",
164 "setMsgSeverity");
165#endif /* DEBUG_SEV */
166 }
167 } else {
168 LeptMsgSeverity = newsev;
169#if DEBUG_SEV
170 L_INFO("message severity set to %d\n", "setMsgSeverity", newsev);
171#endif /* DEBUG_SEV */
172 }
173
174 return oldsev;
175}
176
177
178/*----------------------------------------------------------------------*
179 * Error return functions, invoked by macros *
180 *----------------------------------------------------------------------*
181 * *
182 * (1) These error functions print messages to stderr and allow *
183 * exit from the function that called them. *
184 * (2) They must be invoked only by the three argument macros *
185 * ERROR_INT, ERROR_FLOAT, ERROR_PTR *
186 * or the four argument macros *
187 * ERROR_INT_1, ERROR_FLOAT_1, ERROR_PTR_1 *
188 * which are in environ.h. *
189 * (3) The print output can be disabled at compile time, either *
190 * by using -DNO_CONSOLE_IO or by setting LeptMsgSeverity. *
191 *----------------------------------------------------------------------*/
200l_int32
201returnErrorInt(const char *msg,
202 const char *procname,
203 l_int32 ival)
204{
205 lept_stderr("Error in %s: %s\n", procname, msg);
206 return ival;
207}
208
209
218l_float32
219returnErrorFloat(const char *msg,
220 const char *procname,
221 l_float32 fval)
222{
223 lept_stderr("Error in %s: %s\n", procname, msg);
224 return fval;
225}
226
227
236void *
237returnErrorPtr(const char *msg,
238 const char *procname,
239 void *pval)
240{
241 lept_stderr("Error in %s: %s\n", procname, msg);
242 return pval;
243}
244
245
256l_int32
257returnErrorInt1(const char *msg,
258 const char *arg,
259 const char *procname,
260 l_int32 ival)
261{
262 lept_stderr("Leptonica Error in %s: %s: %s\n", procname, msg, arg);
263 return ival;
264}
265
266
277l_float32
278returnErrorFloat1(const char *msg,
279 const char *arg,
280 const char *procname,
281 l_float32 fval)
282{
283 lept_stderr("Leptonica Error in %s: %s: %s\n", procname, msg, arg);
284 return fval;
285}
286
287
298void *
299returnErrorPtr1(const char *msg,
300 const char *arg,
301 const char *procname,
302 void *pval)
303{
304 lept_stderr("Leptonica Error in %s: %s: %s\n", procname, msg, arg);
305 return pval;
306}
307
308
309/*------------------------------------------------------------------------*
310 * Runtime redirection of stderr *
311 *------------------------------------------------------------------------*
312 * *
313 * The user can provide a callback function to redirect messages *
314 * that would otherwise go to stderr. Here are two examples: *
315 * (1) to stop all messages: *
316 * void send_to_devnull(const char *msg) {} *
317 * (2) to write to the system logger: *
318 * void send_to_syslog(const char *msg) { *
319 * syslog(1, msg); *
320 * } *
321 * These would then be registered using *
322 * leptSetStderrHandler(send_to_devnull); *
323 * and *
324 * leptSetStderrHandler(send_to_syslog); *
325 *------------------------------------------------------------------------*/
326 /* By default, all messages go to stderr */
327static void lept_default_stderr_handler(const char *formatted_msg)
328{
329 if (formatted_msg)
330 fputs(formatted_msg, stderr);
331}
332
333 /* The stderr callback handler is private to leptonica.
334 * By default it writes to stderr. */
335void (*stderr_handler)(const char *) = lept_default_stderr_handler;
336
337
351void leptSetStderrHandler(void (*handler)(const char *))
352{
353 if (handler)
354 stderr_handler = handler;
355 else
356 stderr_handler = lept_default_stderr_handler;
357}
358
359
360#define MAX_DEBUG_MESSAGE 2000
377void lept_stderr(const char *fmt, ...)
378{
379va_list args;
380char msg[MAX_DEBUG_MESSAGE];
381l_int32 n;
382
383 va_start(args, fmt);
384 n = vsnprintf(msg, sizeof(msg), fmt, args);
385 va_end(args);
386 if (n < 0)
387 return;
388 (*stderr_handler)(msg);
389}
390
391
392/*--------------------------------------------------------------------*
393 * Test files for equivalence *
394 *--------------------------------------------------------------------*/
403l_ok
404filesAreIdentical(const char *fname1,
405 const char *fname2,
406 l_int32 *psame)
407{
408l_int32 i, same;
409size_t nbytes1, nbytes2;
410l_uint8 *array1, *array2;
411
412 if (!psame)
413 return ERROR_INT("&same not defined", __func__, 1);
414 *psame = 0;
415 if (!fname1 || !fname2)
416 return ERROR_INT("both names not defined", __func__, 1);
417
418 nbytes1 = nbytesInFile(fname1);
419 nbytes2 = nbytesInFile(fname2);
420 if (nbytes1 != nbytes2)
421 return 0;
422
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) {
426 LEPT_FREE(array1);
427 return ERROR_INT("array2 not read", __func__, 1);
428 }
429 same = 1;
430 for (i = 0; i < nbytes1; i++) {
431 if (array1[i] != array2[i]) {
432 same = 0;
433 break;
434 }
435 }
436 LEPT_FREE(array1);
437 LEPT_FREE(array2);
438 *psame = same;
439
440 return 0;
441}
442
443
444/*--------------------------------------------------------------------------*
445 * 16 and 32 bit byte-swapping on big endian and little endian machines *
446 *--------------------------------------------------------------------------*
447 * *
448 * These are typically used for I/O conversions: *
449 * (1) endian conversion for data that was read from a file *
450 * (2) endian conversion on data before it is written to a file *
451 *--------------------------------------------------------------------------*/
452
453/*--------------------------------------------------------------------*
454 * 16-bit byte swapping *
455 *--------------------------------------------------------------------*/
456#ifdef L_BIG_ENDIAN
457
458l_uint16
459convertOnBigEnd16(l_uint16 shortin)
460{
461 return ((shortin << 8) | (shortin >> 8));
462}
463
464l_uint16
465convertOnLittleEnd16(l_uint16 shortin)
466{
467 return shortin;
468}
469
470#else /* L_LITTLE_ENDIAN */
471
472l_uint16
473convertOnLittleEnd16(l_uint16 shortin)
474{
475 return ((shortin << 8) | (shortin >> 8));
476}
477
478l_uint16
479convertOnBigEnd16(l_uint16 shortin)
480{
481 return shortin;
482}
483
484#endif /* L_BIG_ENDIAN */
485
486
487/*--------------------------------------------------------------------*
488 * 32-bit byte swapping *
489 *--------------------------------------------------------------------*/
490#ifdef L_BIG_ENDIAN
491
492l_uint32
493convertOnBigEnd32(l_uint32 wordin)
494{
495 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
496 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
497}
498
499l_uint32
500convertOnLittleEnd32(l_uint32 wordin)
501{
502 return wordin;
503}
504
505#else /* L_LITTLE_ENDIAN */
506
507l_uint32
508convertOnLittleEnd32(l_uint32 wordin)
509{
510 return ((wordin << 24) | ((wordin << 8) & 0x00ff0000) |
511 ((wordin >> 8) & 0x0000ff00) | (wordin >> 24));
512}
513
514l_uint32
515convertOnBigEnd32(l_uint32 wordin)
516{
517 return wordin;
518}
519
520#endif /* L_BIG_ENDIAN */
521
522
523/*---------------------------------------------------------------------*
524 * File corruption and byte replacement operations *
525 *---------------------------------------------------------------------*/
546l_ok
547fileCorruptByDeletion(const char *filein,
548 l_float32 loc,
549 l_float32 size,
550 const char *fileout)
551{
552l_int32 i, locb, sizeb, rembytes;
553size_t inbytes, outbytes;
554l_uint8 *datain, *dataout;
555
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);
560 if (size <= 0.0)
561 return ERROR_INT("size must be > 0.0", __func__, 1);
562 if (loc + size > 1.0)
563 size = 1.0f - loc;
564
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); /* >= 1 */
571 L_INFO("Removed %d bytes at location %d\n", __func__, sizeb, locb);
572 rembytes = inbytes - locb - sizeb; /* >= 0; to be copied, after excision */
573
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);
581
582 LEPT_FREE(datain);
583 LEPT_FREE(dataout);
584 return 0;
585}
586
587
608l_ok
609fileCorruptByMutation(const char *filein,
610 l_float32 loc,
611 l_float32 size,
612 const char *fileout)
613{
614l_int32 i, locb, sizeb;
615size_t bytes;
616l_uint8 *data;
617
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);
622 if (size <= 0.0)
623 return ERROR_INT("size must be > 0.0", __func__, 1);
624 if (loc + size > 1.0)
625 size = 1.0f - loc;
626
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); /* >= 1 */
633 L_INFO("Randomizing %d bytes at location %d\n", __func__, sizeb, locb);
634
635 /* Make an array of random bytes and do the substitution */
636 for (i = 0; i < sizeb; i++) {
637 data[locb + i] =
638 (l_uint8)(255.9 * ((l_float64)rand() / (l_float64)RAND_MAX));
639 }
640
641 l_binaryWrite(fileout, "w", data, bytes);
642 LEPT_FREE(data);
643 return 0;
644}
645
646
667l_ok
668fileReplaceBytes(const char *filein,
669 l_int32 start,
670 l_int32 nbytes,
671 l_uint8 *newdata,
672 size_t newsize,
673 const char *fileout)
674{
675l_int32 i, index;
676size_t inbytes, outbytes;
677l_uint8 *datain, *dataout;
678
679 if (!filein || !fileout)
680 return ERROR_INT("filein and fileout not both specified", __func__, 1);
681
682 datain = l_binaryRead(filein, &inbytes);
683 if (start + nbytes > inbytes)
684 L_WARNING("start + nbytes > length(filein) = %zu\n", __func__, inbytes);
685
686 if (!newdata) newsize = 0;
687 outbytes = inbytes - nbytes + newsize;
688 if ((dataout = (l_uint8 *)LEPT_CALLOC(outbytes, 1)) == NULL) {
689 LEPT_FREE(datain);
690 return ERROR_INT("calloc fail for dataout", __func__, 1);
691 }
692
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; /* for datain */
698 start += newsize; /* for dataout */
699 for (i = start; i < outbytes; i++, index++)
700 dataout[i] = datain[index];
701 l_binaryWrite(fileout, "w", dataout, outbytes);
702
703 LEPT_FREE(datain);
704 LEPT_FREE(dataout);
705 return 0;
706}
707
708
709/*---------------------------------------------------------------------*
710 * Generate random integer in given interval *
711 *---------------------------------------------------------------------*/
721l_ok
723 l_int32 end,
724 l_int32 seed,
725 l_int32 *pval)
726{
727l_float64 range;
728
729 if (!pval)
730 return ERROR_INT("&val not defined", __func__, 1);
731 *pval = 0;
732 if (end < start)
733 return ERROR_INT("invalid range", __func__, 1);
734
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));
739 return 0;
740}
741
742
743/*---------------------------------------------------------------------*
744 * Simple math functions *
745 *---------------------------------------------------------------------*/
760l_int32
761lept_roundftoi(l_float32 fval)
762{
763 return (fval >= 0.0) ? (l_int32)(fval + 0.5) : (l_int32)(fval - 0.5);
764}
765
766
773l_int32
774lept_floor(l_float32 fval)
775{
776 return (fval >= 0.0) ? (l_int32)(fval) : -(l_int32)(-fval);
777}
778
779
794l_int32
795lept_ceiling(l_float32 fval)
796{
797 return (fval == (l_int32)fval ? (l_int32)fval :
798 fval > 0.0 ? 1 + (l_int32)(fval) : -(1 + (l_int32)(-fval)));
799}
800
801
802/*---------------------------------------------------------------------*
803 * 64-bit hash functions *
804 *---------------------------------------------------------------------*/
827l_ok
828l_hashStringToUint64(const char *str,
829 l_uint64 *phash)
830{
831l_uint64 hash, mulp;
832
833 if (phash) *phash = 0;
834 if (!str || (str[0] == '\0'))
835 return ERROR_INT("str not defined or empty", __func__, 1);
836 if (!phash)
837 return ERROR_INT("&hash not defined", __func__, 1);
838
839 mulp = 26544357894361247; /* prime, about 1/700 of the max uint64 */
840 hash = 104395301;
841 while (*str) {
842 hash += (*str++ * mulp) ^ (hash >> 7); /* shift [1...23] are ok */
843 }
844 *phash = hash ^ (hash << 37);
845 return 0;
846}
847
848
864l_ok
866 l_uint64 *phash)
867{
868l_uint64 h;
869l_uint8 *p;
870
871 if (phash) *phash = 0;
872 if (!str || (str[0] == '\0'))
873 return ERROR_INT("str not defined or empty", __func__, 1);
874 if (!phash)
875 return ERROR_INT("&hash not defined", __func__, 1);
876
877 h = 0;
878 for (p = (l_uint8 *)str; *p != '\0'; p++)
879 h = 37 * h + *p; /* 37 is good prime number for this */
880 *phash = h;
881 return 0;
882}
883
884
898l_ok
900 l_int32 y,
901 l_uint64 *phash)
902{
903 if (!phash)
904 return ERROR_INT("&hash not defined", __func__, 1);
905
906 *phash = (l_uint64)(2173249142.3849 * x + 3763193258.6227 * y);
907 return 0;
908}
909
910
926l_ok
928 l_uint64 *phash)
929{
930 if (!phash)
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;
934 return 0;
935}
936
937
938/*---------------------------------------------------------------------*
939 * Prime finders *
940 *---------------------------------------------------------------------*/
948l_ok
950 l_uint32 *pprime)
951{
952l_int32 i, is_prime;
953
954 if (!pprime)
955 return ERROR_INT("&prime not defined", __func__, 1);
956 *pprime = 0;
957 if (start <= 0)
958 return ERROR_INT("start must be > 0", __func__, 1);
959
960 for (i = start + 1; ; i++) {
961 lept_isPrime(i, &is_prime, NULL);
962 if (is_prime) {
963 *pprime = i;
964 return 0;
965 }
966 }
967
968 return ERROR_INT("prime not found!", __func__, 1);
969}
970
971
981l_ok
982lept_isPrime(l_uint64 n,
983 l_int32 *pis_prime,
984 l_uint32 *pfactor)
985{
986l_uint32 div;
987l_uint64 limit, ratio;
988
989 if (pis_prime) *pis_prime = 0;
990 if (pfactor) *pfactor = 0;
991 if (!pis_prime)
992 return ERROR_INT("&is_prime not defined", __func__, 1);
993 if (n <= 0)
994 return ERROR_INT("n must be > 0", __func__, 1);
995
996 if (n % 2 == 0) {
997 if (pfactor) *pfactor = 2;
998 return 0;
999 }
1000
1001 limit = (l_uint64)sqrt((l_float64)n);
1002 for (div = 3; div < limit; div += 2) {
1003 ratio = n / div;
1004 if (ratio * div == n) {
1005 if (pfactor) *pfactor = div;
1006 return 0;
1007 }
1008 }
1009
1010 *pis_prime = 1;
1011 return 0;
1012}
1013
1014
1015/*---------------------------------------------------------------------*
1016 * Gray code conversion *
1017 *---------------------------------------------------------------------*/
1030l_uint32
1032{
1033 return (val >> 1) ^ val;
1034}
1035
1036
1043l_uint32
1045{
1046l_uint32 shift;
1047
1048 for (shift = 1; shift < 32; shift <<= 1)
1049 val ^= val >> shift;
1050 return val;
1051}
1052
1053
1054/*---------------------------------------------------------------------*
1055 * Leptonica version number *
1056 *---------------------------------------------------------------------*/
1065char *
1067{
1068size_t bufsize = 100;
1069
1070 char *version = (char *)LEPT_CALLOC(bufsize, sizeof(char));
1071
1072#ifdef _MSC_VER
1073 #ifdef _USRDLL
1074 char dllStr[] = "DLL";
1075 #else
1076 char dllStr[] = "LIB";
1077 #endif
1078 #ifdef _DEBUG
1079 char debugStr[] = "Debug";
1080 #else
1081 char debugStr[] = "Release";
1082 #endif
1083 #ifdef _M_IX86
1084 char bitStr[] = " x86";
1085 #elif _M_X64
1086 char bitStr[] = " x64";
1087 #else
1088 char bitStr[] = "";
1089 #endif
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);
1093
1094#else
1095
1096 snprintf(version, bufsize, "leptonica-%d.%d.%d", LIBLEPT_MAJOR_VERSION,
1097 LIBLEPT_MINOR_VERSION, LIBLEPT_PATCH_VERSION);
1098
1099#endif /* _MSC_VER */
1100 return version;
1101}
1102
1103
1104/*---------------------------------------------------------------------*
1105 * Timing procs *
1106 *---------------------------------------------------------------------*/
1107#if !defined(_WIN32) && !defined(__Fuchsia__)
1108
1109#include <sys/time.h>
1110#include <sys/resource.h>
1111
1112static struct rusage rusage_before;
1113static struct rusage rusage_after;
1114
1124void
1126{
1127 getrusage(RUSAGE_SELF, &rusage_before);
1128}
1129
1130l_float32
1131stopTimer(void)
1132{
1133l_int32 tsec, tusec;
1134
1135 getrusage(RUSAGE_SELF, &rusage_after);
1136
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);
1140}
1141
1142
1156L_TIMER
1158{
1159struct rusage *rusage_start;
1160
1161 rusage_start = (struct rusage *)LEPT_CALLOC(1, sizeof(struct rusage));
1162 getrusage(RUSAGE_SELF, rusage_start);
1163 return rusage_start;
1164}
1165
1166l_float32
1167stopTimerNested(L_TIMER rusage_start)
1168{
1169l_int32 tsec, tusec;
1170struct rusage rusage_stop;
1171
1172 getrusage(RUSAGE_SELF, &rusage_stop);
1173
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);
1180}
1181
1182
1190void
1191l_getCurrentTime(l_int32 *sec,
1192 l_int32 *usec)
1193{
1194struct timeval tv;
1195
1196 gettimeofday(&tv, NULL);
1197 if (sec) *sec = (l_int32)tv.tv_sec;
1198 if (usec) *usec = (l_int32)tv.tv_usec;
1199}
1200
1201#elif defined(__Fuchsia__) /* resource.h not implemented on Fuchsia. */
1202
1203 /* Timer functions are used for testing and debugging, and
1204 * are stubbed out. If they are needed in the future, they
1205 * can be implemented in Fuchsia using the zircon syscall
1206 * zx_object_get_info() in ZX_INFOR_THREAD_STATS mode. */
1207void
1208startTimer(void)
1209{
1210}
1211
1212l_float32
1213stopTimer(void)
1214{
1215 return 0.0;
1216}
1217
1218L_TIMER
1219startTimerNested(void)
1220{
1221 return NULL;
1222}
1223
1224l_float32
1225stopTimerNested(L_TIMER rusage_start)
1226{
1227 return 0.0;
1228}
1229
1230void
1231l_getCurrentTime(l_int32 *sec,
1232 l_int32 *usec)
1233{
1234}
1235
1236#else /* _WIN32 : resource.h not implemented under Windows */
1237
1238 /* Note: if division by 10^7 seems strange, the time is expressed
1239 * as the number of 100-nanosecond intervals that have elapsed
1240 * since 12:00 A.M. January 1, 1601. */
1241
1242static ULARGE_INTEGER utime_before;
1243static ULARGE_INTEGER utime_after;
1244
1245void
1246startTimer(void)
1247{
1248HANDLE this_process;
1249FILETIME start, stop, kernel, user;
1250
1251 this_process = GetCurrentProcess();
1252
1253 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
1254
1255 utime_before.LowPart = user.dwLowDateTime;
1256 utime_before.HighPart = user.dwHighDateTime;
1257}
1258
1259l_float32
1260stopTimer(void)
1261{
1262HANDLE this_process;
1263FILETIME start, stop, kernel, user;
1264ULONGLONG hnsec; /* in units of hecto-nanosecond (100 ns) intervals */
1265
1266 this_process = GetCurrentProcess();
1267
1268 GetProcessTimes(this_process, &start, &stop, &kernel, &user);
1269
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;
1274}
1275
1276L_TIMER
1277startTimerNested(void)
1278{
1279HANDLE this_process;
1280FILETIME start, stop, kernel, user;
1281ULARGE_INTEGER *utime_start;
1282
1283 this_process = GetCurrentProcess();
1284
1285 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1286
1287 utime_start = (ULARGE_INTEGER *)LEPT_CALLOC(1, sizeof(ULARGE_INTEGER));
1288 utime_start->LowPart = user.dwLowDateTime;
1289 utime_start->HighPart = user.dwHighDateTime;
1290 return utime_start;
1291}
1292
1293l_float32
1294stopTimerNested(L_TIMER utime_start)
1295{
1296HANDLE this_process;
1297FILETIME start, stop, kernel, user;
1298ULARGE_INTEGER utime_stop;
1299ULONGLONG hnsec; /* in units of 100 ns intervals */
1300
1301 this_process = GetCurrentProcess ();
1302
1303 GetProcessTimes (this_process, &start, &stop, &kernel, &user);
1304
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;
1310}
1311
1312void
1313l_getCurrentTime(l_int32 *sec,
1314 l_int32 *usec)
1315{
1316ULARGE_INTEGER utime, birthunix;
1317FILETIME systemtime;
1318LONGLONG birthunixhnsec = 116444736000000000; /*in units of 100 ns */
1319LONGLONG usecs;
1320
1321 GetSystemTimeAsFileTime(&systemtime);
1322 utime.LowPart = systemtime.dwLowDateTime;
1323 utime.HighPart = systemtime.dwHighDateTime;
1324
1325 birthunix.LowPart = (DWORD) birthunixhnsec;
1326 birthunix.HighPart = birthunixhnsec >> 32;
1327
1328 usecs = (LONGLONG) ((utime.QuadPart - birthunix.QuadPart) / 10);
1329
1330 if (sec) *sec = (l_int32) (usecs / 1000000);
1331 if (usec) *usec = (l_int32) (usecs % 1000000);
1332}
1333
1334#endif
1335
1336
1353{
1354L_WALLTIMER *timer;
1355
1356 timer = (L_WALLTIMER *)LEPT_CALLOC(1, sizeof(L_WALLTIMER));
1357 l_getCurrentTime(&timer->start_sec, &timer->start_usec);
1358 return timer;
1359}
1360
1367l_float32
1369{
1370l_int32 tsec, tusec;
1371L_WALLTIMER *timer;
1372
1373 if (!ptimer)
1374 return (l_float32)ERROR_FLOAT("&timer not defined", __func__, 0.0);
1375 timer = *ptimer;
1376 if (!timer)
1377 return (l_float32)ERROR_FLOAT("timer not defined", __func__, 0.0);
1378
1379 l_getCurrentTime(&timer->stop_sec, &timer->stop_usec);
1380 tsec = timer->stop_sec - timer->start_sec;
1381 tusec = timer->stop_usec - timer->start_usec;
1382 LEPT_FREE(timer);
1383 *ptimer = NULL;
1384 return (tsec + ((l_float32)tusec) / 1000000.0f);
1385}
1386
1387
1400char *
1402{
1403char buf[128] = "", sep = 'Z';
1404l_int32 gmt_offset, relh, relm;
1405time_t ut, lt;
1406struct tm Tm;
1407struct tm *tptr = &Tm;
1408
1409 ut = time(NULL);
1410
1411 /* This generates a second "time_t" value by calling "gmtime" to
1412 fill in a "tm" structure expressed as UTC and then calling
1413 "mktime", which expects a "tm" structure expressed as the
1414 local time. The result is a value that is offset from the
1415 value returned by the "time" function by the local UTC offset.
1416 "tm_isdst" is set to -1 to tell "mktime" to determine for
1417 itself whether DST is in effect. This is necessary because
1418 "gmtime" always sets "tm_isdst" to 0, which would tell
1419 "mktime" to presume that DST is not in effect. */
1420#ifdef _WIN32
1421 #ifdef _MSC_VER
1422 gmtime_s(tptr, &ut);
1423 #else /* mingw */
1424 tptr = gmtime(&ut);
1425 #endif
1426#else
1427 gmtime_r(&ut, tptr);
1428#endif
1429 tptr->tm_isdst = -1;
1430 lt = mktime(tptr);
1431
1432 /* Calls "difftime" to obtain the resulting difference in seconds,
1433 * because "time_t" is an opaque type, per the C standard. */
1434 gmt_offset = (l_int32) difftime(ut, lt);
1435 if (gmt_offset > 0)
1436 sep = '+';
1437 else if (gmt_offset < 0)
1438 sep = '-';
1439 relh = L_ABS(gmt_offset) / 3600;
1440 relm = (L_ABS(gmt_offset) % 3600) / 60;
1441
1442#ifdef _WIN32
1443 #ifdef _MSC_VER
1444 localtime_s(tptr, &ut);
1445 #else /* mingw */
1446 tptr = localtime(&ut);
1447 #endif
1448#else
1449 localtime_r(&ut, tptr);
1450#endif
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);
1454}
l_ok fileCorruptByMutation(const char *filein, l_float32 loc, l_float32 size, const char *fileout)
fileCorruptByMutation()
Definition utils1.c:609
char * l_getFormattedDate(void)
l_getFormattedDate()
Definition utils1.c:1401
l_ok filesAreIdentical(const char *fname1, const char *fname2, l_int32 *psame)
filesAreIdentical()
Definition utils1.c:404
l_ok fileReplaceBytes(const char *filein, l_int32 start, l_int32 nbytes, l_uint8 *newdata, size_t newsize, const char *fileout)
fileReplaceBytes()
Definition utils1.c:668
l_float32 stopWallTimer(L_WALLTIMER **ptimer)
stopWallTimer()
Definition utils1.c:1368
void startTimer(void)
startTimer(), stopTimer()
Definition utils1.c:1125
l_ok l_hashStringToUint64Fast(const char *str, l_uint64 *phash)
l_hashStringToUint64Fast()
Definition utils1.c:865
l_ok genRandomIntOnInterval(l_int32 start, l_int32 end, l_int32 seed, l_int32 *pval)
genRandomIntOnInterval()
Definition utils1.c:722
void lept_stderr(const char *fmt,...)
lept_stderr()
Definition utils1.c:377
l_ok lept_isPrime(l_uint64 n, l_int32 *pis_prime, l_uint32 *pfactor)
lept_isPrime()
Definition utils1.c:982
char * getLeptonicaVersion(void)
getLeptonicaVersion()
Definition utils1.c:1066
void leptSetStderrHandler(void(*handler)(const char *))
leptSetStderrHandler()
Definition utils1.c:351
void * returnErrorPtr1(const char *msg, const char *arg, const char *procname, void *pval)
returnErrorPtr1()
Definition utils1.c:299
l_int32 returnErrorInt1(const char *msg, const char *arg, const char *procname, l_int32 ival)
returnErrorInt1()
Definition utils1.c:257
l_float32 returnErrorFloat(const char *msg, const char *procname, l_float32 fval)
returnErrorFloat()
Definition utils1.c:219
l_int32 lept_floor(l_float32 fval)
lept_floor()
Definition utils1.c:774
l_ok findNextLargerPrime(l_int32 start, l_uint32 *pprime)
findNextLargerPrime()
Definition utils1.c:949
l_ok fileCorruptByDeletion(const char *filein, l_float32 loc, l_float32 size, const char *fileout)
fileCorruptByDeletion()
Definition utils1.c:547
LEPT_DLL l_int32 LeptMsgSeverity
Definition utils1.c:124
l_ok l_hashStringToUint64(const char *str, l_uint64 *phash)
l_hashStringToUint64()
Definition utils1.c:828
void l_getCurrentTime(l_int32 *sec, l_int32 *usec)
l_getCurrentTime()
Definition utils1.c:1191
l_float32 returnErrorFloat1(const char *msg, const char *arg, const char *procname, l_float32 fval)
returnErrorFloat1()
Definition utils1.c:278
L_TIMER startTimerNested(void)
startTimerNested(), stopTimerNested()
Definition utils1.c:1157
l_int32 setMsgSeverity(l_int32 newsev)
setMsgSeverity()
Definition utils1.c:148
l_int32 lept_roundftoi(l_float32 fval)
lept_roundftoi()
Definition utils1.c:761
void * returnErrorPtr(const char *msg, const char *procname, void *pval)
returnErrorPtr()
Definition utils1.c:237
l_uint32 convertGrayCodeToInt(l_uint32 val)
convertGrayCodeToInt()
Definition utils1.c:1044
l_int32 lept_ceiling(l_float32 fval)
lept_ceiling()
Definition utils1.c:795
l_int32 returnErrorInt(const char *msg, const char *procname, l_int32 ival)
returnErrorInt()
Definition utils1.c:201
L_WALLTIMER * startWallTimer(void)
startWallTimer()
Definition utils1.c:1352
l_uint32 convertIntToGrayCode(l_uint32 val)
convertIntToGrayCode()
Definition utils1.c:1031
l_ok l_hashFloat64ToUint64(l_float64 val, l_uint64 *phash)
l_hashFloat64ToUint64()
Definition utils1.c:927
l_ok l_hashPtToUint64(l_int32 x, l_int32 y, l_uint64 *phash)
l_hashPtToUint64()
Definition utils1.c:899