Leptonica  1.77.0
Image processing and image analysis suite
environ.h
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 
27 #ifndef LEPTONICA_ENVIRON_H
28 #define LEPTONICA_ENVIRON_H
29 
30 /*------------------------------------------------------------------------*
31  * Defines and includes differ for Unix and Windows. Also for Windows, *
32  * differentiate between conditionals based on platform and compiler. *
33  * For platforms: *
34  * _WIN32 => Windows, 32- or 64-bit *
35  * _WIN64 => Windows, 64-bit only *
36  * __CYGWIN__ => Cygwin *
37  * For compilers: *
38  * __GNUC__ => gcc *
39  * _MSC_VER => msvc *
40  *------------------------------------------------------------------------*/
41 
42 /* MS VC++ does not provide stdint.h, so define the missing types here */
43 
44 
45 #ifndef _MSC_VER
46 #include <stdint.h>
47 
48 #else
49 /* Note that _WIN32 is defined for both 32 and 64 bit applications,
50  whereas _WIN64 is defined only for the latter */
51 
52 #ifdef _WIN64
53 typedef __int64 intptr_t;
54 typedef unsigned __int64 uintptr_t;
55 #else
56 typedef int intptr_t;
57 typedef unsigned int uintptr_t;
58 #endif
59 
60 /* VC++6 doesn't seem to have powf, expf. */
61 #if (_MSC_VER < 1400)
62 #define powf(x, y) (float)pow((double)(x), (double)(y))
63 #define expf(x) (float)exp((double)(x))
64 #endif
65 
66 #endif /* _MSC_VER */
67 
68 /* Windows specifics */
69 #ifdef _WIN32
70  /* DLL EXPORTS and IMPORTS */
71  #if defined(LIBLEPT_EXPORTS)
72  #define LEPT_DLL __declspec(dllexport)
73  #elif defined(LIBLEPT_IMPORTS)
74  #define LEPT_DLL __declspec(dllimport)
75  #else
76  #define LEPT_DLL
77  #endif
78 #else /* non-Windows specifics */
79  #include <stdint.h>
80  #define LEPT_DLL
81 #endif /* _WIN32 */
82 
83 typedef intptr_t l_intptr_t;
84 typedef uintptr_t l_uintptr_t;
85 
86 
87 /*--------------------------------------------------------------------*
88  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
89  * USER CONFIGURABLE *
90  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
91  * Environment variables with I/O libraries *
92  * Manual Configuration Only: NOT AUTO_CONF *
93  *--------------------------------------------------------------------*/
94 /*
95  * Leptonica provides interfaces to link to several external image
96  * I/O libraries, plus zlib. Setting any of these to 0 here causes
97  * non-functioning stubs to be linked.
98  */
99 #if !defined(HAVE_CONFIG_H) && !defined(ANDROID_BUILD) && !defined(OS_IOS)
100 
101  #if !defined(HAVE_LIBJPEG)
102  #define HAVE_LIBJPEG 1
103  #endif
104  #if !defined(HAVE_LIBTIFF)
105  #define HAVE_LIBTIFF 1
106  #endif
107  #if !defined(HAVE_LIBPNG)
108  #define HAVE_LIBPNG 1
109  #endif
110  #if !defined(HAVE_LIBZ)
111  #define HAVE_LIBZ 1
112  #endif
113  #if !defined(HAVE_LIBGIF)
114  #define HAVE_LIBGIF 0
115  #endif
116  #if !defined(HAVE_LIBUNGIF)
117  #define HAVE_LIBUNGIF 0
118  #endif
119  #if !defined(HAVE_LIBWEBP)
120  #define HAVE_LIBWEBP 0
121  #endif
122  #if !defined(HAVE_LIBJP2K)
123  #define HAVE_LIBJP2K 0
124  #endif
125 
126  /*-----------------------------------------------------------------------*
127  * Leptonica supports OpenJPEG 2.0+. If you have a version of openjpeg *
128  * (HAVE_LIBJP2K == 1) that is >= 2.0, set the path to the openjpeg.h *
129  * header in angle brackets here. *
130  *-----------------------------------------------------------------------*/
131  #define LIBJP2K_HEADER <openjpeg-2.3/openjpeg.h>
132 
133 #endif /* ! HAVE_CONFIG_H etc. */
134 
135 /*--------------------------------------------------------------------*
136  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
137  * USER CONFIGURABLE *
138  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
139  * Environ variables for image I/O without external libraries *
140  *--------------------------------------------------------------------*/
141 /*
142  * Leptonica supplies I/O support without using external libraries for:
143  * * image read/write for bmp, pnm
144  * * header read for jp2k
145  * * image wrapping write for pdf and ps.
146  * Setting any of these to 0 causes non-functioning stubs to be linked.
147  */
148 #define USE_BMPIO 1
149 #define USE_PNMIO 1
150 #define USE_JP2KHEADER 1
151 #define USE_PDFIO 1
152 #define USE_PSIO 1
153 
154 
155 /*-------------------------------------------------------------------------*
156  * On linux systems, you can do I/O between Pix and memory. Specifically,
157  * you can compress (write compressed data to memory from a Pix) and
158  * uncompress (read from compressed data in memory to a Pix).
159  * For jpeg, png, jp2k, gif, pnm and bmp, these use the non-posix GNU
160  * functions fmemopen() and open_memstream(). These functions are not
161  * available on other systems.
162  * To use these functions in linux, you must define HAVE_FMEMOPEN to 1.
163  * To use them on MacOS, which does not support these functions, set it to 0.
164  *-------------------------------------------------------------------------*/
165 #if !defined(HAVE_CONFIG_H) && !defined(ANDROID_BUILD) && !defined(OS_IOS) && \
166  !defined(_WIN32)
167 #define HAVE_FMEMOPEN 1
168 #endif /* ! HAVE_CONFIG_H etc. */
169 
170 /*-------------------------------------------------------------------------*
171  * fstatat() is defined by POSIX, but some systems do not support it. *
172  * One example is older macOS systems (pre-10.10). *
173  * Play it safe and set the default value to 0. *
174  *-------------------------------------------------------------------------*/
175 #if !defined(HAVE_CONFIG_H)
176 #define HAVE_FSTATAT 0
177 #endif /* ! HAVE_CONFIG_H */
178 
179 /*--------------------------------------------------------------------*
180  * It is desirable on Windows to have all temp files written to the same
181  * subdirectory of the Windows <Temp> directory, because files under <Temp>
182  * persist after reboot, and the regression tests write a lot of files.
183  * We write all test files to /tmp/lept or subdirectories of /tmp/lept.
184  * Windows temp files are specified as in unix, but have the translation
185  * /tmp/lept/xxx --> <Temp>/lept/xxx
186  *--------------------------------------------------------------------*/
187 
188 
189 /*--------------------------------------------------------------------*
190  * Built-in types *
191  *--------------------------------------------------------------------*/
192 typedef int l_ok;
193 typedef signed char l_int8;
194 typedef unsigned char l_uint8;
195 typedef short l_int16;
196 typedef unsigned short l_uint16;
197 typedef int l_int32;
198 typedef unsigned int l_uint32;
199 typedef float l_float32;
200 typedef double l_float64;
201 #ifdef COMPILER_MSVC
202 typedef __int64 l_int64;
203 typedef unsigned __int64 l_uint64;
204 #else
205 typedef long long l_int64;
206 typedef unsigned long long l_uint64;
207 #endif /* COMPILER_MSVC */
208 
209 
210 /*-------------------------------------------------------------------------*
211  * For security, the library is distributed in a configuration that does *
212  * not permit (1) forking with 'system', which is used for displaying *
213  * images and generating gnuplots, and (2) writing files with specified *
214  * compiled-in file names. All such writes are with functions such as *
215  * pixWriteDebug() where the "Debug" is appended to the usual name. *
216  * Whether the "Debug" version defaults to the standard version or is a *
217  * no-op depends on the value of this global variable. The default value *
218  * of LeptDebugOK is 0, and it is set in writefile.c. This value can be *
219  * over-ridden, for development and debugging, by setLeptDebugOK(). *
220  *-------------------------------------------------------------------------*/
221 LEPT_DLL extern l_int32 LeptDebugOK; /* default is 0 */
222 
223 
224 /*------------------------------------------------------------------------*
225  * Standard macros *
226  *------------------------------------------------------------------------*/
227 #ifndef L_MIN
228 
229 #define L_MIN(x,y) (((x) < (y)) ? (x) : (y))
230 #endif
231 
232 #ifndef L_MAX
233 
234 #define L_MAX(x,y) (((x) > (y)) ? (x) : (y))
235 #endif
236 
237 #ifndef L_ABS
238 
239 #define L_ABS(x) (((x) < 0) ? (-1 * (x)) : (x))
240 #endif
241 
242 #ifndef L_SIGN
243 
244 #define L_SIGN(x) (((x) < 0) ? -1 : 1)
245 #endif
246 
247 #ifndef UNDEF
248 
249 #define UNDEF -1
250 #endif
251 
252 #ifndef NULL
253 
254 #define NULL 0
255 #endif
256 
257 #ifndef TRUE
258 
259 #define TRUE 1
260 #endif
261 
262 #ifndef FALSE
263 
264 #define FALSE 0
265 #endif
266 
267 
268 /*--------------------------------------------------------------------*
269  * Environment variables for endian dependence *
270  *--------------------------------------------------------------------*/
271 /*
272  * To control conditional compilation, one of two variables
273  *
274  * L_LITTLE_ENDIAN (e.g., for Intel X86)
275  * L_BIG_ENDIAN (e.g., for Sun SPARC, Mac Power PC)
276  *
277  * is defined when the GCC compiler is invoked.
278  * All code should compile properly for both hardware architectures.
279  */
280 
281 
282 /*------------------------------------------------------------------------*
283  * Simple search state variables *
284  *------------------------------------------------------------------------*/
286 enum {
287  L_NOT_FOUND = 0,
288  L_FOUND = 1
289 };
290 
291 
292 /*------------------------------------------------------------------------*
293  * Path separator conversion *
294  *------------------------------------------------------------------------*/
296 enum {
297  UNIX_PATH_SEPCHAR = 0,
298  WIN_PATH_SEPCHAR = 1
299 };
300 
301 
302 /*------------------------------------------------------------------------*
303  * Timing structs *
304  *------------------------------------------------------------------------*/
305 typedef void *L_TIMER;
306 
308 struct L_WallTimer {
309  l_int32 start_sec;
310  l_int32 start_usec;
311  l_int32 stop_sec;
312  l_int32 stop_usec;
313 };
314 typedef struct L_WallTimer L_WALLTIMER;
315 
316 
317 /*------------------------------------------------------------------------*
318  * Standard memory allocation *
319  * *
320  * These specify the memory management functions that are used *
321  * on all heap data except for Pix. Memory management for Pix *
322  * also defaults to malloc and free. See pix1.c for details. *
323  *------------------------------------------------------------------------*/
324 #define LEPT_MALLOC(blocksize) malloc(blocksize)
325 #define LEPT_CALLOC(numelem, elemsize) calloc(numelem, elemsize)
326 #define LEPT_REALLOC(ptr, blocksize) realloc(ptr, blocksize)
327 #define LEPT_FREE(ptr) free(ptr)
328 
329 
330 /*------------------------------------------------------------------------*
331  * Control printing of error, warning, and info messages *
332  * *
333  * To omit all messages to stderr, simply define NO_CONSOLE_IO on the *
334  * command line. For finer grained control, we have a mechanism *
335  * based on the message severity level. The following assumes that *
336  * NO_CONSOLE_IO is not defined. *
337  * *
338  * Messages are printed if the message severity is greater than or equal *
339  * to the current severity threshold. The current severity threshold *
340  * is the greater of the compile-time severity, which is the minimum *
341  * severity that can be reported, and the run-time severity, which is *
342  * the severity threshold at the moment. *
343  * *
344  * The compile-time threshold determines which messages are compiled *
345  * into the library for potential printing. Messages below the *
346  * compile-time threshold are omitted and can never be printed. The *
347  * default compile-time threshold is L_SEVERITY_INFO, but this may be *
348  * overridden by defining MINIMUM_SEVERITY to the desired enumeration *
349  * identifier on the compiler command line. Defining NO_CONSOLE_IO on *
350  * the command line is the same as setting MINIMUM_SEVERITY to *
351  * L_SEVERITY_NONE. *
352  * *
353  * The run-time threshold determines which messages are printed during *
354  * library execution. It defaults to the compile-time threshold but *
355  * may be changed either statically by defining DEFAULT_SEVERITY to *
356  * the desired enumeration identifier on the compiler command line, or *
357  * dynamically by calling setMsgSeverity() to specify a new threshold. *
358  * The run-time threshold may also be set from the value of the *
359  * environment variable LEPT_MSG_SEVERITY by calling setMsgSeverity() *
360  * and specifying L_SEVERITY_EXTERNAL. *
361  * *
362  * In effect, the compile-time threshold setting says, "Generate code *
363  * to permit messages of equal or greater severity than this to be *
364  * printed, if desired," whereas the run-time threshold setting says, *
365  * "Print messages that have an equal or greater severity than this." *
366  *------------------------------------------------------------------------*/
367 
369 enum {
370  L_SEVERITY_EXTERNAL = 0, /* Get the severity from the environment */
371  L_SEVERITY_ALL = 1, /* Lowest severity: print all messages */
372  L_SEVERITY_DEBUG = 2, /* Print debugging and higher messages */
373  L_SEVERITY_INFO = 3, /* Print informational and higher messages */
374  L_SEVERITY_WARNING = 4, /* Print warning and higher messages */
375  L_SEVERITY_ERROR = 5, /* Print error and higher messages */
376  L_SEVERITY_NONE = 6 /* Highest severity: print no messages */
377 };
378 
379 /* No message less than the compile-time threshold will ever be
380  * reported, regardless of the current run-time threshold. This allows
381  * selection of the set of messages to include in the library. For
382  * example, setting the threshold to L_SEVERITY_WARNING eliminates all
383  * informational messages from the library. With that setting, both
384  * warning and error messages would be printed unless setMsgSeverity()
385  * was called, or DEFAULT_SEVERITY was redefined, to set the run-time
386  * severity to L_SEVERITY_ERROR. In that case, only error messages
387  * would be printed.
388  *
389  * This mechanism makes the library smaller and faster, by eliminating
390  * undesired message reporting and the associated run-time overhead for
391  * message threshold checking, because code for messages whose severity
392  * is lower than MINIMUM_SEVERITY won't be generated.
393  *
394  * A production library might typically permit ERROR messages to be
395  * generated, and a development library might permit DEBUG and higher.
396  * The actual messages printed (as opposed to generated) would depend
397  * on the current run-time severity threshold.
398  *
399  * This is a complex mechanism and a few examples may help.
400  * (1) No output permitted under any circumstances.
401  * Use: -DNO_CONSOLE_IO or -DMINIMUM_SEVERITY=6
402  * (2) Suppose you want to only allow error messages, and you don't
403  * want to permit info or warning messages at runtime.
404  * Use: -DMINIMUM_SEVERITY=5
405  * (3) Suppose you want to only allow error messages by default,
406  * but you will permit this to be over-ridden at runtime.
407  * Use: -DDEFAULT_SEVERITY=5
408  * and to allow info and warning override:
409  * setMsgSeverity(L_SEVERITY_INFO);
410  */
411 
412 #ifdef NO_CONSOLE_IO
413  #undef MINIMUM_SEVERITY
414  #undef DEFAULT_SEVERITY
415 
416  #define MINIMUM_SEVERITY L_SEVERITY_NONE
417  #define DEFAULT_SEVERITY L_SEVERITY_NONE
419 #else
420  #ifndef MINIMUM_SEVERITY
421  #define MINIMUM_SEVERITY L_SEVERITY_INFO
422  #endif
423 
424  #ifndef DEFAULT_SEVERITY
425  #define DEFAULT_SEVERITY MINIMUM_SEVERITY
426  #endif
427 #endif
428 
429 
431 LEPT_DLL extern l_int32 LeptMsgSeverity;
432 
433 /*
434  * <pre>
435  * Usage
436  * =====
437  * Messages are of two types.
438  *
439  * (1) The messages
440  * ERROR_INT(a,b,c) : returns l_int32
441  * ERROR_FLOAT(a,b,c) : returns l_float32
442  * ERROR_PTR(a,b,c) : returns void*
443  * are used to return from functions and take a fixed set of parameters:
444  * a : <message string>
445  * b : procName
446  * c : <return value from function>
447  * where procName is the name of the local variable naming the function.
448  *
449  * (2) The purely informational L_* messages
450  * L_ERROR(a,...)
451  * L_WARNING(a,...)
452  * L_INFO(a,...)
453  * do not take a return value, but they take at least two parameters:
454  * a : <message string> with optional format conversions
455  * v1 : procName (this must be included as the first vararg)
456  * v2, ... : optional varargs to match format converters in the message
457  *
458  * To return an error from a function that returns void, use:
459  * L_ERROR(<message string>, procName, [...])
460  * return;
461  *
462  * Implementation details
463  * ======================
464  * Messages are defined with the IF_SEV macro. The first parameter is
465  * the message severity, the second is the function to call if the
466  * message is to be printed, and the third is the return value if the
467  * message is to be suppressed. For example, we might have an
468  * informational message defined as:
469  *
470  * IF_SEV(L_SEVERITY_INFO, fprintf(.......), 0)
471  *
472  * The macro expands into a conditional. Because the first comparison
473  * is between two constants, an optimizing compiler will remove either
474  * the comparison (if it's true) or the entire macro expansion (if it
475  * is false). This means that there is no run-time overhead for
476  * messages whose severity falls below the minimum specified at compile
477  * time, and for others the overhead is one (not two) comparisons.
478  *
479  * The L_nnn() macros below do not return a value, but because the
480  * conditional operator requires one for the false condition, we
481  * specify a void expression.
482  * </pre>
483  */
484 
485 #ifdef NO_CONSOLE_IO
486 
487  #define PROCNAME(name)
488  #define ERROR_INT(a,b,c) ((l_int32)(c))
489  #define ERROR_FLOAT(a,b,c) ((l_float32)(c))
490  #define ERROR_PTR(a,b,c) ((void *)(c))
491  #define L_ERROR(a,...)
492  #define L_WARNING(a,...)
493  #define L_INFO(a,...)
494 
495 #else
496 
497  #define PROCNAME(name) static const char procName[] = name
498  #define IF_SEV(l,t,f) \
499  ((l) >= MINIMUM_SEVERITY && (l) >= LeptMsgSeverity ? (t) : (f))
500 
501  #define ERROR_INT(a,b,c) \
502  IF_SEV(L_SEVERITY_ERROR, returnErrorInt((a),(b),(c)), (l_int32)(c))
503  #define ERROR_FLOAT(a,b,c) \
504  IF_SEV(L_SEVERITY_ERROR, returnErrorFloat((a),(b),(c)), (l_float32)(c))
505  #define ERROR_PTR(a,b,c) \
506  IF_SEV(L_SEVERITY_ERROR, returnErrorPtr((a),(b),(c)), (void *)(c))
507 
508  #define L_ERROR(a,...) \
509  IF_SEV(L_SEVERITY_ERROR, \
510  (void)fprintf(stderr, "Error in %s: " a, __VA_ARGS__), \
511  (void)0)
512  #define L_WARNING(a,...) \
513  IF_SEV(L_SEVERITY_WARNING, \
514  (void)fprintf(stderr, "Warning in %s: " a, __VA_ARGS__), \
515  (void)0)
516  #define L_INFO(a,...) \
517  IF_SEV(L_SEVERITY_INFO, \
518  (void)fprintf(stderr, "Info in %s: " a, __VA_ARGS__), \
519  (void)0)
520 
521 #if 0 /* Alternative method for controlling L_* message output */
522  #define L_ERROR(a,...) \
523  { if (L_SEVERITY_ERROR >= MINIMUM_SEVERITY && \
524  L_SEVERITY_ERROR >= LeptMsgSeverity) \
525  fprintf(stderr, "Error in %s: " a, __VA_ARGS__) \
526  }
527  #define L_WARNING(a,...) \
528  { if (L_SEVERITY_WARNING >= MINIMUM_SEVERITY && \
529  L_SEVERITY_WARNING >= LeptMsgSeverity) \
530  fprintf(stderr, "Warning in %s: " a, __VA_ARGS__) \
531  }
532  #define L_INFO(a,...) \
533  { if (L_SEVERITY_INFO >= MINIMUM_SEVERITY && \
534  L_SEVERITY_INFO >= LeptMsgSeverity) \
535  fprintf(stderr, "Info in %s: " a, __VA_ARGS__) \
536  }
537 #endif
538 
539 #endif /* NO_CONSOLE_IO */
540 
541 
542 /*------------------------------------------------------------------------*
543  * snprintf() renamed in MSVC (pre-VS2015) *
544  *------------------------------------------------------------------------*/
545 #if defined _MSC_VER && _MSC_VER < 1900
546 #define snprintf(buf, size, ...) _snprintf_s(buf, size, _TRUNCATE, __VA_ARGS__)
547 #endif
548 
549 
550 #endif /* LEPTONICA_ENVIRON_H */
LEPT_DLL l_int32 LeptMsgSeverity
Definition: utils1.c:112