1 #ifndef GD_H
2 #define GD_H 1
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 /* default fontpath for unix systems */
9 #define DEFAULT_FONTPATH "/usr/share/fonts/truetype"
10 #define PATHSEPARATOR ":"
11 
12 /* gd.h: declarations file for the graphic-draw module.
13  * Permission to use, copy, modify, and distribute this software and its
14  * documentation for any purpose and without fee is hereby granted, provided
15  * that the above copyright notice appear in all copies and that both that
16  * copyright notice and this permission notice appear in supporting
17  * documentation.  This software is provided "AS IS." Thomas Boutell and
18  * Boutell.Com, Inc. disclaim all warranties, either express or implied,
19  * including but not limited to implied warranties of merchantability and
20  * fitness for a particular purpose, with respect to this code and accompanying
21  * documentation. */
22 
23 /* stdio is needed for file I/O. */
24 #include <stdio.h>
25 #include <gd_io.h>
26 #include <gd_clip.h>
27 
28 #pragma GCC visibility push(hidden)
29 
30 /* The maximum number of palette entries in palette-based images.
31 	In the wonderful new world of gd 2.0, you can of course have
32 	many more colors when using truecolor mode. */
33 
34 #define gdMaxColors 256
35 
36 /* Image type. See functions below; you will not need to change
37 	the elements directly. Use the provided macros to
38 	access sx, sy, the color table, and colorsTotal for
39 	read-only purposes. */
40 
41 /* If 'truecolor' is set true, the image is truecolor;
42 	pixels are represented by integers, which
43 	must be 32 bits wide or more.
44 
45 	True colors are repsented as follows:
46 
47 	ARGB
48 
49 	Where 'A' (alpha channel) occupies only the
50 	LOWER 7 BITS of the MSB. This very small
51 	loss of alpha channel resolution allows gd 2.x
52 	to keep backwards compatibility by allowing
53 	signed integers to be used to represent colors,
54 	and negative numbers to represent special cases,
55 	just as in gd 1.x. */
56 
57 #define gdAlphaMax 127
58 #define gdAlphaOpaque 0
59 #define gdAlphaTransparent 127
60 #define gdRedMax 255
61 #define gdGreenMax 255
62 #define gdBlueMax 255
63 #define gdTrueColorGetAlpha(c) (((c) & 0x7F000000) >> 24)
64 #define gdTrueColorGetRed(c) (((c) & 0xFF0000) >> 16)
65 #define gdTrueColorGetGreen(c) (((c) & 0x00FF00) >> 8)
66 #define gdTrueColorGetBlue(c) ((c) & 0x0000FF)
67 
68 /* This function accepts truecolor pixel values only. The
69 	source color is composited with the destination color
70 	based on the alpha channel value of the source color.
71 	The resulting color is opaque. */
72 
73 int gdAlphaBlend(int dest, int src);
74 
75 typedef struct gdImageStruct {
76 	/* Palette-based image pixels */
77 	unsigned char ** pixels;
78 	int sx;
79 	int sy;
80 	/* These are valid in palette images only. See also
81 		'alpha', which appears later in the structure to
82 		preserve binary backwards compatibility */
83 	int colorsTotal;
84 	int red[gdMaxColors];
85 	int green[gdMaxColors];
86 	int blue[gdMaxColors];
87 	int open[gdMaxColors];
88 	/* For backwards compatibility, this is set to the
89 		first palette entry with 100% transparency,
90 		and is also set and reset by the
91 		gdImageColorTransparent function. Newer
92 		applications can allocate palette entries
93 		with any desired level of transparency; however,
94 		bear in mind that many viewers, notably
95 		many web browsers, fail to implement
96 		full alpha channel for PNG and provide
97 		support for full opacity or transparency only. */
98 	int transparent;
99 	int *polyInts;
100 	int polyAllocated;
101 	struct gdImageStruct *brush;
102 	struct gdImageStruct *tile;
103 	int brushColorMap[gdMaxColors];
104 	int tileColorMap[gdMaxColors];
105 	int styleLength;
106 	int stylePos;
107 	int *style;
108 	int interlace;
109 	/* New in 2.0: thickness of line. Initialized to 1. */
110 	int thick;
111 	/* New in 2.0: alpha channel for palettes. Note that only
112 		Macintosh Internet Explorer and (possibly) Netscape 6
113 		really support multiple levels of transparency in
114 		palettes, to my knowledge, as of 2/15/01. Most
115 		common browsers will display 100% opaque and
116 		100% transparent correctly, and do something
117 		unpredictable and/or undesirable for levels
118 		in between. TBB */
119 	int alpha[gdMaxColors];
120 	/* Truecolor flag and pixels. New 2.0 fields appear here at the
121 		end to minimize breakage of existing object code. */
122 	int trueColor;
123 	int ** tpixels;
124 	/* Should alpha channel be copied, or applied, each time a
125 		pixel is drawn? This applies to truecolor images only.
126 		No attempt is made to alpha-blend in palette images,
127 		even if semitransparent palette entries exist.
128 		To do that, build your image as a truecolor image,
129 		then quantize down to 8 bits. */
130 	int alphaBlendingFlag;
131 	/* Should the alpha channel of the image be saved? This affects
132 		PNG at the moment; other future formats may also
133 		have that capability. JPEG doesn't. */
134 	int saveAlphaFlag;
135 	gdClipSet* clip; /* See <gd_clip.h> */
136 	int * _tpixels; /* contiguous pixel array */
137 } gdImage;
138 
139 typedef gdImage * gdImagePtr;
140 
141 typedef struct {
142 	/* # of characters in font */
143 	int nchars;
144 	/* First character is numbered... (usually 32 = space) */
145 	int offset;
146 	/* Character width and height */
147 	int w;
148 	int h;
149 	/* Font data; array of characters, one row after another.
150 		Easily included in code, also easily loaded from
151 		data files. */
152 	char *data;
153 } gdFont;
154 
155 /* Text functions take these. */
156 typedef gdFont *gdFontPtr;
157 
158 /* For backwards compatibility only. Use gdImageSetStyle()
159 	for MUCH more flexible line drawing. Also see
160 	gdImageSetBrush(). */
161 #define gdDashSize 4
162 
163 /* Special colors. */
164 
165 #define gdStyled (-2)
166 #define gdBrushed (-3)
167 #define gdStyledBrushed (-4)
168 #define gdTiled (-5)
169 
170 /* NOT the same as the transparent color index.
171 	This is used in line styles only. */
172 #define gdTransparent (-6)
173 
174 /* Functions to manipulate images. */
175 
176 /* Creates a palette-based image (up to 256 colors). */
177 gdImagePtr gdImageCreate(int sx, int sy);
178 
179 /* An alternate name for the above (2.0). */
180 #define gdImageCreatePalette gdImageCreate
181 
182 /* Creates a truecolor image (millions of colors). */
183 gdImagePtr gdImageCreateTrueColor(int sx, int sy);
184 
185 /* Creates an image from various file types. These functions
186 	return a palette or truecolor image based on the
187 	nature of the file being loaded. Truecolor PNG
188 	stays truecolor; palette PNG stays palette-based;
189 	JPEG is always truecolor. */
190 gdImagePtr gdImageCreateFromPng(FILE *fd);
191 gdImagePtr gdImageCreateFromPngCtx(gdIOCtxPtr in);
192 gdImagePtr gdImageCreateFromWBMP(FILE *inFile);
193 gdImagePtr gdImageCreateFromWBMPCtx(gdIOCtx *infile);
194 gdImagePtr gdImageCreateFromJpeg(FILE *infile);
195 gdImagePtr gdImageCreateFromJpegCtx(gdIOCtx *infile);
196 
197 /* ClipSet addition */
198 void gdClipSetFree(gdImagePtr im);
199 void gdClipSetReset(gdImagePtr im);
200 void gdClipSetAdd(gdImagePtr im,gdClipRectanglePtr rect);
201 
202 /* A custom data source. */
203 /* The source function must return -1 on error, otherwise the number
204         of bytes fetched. 0 is EOF, not an error! */
205 /* context will be passed to your source function. */
206 
207 typedef struct {
208         int (*source) (void *context, char *buffer, int len);
209         void *context;
210 } gdSource, *gdSourcePtr;
211 
212 gdImagePtr gdImageCreateFromPngSource(gdSourcePtr in);
213 
214 gdImagePtr gdImageCreateFromGd(FILE *in);
215 gdImagePtr gdImageCreateFromGdCtx(gdIOCtxPtr in);
216 
217 gdImagePtr gdImageCreateFromGd2(FILE *in);
218 gdImagePtr gdImageCreateFromGd2Ctx(gdIOCtxPtr in);
219 
220 gdImagePtr gdImageCreateFromGd2Part(FILE *in, int srcx, int srcy, int w, int h);
221 gdImagePtr gdImageCreateFromGd2PartCtx(gdIOCtxPtr in, int srcx, int srcy, int w, int h);
222 
223 gdImagePtr gdImageCreateFromXbm(FILE *fd);
224 
225 void gdImageDestroy(gdImagePtr im);
226 
227 /* Replaces or blends with the background depending on the
228 	most recent call to gdImageAlphaBlending and the
229 	alpha channel value of 'color'; default is to overwrite.
230 	Tiling and line styling are also implemented
231 	here. All other gd drawing functions pass through this call,
232 	allowing for many useful effects. */
233 
234 void gdImageSetPixel(gdImagePtr im, int x, int y, int color);
235 
236 int gdImageGetPixel(gdImagePtr im, int x, int y);
237 
238 void gdImageLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
239 
240 /* For backwards compatibility only. Use gdImageSetStyle()
241 	for much more flexible line drawing. */
242 void gdImageDashedLine(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
243 /* Corners specified (not width and height). Upper left first, lower right
244  	second. */
245 void gdImageRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
246 /* Solid bar. Upper left corner first, lower right corner second. */
247 void gdImageFilledRectangle(gdImagePtr im, int x1, int y1, int x2, int y2, int color);
248 int gdImageBoundsSafe(gdImagePtr im, int x, int y);
249 void gdImageChar(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color);
250 void gdImageCharUp(gdImagePtr im, gdFontPtr f, int x, int y, int c, int color);
251 void gdImageString(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color);
252 void gdImageStringUp(gdImagePtr im, gdFontPtr f, int x, int y, unsigned char *s, int color);
253 void gdImageString16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
254 void gdImageStringUp16(gdImagePtr im, gdFontPtr f, int x, int y, unsigned short *s, int color);
255 
256 /* Calls gdImageStringFT. Provided for backwards compatibility only. */
257 char *gdImageStringTTF(gdImage *im, int *brect, int fg, char *fontlist,
258                 double ptsize, double angle, int x, int y, char *string);
259 
260 /* FreeType 2 text output */
261 char *gdImageStringFT(gdImage *im, int *brect, int fg, char *fontlist,
262                 double ptsize, double angle, int x, int y, char *string);
263 
264 /* Point type for use in polygon drawing. */
265 typedef struct {
266 	int x, y;
267 } gdPoint, *gdPointPtr;
268 
269 void gdImagePolygon(gdImagePtr im, gdPointPtr p, int n, int c);
270 void gdImageFilledPolygon(gdImagePtr im, gdPointPtr p, int n, int c);
271 
272 /* These functions still work with truecolor images,
273 	for which they never return error. */
274 int gdImageColorAllocate(gdImagePtr im, int r, int g, int b);
275 /* gd 2.0: palette entries with non-opaque transparency are permitted. */
276 int gdImageColorAllocateAlpha(gdImagePtr im, int r, int g, int b, int a);
277 /* Assumes opaque is the preferred alpha channel value */
278 int gdImageColorClosest(gdImagePtr im, int r, int g, int b);
279 /* Closest match taking all four parameters into account.
280 	A slightly different color with the same transparency
281 	beats the exact same color with radically different
282 	transparency */
283 int gdImageColorClosestAlpha(gdImagePtr im, int r, int g, int b, int a);
284 /* Returns exact, 100% opaque matches only */
285 int gdImageColorExact(gdImagePtr im, int r, int g, int b);
286 /* Returns an exact match only, including alpha */
287 int gdImageColorExactAlpha(gdImagePtr im, int r, int g, int b, int a);
288 /* Opaque only */
289 int gdImageColorResolve(gdImagePtr im, int r, int g, int b);
290 /* Based on gdImageColorExactAlpha and gdImageColorClosestAlpha */
291 int gdImageColorResolveAlpha(gdImagePtr im, int r, int g, int b, int a);
292 
293 /* A simpler way to obtain an opaque truecolor value for drawing on a
294 	truecolor image. Not for use with palette images! */
295 
296 #define gdTrueColor(r, g, b) (((r) << 16) + \
297 	((g) << 8) + \
298 	(b))
299 
300 /* Returns a truecolor value with an alpha channel component.
301 	gdAlphaMax (127, **NOT 255**) is transparent, 0 is completely
302 	opaque. */
303 
304 #define gdTrueColorAlpha(r, g, b, a) (((a) << 24) + \
305 	((r) << 16) + \
306 	((g) << 8) + \
307 	(b))
308 
309 void gdImageColorDeallocate(gdImagePtr im, int color);
310 
311 /* Specifies a color index (if a palette image) or an
312 	RGB color (if a truecolor image) which should be
313 	considered 100% transparent. FOR TRUECOLOR IMAGES,
314 	THIS IS IGNORED IF AN ALPHA CHANNEL IS BEING
315 	SAVED. Use gdImageSaveAlpha(im, 0); to
316 	turn off the saving of a full alpha channel in
317 	a truecolor image. Note that gdImageColorTransparent
318 	is usually compatible with older browsers that
319 	do not understand full alpha channels well. TBB */
320 void gdImageColorTransparent(gdImagePtr im, int color);
321 
322 void gdImagePaletteCopy(gdImagePtr dst, gdImagePtr src);
323 void gdImagePng(gdImagePtr im, FILE *out);
324 void gdImagePngCtx(gdImagePtr im, gdIOCtx *out);
325 
326 void gdImageWBMP(gdImagePtr image, int fg, FILE *out);
327 void gdImageWBMPCtx(gdImagePtr image, int fg, gdIOCtx *out);
328 
329 /* Guaranteed to correctly free memory returned
330 	by the gdImage*Ptr functions */
331 void gdFree(void *m);
332 
333 /* Best to free this memory with gdFree(), not free() */
334 void *gdImageWBMPPtr(gdImagePtr im, int *size, int fg);
335 
336 void gdImageJpeg(gdImagePtr im, FILE *out, int quality);
337 void gdImageJpegCtx(gdImagePtr im, gdIOCtx *out, int quality);
338 
339 /* Best to free this memory with gdFree(), not free() */
340 void *gdImageJpegPtr(gdImagePtr im, int *size, int quality);
341 
342 /* A custom data sink. For backwards compatibility. Use
343 	gdIOCtx instead. */
344 /* The sink function must return -1 on error, otherwise the number
345         of bytes written, which must be equal to len. */
346 /* context will be passed to your sink function. */
347 typedef struct {
348         int (*sink) (void *context, const char *buffer, int len);
349         void *context;
350 } gdSink, *gdSinkPtr;
351 
352 void gdImagePngToSink(gdImagePtr im, gdSinkPtr out);
353 
354 void gdImageGd(gdImagePtr im, FILE *out);
355 void gdImageGd2(gdImagePtr im, FILE *out, int cs, int fmt);
356 
357 /* Best to free this memory with gdFree(), not free() */
358 void* gdImageGdPtr(gdImagePtr im, int *size);
359 
360 /* Best to free this memory with gdFree(), not free() */
361 void* gdImageGd2Ptr(gdImagePtr im, int cs, int fmt, int *size);
362 
363 void gdImageEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color);
364 
365 /* Style is a bitwise OR ( | operator ) of these.
366 	gdArc and gdChord are mutually exclusive;
367 	gdChord just connects the starting and ending
368 	angles with a straight line, while gdArc produces
369 	a rounded edge. gdPie is a synonym for gdArc.
370 	gdNoFill indicates that the arc or chord should be
371 	outlined, not filled. gdEdged, used together with
372 	gdNoFill, indicates that the beginning and ending
373 	angles should be connected to the center; this is
374 	a good way to outline (rather than fill) a
375 	'pie slice'. */
376 #define gdArc   0
377 #define gdPie   gdArc
378 #define gdChord 1
379 #define gdNoFill 2
380 #define gdEdged 4
381 
382 void gdImageFilledArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color, int style);
383 void gdImageArc(gdImagePtr im, int cx, int cy, int w, int h, int s, int e, int color);
384 void gdImageFilledEllipse(gdImagePtr im, int cx, int cy, int w, int h, int color);
385 void gdImageFillToBorder(gdImagePtr im, int x, int y, int border, int color);
386 void gdImageFill(gdImagePtr im, int x, int y, int color);
387 void gdImageCopy(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int w, int h);
388 void gdImageCopyMerge(gdImagePtr dst, gdImagePtr src, int dstX, int dstY,
389 			int srcX, int srcY, int w, int h, int pct);
390 void gdImageCopyMergeGray(gdImagePtr dst, gdImagePtr src, int dstX, int dstY,
391                         int srcX, int srcY, int w, int h, int pct);
392 
393 /* Stretches or shrinks to fit, as needed. Does NOT attempt
394 	to average the entire set of source pixels that scale down onto the
395 	destination pixel. */
396 void gdImageCopyResized(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);
397 
398 /* gd 2.0: stretches or shrinks to fit, as needed. When called with a
399 	truecolor destination image, this function averages the
400 	entire set of source pixels that scale down onto the
401 	destination pixel, taking into account what portion of the
402 	destination pixel each source pixel represents. This is a
403 	floating point operation, but this is not a performance issue
404 	on modern hardware, except for some embedded devices. If the
405 	destination is a palette image, gdImageCopyResized is
406 	substituted automatically. */
407 void gdImageCopyResampled(gdImagePtr dst, gdImagePtr src, int dstX, int dstY, int srcX, int srcY, int dstW, int dstH, int srcW, int srcH);
408 
409 void gdImageSetBrush(gdImagePtr im, gdImagePtr brush);
410 void gdImageSetTile(gdImagePtr im, gdImagePtr tile);
411 void gdImageSetStyle(gdImagePtr im, int *style, int noOfPixels);
412 /* Line thickness (defaults to 1). Affects lines, ellipses,
413 	rectangles, polygons and so forth. */
414 void gdImageSetThickness(gdImagePtr im, int thickness);
415 /* On or off (1 or 0) for all three of these. */
416 void gdImageInterlace(gdImagePtr im, int interlaceArg);
417 void gdImageAlphaBlending(gdImagePtr im, int alphaBlendingArg);
418 void gdImageSaveAlpha(gdImagePtr im, int saveAlphaArg);
419 
420 /* Macros to access information about images. */
421 
422 /* Returns nonzero if the image is a truecolor image,
423 	zero for a palette image. */
424 
425 #define gdImageTrueColor(im) ((im)->trueColor)
426 
427 #define gdImageSX(im) ((im)->sx)
428 #define gdImageSY(im) ((im)->sy)
429 #define gdImageColorsTotal(im) ((im)->colorsTotal)
430 #define gdImageRed(im, c) ((im)->trueColor ? gdTrueColorGetRed(c) : \
431 	(im)->red[(c)])
432 #define gdImageGreen(im, c) ((im)->trueColor ? gdTrueColorGetGreen(c) : \
433 	(im)->green[(c)])
434 #define gdImageBlue(im, c) ((im)->trueColor ? gdTrueColorGetBlue(c) : \
435 	(im)->blue[(c)])
436 #define gdImageAlpha(im, c) ((im)->trueColor ? gdTrueColorGetAlpha(c) : \
437 	(im)->alpha[(c)])
438 #define gdImageGetTransparent(im) ((im)->transparent)
439 #define gdImageGetInterlaced(im) ((im)->interlace)
440 
441 /* These macros provide direct access to pixels in
442 	palette-based and truecolor images, respectively.
443 	If you use these macros, you must perform your own
444 	bounds checking. Use of the macro for the correct type
445 	of image is also your responsibility. */
446 #define gdImagePalettePixel(im, x, y) (im)->pixels[(y)][(x)]
447 #define gdImageTrueColorPixel(im, x, y) (im)->tpixels[(y)][(x)]
448 
449 /* I/O Support routines. */
450 
451 gdIOCtx* gdNewFileCtx(FILE*);
452 gdIOCtx* gdNewDynamicCtx(int, void*);
453 gdIOCtx* gdNewSSCtx(gdSourcePtr in, gdSinkPtr out);
454 void* gdDPExtractData(struct gdIOCtx* ctx, int *size);
455 
456 #define GD2_CHUNKSIZE           128
457 #define GD2_CHUNKSIZE_MIN	64
458 #define GD2_CHUNKSIZE_MAX       4096
459 
460 #define GD2_VERS                2
461 #define GD2_ID                  "gd2"
462 #define GD2_FMT_RAW             1
463 #define GD2_FMT_COMPRESSED      2
464 
465 /* Image comparison definitions */
466 int gdImageCompare(gdImagePtr im1, gdImagePtr im2);
467 
468 #define GD_CMP_IMAGE		1	/* Actual image IS different */
469 #define GD_CMP_NUM_COLORS	2	/* Number of Colours in pallette differ */
470 #define GD_CMP_COLOR		4	/* Image colours differ */
471 #define GD_CMP_SIZE_X		8	/* Image width differs */
472 #define GD_CMP_SIZE_Y		16	/* Image heights differ */
473 #define GD_CMP_TRANSPARENT	32	/* Transparent colour */
474 #define GD_CMP_BACKGROUND	64	/* Background colour */
475 #define GD_CMP_INTERLACE	128	/* Interlaced setting */
476 #define GD_CMP_TRUECOLOR	256	/* Truecolor vs palette differs */
477 
478 /* resolution affects ttf font rendering, particularly hinting */
479 #define GD_RESOLUTION           96      /* pixels per inch */
480 
481 #pragma GCC visibility pop
482 
483 #ifdef __cplusplus
484 }
485 #endif
486 
487 #endif /* GD_H */
488