1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 
5 #include <stdio.h>
6 #include <math.h>
7 #include <stdlib.h>
8 #include "gd.h"
9 #include "gdfontt.h"
10 #include "gdfonts.h"
11 #include "gdfontmb.h"
12 #include "gdfontl.h"
13 #include "gdfontg.h"
14 
15 int
main(void)16 main (void)
17 {
18 	/* Input and output files */
19 	FILE *in;
20 	FILE *out;
21 
22 	/* Input and output images */
23 	gdImagePtr im_in = 0, im_out = 0;
24 
25 	/* Brush image */
26 	gdImagePtr brush;
27 
28 	/* Color indexes */
29 	int white;
30 	int blue;
31 	int red;
32 	int green;
33 
34 	/* Points for polygon */
35 	gdPoint points[3];
36 	int i;
37 
38 	/* gd fonts for font test */
39 	gdFontPtr fonts[5];
40 	int y;
41 
42 	/* Create output image, in true color. */
43 	im_out = gdImageCreateTrueColor (256 + 384, 384);
44 	/* 2.0.2: first color allocated would automatically be background in a
45 	   palette based image. Since this is a truecolor image, with an
46 	   automatic background of black, we must fill it explicitly. */
47 	white = gdImageColorAllocate (im_out, 255, 255, 255);
48 	gdImageFilledRectangle (im_out, 0, 0, gdImageSX (im_out),
49 	                        gdImageSY (im_out), white);
50 
51 	/* Set transparent color. */
52 	gdImageColorTransparent (im_out, white);
53 
54 	/* Try to load demoin.png and paste part of it into the
55 	   output image. */
56 	in = fopen ("demoin.png", "rb");
57 	if (!in) {
58 		fprintf(stderr, "Can't load source image; this demo\n");
59 		fprintf(stderr, "is much more impressive if demoin.png\n");
60 		fprintf(stderr, "is available.\n");
61 		im_in = 0;
62 	} else {
63 		int a;
64 		im_in = gdImageCreateFromPng (in);
65 		fclose (in);
66 		/* Now copy, and magnify as we do so */
67 		gdImageCopyResampled (im_out, im_in, 32, 32, 0, 0, 192, 192, 255, 255);
68 		/* Now display variously rotated space shuttles in a circle of our own */
69 		for (a = 0; (a < 360); a += 45) {
70 			int cx = cos (a * .0174532925) * 128;
71 			int cy = -sin (a * .0174532925) * 128;
72 			gdImageCopyRotated (im_out, im_in,
73 			                    256 + 192 + cx, 192 + cy,
74 			                    0, 0, gdImageSX (im_in), gdImageSY (im_in), a);
75 		}
76 	}
77 	red = gdImageColorAllocate (im_out, 255, 0, 0);
78 	green = gdImageColorAllocate (im_out, 0, 255, 0);
79 	blue = gdImageColorAllocate (im_out, 0, 0, 255);
80 	/* Fat Rectangle */
81 	gdImageSetThickness (im_out, 4);
82 	gdImageLine (im_out, 16, 16, 240, 16, green);
83 	gdImageLine (im_out, 240, 16, 240, 240, green);
84 	gdImageLine (im_out, 240, 240, 16, 240, green);
85 	gdImageLine (im_out, 16, 240, 16, 16, green);
86 	gdImageSetThickness (im_out, 1);
87 	/* Circle */
88 	gdImageArc (im_out, 128, 128, 60, 20, 0, 720, blue);
89 	/* Arc */
90 	gdImageArc (im_out, 128, 128, 40, 40, 90, 270, blue);
91 	/* Flood fill: doesn't do much on a continuously
92 	   variable tone jpeg original. */
93 	gdImageFill (im_out, 8, 8, blue);
94 	/* Polygon */
95 	points[0].x = 64;
96 	points[0].y = 0;
97 	points[1].x = 0;
98 	points[1].y = 128;
99 	points[2].x = 128;
100 	points[2].y = 128;
101 	gdImageFilledPolygon (im_out, points, 3, green);
102 	/* 2.0.12: Antialiased Polygon */
103 	gdImageSetAntiAliased (im_out, green);
104 	for (i = 0; (i < 3); i++) {
105 		points[i].x += 128;
106 	}
107 	gdImageFilledPolygon (im_out, points, 3, gdAntiAliased);
108 	/* Brush. A fairly wild example also involving a line style! */
109 	if (im_in) {
110 		int style[8];
111 		brush = gdImageCreateTrueColor (16, 16);
112 		gdImageCopyResized (brush, im_in,
113 		                    0, 0, 0, 0,
114 		                    gdImageSX (brush), gdImageSY (brush),
115 		                    gdImageSX (im_in), gdImageSY (im_in));
116 		gdImageSetBrush (im_out, brush);
117 		/* With a style, so they won't overprint each other.
118 		   Normally, they would, yielding a fat-brush effect. */
119 		style[0] = 0;
120 		style[1] = 0;
121 		style[2] = 0;
122 		style[3] = 0;
123 		style[4] = 0;
124 		style[5] = 0;
125 		style[6] = 0;
126 		style[7] = 1;
127 		gdImageSetStyle (im_out, style, 8);
128 		/* Draw the styled, brushed line */
129 		gdImageLine (im_out, 0, 255, 255, 0, gdStyledBrushed);
130 	}
131 	/* Text (non-truetype; see gdtestft for a freetype demo) */
132 	fonts[0] = gdFontGetTiny ();
133 	fonts[1] = gdFontGetSmall ();
134 	fonts[2] = gdFontGetMediumBold ();
135 	fonts[3] = gdFontGetLarge ();
136 	fonts[4] = gdFontGetGiant ();
137 	y = 0;
138 	for (i = 0; (i <= 4); i++) {
139 		gdImageString (im_out, fonts[i], 32, 32 + y, (unsigned char *) "hi",
140 		               red);
141 		y += fonts[i]->h;
142 	}
143 	y = 0;
144 	for (i = 0; (i <= 4); i++) {
145 		gdImageStringUp (im_out, fonts[i], 64 + y, 64,
146 		                 (unsigned char *) "hi", red);
147 		y += fonts[i]->h;
148 	}
149 	/* Random antialiased lines; coordinates all over the image,
150 	   but the output will respect a small clipping rectangle */
151 	gdImageSetClip (im_out, 0, gdImageSY (im_out) - 100,
152 	                100, gdImageSY (im_out));
153 	/* Fixed seed for reproducibility of results */
154 	srand (100);
155 	for (i = 0; (i < 100); i++) {
156 		int x1 = rand () % gdImageSX (im_out);
157 		int y1 = rand () % gdImageSY (im_out);
158 		int x2 = rand () % gdImageSX (im_out);
159 		int y2 = rand () % gdImageSY (im_out);
160 		gdImageSetAntiAliased (im_out, white);
161 		gdImageLine (im_out, x1, y1, x2, y2, gdAntiAliased);
162 	}
163 	/* Make output image interlaced (progressive, in the case of JPEG) */
164 	gdImageInterlace (im_out, 1);
165 	out = fopen ("demoout.png", "wb");
166 	/* Write PNG */
167 	gdImagePng (im_out, out);
168 	fclose (out);
169 	out = fopen ("demoout.gif", "wb");
170 	/* Write GIF (2.0.28) */
171 	gdImageGif (im_out, out);
172 	fclose (out);
173 	/* 2.0.12: also write a paletteized png comparable to the gif */
174 	out = fopen ("demooutp.png", "wb");
175 	gdImageTrueColorToPalette (im_out, 0, 256);
176 	gdImagePng (im_out, out);
177 	fclose (out);
178 	gdImageDestroy (im_out);
179 	if (im_in) {
180 		gdImageDestroy (im_in);
181 	}
182 	return 0;
183 }
184