environ.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. #ifndef LEPTONICA_ENVIRON_H
  27. #define LEPTONICA_ENVIRON_H
  28. /*------------------------------------------------------------------------*
  29. * Defines and includes differ for Unix and Windows. Also for Windows, *
  30. * differentiate between conditionals based on platform and compiler. *
  31. * For platforms: *
  32. * _WIN32 => Windows, 32- or 64-bit *
  33. * _WIN64 => Windows, 64-bit only *
  34. * __CYGWIN__ => Cygwin *
  35. * For compilers: *
  36. * __GNUC__ => gcc *
  37. * _MSC_VER => msvc *
  38. *------------------------------------------------------------------------*/
  39. /* MS VC++ does not provide stdint.h, so define the missing types here */
  40. #ifndef _MSC_VER
  41. #include <stdint.h>
  42. #else
  43. /* Note that _WIN32 is defined for both 32 and 64 bit applications,
  44. whereas _WIN64 is defined only for the latter */
  45. #ifdef _WIN64
  46. typedef __int64 intptr_t;
  47. typedef unsigned __int64 uintptr_t;
  48. #else
  49. typedef int intptr_t;
  50. typedef unsigned int uintptr_t;
  51. #endif
  52. /* VC++6 doesn't seem to have powf, expf. */
  53. #if (_MSC_VER < 1400)
  54. #define powf(x, y) (float)pow((double)(x), (double)(y))
  55. #define expf(x) (float)exp((double)(x))
  56. #endif
  57. #endif /* _MSC_VER */
  58. /* Windows specifics */
  59. #ifdef _WIN32
  60. /* DLL EXPORTS and IMPORTS */
  61. #if defined(LIBLEPT_EXPORTS)
  62. #define LEPT_DLL __declspec(dllexport)
  63. #elif defined(LIBLEPT_IMPORTS)
  64. #define LEPT_DLL __declspec(dllimport)
  65. #else
  66. #define LEPT_DLL
  67. #endif
  68. #else /* non-Windows specifics */
  69. #include <stdint.h>
  70. #define LEPT_DLL
  71. #endif /* _WIN32 */
  72. typedef intptr_t l_intptr_t;
  73. typedef uintptr_t l_uintptr_t;
  74. typedef void *L_TIMER;
  75. /*--------------------------------------------------------------------*
  76. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
  77. * USER CONFIGURABLE *
  78. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
  79. * Environment variables with I/O libraries *
  80. * Manual Configuration Only: NOT AUTO_CONF *
  81. *--------------------------------------------------------------------*/
  82. /*
  83. * Leptonica provides interfaces to link to several external image
  84. * I/O libraries, plus zlib. Setting any of these to 0 here causes
  85. * non-functioning stubs to be linked.
  86. */
  87. #ifndef HAVE_CONFIG_H
  88. #define HAVE_LIBJPEG 1
  89. #define HAVE_LIBTIFF 1
  90. #define HAVE_LIBPNG 1
  91. #define HAVE_LIBZ 1
  92. #define HAVE_LIBGIF 1
  93. #define HAVE_LIBUNGIF 0
  94. #define HAVE_LIBWEBP 0
  95. #define HAVE_LIBJP2K 0
  96. #endif /* ~HAVE_CONFIG_H */
  97. /*
  98. * On linux systems, you can do I/O between Pix and memory. Specifically,
  99. * you can compress (write compressed data to memory from a Pix) and
  100. * uncompress (read from compressed data in memory to a Pix).
  101. * For jpeg, png, pnm and bmp, these use the non-posix GNU functions
  102. * fmemopen() and open_memstream(). These functions are not
  103. * available on other systems. To use these functions in linux,
  104. * you must define HAVE_FMEMOPEN to be 1 here.
  105. */
  106. #ifndef HAVE_CONFIG_H
  107. #define HAVE_FMEMOPEN 0
  108. #endif /* ~HAVE_CONFIG_H */
  109. /*--------------------------------------------------------------------*
  110. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
  111. * USER CONFIGURABLE *
  112. * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*
  113. * Environ variables for image I/O without external libraries *
  114. *--------------------------------------------------------------------*/
  115. /*
  116. * Leptonica supplies I/O support without using external libraries for:
  117. * * image read/write for bmp, pnm
  118. * * header read for jp2k
  119. * * image wrapping write for pdf and ps.
  120. * Setting any of these to 0 causes non-functioning stubs to be linked.
  121. */
  122. #define USE_BMPIO 1
  123. #define USE_PNMIO 1
  124. #define USE_JP2KHEADER 1
  125. #define USE_PDFIO 1
  126. #define USE_PSIO 1
  127. /*--------------------------------------------------------------------*
  128. * Built-in types *
  129. *--------------------------------------------------------------------*/
  130. typedef signed char l_int8;
  131. typedef unsigned char l_uint8;
  132. typedef short l_int16;
  133. typedef unsigned short l_uint16;
  134. typedef int l_int32;
  135. typedef unsigned int l_uint32;
  136. typedef float l_float32;
  137. typedef double l_float64;
  138. #ifdef COMPILER_MSVC
  139. typedef __int64 l_int64;
  140. typedef unsigned __int64 l_uint64;
  141. #else
  142. typedef long long l_int64;
  143. typedef unsigned long long l_uint64;
  144. #endif /* COMPILER_MSVC */
  145. /*------------------------------------------------------------------------*
  146. * Standard macros *
  147. *------------------------------------------------------------------------*/
  148. #ifndef L_MIN
  149. #define L_MIN(x,y) (((x) < (y)) ? (x) : (y))
  150. #endif
  151. #ifndef L_MAX
  152. #define L_MAX(x,y) (((x) > (y)) ? (x) : (y))
  153. #endif
  154. #ifndef L_ABS
  155. #define L_ABS(x) (((x) < 0) ? (-1 * (x)) : (x))
  156. #endif
  157. #ifndef L_SIGN
  158. #define L_SIGN(x) (((x) < 0) ? -1 : 1)
  159. #endif
  160. #ifndef UNDEF
  161. #define UNDEF -1
  162. #endif
  163. #ifndef NULL
  164. #define NULL 0
  165. #endif
  166. #ifndef TRUE
  167. #define TRUE 1
  168. #endif
  169. #ifndef FALSE
  170. #define FALSE 0
  171. #endif
  172. /*--------------------------------------------------------------------*
  173. * Environment variables for endian dependence *
  174. *--------------------------------------------------------------------*/
  175. /*
  176. * To control conditional compilation, one of two variables
  177. *
  178. * L_LITTLE_ENDIAN (e.g., for Intel X86)
  179. * L_BIG_ENDIAN (e.g., for Sun SPARC, Mac Power PC)
  180. *
  181. * is defined when the GCC compiler is invoked.
  182. * All code should compile properly for both hardware architectures.
  183. */
  184. /*------------------------------------------------------------------------*
  185. * Simple search state variables *
  186. *------------------------------------------------------------------------*/
  187. enum {
  188. L_NOT_FOUND = 0,
  189. L_FOUND = 1
  190. };
  191. /*------------------------------------------------------------------------*
  192. * Path separator conversion *
  193. *------------------------------------------------------------------------*/
  194. enum {
  195. UNIX_PATH_SEPCHAR = 0,
  196. WIN_PATH_SEPCHAR = 1
  197. };
  198. /*------------------------------------------------------------------------*
  199. * Standard memory allocation *
  200. * *
  201. * These specify the memory management functions that are used *
  202. * on all heap data except for Pix. Memory management for Pix *
  203. * also defaults to malloc and free. See pix1.c for details. *
  204. *------------------------------------------------------------------------*/
  205. #define MALLOC(blocksize) malloc(blocksize)
  206. #define CALLOC(numelem, elemsize) calloc(numelem, elemsize)
  207. #define REALLOC(ptr, blocksize) realloc(ptr, blocksize)
  208. #define FREE(ptr) free(ptr)
  209. /*------------------------------------------------------------------------*
  210. * Control printing of error, warning, and info messages *
  211. * *
  212. * To omit all messages to stderr, simply define NO_CONSOLE_IO on the *
  213. * command line. For finer grained control, we have a mechanism *
  214. * based on the message severity level. The following assumes that *
  215. * NO_CONSOLE_IO is not defined. *
  216. * *
  217. * Messages are printed if the message severity is greater than or equal *
  218. * to the current severity threshold. The current severity threshold *
  219. * is the greater of the compile-time severity, which is the minimum *
  220. * severity that can be reported, and the run-time severity, which is *
  221. * the severity threshold at the moment. *
  222. * *
  223. * The compile-time threshold determines which messages are compiled *
  224. * into the library for potential printing. Messages below the *
  225. * compile-time threshold are omitted and can never be printed. The *
  226. * default compile-time threshold is L_SEVERITY_INFO, but this may be *
  227. * overridden by defining MINIMUM_SEVERITY to the desired enumeration *
  228. * identifier on the compiler command line. Defining NO_CONSOLE_IO on *
  229. * the command line is the same as setting MINIMUM_SEVERITY to *
  230. * L_SEVERITY_NONE. *
  231. * *
  232. * The run-time threshold determines which messages are printed during *
  233. * library execution. It defaults to the compile-time threshold but *
  234. * may be changed either statically by defining DEFAULT_SEVERITY to *
  235. * the desired enumeration identifier on the compiler command line, or *
  236. * dynamically by calling setMsgSeverity() to specify a new threshold. *
  237. * The run-time threshold may also be set from the value of the *
  238. * environment variable LEPT_MSG_SEVERITY by calling setMsgSeverity() *
  239. * and specifying L_SEVERITY_EXTERNAL. *
  240. * *
  241. * In effect, the compile-time threshold setting says, "Generate code *
  242. * to permit messages of equal or greater severity than this to be *
  243. * printed, if desired," whereas the run-time threshold setting says, *
  244. * "Print messages that have an equal or greater severity than this." *
  245. *------------------------------------------------------------------------*/
  246. enum {
  247. L_SEVERITY_EXTERNAL = 0, /* Get the severity from the environment */
  248. L_SEVERITY_ALL = 1, /* Lowest severity: print all messages */
  249. L_SEVERITY_DEBUG = 2, /* Print debugging and higher messages */
  250. L_SEVERITY_INFO = 3, /* Print informational and higher messages */
  251. L_SEVERITY_WARNING = 4, /* Print warning and higher messages */
  252. L_SEVERITY_ERROR = 5, /* Print error and higher messages */
  253. L_SEVERITY_NONE = 6 /* Highest severity: print no messages */
  254. };
  255. /* No message less than the compile-time threshold will ever be
  256. * reported, regardless of the current run-time threshold. This allows
  257. * selection of the set of messages to include in the library. For
  258. * example, setting the threshold to L_SEVERITY_WARNING eliminates all
  259. * informational messages from the library. With that setting, both
  260. * warning and error messages would be printed unless setMsgSeverity()
  261. * was called, or DEFAULT_SEVERITY was redefined, to set the run-time
  262. * severity to L_SEVERITY_ERROR. In that case, only error messages
  263. * would be printed.
  264. *
  265. * This mechanism makes the library smaller and faster, by eliminating
  266. * undesired message reporting and the associated run-time overhead for
  267. * message threshold checking, because code for messages whose severity
  268. * is lower than MINIMUM_SEVERITY won't be generated.
  269. *
  270. * A production library might typically permit WARNING and higher
  271. * messages to be generated, and a development library might permit
  272. * DEBUG and higher. The actual messages printed (as opposed to
  273. * generated) would depend on the current run-time severity threshold.
  274. */
  275. #ifdef NO_CONSOLE_IO
  276. #undef MINIMUM_SEVERITY
  277. #undef DEFAULT_SEVERITY
  278. #define MINIMUM_SEVERITY L_SEVERITY_NONE
  279. #define DEFAULT_SEVERITY L_SEVERITY_NONE
  280. #else
  281. #ifndef MINIMUM_SEVERITY
  282. #define MINIMUM_SEVERITY L_SEVERITY_INFO /* Compile-time default */
  283. #endif
  284. #ifndef DEFAULT_SEVERITY
  285. #define DEFAULT_SEVERITY MINIMUM_SEVERITY /* Run-time default */
  286. #endif
  287. #endif
  288. /* The run-time message severity threshold is defined in utils.c. */
  289. LEPT_DLL extern l_int32 LeptMsgSeverity;
  290. /*
  291. * Usage
  292. * =====
  293. * Messages are of two types.
  294. *
  295. * (1) The messages
  296. * ERROR_INT(a,b,c) : returns l_int32
  297. * ERROR_FLOAT(a,b,c) : returns l_float32
  298. * ERROR_PTR(a,b,c) : returns void*
  299. * are used to return from functions and take a fixed set of parameters:
  300. * a : <message string>
  301. * b : procName
  302. * c : <return value from function>
  303. * where procName is the name of the local variable naming the function.
  304. *
  305. * (2) The purely informational L_* messages
  306. * L_ERROR(a,...)
  307. * L_WARNING(a,...)
  308. * L_INFO(a,...)
  309. * do not take a return value, but they take at least two parameters:
  310. * a : <message string> with optional format conversions
  311. * v1 : procName (this must be included as the first vararg)
  312. * v2, ... : optional varargs to match format converters in the message
  313. *
  314. * To return an error from a function that returns void, use:
  315. * L_ERROR(<message string>, procName, [...])
  316. * return;
  317. *
  318. * Implementation details
  319. * ======================
  320. * Messages are defined with the IF_SEV macro. The first parameter is
  321. * the message severity, the second is the function to call if the
  322. * message is to be printed, and the third is the return value if the
  323. * message is to be suppressed. For example, we might have an
  324. * informational message defined as:
  325. *
  326. * IF_SEV(L_SEVERITY_INFO, fprintf(.......), 0)
  327. *
  328. * The macro expands into a conditional. Because the first comparison
  329. * is between two constants, an optimizing compiler will remove either
  330. * the comparison (if it's true) or the entire macro expansion (if it
  331. * is false). This means that there is no run-time overhead for
  332. * messages whose severity falls below the minimum specified at compile
  333. * time, and for others the overhead is one (not two) comparisons.
  334. *
  335. * The L_nnn() macros below do not return a value, but because the
  336. * conditional operator requires one for the false condition, we
  337. * specify a void expression.
  338. */
  339. #ifdef NO_CONSOLE_IO
  340. #define PROCNAME(name)
  341. #define ERROR_INT(a,b,c) ((l_int32)(c))
  342. #define ERROR_FLOAT(a,b,c) ((l_float32)(c))
  343. #define ERROR_PTR(a,b,c) ((void *)(c))
  344. #define L_ERROR(a,...)
  345. #define L_WARNING(a,...)
  346. #define L_INFO(a,...)
  347. #else
  348. #define PROCNAME(name) static const char procName[] = name
  349. #define IF_SEV(l,t,f) \
  350. ((l) >= MINIMUM_SEVERITY && (l) >= LeptMsgSeverity ? (t) : (f))
  351. #define ERROR_INT(a,b,c) \
  352. IF_SEV(L_SEVERITY_ERROR, returnErrorInt((a),(b),(c)), (l_int32)(c))
  353. #define ERROR_FLOAT(a,b,c) \
  354. IF_SEV(L_SEVERITY_ERROR, returnErrorFloat((a),(b),(c)), (l_float32)(c))
  355. #define ERROR_PTR(a,b,c) \
  356. IF_SEV(L_SEVERITY_ERROR, returnErrorPtr((a),(b),(c)), (void *)(c))
  357. #define L_ERROR(a,...) \
  358. IF_SEV(L_SEVERITY_ERROR, \
  359. (void)fprintf(stderr, "Error in %s: " a, __VA_ARGS__), \
  360. (void)0)
  361. #define L_WARNING(a,...) \
  362. IF_SEV(L_SEVERITY_WARNING, \
  363. (void)fprintf(stderr, "Warning in %s: " a, __VA_ARGS__), \
  364. (void)0)
  365. #define L_INFO(a,...) \
  366. IF_SEV(L_SEVERITY_INFO, \
  367. (void)fprintf(stderr, "Info in %s: " a, __VA_ARGS__), \
  368. (void)0)
  369. #if 0 /* Alternative method for controlling L_* message output */
  370. #define L_ERROR(a,...) \
  371. { if (L_SEVERITY_ERROR >= MINIMUM_SEVERITY && \
  372. L_SEVERITY_ERROR >= LeptMsgSeverity) \
  373. fprintf(stderr, "Error in %s: " a, __VA_ARGS__) \
  374. }
  375. #define L_WARNING(a,...) \
  376. { if (L_SEVERITY_WARNING >= MINIMUM_SEVERITY && \
  377. L_SEVERITY_WARNING >= LeptMsgSeverity) \
  378. fprintf(stderr, "Warning in %s: " a, __VA_ARGS__) \
  379. }
  380. #define L_INFO(a,...) \
  381. { if (L_SEVERITY_INFO >= MINIMUM_SEVERITY && \
  382. L_SEVERITY_INFO >= LeptMsgSeverity) \
  383. fprintf(stderr, "Info in %s: " a, __VA_ARGS__) \
  384. }
  385. #endif
  386. #endif /* NO_CONSOLE_IO */
  387. /*------------------------------------------------------------------------*
  388. * snprintf() renamed in MSVC *
  389. *------------------------------------------------------------------------*/
  390. #ifdef _MSC_VER
  391. #define snprintf(buf, size, ...) _snprintf_s(buf, size, _TRUNCATE, __VA_ARGS__)
  392. #endif
  393. #endif /* LEPTONICA_ENVIRON_H */