![]() |
Leptonica 1.85.0
Image processing and image analysis suite
|
#include <time.h>#include "allheaders.h"#include <math.h>#include <sys/time.h>#include <sys/resource.h>Go to the source code of this file.
Macros | |
| #define | DEBUG_SEV 0 |
| #define | MAX_DEBUG_MESSAGE 2000 |
Functions | |
| l_int32 | setMsgSeverity (l_int32 newsev) |
| l_int32 | returnErrorInt (const char *msg, const char *procname, l_int32 ival) |
| l_float32 | returnErrorFloat (const char *msg, const char *procname, l_float32 fval) |
| void * | returnErrorPtr (const char *msg, const char *procname, void *pval) |
| l_int32 | returnErrorInt1 (const char *msg, const char *arg, const char *procname, l_int32 ival) |
| l_float32 | returnErrorFloat1 (const char *msg, const char *arg, const char *procname, l_float32 fval) |
| void * | returnErrorPtr1 (const char *msg, const char *arg, const char *procname, void *pval) |
| static void | lept_default_stderr_handler (const char *formatted_msg) |
| void | leptSetStderrHandler (void(*handler)(const char *)) |
| void | lept_stderr (const char *fmt,...) |
| l_ok | filesAreIdentical (const char *fname1, const char *fname2, l_int32 *psame) |
| l_uint16 | convertOnLittleEnd16 (l_uint16 shortin) |
| l_uint16 | convertOnBigEnd16 (l_uint16 shortin) |
| l_uint32 | convertOnLittleEnd32 (l_uint32 wordin) |
| l_uint32 | convertOnBigEnd32 (l_uint32 wordin) |
| l_ok | fileCorruptByDeletion (const char *filein, l_float32 loc, l_float32 size, const char *fileout) |
| l_ok | fileCorruptByMutation (const char *filein, l_float32 loc, l_float32 size, const char *fileout) |
| l_ok | fileReplaceBytes (const char *filein, l_int32 start, l_int32 nbytes, l_uint8 *newdata, size_t newsize, const char *fileout) |
| l_ok | genRandomIntOnInterval (l_int32 start, l_int32 end, l_int32 seed, l_int32 *pval) |
| l_int32 | lept_roundftoi (l_float32 fval) |
| l_int32 | lept_floor (l_float32 fval) |
| l_int32 | lept_ceiling (l_float32 fval) |
| l_ok | l_hashStringToUint64 (const char *str, l_uint64 *phash) |
| l_ok | l_hashStringToUint64Fast (const char *str, l_uint64 *phash) |
| l_ok | l_hashPtToUint64 (l_int32 x, l_int32 y, l_uint64 *phash) |
| l_ok | l_hashFloat64ToUint64 (l_float64 val, l_uint64 *phash) |
| l_ok | findNextLargerPrime (l_int32 start, l_uint32 *pprime) |
| l_ok | lept_isPrime (l_uint64 n, l_int32 *pis_prime, l_uint32 *pfactor) |
| l_uint32 | convertIntToGrayCode (l_uint32 val) |
| l_uint32 | convertGrayCodeToInt (l_uint32 val) |
| char * | getLeptonicaVersion (void) |
| void | startTimer (void) |
| l_float32 | stopTimer (void) |
| L_TIMER | startTimerNested (void) |
| l_float32 | stopTimerNested (L_TIMER rusage_start) |
| void | l_getCurrentTime (l_int32 *sec, l_int32 *usec) |
| L_WALLTIMER * | startWallTimer (void) |
| l_float32 | stopWallTimer (L_WALLTIMER **ptimer) |
| char * | l_getFormattedDate (void) |
Variables | |
| LEPT_DLL l_int32 | LeptMsgSeverity = DEFAULT_SEVERITY |
| void(* | stderr_handler )(const char *) = lept_default_stderr_handler |
| static struct rusage | rusage_before |
| static struct rusage | rusage_after |
------------------------------------------
This file has these utilities:
- error, warning and info messages
- redirection of stderr
- low-level endian conversions
- file corruption operations
- random and prime number operations
- 64-bit hash functions
- leptonica version number accessor
- timing and date operations
------------------------------------------
Control of error, warning and info messages
l_int32 setMsgSeverity()
Error return functions, invoked by macros
l_int32 returnErrorInt()
l_float32 returnErrorFloat()
void *returnErrorPtr()
l_int32 returnErrorInt1()
l_float32 returnErrorFloat1()
void *returnErrorPtr1()
Runtime redirection of stderr
void leptSetStderrHandler()
void lept_stderr()
Test files for equivalence
l_int32 filesAreIdentical()
Byte-swapping data conversion
l_uint16 convertOnBigEnd16()
l_uint32 convertOnBigEnd32()
l_uint16 convertOnLittleEnd16()
l_uint32 convertOnLittleEnd32()
File corruption and byte replacement operations
l_int32 fileCorruptByDeletion()
l_int32 fileCorruptByMutation()
l_int32 fileReplaceBytes()
Generate random integer in given interval
l_int32 genRandomIntOnInterval()
Simple math functions
l_int32 lept_roundftoi()
l_int32 lept_floor()
l_int32 lept_ceiling()
64-bit hash functions
l_int32 l_hashStringToUint64()
l_int32 l_hashStringToUint64Fast()
l_int32 l_hashPtToUint64()
l_int32 l_hashFloat64ToUint64()
Prime finders
l_int32 findNextLargerPrime()
l_int32 lept_isPrime()
Gray code conversion
l_uint32 convertIntToGrayCode()
l_uint32 convertGrayCodeToInt()
Leptonica version number
char *getLeptonicaVersion()
Timing
void startTimer()
l_float32 stopTimer()
L_TIMER startTimerNested()
l_float32 stopTimerNested()
void l_getCurrentTime()
L_WALLTIMER *startWallTimer()
l_float32 stopWallTimer()
void l_getFormattedDate()
For all issues with cross-platform development, see utils2.c.
Definition in file utils1.c.
| l_uint32 convertGrayCodeToInt | ( | l_uint32 | val | ) |
| [in] | val | gray code value |
| l_uint32 convertIntToGrayCode | ( | l_uint32 | val | ) |
| [in] | val | integer value |
Notes:
(1) Gray code values corresponding to integers differ by
only one bit transition between successive integers.
| l_ok fileCorruptByDeletion | ( | const char * | filein, |
| l_float32 | loc, | ||
| l_float32 | size, | ||
| const char * | fileout ) |
| [in] | filein | |
| [in] | loc | fractional location of start of deletion |
| [in] | size | fractional size of deletion |
| [in] | fileout | corrupted file |
Notes:
(1) loc and size are expressed as a fraction of the file size.
(2) This makes a copy of the data in filein, where bytes in the
specified region have deleted.
(3) If (loc + size) >= 1.0, this deletes from the position
represented by loc to the end of the file.
(4) It is useful for testing robustness of I/O wrappers when the
data is corrupted, by simulating data corruption by deletion.
| l_ok fileCorruptByMutation | ( | const char * | filein, |
| l_float32 | loc, | ||
| l_float32 | size, | ||
| const char * | fileout ) |
| [in] | filein | |
| [in] | loc | fractional location of start of randomization |
| [in] | size | fractional size of randomization |
| [in] | fileout | corrupted file |
Notes:
(1) loc and size are expressed as a fraction of the file size.
(2) This makes a copy of the data in filein, where bytes in the
specified region have been replaced by random data.
(3) If (loc + size) >= 1.0, this modifies data from the position
represented by loc to the end of the file.
(4) It is useful for testing robustness of I/O wrappers when the
data is corrupted, by simulating data corruption.
| l_ok fileReplaceBytes | ( | const char * | filein, |
| l_int32 | start, | ||
| l_int32 | nbytes, | ||
| l_uint8 * | newdata, | ||
| size_t | newsize, | ||
| const char * | fileout ) |
| [in] | filein | input file |
| [in] | start | start location for replacement |
| [in] | nbytes | number of bytes to be removed |
| [in] | newdata | replacement bytes |
| [in] | newsize | size of replacement bytes |
| [in] | fileout | output file |
Notes:
(1) To remove nbytes without replacement, set newdata == NULL.
(2) One use is for replacing the date/time in a pdf file by a
string of 12 '0's, effectively removing the date without
invalidating the byte counters in the pdf file:
fileReplaceBytes(filein 86 12 (char *)"000000000000" 12 fileout
| l_ok filesAreIdentical | ( | const char * | fname1, |
| const char * | fname2, | ||
| l_int32 * | psame ) |
| [in] | fname1 | |
| [in] | fname2 | |
| [out] | psame | 1 if identical; 0 if different |
| l_ok findNextLargerPrime | ( | l_int32 | start, |
| l_uint32 * | pprime ) |
| [in] | start | |
| [out] | pprime | first prime larger than start |
Definition at line 949 of file utils1.c.
References lept_isPrime().
| l_ok genRandomIntOnInterval | ( | l_int32 | start, |
| l_int32 | end, | ||
| l_int32 | seed, | ||
| l_int32 * | pval ) |
| [in] | start | beginning of interval; can be < 0 |
| [in] | end | end of interval; must be >= start |
| [in] | seed | use 0 to skip; otherwise call srand |
| [out] | pval | random integer in interval [start ... end] |
| char * getLeptonicaVersion | ( | void | ) |
Return: string of version number (e.g., 'leptonica-1.74.2')
Notes: (1) The caller has responsibility to free the memory.
| void l_getCurrentTime | ( | l_int32 * | sec, |
| l_int32 * | usec ) |
| [out] | sec | [optional] in seconds since birth of Unix |
| [out] | usec | [optional] in microseconds since birth of Unix |
Definition at line 1191 of file utils1.c.
Referenced by startWallTimer(), and stopWallTimer().
| char * l_getFormattedDate | ( | void | ) |
Notes:
(1) This is used in pdf, in the form specified in section 3.8.2 of
http://partners.adobe.com/public/developer/en/pdf/PDFReference.pdf
(2) Contributed by Dave Bryan. Works on all platforms.
| l_ok l_hashFloat64ToUint64 | ( | l_float64 | val, |
| l_uint64 * | phash ) |
| [in] | val | |
| [out] | phash | hash key |
Notes:
(1) This is a simple hash for using hashmaps with 64-bit float data.
(2) The resulting hash is called a "key" in a lookup operation.
The bucket for val in a hashmap is then found by taking the mod
of the hash key with the number of buckets (which is prime).
| l_ok l_hashPtToUint64 | ( | l_int32 | x, |
| l_int32 | y, | ||
| l_uint64 * | phash ) |
| [in] | x,y | |
| [out] | phash | hash value |
Notes:
(1) This simple hash function has no collisions for
any of 400 million points with x and y up to 20000.
| l_ok l_hashStringToUint64 | ( | const char * | str, |
| l_uint64 * | phash ) |
| [in] | str | |
| [out] | phash | hash value |
Notes:
(1) The intent of the hash is to avoid collisions by mapping
the string as randomly as possible into 64 bits.
(2) To the extent that the hashes are random, the probability of
a collision can be approximated by the square of the number
of strings divided by 2^64. For 1 million strings, the
collision probability is about 1 in 16 million.
(3) I expect non-randomness of the distribution to be most evident
for small text strings. This hash function has been tested
for all 5-character text strings composed of 26 letters,
of which there are 26^5 = 12356630. There are no hash
collisions for this set.
| l_ok l_hashStringToUint64Fast | ( | const char * | str, |
| l_uint64 * | phash ) |
| [in] | str | |
| [out] | phash | hash value |
Notes:
(1) This very simple hash algorithm is described in "The Practice
of Programming" by Kernighan and Pike, p. 57 (1999).
(2) The returned hash value would then be hashed into an index into
the hashtable, using the mod operator with the hashtable size.
| l_int32 lept_ceiling | ( | l_float32 | fval | ) |
| [in] | fval |
Notes:
(1) If fval is equal to its interger value, return that.
Otherwise:
For fval > 0, fval --> 1 + floor(fval)
For fval < 0, fval --> -(1 + floor(-fval))
|
static |
| l_int32 lept_floor | ( | l_float32 | fval | ) |
| [in] | fval |
| l_ok lept_isPrime | ( | l_uint64 | n, |
| l_int32 * | pis_prime, | ||
| l_uint32 * | pfactor ) |
| [in] | n | 64-bit unsigned |
| [out] | pis_prime | 1 if prime, 0 otherwise |
| [out] | pfactor | [optional] smallest divisor, or 0 on error or if prime |
Definition at line 982 of file utils1.c.
Referenced by findNextLargerPrime().
| l_int32 lept_roundftoi | ( | l_float32 | fval | ) |
| [in] | fval |
Notes:
(1) For fval >= 0, fval --> round(fval) == floor(fval + 0.5)
For fval < 0, fval --> -round(-fval))
This is symmetric around 0.
e.g., for fval in (-0.5 ... 0.5), fval --> 0
| void lept_stderr | ( | const char * | fmt, |
| ... ) |
| [in] | fmt | format string |
| [in] | ... | varargs |
Notes:
(1) This is a replacement for fprintf(), to allow redirection
of output. All calls to fprintf(stderr, ...) are replaced
with calls to lept_stderr(...).
(2) The message size is limited to 2K bytes.
(3) This utility was provided by jbarlow83.
Definition at line 377 of file utils1.c.
Referenced by returnErrorFloat(), returnErrorFloat1(), returnErrorInt(), returnErrorInt1(), returnErrorPtr(), and returnErrorPtr1().
| void leptSetStderrHandler | ( | void(* | handler )(const char *) | ) |
| [in] | handler | callback function for lept_stderr output |
Notes:
(1) This registers a handler for redirection of output to stderr
at runtime.
(2) If called with NULL, the output goes to stderr.
| l_float32 returnErrorFloat | ( | const char * | msg, |
| const char * | procname, | ||
| l_float32 | fval ) |
| [in] | msg | error message |
| [in] | procname | use func |
| [in] | fval | return float error val |
Definition at line 219 of file utils1.c.
References lept_stderr().
| l_float32 returnErrorFloat1 | ( | const char * | msg, |
| const char * | arg, | ||
| const char * | procname, | ||
| l_float32 | fval ) |
| [in] | msg | error message |
| [in] | arg | additional error message argument (will be appended to the error message) |
| [in] | procname | use func |
| [in] | fval | return float error val |
Definition at line 278 of file utils1.c.
References lept_stderr().
| l_int32 returnErrorInt | ( | const char * | msg, |
| const char * | procname, | ||
| l_int32 | ival ) |
| [in] | msg | error message |
| [in] | procname | use func |
| [in] | ival | return error val (typically 1 for an error) |
Definition at line 201 of file utils1.c.
References lept_stderr().
| l_int32 returnErrorInt1 | ( | const char * | msg, |
| const char * | arg, | ||
| const char * | procname, | ||
| l_int32 | ival ) |
| [in] | msg | error message |
| [in] | arg | additional error message argument (will be appended to the error message) |
| [in] | procname | use func |
| [in] | ival | return error val; typically 1 for an error return |
Definition at line 257 of file utils1.c.
References lept_stderr().
| void * returnErrorPtr | ( | const char * | msg, |
| const char * | procname, | ||
| void * | pval ) |
| [in] | msg | error message |
| [in] | procname | use func |
| [in] | pval | return error val (typically null for an error) |
Definition at line 237 of file utils1.c.
References lept_stderr().
| void * returnErrorPtr1 | ( | const char * | msg, |
| const char * | arg, | ||
| const char * | procname, | ||
| void * | pval ) |
| [in] | msg | error message |
| [in] | arg | additional error message argument (will be appended to the error message) |
| [in] | procname | use func |
| [in] | pval | return error val (typically null for an error) |
Definition at line 299 of file utils1.c.
References lept_stderr().
| l_int32 setMsgSeverity | ( | l_int32 | newsev | ) |
| [in] | newsev |
Notes:
(1) setMsgSeverity() allows the user to specify the desired
message severity threshold. Messages of equal or greater
severity will be output. The previous message severity is
returned when the new severity is set.
(2) If L_SEVERITY_EXTERNAL is passed, then the severity will be
obtained from the LEPT_MSG_SEVERITY environment variable.
Definition at line 148 of file utils1.c.
References LeptMsgSeverity.
| void startTimer | ( | void | ) |
startTimer(), stopTimer()
Notes: (1) These measure the cpu time elapsed between the two calls: startTimer(); .... lept_stderr( "Elapsed time = %7.3f sec\n", stopTimer());
| L_TIMER startTimerNested | ( | void | ) |
startTimerNested(), stopTimerNested()
Example of usage:
L_TIMER t1 = startTimerNested(); .... L_TIMER t2 = startTimerNested(); .... lept_stderr( "Elapsed time 2 = %7.3f sec\n", stopTimerNested(t2)); .... lept_stderr( "Elapsed time 1 = %7.3f sec\n", stopTimerNested(t1));
| L_WALLTIMER * startWallTimer | ( | void | ) |
Notes:
(1) These measure the wall clock time elapsed between the two calls:
L_WALLTIMER *timer = startWallTimer();
....
lept_stderr( "Elapsed time = %f sec\n", stopWallTimer(&timer);
(2) Note that the timer object is destroyed by stopWallTimer().
Definition at line 1352 of file utils1.c.
References l_getCurrentTime().
| l_float32 stopWallTimer | ( | L_WALLTIMER ** | ptimer | ) |
| [in,out] | ptimer | walltimer pointer |
Definition at line 1368 of file utils1.c.
References l_getCurrentTime().
| LEPT_DLL l_int32 LeptMsgSeverity = DEFAULT_SEVERITY |
The run-time message severity threshold is defined in utils1.c.
Definition at line 124 of file utils1.c.
Referenced by setMsgSeverity().