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