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