xref: /PHP-7.2/ext/gd/libgd/gd.h (revision 68a0639c)
1 #ifndef GD_H
2 #define GD_H 1
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "php_compat.h"
13 
14 #define GD_MAJOR_VERSION 2
15 #define GD_MINOR_VERSION 0
16 #define GD_RELEASE_VERSION 35
17 #define GD_EXTRA_VERSION ""
18 #define GD_VERSION_STRING "2.0.35"
19 
20 #ifdef NETWARE
21 /* default fontpath for netware systems */
22 #define DEFAULT_FONTPATH "sys:/java/nwgfx/lib/x11/fonts/ttf;."
23 #define PATHSEPARATOR ";"
24 #elif defined(WIN32)
25 /* default fontpath for windows systems */
26 #define DEFAULT_FONTPATH "c:\\winnt\\fonts;c:\\windows\\fonts;."
27 #define PATHSEPARATOR ";"
28 #else
29 /* default fontpath for unix systems */
30 #define DEFAULT_FONTPATH "/usr/X11R6/lib/X11/fonts/TrueType:/usr/X11R6/lib/X11/fonts/truetype:/usr/X11R6/lib/X11/fonts/TTF:/usr/share/fonts/TrueType:/usr/share/fonts/truetype:/usr/openwin/lib/X11/fonts/TrueType:/usr/X11R6/lib/X11/fonts/Type1:."
31 #define PATHSEPARATOR ":"
32 #endif
33 
34 /* gd.h: declarations file for the graphic-draw module.
35  * Permission to use, copy, modify, and distribute this software and its
36  * documentation for any purpose and without fee is hereby granted, provided
37  * that the above copyright notice appear in all copies and that both that
38  * copyright notice and this permission notice appear in supporting
39  * documentation.  This software is provided "AS IS." Thomas Boutell and
40  * Boutell.Com, Inc. disclaim all warranties, either express or implied,
41  * including but not limited to implied warranties of merchantability and
42  * fitness for a particular purpose, with respect to this code and accompanying
43  * documentation. */
44 
45 /* stdio is needed for file I/O. */
46 #include <stdio.h>
47 #include "gd_io.h"
48 
49 /* va_list needed in gdErrorMethod */
50 #ifdef PHP_WIN32
51 # include <stdarg.h>
52 #else
53 # if HAVE_STDARG_H
54 # include <stdarg.h>
55 # else
56 #  if HAVE_SYS_VARARGS_H
57 #  include <sys/varargs.h>
58 #  endif
59 # endif
60 #endif
61 
62 /* The maximum number of palette entries in palette-based images.
63 	In the wonderful new world of gd 2.0, you can of course have
64 	many more colors when using truecolor mode. */
65 
66 #define gdMaxColors 256
67 
68 /* Image type. See functions below; you will not need to change
69 	the elements directly. Use the provided macros to
70 	access sx, sy, the color table, and colorsTotal for
71 	read-only purposes. */
72 
73 /* If 'truecolor' is set true, the image is truecolor;
74 	pixels are represented by integers, which
75 	must be 32 bits wide or more.
76 
77 	True colors are repsented as follows:
78 
79 	ARGB
80 
81 	Where 'A' (alpha channel) occupies only the
82 	LOWER 7 BITS of the MSB. This very small
83 	loss of alpha channel resolution allows gd 2.x
84 	to keep backwards compatibility by allowing
85 	signed integers to be used to represent colors,
86 	and negative numbers to represent special cases,
87 	just as in gd 1.x. */
88 
89 #define gdAlphaMax 127
90 #define gdAlphaOpaque 0
91 #define gdAlphaTransparent 127
92 #define gdRedMax 255
93 #define gdGreenMax 255
94 #define gdBlueMax 255
95 #define gdTrueColorGetAlpha(c) (((c) & 0x7F000000) >> 24)
96 #define gdTrueColorGetRed(c) (((c) & 0xFF0000) >> 16)
97 #define gdTrueColorGetGreen(c) (((c) & 0x00FF00) >> 8)
98 #define gdTrueColorGetBlue(c) ((c) & 0x0000FF)
99 #define gdEffectReplace 0
100 #define gdEffectAlphaBlend 1
101 #define gdEffectNormal 2
102 #define gdEffectOverlay 3
103 #define gdEffectMultiply 4
104 
105 #define GD_TRUE 1
106 #define GD_FALSE 0
107 
108 #define GD_EPSILON 1e-6
109 
110 /* This function accepts truecolor pixel values only. The
111 	source color is composited with the destination color
112 	based on the alpha channel value of the source color.
113 	The resulting color is opaque. */
114 
115 int gdAlphaBlend(int dest, int src);
116 int gdLayerOverlay(int dst, int src);
117 int gdLayerMultiply(int dest, int src);
118 
119 /**
120  * Group: Transform
121  *
122  * Constants: gdInterpolationMethod
123 
124  *  GD_BELL				 - Bell
125  *  GD_BESSEL			 - Bessel
126  *  GD_BILINEAR_FIXED 	 - fixed point bilinear
127  *  GD_BICUBIC 			 - Bicubic
128  *  GD_BICUBIC_FIXED 	 - fixed point bicubic integer
129  *  GD_BLACKMAN			 - Blackman
130  *  GD_BOX				 - Box
131  *  GD_BSPLINE			 - BSpline
132  *  GD_CATMULLROM		 - Catmullrom
133  *  GD_GAUSSIAN			 - Gaussian
134  *  GD_GENERALIZED_CUBIC - Generalized cubic
135  *  GD_HERMITE			 - Hermite
136  *  GD_HAMMING			 - Hamming
137  *  GD_HANNING			 - Hannig
138  *  GD_MITCHELL			 - Mitchell
139  *  GD_NEAREST_NEIGHBOUR - Nearest neighbour interpolation
140  *  GD_POWER			 - Power
141  *  GD_QUADRATIC		 - Quadratic
142  *  GD_SINC				 - Sinc
143  *  GD_TRIANGLE			 - Triangle
144  *  GD_WEIGHTED4		 - 4 pixels weighted bilinear interpolation
145  *
146  * See also:
147  *  <gdSetInterpolationMethod>
148  **/
149 typedef enum {
150 	GD_DEFAULT          = 0,
151 	GD_BELL,
152 	GD_BESSEL,
153 	GD_BILINEAR_FIXED,
154 	GD_BICUBIC,
155 	GD_BICUBIC_FIXED,
156 	GD_BLACKMAN,
157 	GD_BOX,
158 	GD_BSPLINE,
159 	GD_CATMULLROM,
160 	GD_GAUSSIAN,
161 	GD_GENERALIZED_CUBIC,
162 	GD_HERMITE,
163 	GD_HAMMING,
164 	GD_HANNING,
165 	GD_MITCHELL,
166 	GD_NEAREST_NEIGHBOUR,
167 	GD_POWER,
168 	GD_QUADRATIC,
169 	GD_SINC,
170 	GD_TRIANGLE,
171 	GD_WEIGHTED4,
172 	GD_METHOD_COUNT = 21
173 } gdInterpolationMethod;
174 
175 /* define struct with name and func ptr and add it to gdImageStruct gdInterpolationMethod interpolation; */
176 
177 /* Interpolation function ptr */
178 typedef double (* interpolation_method )(double);
179 
180 typedef struct gdImageStruct {
181 	/* Palette-based image pixels */
182 	unsigned char ** pixels;
183 	int sx;
184 	int sy;
185 	/* These are valid in palette images only. See also
186 		'alpha', which appears later in the structure to
187 		preserve binary backwards compatibility */
188 	int colorsTotal;
189 	int red[gdMaxColors];
190 	int green[gdMaxColors];
191 	int blue[gdMaxColors];
192 	int open[gdMaxColors];
193 	/* For backwards compatibility, this is set to the
194 		first palette entry with 100% transparency,
195 		and is also set and reset by the
196 		gdImageColorTransparent function. Newer
197 		applications can allocate palette entries
198 		with any desired level of transparency; however,
199 		bear in mind that many viewers, notably
200 		many web browsers, fail to implement
201 		full alpha channel for PNG and provide
202 		support for full opacity or transparency only. */
203 	int transparent;
204 	int *polyInts;
205 	int polyAllocated;
206 	struct gdImageStruct *brush;
207 	struct gdImageStruct *tile;
208 	int brushColorMap[gdMaxColors];
209 	int tileColorMap[gdMaxColors];
210 	int styleLength;
211 	int stylePos;
212 	int *style;
213 	int interlace;
214 	/* New in 2.0: thickness of line. Initialized to 1. */
215 	int thick;
216 	/* New in 2.0: alpha channel for palettes. Note that only
217 		Macintosh Internet Explorer and (possibly) Netscape 6
218 		really support multiple levels of transparency in
219 		palettes, to my knowledge, as of 2/15/01. Most
220 		common browsers will display 100% opaque and
221 		100% transparent correctly, and do something
222 		unpredictable and/or undesirable for levels
223 		in between. TBB */
224 	int alpha[gdMaxColors];
225 	/* Truecolor flag and pixels. New 2.0 fields appear here at the
226 		end to minimize breakage of existing object code. */
227 	int trueColor;
228 	int ** tpixels;
229 	/* Should alpha channel be copied, or applied, each time a
230 		pixel is drawn? This applies to truecolor images only.
231 		No attempt is made to alpha-blend in palette images,
232 		even if semitransparent palette entries exist.
233 		To do that, build your image as a truecolor image,
234 		then quantize down to 8 bits. */
235 	int alphaBlendingFlag;
236 	/* Should the alpha channel of the image be saved? This affects
237 		PNG at the moment; other future formats may also
238 		have that capability. JPEG doesn't. */
239 	int saveAlphaFlag;
240 
241 	/* 2.0.12: anti-aliased globals. 2.0.26: just a few vestiges after
242 	  switching to the fast, memory-cheap implementation from PHP-gd. */
243 	int AA;
244 	int AA_color;
245 	int AA_dont_blend;
246 
247 	/* 2.0.12: simple clipping rectangle. These values must be checked for safety when set; please use gdImageSetClip */
248 	int cx1;
249 	int cy1;
250 	int cx2;
251 	int cy2;
252 	unsigned int res_x;
253 	unsigned int res_y;
254 	gdInterpolationMethod interpolation_id;
255 	interpolation_method interpolation;
256 } gdImage;
257 
258 typedef gdImage * gdImagePtr;
259 
260 /* Point type for use in polygon drawing. */
261 
262 /**
263  * Group: Types
264  *
265  * typedef: gdPointF
266  *  Defines a point in a 2D coordinate system using floating point
267  *  values.
268  * x - Floating point position (increase from left to right)
269  * y - Floating point Row position (increase from top to bottom)
270  *
271  * typedef: gdPointFPtr
272  *  Pointer to a <gdPointF>
273  *
274  * See also:
275  *  <gdImageCreate>, <gdImageCreateTrueColor>,
276  **/
277 typedef struct
278 {
279 	double x, y;
280 }
281 gdPointF, *gdPointFPtr;
282 
283 typedef struct {
284 	/* # of characters in font */
285 	int nchars;
286 	/* First character is numbered... (usually 32 = space) */
287 	int offset;
288 	/* Character width and height */
289 	int w;
290 	int h;
291 	/* Font data; array of characters, one row after another.
292 		Easily included in code, also easily loaded from
293 		data files. */
294 	char *data;
295 } gdFont;
296 
297 /* Text functions take these. */
298 typedef gdFont *gdFontPtr;
299 
300 typedef void(*gdErrorMethod)(int, const char *, va_list);
301 
302 void gdSetErrorMethod(gdErrorMethod);
303 void gdClearErrorMethod(void);
304 
305 /**
306  * Group: Types
307  *
308  * typedef: gdRect
309  *  Defines a rectilinear region.
310  *
311  *  x - left position
312  *  y - right position
313  *  width - Rectangle width
314  *  height - Rectangle height
315  *
316  * typedef: gdRectPtr
317  *  Pointer to a <gdRect>
318  *
319  * See also:
320  *  <gdSetInterpolationMethod>
321  **/
322 typedef struct
323 {
324 	int x, y;
325 	int width, height;
326 }
327 gdRect, *gdRectPtr;
328 
329 /* For backwards compatibility only. Use gdImageSetStyle()
330 	for MUCH more flexible line drawing. Also see
331 	gdImageSetBrush(). */
332 #define gdDashSize 4
333 
334 /* Special colors. */
335 
336 #define gdStyled (-2)
337 #define gdBrushed (-3)
338 #define gdStyledBrushed (-4)
339 #define gdTiled (-5)
340 
341 /* NOT the same as the transparent color index.
342 	This is used in line styles only. */
343 #define gdTransparent (-6)
344 
345 #define gdAntiAliased (-7)
346 
347 /* Functions to manipulate images. */
348 
349 /* Creates a palette-based image (up to 256 colors). */
350 gdImagePtr gdImageCreate(int sx, int sy);
351 
352 /* An alternate name for the above (2.0). */
353 #define gdImageCreatePalette gdImageCreate
354 
355 /* Creates a truecolor image (millions of colors). */
356 gdImagePtr gdImageCreateTrueColor(int sx, int sy);
357 
358 /* Creates an image from various file types. These functions
359 	return a palette or truecolor image based on the
360 	nature of the file being loaded. Truecolor PNG
361 	stays truecolor; palette PNG stays palette-based;
362 	JPEG is always truecolor. */
363 gdImagePtr gdImageCreateFromPng(FILE *fd);
364 gdImagePtr gdImageCreateFromPngCtx(gdIOCtxPtr in);
365 gdImagePtr gdImageCreateFromWBMP(FILE *inFile);
366 gdImagePtr gdImageCreateFromWBMPCtx(gdIOCtx *infile);
367 gdImagePtr gdImageCreateFromJpeg(FILE *infile);
368 gdImagePtr gdImageCreateFromJpegEx(FILE *infile, int ignore_warning);
369 gdImagePtr gdImageCreateFromJpegCtx(gdIOCtx *infile);
370 gdImagePtr gdImageCreateFromJpegCtxEx(gdIOCtx *infile, int ignore_warning);
371 gdImagePtr gdImageCreateFromJpegPtr (int size, void *data);
372 gdImagePtr gdImageCreateFromJpegPtrEx (int size, void *data, int ignore_warning);
373 gdImagePtr gdImageCreateFromWebp(FILE *fd);
374 gdImagePtr gdImageCreateFromWebpCtx(gdIOCtxPtr in);
375 gdImagePtr gdImageCreateFromWebpPtr (int size, void *data);
376 
377 gdImagePtr gdImageCreateFromBmp (FILE * inFile);
378 gdImagePtr gdImageCreateFromBmpPtr (int size, void *data);
379 gdImagePtr gdImageCreateFromBmpCtx (gdIOCtxPtr infile);
380 
381 int gdJpegGetVersionInt();
382 const char * gdPngGetVersionString();
383 
384 int gdJpegGetVersionInt();
385 const char * gdJpegGetVersionString();
386 
387 /* A custom data source. */
388 /* The source function must return -1 on error, otherwise the number
389         of bytes fetched. 0 is EOF, not an error! */
390 /* context will be passed to your source function. */
391 
392 typedef struct {
393         int (*source) (void *context, char *buffer, int len);
394         void *context;
395 } gdSource, *gdSourcePtr;
396 
397 gdImagePtr gdImageCreateFromPngSource(gdSourcePtr in);
398 
399 gdImagePtr gdImageCreateFromGd(FILE *in);
400 gdImagePtr gdImageCreateFromGdCtx(gdIOCtxPtr in);
401 
402 gdImagePtr gdImageCreateFromGd2(FILE *in);
403 gdImagePtr gdImageCreateFromGd2Ctx(gdIOCtxPtr in);
404 
405 gdImagePtr gdImageCreateFromGd2Part(FILE *in, int srcx, int srcy, int w, int h);
406 gdImagePtr gdImageCreateFromGd2PartCtx(gdIOCtxPtr in, int srcx, int srcy, int w, int h);
407 
408 gdImagePtr gdImageCreateFromXbm(FILE *fd);
409 void gdImageXbmCtx(gdImagePtr image, char* file_name, int fg, gdIOCtx * out);
410 
411 gdImagePtr gdImageCreateFromXpm (char *filename);
412 
413 void gdImageDestroy(gdImagePtr im);
414 
415 /* Replaces or blends with the background depending on the
416 	most recent call to gdImageAlphaBlending and the
417 	alpha channel value of 'color'; default is to overwrite.
418 	Tiling and line styling are also implemented
419 	here. All other gd drawing functions pass through this call,
420 	allowing for many useful effects. */
421 
422 void gdImageSetPixel(gdImagePtr im, int x, int y, int color);
423 
424 int gdImageGetTrueColorPixel (gdImagePtr im, int x, int y);
425 int gdImageGetPixel(gdImagePtr im, int x, int y);
426 
427 void gdImageAABlend(gdImagePtr im);
428 
429 void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
430 void gdImageAALine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
431 
432 /* For backwards compatibility only. Use gdImageSetStyle()
433 	for much more flexible line drawing. */
434 void gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
435 /* Corners specified (not width and height). Upper left first, lower right
436  	second. */
437 void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
438 /* Solid bar. Upper left corner first, lower right corner second. */
439 void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
440 void gdImageSetClip(gdImagePtr im, int x1, int y1, int x2, int y2);
441 void gdImageGetClip(gdImagePtr im, int *x1P, int *y1P, int *x2P, int *y2P);
442 void gdImageSetResolution(gdImagePtr im, const unsigned int res_x, const unsigned int res_y);
443 void gdImageChar(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color);
444 void gdImageCharUp(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color);
445 void gdImageString(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color);
446 void gdImageStringUp(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color);
447 void gdImageString16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
448 void gdImageStringUp16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
449 
450 /*
451  * The following functions are required to be called prior to the
452  * use of any sort of threads in a module load / shutdown function
453  * respectively.
454  */
455 void gdFontCacheMutexSetup();
456 void gdFontCacheMutexShutdown();
457 
458 /* 2.0.16: for thread-safe use of gdImageStringFT and friends,
459  * call this before allowing any thread to call gdImageStringFT.
460  * Otherwise it is invoked by the first thread to invoke
461  * gdImageStringFT, with a very small but real risk of a race condition.
462  * Return 0 on success, nonzero on failure to initialize freetype.
463  */
464 int gdFontCacheSetup(void);
465 
466 /* Optional: clean up after application is done using fonts in gdImageStringFT(). */
467 void gdFontCacheShutdown(void);
468 
469 /* Calls gdImageStringFT. Provided for backwards compatibility only. */
470 char *gdImageStringTTF(gdImage *im, int *brect, int fg, char *fontlist,
471                 double ptsize, double angle, int x, int y, char *string);
472 
473 /* FreeType 2 text output */
474 char *gdImageStringFT(gdImage *im, int *brect, int fg, char *fontlist,
475                 double ptsize, double angle, int x, int y, char *string);
476 
477 typedef struct {
478 	double linespacing;	/* fine tune line spacing for '\n' */
479 	int flags;		/* Logical OR of gdFTEX_ values */
480 	int charmap;		/* TBB: 2.0.12: may be gdFTEX_Unicode,
481 				   gdFTEX_Shift_JIS, gdFTEX_Big5 or gdFTEX_MacRoman;
482 				   when not specified, maps are searched
483 				   for in the above order. */
484 	int hdpi;
485 	int vdpi;
486 }
487  gdFTStringExtra, *gdFTStringExtraPtr;
488 
489 #define gdFTEX_LINESPACE 1
490 #define gdFTEX_CHARMAP 2
491 #define gdFTEX_RESOLUTION 4
492 
493 /* These are NOT flags; set one in 'charmap' if you set the gdFTEX_CHARMAP bit in 'flags'. */
494 #define gdFTEX_Unicode 0
495 #define gdFTEX_Shift_JIS 1
496 #define gdFTEX_Big5 2
497 #define gdFTEX_MacRoman 3
498 
499 /* FreeType 2 text output with fine tuning */
500 char *
501 gdImageStringFTEx(gdImage * im, int *brect, int fg, char * fontlist,
502 		double ptsize, double angle, int x, int y, char * string,
503 		gdFTStringExtraPtr strex);
504 
505 
506 /* Point type for use in polygon drawing. */
507 typedef struct {
508 	int x, y;
509 } gdPoint, *gdPointPtr;
510 
511 void gdImagePolygon(gdImagePtr im, gdPointPtr p, int n, int c);
512 void gdImageOpenPolygon(gdImagePtr im, gdPointPtr p, int n, int c);
513 void gdImageFilledPolygon(gdImagePtr im, gdPointPtr p, int n, int c);
514 
515 /* These functions still work with truecolor images,
516 	for which they never return error. */
517 int gdImageColorAllocate(gdImagePtr im, int r, int g, int b);
518 /* gd 2.0: palette entries with non-opaque transparency are permitted. */
519 int gdImageColorAllocateAlpha(gdImagePtr im, int r, int g, int b, int a);
520 /* Assumes opaque is the preferred alpha channel value */
521 int gdImageColorClosest(gdImagePtr im, int r, int g, int b);
522 /* Closest match taking all four parameters into account.
523 	A slightly different color with the same transparency
524 	beats the exact same color with radically different
525 	transparency */
526 int gdImageColorClosestAlpha(gdImagePtr im, int r, int g, int b, int a);
527 /* An alternate method */
528 int gdImageColorClosestHWB(gdImagePtr im, int r, int g, int b);
529 /* Returns exact, 100% opaque matches only */
530 int gdImageColorExact(gdImagePtr im, int r, int g, int b);
531 /* Returns an exact match only, including alpha */
532 int gdImageColorExactAlpha(gdImagePtr im, int r, int g, int b, int a);
533 /* Opaque only */
534 int gdImageColorResolve(gdImagePtr im, int r, int g, int b);
535 /* Based on gdImageColorExactAlpha and gdImageColorClosestAlpha */
536 int gdImageColorResolveAlpha(gdImagePtr im, int r, int g, int b, int a);
537 
538 /* A simpler way to obtain an opaque truecolor value for drawing on a
539 	truecolor image. Not for use with palette images! */
540 
541 #define gdTrueColor(r, g, b) (((r) << 16) + \
542 	((g) << 8) + \
543 	(b))
544 
545 /* Returns a truecolor value with an alpha channel component.
546 	gdAlphaMax (127, **NOT 255**) is transparent, 0 is completely
547 	opaque. */
548 
549 #define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + \
550 	((r) << 16) + \
551 	((g) << 8) + \
552 	(b))
553 
554 void gdImageColorDeallocate(gdImagePtr im, int color);
555 
556 /* Converts a truecolor image to a palette-based image,
557 	using a high-quality two-pass quantization routine
558 	which attempts to preserve alpha channel information
559 	as well as R/G/B color information when creating
560 	a palette. If ditherFlag is set, the image will be
561 	dithered to approximate colors better, at the expense
562 	of some obvious "speckling." colorsWanted can be
563 	anything up to 256. If the original source image
564 	includes photographic information or anything that
565 	came out of a JPEG, 256 is strongly recommended.
566 
567 	Better yet, don't use this function -- write real
568 	truecolor PNGs and JPEGs. The disk space gain of
569         conversion to palette is not great (for small images
570         it can be negative) and the quality loss is ugly. */
571 
572 gdImagePtr gdImageCreatePaletteFromTrueColor (gdImagePtr im, int ditherFlag, int colorsWanted);
573 
574 int gdImageTrueColorToPalette(gdImagePtr im, int ditherFlag, int colorsWanted);
575 int gdImagePaletteToTrueColor(gdImagePtr src);
576 
577 /* An attempt at getting the results of gdImageTrueColorToPalette
578 	to look a bit more like the original (im1 is the original
579 	and im2 is the palette version */
580 int gdImageColorMatch(gdImagePtr im1, gdImagePtr im2);
581 
582 /* Specifies a color index (if a palette image) or an
583 	RGB color (if a truecolor image) which should be
584 	considered 100% transparent. FOR TRUECOLOR IMAGES,
585 	THIS IS IGNORED IF AN ALPHA CHANNEL IS BEING
586 	SAVED. Use gdImageSaveAlpha(im, 0); to
587 	turn off the saving of a full alpha channel in
588 	a truecolor image. Note that gdImageColorTransparent
589 	is usually compatible with older browsers that
590 	do not understand full alpha channels well. TBB */
591 void gdImageColorTransparent(gdImagePtr im, int color);
592 
593 void gdImagePaletteCopy(gdImagePtr dst, gdImagePtr src);
594 void gdImagePng(gdImagePtr im, FILE *out);
595 void gdImagePngCtx(gdImagePtr im, gdIOCtx *out);
596 void gdImageGif(gdImagePtr im, FILE *out);
597 void gdImageGifCtx(gdImagePtr im, gdIOCtx *out);
598 
599 void * gdImageBmpPtr(gdImagePtr im, int *size, int compression);
600 void gdImageBmp(gdImagePtr im, FILE *outFile, int compression);
601 void gdImageBmpCtx(gdImagePtr im, gdIOCtxPtr out, int compression);
602 
603 /* 2.0.12: Compression level: 0-9 or -1, where 0 is NO COMPRESSION at all,
604  * 1 is FASTEST but produces larger files, 9 provides the best
605  * compression (smallest files) but takes a long time to compress, and
606  * -1 selects the default compiled into the zlib library.
607  */
608 void gdImagePngEx(gdImagePtr im, FILE * out, int level, int basefilter);
609 void gdImagePngCtxEx(gdImagePtr im, gdIOCtx * out, int level, int basefilter);
610 
611 void gdImageWBMP(gdImagePtr image, int fg, FILE *out);
612 void gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
613 
614 /* Guaranteed to correctly free memory returned
615 	by the gdImage*Ptr functions */
616 void gdFree(void *m);
617 
618 /* Best to free this memory with gdFree(), not free() */
619 void *gdImageWBMPPtr(gdImagePtr im, int *size, int fg);
620 
621 /* 100 is highest quality (there is always a little loss with JPEG).
622        0 is lowest. 10 is about the lowest useful setting. */
623 void gdImageJpeg(gdImagePtr im, FILE *out, int quality);
624 void gdImageJpegCtx(gdImagePtr im, gdIOCtx *out, int quality);
625 
626 void gdImageWebpCtx (gdImagePtr im, gdIOCtx * outfile, int quantization);
627 
628 /* Best to free this memory with gdFree(), not free() */
629 void *gdImageJpegPtr(gdImagePtr im, int *size, int quality);
630 
631 gdImagePtr gdImageCreateFromGif(FILE *fd);
632 gdImagePtr gdImageCreateFromGifCtx(gdIOCtxPtr in);
633 gdImagePtr gdImageCreateFromGifSource(gdSourcePtr in);
634 
635 /* A custom data sink. For backwards compatibility. Use
636 	gdIOCtx instead. */
637 /* The sink function must return -1 on error, otherwise the number
638         of bytes written, which must be equal to len. */
639 /* context will be passed to your sink function. */
640 typedef struct {
641         int (*sink) (void *context, const char *buffer, int len);
642         void *context;
643 } gdSink, *gdSinkPtr;
644 
645 void gdImagePngToSink(gdImagePtr im, gdSinkPtr out);
646 
647 void gdImageGd(gdImagePtr im, FILE *out);
648 void gdImageGd2(gdImagePtr im, FILE *out, int cs, int fmt);
649 
650 /* Best to free this memory with gdFree(), not free() */
651 void* gdImagePngPtr(gdImagePtr im, int *size);
652 
653 /* Best to free this memory with gdFree(), not free() */
654 void* gdImageGdPtr(gdImagePtr im, int *size);
655 void *gdImagePngPtrEx(gdImagePtr im, int *size, int level, int basefilter);
656 
657 /* Best to free this memory with gdFree(), not free() */
658 void* gdImageGd2Ptr(gdImagePtr im, int cs, int fmt, int *size);
659 
660 void gdImageEllipse(gdImagePtr im, int cx, int cy, int w, int h, int c);
661 
662 /* Style is a bitwise OR ( | operator ) of these.
663 	gdArc and gdChord are mutually exclusive;
664 	gdChord just connects the starting and ending
665 	angles with a straight line, while gdArc produces
666 	a rounded edge. gdPie is a synonym for gdArc.
667 	gdNoFill indicates that the arc or chord should be
668 	outlined, not filled. gdEdged, used together with
669 	gdNoFill, indicates that the beginning and ending
670 	angles should be connected to the center; this is
671 	a good way to outline (rather than fill) a
672 	'pie slice'. */
673 #define gdArc   0
674 #define gdPie   gdArc
675 #define gdChord 1
676 #define gdNoFill 2
677 #define gdEdged 4
678 
679 void gdImageFilledArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color, int style);
680 void gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color);
681 void gdImageFilledEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color);
682 void gdImageFillToBorder(gdImagePtr im, int x, int y, int border, int color);
683 void gdImageFill(gdImagePtr im, int x, int y, int color);
684 void gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h);
685 void gdImageCopyMerge(gdImagePtr dst, gdImagePtr src, int dstX, int dstY,
686 			int srcX, int srcY, int w, int h, int pct);
687 void gdImageCopyMergeGray(gdImagePtr dst, gdImagePtr src, int dstX, int dstY,
688                         int srcX, int srcY, int w, int h, int pct);
689 
690 /* Stretches or shrinks to fit, as needed. Does NOT attempt
691 	to average the entire set of source pixels that scale down onto the
692 	destination pixel. */
693 void gdImageCopyResized(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);
694 
695 /* gd 2.0: stretches or shrinks to fit, as needed. When called with a
696 	truecolor destination image, this function averages the
697 	entire set of source pixels that scale down onto the
698 	destination pixel, taking into account what portion of the
699 	destination pixel each source pixel represents. This is a
700 	floating point operation, but this is not a performance issue
701 	on modern hardware, except for some embedded devices. If the
702 	destination is a palette image, gdImageCopyResized is
703 	substituted automatically. */
704 void gdImageCopyResampled(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);
705 
706 gdImagePtr gdImageRotate90(gdImagePtr src, int ignoretransparent);
707 gdImagePtr gdImageRotate180(gdImagePtr src, int ignoretransparent);
708 gdImagePtr gdImageRotate270(gdImagePtr src, int ignoretransparent);
709 gdImagePtr gdImageRotateInterpolated(const gdImagePtr src, const float angle, int bgcolor);
710 
711 void gdImageSetBrush(gdImagePtr im, gdImagePtr brush);
712 void gdImageSetTile(gdImagePtr im, gdImagePtr tile);
713 void gdImageSetAntiAliased(gdImagePtr im, int c);
714 void gdImageSetAntiAliasedDontBlend(gdImagePtr im, int c, int dont_blend);
715 void gdImageSetStyle(gdImagePtr im, int *style, int noOfPixels);
716 /* Line thickness (defaults to 1). Affects lines, ellipses,
717 	rectangles, polygons and so forth. */
718 void gdImageSetThickness(gdImagePtr im, int thickness);
719 /* On or off (1 or 0) for all three of these. */
720 void gdImageInterlace(gdImagePtr im, int interlaceArg);
721 void gdImageAlphaBlending(gdImagePtr im, int alphaBlendingArg);
722 void gdImageAntialias(gdImagePtr im, int antialias);
723 void gdImageSaveAlpha(gdImagePtr im, int saveAlphaArg);
724 
725 enum gdPixelateMode {
726 	GD_PIXELATE_UPPERLEFT,
727 	GD_PIXELATE_AVERAGE
728 };
729 
730 int gdImagePixelate(gdImagePtr im, int block_size, const unsigned int mode);
731 
732 /* Macros to access information about images. */
733 
734 /* Returns nonzero if the image is a truecolor image,
735 	zero for a palette image. */
736 
737 #define gdImageTrueColor(im) ((im)->trueColor)
738 
739 #define gdImageSX(im) ((im)->sx)
740 #define gdImageSY(im) ((im)->sy)
741 #define gdImageColorsTotal(im) ((im)->colorsTotal)
742 #define gdImageRed(im, c) ((im)->trueColor ? gdTrueColorGetRed(c) : \
743 	(im)->red[(c)])
744 #define gdImageGreen(im, c) ((im)->trueColor ? gdTrueColorGetGreen(c) : \
745 	(im)->green[(c)])
746 #define gdImageBlue(im, c) ((im)->trueColor ? gdTrueColorGetBlue(c) : \
747 	(im)->blue[(c)])
748 #define gdImageAlpha(im, c) ((im)->trueColor ? gdTrueColorGetAlpha(c) : \
749 	(im)->alpha[(c)])
750 #define gdImageGetTransparent(im) ((im)->transparent)
751 #define gdImageGetInterlaced(im) ((im)->interlace)
752 
753 /* These macros provide direct access to pixels in
754 	palette-based and truecolor images, respectively.
755 	If you use these macros, you must perform your own
756 	bounds checking. Use of the macro for the correct type
757 	of image is also your responsibility. */
758 #define gdImagePalettePixel(im, x, y) (im)->pixels[(y)][(x)]
759 #define gdImageTrueColorPixel(im, x, y) (im)->tpixels[(y)][(x)]
760 #define gdImageResolutionX(im) (im)->res_x
761 #define gdImageResolutionY(im) (im)->res_y
762 
763 /* I/O Support routines. */
764 
765 gdIOCtx* gdNewFileCtx(FILE*);
766 gdIOCtx* gdNewDynamicCtx(int, void*);
767 gdIOCtx *gdNewDynamicCtxEx(int size, void *data, int freeFlag);
768 gdIOCtx* gdNewSSCtx(gdSourcePtr in, gdSinkPtr out);
769 void* gdDPExtractData(struct gdIOCtx* ctx, int *size);
770 
771 #define GD2_CHUNKSIZE           128
772 #define GD2_CHUNKSIZE_MIN	64
773 #define GD2_CHUNKSIZE_MAX       4096
774 
775 #define GD2_VERS                2
776 #define GD2_ID                  "gd2"
777 #define GD2_FMT_RAW             1
778 #define GD2_FMT_COMPRESSED      2
779 
780 
781 /* filters section
782  *
783  * Negate the imag src, white becomes black,
784  * The red, green, and blue intensities of an image are negated.
785  * White becomes black, yellow becomes blue, etc.
786  */
787 int gdImageNegate(gdImagePtr src);
788 
789 /* Convert the image src to a grayscale image */
790 int gdImageGrayScale(gdImagePtr src);
791 
792 /* Set the brightness level <brightness> for the image src */
793 int gdImageBrightness(gdImagePtr src, int brightness);
794 
795 /* Set the contrast level <contrast> for the image <src> */
796 int gdImageContrast(gdImagePtr src, double contrast);
797 
798 /* Simply adds or subtracts respectively red, green or blue to a pixel */
799 int gdImageColor(gdImagePtr src, const int red, const int green, const int blue, const int alpha);
800 
801 /* Image convolution by a 3x3 custom matrix */
802 int gdImageConvolution(gdImagePtr src, float ft[3][3], float filter_div, float offset);
803 
804 int gdImageEdgeDetectQuick(gdImagePtr src);
805 
806 int gdImageGaussianBlur(gdImagePtr im);
807 
808 int gdImageSelectiveBlur( gdImagePtr src);
809 
810 int gdImageEmboss(gdImagePtr im);
811 
812 int gdImageMeanRemoval(gdImagePtr im);
813 
814 int gdImageSmooth(gdImagePtr im, float weight);
815 
816 /* Image comparison definitions */
817 int gdImageCompare(gdImagePtr im1, gdImagePtr im2);
818 
819 void gdImageFlipHorizontal(gdImagePtr im);
820 void gdImageFlipVertical(gdImagePtr im);
821 void gdImageFlipBoth(gdImagePtr im);
822 
823 #define GD_FLIP_HORINZONTAL 1
824 #define GD_FLIP_VERTICAL 2
825 #define GD_FLIP_BOTH 3
826 
827 /**
828  * Group: Crop
829  *
830  * Constants: gdCropMode
831  *  GD_CROP_DEFAULT - Default crop mode (4 corners or background)
832  *  GD_CROP_TRANSPARENT - Crop using the transparent color
833  *  GD_CROP_BLACK - Crop black borders
834  *  GD_CROP_WHITE - Crop white borders
835  *  GD_CROP_SIDES - Crop using colors of the 4 corners
836  *
837  * See also:
838  *  <gdImageAutoCrop>
839  **/
840 enum gdCropMode {
841 	GD_CROP_DEFAULT = 0,
842 	GD_CROP_TRANSPARENT,
843 	GD_CROP_BLACK,
844 	GD_CROP_WHITE,
845 	GD_CROP_SIDES,
846 	GD_CROP_THRESHOLD
847 };
848 
849 gdImagePtr gdImageCrop(gdImagePtr src, const gdRectPtr crop);
850 gdImagePtr gdImageCropAuto(gdImagePtr im, const unsigned int mode);
851 gdImagePtr gdImageCropThreshold(gdImagePtr im, const unsigned int color, const float threshold);
852 
853 int gdImageSetInterpolationMethod(gdImagePtr im, gdInterpolationMethod id);
854 
855 gdImagePtr gdImageScaleBilinear(gdImagePtr im, const unsigned int new_width, const unsigned int new_height);
856 gdImagePtr gdImageScaleBicubic(gdImagePtr src_img, const unsigned int new_width, const unsigned int new_height);
857 gdImagePtr gdImageScaleBicubicFixed(gdImagePtr src, const unsigned int width, const unsigned int height);
858 gdImagePtr gdImageScaleNearestNeighbour(gdImagePtr im, const unsigned int width, const unsigned int height);
859 gdImagePtr gdImageScaleTwoPass(const gdImagePtr pOrigImage, const unsigned int uOrigWidth, const unsigned int uOrigHeight, const unsigned int uNewWidth, const unsigned int uNewHeight);
860 gdImagePtr gdImageScale(const gdImagePtr src, const unsigned int new_width, const unsigned int new_height);
861 
862 gdImagePtr gdImageRotateNearestNeighbour(gdImagePtr src, const float degrees, const int bgColor);
863 gdImagePtr gdImageRotateBilinear(gdImagePtr src, const float degrees, const int bgColor);
864 gdImagePtr gdImageRotateBicubicFixed(gdImagePtr src, const float degrees, const int bgColor);
865 gdImagePtr gdImageRotateGeneric(gdImagePtr src, const float degrees, const int bgColor);
866 gdImagePtr gdImageRotateInterpolated(const gdImagePtr src, const float angle, int bgcolor);
867 
868 typedef enum {
869 	GD_AFFINE_TRANSLATE = 0,
870 	GD_AFFINE_SCALE,
871 	GD_AFFINE_ROTATE,
872 	GD_AFFINE_SHEAR_HORIZONTAL,
873 	GD_AFFINE_SHEAR_VERTICAL,
874 } gdAffineStandardMatrix;
875 
876 int gdAffineApplyToPointF (gdPointFPtr dst, const gdPointFPtr src, const double affine[6]);
877 int gdAffineInvert (double dst[6], const double src[6]);
878 int gdAffineFlip (double dst_affine[6], const double src_affine[6], const int flip_h, const int flip_v);
879 int gdAffineConcat (double dst[6], const double m1[6], const double m2[6]);
880 
881 int gdAffineIdentity (double dst[6]);
882 int gdAffineScale (double dst[6], const double scale_x, const double scale_y);
883 int gdAffineRotate (double dst[6], const double angle);
884 int gdAffineShearHorizontal (double dst[6], const double angle);
885 int gdAffineShearVertical(double dst[6], const double angle);
886 int gdAffineTranslate (double dst[6], const double offset_x, const double offset_y);
887 double gdAffineExpansion (const double src[6]);
888 int gdAffineRectilinear (const double src[6]);
889 int gdAffineEqual (const double matrix1[6], const double matrix2[6]);
890 int gdTransformAffineGetImage(gdImagePtr *dst, const gdImagePtr src, gdRectPtr src_area, const double affine[6]);
891 int gdTransformAffineCopy(gdImagePtr dst, int dst_x, int dst_y, const gdImagePtr src, gdRectPtr src_region, const double affine[6]);
892 /*
893 gdTransformAffineCopy(gdImagePtr dst, int x0, int y0, int x1, int y1,
894 			const gdImagePtr src, int src_width, int src_height,
895 			const double affine[6]);
896 */
897 int gdTransformAffineBoundingBox(gdRectPtr src, const double affine[6], gdRectPtr bbox);
898 
899 
900 #define GD_CMP_IMAGE		1	/* Actual image IS different */
901 #define GD_CMP_NUM_COLORS	2	/* Number of Colours in pallette differ */
902 #define GD_CMP_COLOR		4	/* Image colours differ */
903 #define GD_CMP_SIZE_X		8	/* Image width differs */
904 #define GD_CMP_SIZE_Y		16	/* Image heights differ */
905 #define GD_CMP_TRANSPARENT	32	/* Transparent colour */
906 #define GD_CMP_BACKGROUND	64	/* Background colour */
907 #define GD_CMP_INTERLACE	128	/* Interlaced setting */
908 #define GD_CMP_TRUECOLOR	256	/* Truecolor vs palette differs */
909 
910 /* resolution affects ttf font rendering, particularly hinting */
911 #define GD_RESOLUTION           96      /* pixels per inch */
912 
913 #ifdef __cplusplus
914 }
915 #endif
916 
917 /* 2.0.12: this now checks the clipping rectangle */
918 #define gdImageBoundsSafe(im, x, y) (!((((y) < (im)->cy1) || ((y) > (im)->cy2)) || (((x) < (im)->cx1) || ((x) > (im)->cx2))))
919 
920 #endif /* GD_H */
921