1 /*
2   Copyright (C) 2003 - 2020 GraphicsMagick Group
3   Copyright (C) 2002 ImageMagick Studio
4 
5   This program is covered by multiple licenses, which are described in
6   Copyright.txt. You should have received a copy of Copyright.txt with this
7   package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.
8 
9   GraphicsMagick Application Programming Interface declarations.
10 */
11 #ifndef _MAGICK_STUDIO_H
12 #define _MAGICK_STUDIO_H
13 
14 #if defined(__cplusplus) || defined(c_plusplus)
15 extern "C" {
16 #endif
17 
18 /*
19   This define is not used by GraphicsMagick and it causes some headers
20   from other installed packages (e.g. MinGW libpthread) to misbehave.
21 */
22 #undef HAVE_CONFIG_H
23 
24 /*
25   Note that the WIN32 and WIN64 definitions are provided by the build
26   configuration rather than the compiler.  Definitions available from the
27   Windows compiler are _WIN32 and _WIN64.  Note that _WIN32 is defined if
28   _WIN64 is defined.
29 */
30 #if defined(WIN32) || defined(WIN64)
31 #  define MSWINDOWS
32 #endif /* defined(WIN32) || defined(WIN64) */
33 
34 #if !defined(MSWINDOWS)
35 #  define POSIX
36 #endif /* !defined(MSWINDOWS) */
37 
38 /*
39   Private functions and types which are not part of the published API
40   should only be exposed when MAGICK_IMPLEMENTATION is defined.
41 */
42 #define MAGICK_IMPLEMENTATION 1
43 
44 #include "magick/magick_config.h"
45 #if defined(__cplusplus) || defined(c_plusplus)
46 #  undef inline
47 #endif /* defined(__cplusplus) || defined(c_plusplus) */
48 
49 /*
50   Allow configuration of cache line size.  If smaller than actual cache line
51   size, then performance may suffer due to false cache line sharing between
52   threads.  Most CPUs have cache lines of 32 or 64 bytes.  IBM Power CPUs have
53   cache lines of 128 bytes.
54 */
55 /* C pre-processor does not support comparing strings. */
56 #if defined(__powerpc__)
57 #  define MAGICK_CACHE_LINE_SIZE 128
58 #else
59 #  define MAGICK_CACHE_LINE_SIZE 64
60 #endif
61 
62 
63 /*
64   Support library symbol prefixing
65 */
66 #if defined(PREFIX_MAGICK_SYMBOLS)
67 #  include "magick/symbols.h"
68 #endif /* defined(PREFIX_MAGICK_SYMBOLS) */
69 
70 #if !defined(const)
71   /*
72     For some stupid reason the zlib headers define 'const' to nothing
73     under AIX unless STDC is defined.
74   */
75 #  define STDC
76 #endif /* !defined(const) */
77 
78 /*
79   For the Windows Visual C++ DLL build, use a Windows resource based
80   message lookup table (i.e. use FormatMessage()).
81  */
82 #if 0
83   /*
84     Currently disabled since feature only seems to work from
85     a DLL
86   */
87 #  if ((defined(MSWINDOWS) && defined(_DLL)) && !defined(__MINGW32__))
88 #    define MAGICK_WINDOWS_MESSAGE_TABLES 1
89 #  endif
90 #endif
91 
92 #include <stdarg.h>
93 #include <stdio.h>
94 #if defined(MSWINDOWS) && defined(_DEBUG)
95 #  define _CRTDBG_MAP_ALLOC
96 #endif
97 #include <stdlib.h>
98 #include <stddef.h> /* C'99 size_t, ptrdiff_t, NULL */
99 #if !defined(MSWINDOWS)
100 #  include <unistd.h>
101 #else
102 #  include <direct.h>
103 #  include <io.h>
104 #  if !defined(HAVE_STRERROR)
105 #    define HAVE_STRERROR
106 #  endif
107 #endif
108 
109 /*
110   Use fseeko() and ftello() if they are available since they use
111   'off_t' rather than 'long'.  It is wrong to use fseeko() and
112   ftello() only on systems with special LFS support since some systems
113   (e.g. FreeBSD) support a 64-bit off_t by default.
114 */
115 #if defined(HAVE_FSEEKO)
116 #  define fseek  fseeko
117 #  define ftell  ftello
118 #endif
119 
120 #if !defined(ExtendedSignedIntegralType)
121 #  define ExtendedSignedIntegralType magick_int64_t
122 #endif
123 #if !defined(ExtendedUnsignedIntegralType)
124 #  define ExtendedUnsignedIntegralType magick_uint64_t
125 #endif
126 
127 #include <string.h>
128 #include <ctype.h>
129 #include <locale.h>
130 #include <errno.h>
131 #include <fcntl.h>
132 #include <math.h>
133 #include <time.h>
134 #include <limits.h>
135 #include <signal.h>
136 #include <assert.h>
137 
138 #include <sys/types.h>
139 #include <sys/stat.h>
140 
141 #if defined(HAVE_FTIME)
142 #  include <sys/timeb.h>
143 #endif
144 
145 #if defined(HAVE_STDINT_H)
146 #  include <stdint.h>
147 #endif
148 
149 #if defined(POSIX)
150 #  if defined(HAVE_SYS_NDIR_H) || defined(HAVE_SYS_DIR_H) || defined(HAVE_NDIR_H)
151 #    define dirent direct
152 #    define NAMLEN(dirent) (dirent)->d_namlen
153 #    if defined(HAVE_SYS_NDIR_H)
154 #      include <sys/ndir.h>
155 #    endif
156 #    if defined(HAVE_SYS_DIR_H)
157 #      include <sys/dir.h>
158 #    endif
159 #    if defined(HAVE_NDIR_H)
160 #      include <ndir.h>
161 #    endif
162 #  else
163 #    include <dirent.h>
164 #    define NAMLEN(dirent) strlen((dirent)->d_name)
165 #  endif
166 #  include <sys/wait.h>
167 #  if defined(HAVE_SYS_RESOURCE_H)
168 #    include <sys/resource.h>
169 #  endif /* defined(HAVE_SYS_RESOURCE_H)  */
170 #  if defined(HAVE_SYS_MMAN_H)
171 #    include <sys/mman.h>
172 #  endif /*  defined(HAVE_SYS_MMAN_H) */
173 #  include <pwd.h>
174 #endif
175 
176 #if !defined(S_ISDIR)
177 #  define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
178 #endif
179 
180 #if !defined(S_ISREG)
181 #  define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
182 #endif
183 
184 /*
185   Avoid shadowing system library globals and functions.
186 */
187 #undef gamma
188 #define gamma gamma_magick
189 #undef swab
190 #define swab swab_magick
191 #undef y1
192 #define y1 y1_magick
193 
194 /*
195   Include common bits shared with api.h
196 */
197 #include "magick/common.h"
198 /*
199   Enable use of numeric message IDs and a translation table in order
200   to support multiple locales.
201  */
202 #define MAGICK_IDBASED_MESSAGES 1
203 #if defined(MAGICK_IDBASED_MESSAGES)
204 #  include "magick/locale_c.h"
205 #endif
206 #include "magick/magick_types.h"
207 #include "magick/image.h"
208 #include "magick/list.h"
209 #include "magick/memory.h"
210 
211 #if !defined(MSWINDOWS)
212 #  include <sys/time.h>
213 #  if defined(HAVE_SYS_TIMES_H)
214 #    include <sys/times.h>
215 #  endif
216 #endif
217 
218 #if defined(POSIX)
219 # include "magick/unix_port.h"
220 #endif /* defined(POSIX) */
221 
222 #if defined(MSWINDOWS)
223 # include "magick/nt_base.h"
224 #endif /* defined(MSWINDOWS) */
225 
226 #if defined(HAVE_MMAP_FILEIO) && !defined(MSWINDOWS)
227 # include <sys/mman.h>
228 #endif
229 
230 #if defined(HAVE_PTHREAD)
231 # include <pthread.h>
232 #endif
233 
234 #if defined(HAVE_POLL)
235 # include <sys/poll.h>
236 #endif
237 
238 /*
239   Work around OpenMP implementation bugs noticed in the Open64 5.0 compiler
240   These workarounds will be removed once the bugs have been fixed in a release.
241   Open64 5.0 provides these definitions:
242   __OPENCC__ 5        -- OpenCC major version
243   __OPENCC_MINOR__ 0  -- OpenCC minor version
244   __OPEN64__ "5.0"    -- OpenCC stringified version
245 */
246 #if defined(__OPENCC__)
247 #  undef USE_STATIC_SCHEDULING_ONLY
248 #  define USE_STATIC_SCHEDULING_ONLY 1
249 #endif
250 
251 /*
252   OpenMP support requires version 2.0 (March 2002) or later.
253 */
254 #if defined(_OPENMP) && ((_OPENMP >= 200203) || defined(__OPENCC__))
255 #  include <omp.h>
256 #  define HAVE_OPENMP 1
257 #endif
258 
259 #undef index
260 #undef pipe
261 
262 /*
263   If TRIO library is used, remap snprintf and vsnprintf to TRIO equivalents.
264 */
265 #if defined(HasTRIO)
266 #  include <trio.h>
267 #  define snprintf trio_snprintf
268 #  define HAVE_SNPRINTF 1
269 #  define vsnprintf trio_vsnprintf
270 #  define HAVE_VSNPRINTF 1
271 #endif
272 
273 /*
274   Provide prototypes for several functions which are detected to be
275   available, but which do not provide a prototype due to interface
276   standards conformance (or a bug).
277 */
278 
279 #if defined(HAVE_PREAD) && defined(HAVE_DECL_PREAD) && !HAVE_DECL_PREAD
280 ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset);
281 #endif
282 
283 #if defined(HAVE_PWRITE) && defined(HAVE_DECL_PWRITE) && !HAVE_DECL_PWRITE
284 ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset);
285 #endif
286 
287 #if defined(HAVE_STRLCPY) && defined(HAVE_DECL_STRLCPY) && !HAVE_DECL_STRLCPY
288 extern size_t strlcpy(char *dst, const char *src, size_t dstsize);
289 #endif
290 
291 #if defined(HAVE_VSNPRINTF) && defined(HAVE_DECL_VSNPRINTF) && !HAVE_DECL_VSNPRINTF
292 extern int vsnprintf(char *s, size_t n, const char *format, va_list ap);
293 #endif
294 
295 /*
296   Some 64-bit int support.
297 */
298 #if defined(HAVE_STRTOLL) && (SIZEOF_SIGNED_LONG < 8)
299 #  define MagickStrToL64(str,endptr,base) (strtoll(str,endptr,base))
300 #else
301 #  define MagickStrToL64(str,endptr,base) ((magick_int64_t) strtol(str,endptr,base))
302 #endif
303 #if defined(HAVE_ATOLL) && (SIZEOF_SIGNED_LONG < 8)
304 #  define MagickAtoL64(str) (atoll(str))
305 #else
306 #  define MagickAtoL64(str) ((magick_int64_t) atol(str))
307 #endif
308 
309 /*
310   Review these platform specific definitions.
311 */
312 #if defined(POSIX)
313 #  define DirectorySeparator  "/"
314 #  define DirectoryListSeparator  ':'
315 #  define EditorOptions  " -title \"Edit Image Comment\" -e vi"
316 #  define Exit  exit
317 #  define IsBasenameSeparator(c)  ((c) == '/')
318 #  define PreferencesDefaults  "~/."
319 #  define ProcessPendingEvents(text)
320 #  define ReadCommandlLine(argc,argv)
321 #  define SetNotifyHandlers
322 #  define MagickSleep(seconds) sleep(seconds)
323 #endif
324 
325 #if defined(MSWINDOWS)
326 #  define DirectorySeparator  "\\"
327 #  define DirectoryListSeparator  ';'
328 #  define EditorOptions ""
329 #  define IsBasenameSeparator(c)  (((c) == '/') || ((c) == '\\'))
330 #  define ProcessPendingEvents(text)
331 #  if !defined(PreferencesDefaults)
332 #     define PreferencesDefaults  "~\\."
333 #  endif /* PreferencesDefaults */
334 #  define ReadCommandlLine(argc,argv)
335 #  define SetNotifyHandlers \
336     SetErrorHandler(NTErrorHandler); \
337     SetWarningHandler(NTWarningHandler)
338 #  define MagickSleep(seconds)  Sleep(seconds*1000)
339 #  if !defined(HAVE_TIFFCONF_H)
340 #    define HAVE_TIFFCONF_H
341 #  endif
342 #endif /* defined(MSWINDOWS) */
343 
344 
345 /*
346   Define declarations.
347 */
348 #define AbsoluteValue(x)  ((x) < 0 ? -(x) : (x))
349 #define ArraySize(a) (sizeof(a)/sizeof(a[0]))
350 #define False  0
351 #define DegreesToRadians(x) (MagickPI*(x)/180.0)
352 #define MagickIncarnate(x)  InitializeMagick(x)
353 #define MagickEpsilon  1.0e-12
354 #define MagickPI  3.14159265358979323846264338327950288419716939937510
355 #define MagickSQ2PI 2.50662827463100024161235523934010416269302368164062
356 #if !defined(INFINITY) /* C'99 provides INFINITY but C'89 does not */
357 #  define INFINITY (log(0))
358 #endif
359 #define Max(x,y)  (((x) > (y)) ? (x) : (y))
360 #define Min(x,y)  (((x) < (y)) ? (x) : (y))
361 #define NumberOfObjectsInArray(octets,size) (octets/size) /* rounds down */
362 #define QuantumTick(i,span) \
363   ((((i) % ((Max(101,span)-1)/100)) == 0) || \
364     ((magick_int64_t) (i) == ((magick_int64_t) (span)-1)))
365 #define RadiansToDegrees(x) (180.0*(x)/MagickPI)
366 #define RoundUpToAlignment(offset,alignment)                            \
367   (((offset)+((alignment)-1)) & ~((alignment)-1))
368 #define AssertAlignment(offset,alignment) \
369   (assert((((size_t) offset) % (size_t) alignment) == (size_t) 0))
370 #define NormalizeDepthToOctet(depth) (depth < 8 ? 8 : \
371   (depth < 16 ? 16 : (depth < 32 ? 32 : depth)))
372 #define ScaleColor5to8(x)  (((x) << 3) | ((x) >> 2))
373 #define ScaleColor6to8(x)  (((x) << 2) | ((x) >> 4))
374 #define Swap(x,y) ((x)^=(y), (y)^=(x), (x)^=(y))
375 #define True  1
376 
377 #define DefineNameToString(value) #value
378 #define DefineValueToString(define) DefineNameToString(define)
379 
380 #if !defined(COSF)
381 #  if defined(HAVE_COSF)
382 #    define COSF(f) cosf((float) (f))
383 #  else
384 #    define COSF(d) cos(d)
385 #  endif
386 #endif /* !defined(COSF) */
387 #if !defined(FABSF)
388 #  if defined(HAVE_FABSF)
389 #    define FABSF(f) fabsf((float) (f))
390 #  else
391 #    define FABSF(d) fabs(d)
392 #  endif
393 #endif /* !defined(FABSF) */
394 #if !defined(LOGF)
395 #  if defined(HAVE_LOGF)
396 #    define LOGF(f) logf((float) (f))
397 #  else
398 #    define LOGF(d) log(d)
399 #  endif
400 #endif /* !defined(LOGF) */
401 #if !defined(SINF)
402 #  if defined(HAVE_SINF)
403 #    define SINF(f) sinf((float) (f))
404 #  else
405 #    define SINF(d) sin(d)
406 #  endif
407 #endif /* !defined(SINF) */
408 #if !defined(SQRTF)
409 #  if defined(HAVE_SQRTF)
410 #    define SQRTF(f) sqrtf((float) (f))
411 #  else
412 #    define SQRTF(d) sqrt(d)
413 #  endif
414 #endif /* !defined(SQRTF) */
415 
416 /*
417   atof(), atoi(), and atol() are legacy functions which might not be
418   thread safe, might not enforce reasonable limits, and should not be
419   used for new code.  So we implement them via strtod and strtol.
420 */
421 #define MagickAtoF(str) (strtod(str, (char **)NULL))
422 #define MagickAtoI(str) ((int) strtol(str, (char **)NULL, 10))
423 #define MagickAtoL(str) (strtol(str, (char **)NULL, 10))
424 
425 /*
426   3D effects.
427 */
428 #define AccentuateModulate  ScaleCharToQuantum(80)
429 #define HighlightModulate  ScaleCharToQuantum(125)
430 #define ShadowModulate  ScaleCharToQuantum(135)
431 #define DepthModulate  ScaleCharToQuantum(185)
432 #define TroughModulate  ScaleCharToQuantum(110)
433 
434 /*
435   Define system symbols if not already defined.
436 */
437 #if !defined(STDIN_FILENO)
438 #  define STDIN_FILENO  0x00
439 #endif
440 
441 #if !defined(O_BINARY)
442 #  define O_BINARY  0x00
443 #endif
444 
445 #if !defined(MAP_FAILED)
446 #  define MAP_FAILED      ((void *) -1)
447 #endif
448 
449 #if !defined(PATH_MAX)
450 #  define PATH_MAX 4096
451 #endif
452 
453 /*
454   When using Autoconf configure script, BuildMagickModules is defined by
455   magick/magick_config.h when modules are to be built.  Building modules
456   requires that support for shared libraries be enabled.
457 
458   When using Visual Studio and when using the Autoconf configure script with
459   MinGW these options are supplied when compiling the associated components:
460 
461     libMagick:           _DLL _MAGICKMOD_ _MAGICKLIB_
462     module:              _DLL
463     executable/Magick++: _DLL _MAGICKMOD_
464 
465   The Visual Studio build only knows how to build a static build, or a DLL
466   modules-based build.  The Autotools-based build knows how to build static,
467   shared, and modules builds.
468 
469   Libtool's libltdl is required to build modules when the Autoconf configure
470   script is used.
471 */
472 
473 #if defined(HasLTDL)
474 #  define SupportMagickModules
475 #elif !defined(__MINGW32__) && !defined(__MINGW64__)
476 #  if defined(MSWINDOWS) && defined(_DLL)
477 #    define SupportMagickModules
478 #  endif
479 #endif
480 
481 #if defined(_MAGICKMOD_)
482 #  undef BuildMagickModules
483 #  define BuildMagickModules
484 #endif
485 
486 /*
487   C99 isblank() is not portable enough yet.
488 */
489 #define MagickIsBlank(c) (c== ' ' || c == '\t')
490 
491 #if !defined(MagickMmap)
492 #  define MagickMmap(address,length,protection,access,file,offset) \
493      mmap(address,length,protection,access,file,offset)
494 #endif
495 #if !defined(MagickMsync)
496 #  define MagickMsync(addr,len,flags) msync(addr,len,flags)
497 #endif
498 #if !defined(MagickMunmap)
499 #  define MagickMunmap(addr,len) munmap(addr,len)
500 #endif
501 #if !defined(MagickFtruncate)
502 #  define MagickFtruncate(filedes,length) ftruncate(filedes,length)
503 #endif
504 
505 #if !defined(HAVE_POPEN) && defined(HAVE__POPEN)
506 #  define HAVE_POPEN 1
507 #  define popen _popen
508 #endif /* !defined(HAVE_POPEN) && defined(HAVE__POPEN) */
509 
510 #if !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE)
511 #  define HAVE_PCLOSE 1
512 #  define pclose _pclose
513 #endif /* !defined(HAVE_PCLOSE) && defined(HAVE__PCLOSE) */
514 
515 #if defined(HAVE__EXIT)
516 #  define SignalHandlerExit _exit
517 #else
518 #  define SignalHandlerExit Exit
519 #endif /* defined(HAVE__EXIT) */
520 
521 /*
522   OpenMP function null replacements if not using OpenMP.
523 */
524 #if !defined(HAVE_OPENMP)
525 #  undef omp_get_max_threads
526 #  define omp_get_max_threads() 1
527 #  undef omp_get_num_threads
528 #  define omp_get_num_threads() 1
529 #  undef omp_get_thread_num
530 #  define omp_get_thread_num() 0
531 #  undef omp_set_num_threads
532 #  define omp_set_num_threads(nthreads)
533 #endif /* !defined(HAVE_OPENMP) */
534 
535 /*
536   Common const definitions
537 */
538 #define BackgroundColor "#ffffffffffff"  /* white */
539 #define BorderColor "#dfdfdfdfdfdf"  /* gray */
540 #define DefaultTileFrame "15x15+3+3"
541 #define DefaultTileGeometry "120x120+4+3>"
542 #define DefaultTileLabel "%f\n%wx%h\n%b"
543 #define ForegroundColor "#000000000000"  /* black */
544 #define HighlightColor "#f1f100001e1e" /* light red */
545 #define MatteColor "#bdbdbdbdbdbd"  /* gray */
546 #define PSDensityGeometry "72.0x72.0"
547 #define PSPageGeometry "612x792>"
548 
549 #define LoadImageText "[%s] Loading image: %lux%lu...  "
550 #define SaveImageText "[%s] Saving image: %lux%lu...  "
551 #define LoadImagesText "[%s] Loading images...  "
552 #define SaveImagesText "[%s] Saving images...  "
553 
554 #define DefaultCompressionQuality 75U
555 
556 
557 #if defined(__cplusplus) || defined(c_plusplus)
558 }
559 #endif /* defined(__cplusplus) || defined(c_plusplus) */
560 
561 #endif /* ifndef _MAGICK_STUDIO_H */
562 
563 /*
564  * Local Variables:
565  * mode: c
566  * c-basic-offset: 2
567  * fill-column: 78
568  * End:
569  */
570