1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #ifdef _WIN32
8 #include <process.h>
9 int
unlink(const char * filename)10 unlink (const char *filename)
11 {
12 	return _unlink (filename);
13 }
14 #else
15 #include <unistd.h>		/* for getpid(), unlink() */
16 #endif
17 #include "gd.h"
18 
19 void CompareImages (char *msg, gdImagePtr im1, gdImagePtr im2);
20 
21 static int freadWrapper (void *context, char *buf, int len);
22 static int fwriteWrapper (void *context, const char *buffer, int len);
23 
24 int
main(int argc,char ** argv)25 main (int argc, char **argv)
26 {
27 	gdImagePtr im, ref, im2, im3;
28 	FILE *in, *out;
29 	void *iptr;
30 	int sz;
31 	char of[256];
32 	int colRed, colBlu;
33 	gdSource imgsrc;
34 	gdSink imgsnk;
35 	int foreground;
36 	int i;
37 	gdIOCtx *ctx;
38 
39 	if (argc != 2) {
40 		fprintf(stderr, "Usage: gdtest filename.png\n");
41 		exit (1);
42 	}
43 	in = fopen (argv[1], "rb");
44 	if (!in) {
45 		fprintf(stderr, "Input file does not exist!\n");
46 		exit (1);
47 	}
48 	im = gdImageCreateFromPng (in);
49 
50 	rewind (in);
51 	ref = gdImageCreateFromPng (in);
52 
53 	fclose (in);
54 	if (!im) {
55 		fprintf(stderr, "gdImageCreateFromPng failed.\n");
56                	exit (1);
57 	}
58 	if (!ref) {
59 		fprintf(stderr, "gdImageCreateFromPng failed.\n");
60                	exit (1);
61 	}
62 
63 	printf ("Reference File has %d Palette entries\n", ref->colorsTotal);
64 
65 	CompareImages ("Initial Versions", ref, im);
66 
67         /* */
68         /* Send to GIF File then Ptr */
69         /* */
70         sprintf(of, "%s.gif", argv[1]);
71         out = fopen(of, "wb");
72         gdImageGif(im, out);
73         fclose(out);
74 
75         in = fopen(of, "rb");
76         if (!in) {
77                 fprintf(stderr, "GIF Output file does not exist!\n");
78                 exit(1);
79         }
80         im2 = gdImageCreateFromGif(in);
81         fclose(in);
82 
83         CompareImages("GD->GIF File->GD", ref, im2);
84 
85         unlink(of);
86         gdImageDestroy(im2);
87 
88 	iptr = gdImageGifPtr(im,&sz);
89 	ctx = gdNewDynamicCtx(sz,iptr);
90 	im2 = gdImageCreateFromGifCtx(ctx);
91 
92         CompareImages("GD->GIF ptr->GD", ref, im2);
93 
94 	gdImageDestroy(im2);
95 	ctx->gd_free(ctx);
96 
97 	/* */
98 	/* Send to PNG File then Ptr */
99 	/* */
100 #ifdef VMS
101 	sprintf (of, "%s-png", argv[1]);
102 #else
103 	sprintf (of, "%s.png", argv[1]);
104 #endif
105 	out = fopen (of, "wb");
106 	if (!out) {
107 		fprintf(stderr, "PNG Output file does not exist!\n");
108 		exit (1);
109 	}
110 	gdImagePng (im, out);
111 	fclose (out);
112 
113 	in = fopen (of, "rb");
114 	if (!in) {
115 		fprintf(stderr, "PNG Output file does not exist!\n");
116 		exit (1);
117 	}
118 	im2 = gdImageCreateFromPng (in);
119 	fclose (in);
120 
121 	if (!im2) {
122 		fprintf(stderr, "gdImageCreateFromPng failed.\n");
123 		exit (1);
124 	}
125 
126 	CompareImages ("GD->PNG File->GD", ref, im2);
127 
128 	unlink (of);
129 	gdImageDestroy (im2);
130 
131 	/* 2.0.21: use the new From*Ptr functions */
132 	iptr = gdImagePngPtr (im, &sz);
133 	im2 = gdImageCreateFromPngPtr (sz, iptr);
134 	gdFree (iptr);
135 	if (!im2) {
136 		fprintf(stderr, "gdImageCreateFromPngPtr failed.\n");
137 		exit (1);
138 	}
139 	CompareImages ("GD->PNG ptr->GD", ref, im2);
140 
141 	gdImageDestroy (im2);
142 
143 	/* */
144 	/* Send to GD2 File then Ptr */
145 	/* */
146 #ifdef VMS
147 	sprintf (of, "%s-gd2", argv[1]);
148 #else
149 	sprintf (of, "%s.gd2", argv[1]);
150 #endif
151 	out = fopen (of, "wb");
152 	if (!out) {
153 		fprintf(stderr, "GD2 Output file does not exist!\n");
154 		exit (1);
155 	}
156 	gdImageGd2 (im, out, 128, 2);
157 	fclose (out);
158 
159 	in = fopen (of, "rb");
160 	if (!in) {
161 		fprintf(stderr, "GD2 Output file does not exist!\n");
162 		exit (1);
163 	}
164 	im2 = gdImageCreateFromGd2 (in);
165 	fclose (in);
166 	if (!im2) {
167 		fprintf(stderr, "gdImageCreateFromGd2 failed.\n");
168 		exit (1);
169 	}
170 	CompareImages ("GD->GD2 File->GD", ref, im2);
171 
172 	unlink (of);
173 	gdImageDestroy (im2);
174 
175 	iptr = gdImageGd2Ptr (im, 128, 2, &sz);
176 	/*printf("Got ptr %d (size %d)\n",iptr, sz); */
177 	im2 = gdImageCreateFromGd2Ptr (sz, iptr);
178 	gdFree (iptr);
179 	/*printf("Got img2 %d\n",im2); */
180 	if (!im2) {
181 		fprintf(stderr, "gdImageCreateFromGd2Ptr failed.\n");
182 		exit (1);
183 	}
184 	CompareImages ("GD->GD2 ptr->GD", ref, im2);
185 
186 	gdImageDestroy (im2);
187 
188 	/* */
189 	/* Send to GD File then Ptr */
190 	/* */
191 #ifdef VMS
192 	sprintf (of, "%s-gd", argv[1]);
193 #else
194 	sprintf (of, "%s.gd", argv[1]);
195 #endif
196 	out = fopen (of, "wb");
197 	if (!out) {
198 		fprintf(stderr, "GD Output file does not exist!\n");
199 		exit (1);
200 	}
201 	gdImageGd (im, out);
202 	fclose (out);
203 
204 	in = fopen (of, "rb");
205 	if (!in) {
206 		fprintf(stderr, "GD Output file does not exist!\n");
207 		exit (1);
208 	}
209 	im2 = gdImageCreateFromGd (in);
210 	fclose (in);
211 	if (!im2) {
212 		fprintf(stderr, "gdImageCreateFromGd failed.\n");
213 		exit (1);
214 	}
215 	CompareImages ("GD->GD File->GD", ref, im2);
216 
217 	unlink (of);
218 	gdImageDestroy (im2);
219 
220 	iptr = gdImageGdPtr (im, &sz);
221 	/*printf("Got ptr %d (size %d)\n",iptr, sz); */
222 	im2 = gdImageCreateFromGdPtr (sz, iptr);
223 	gdFree (iptr);
224 	/*printf("Got img2 %d\n",im2); */
225 	if (!im2) {
226 		fprintf(stderr, "gdImageCreateFromGdPtr failed.\n");
227 		exit (1);
228 	}
229 
230 	CompareImages ("GD->GD ptr->GD", ref, im2);
231 
232 	gdImageDestroy (im2);
233 
234 	/*
235 	 * Test gdImageCreateFromPngSource'
236 	 */
237 
238 	in = fopen (argv[1], "rb");
239 
240 	imgsrc.source = freadWrapper;
241 	imgsrc.context = in;
242 	im2 = gdImageCreateFromPngSource (&imgsrc);
243 	fclose (in);
244 
245 	if (im2 == NULL) {
246 		printf
247 		("GD Source: ERROR Null returned by gdImageCreateFromPngSource\n");
248 	} else {
249 		CompareImages ("GD Source", ref, im2);
250 		gdImageDestroy (im2);
251 	};
252 
253 
254 	/*
255 	 * Test gdImagePngToSink'
256 	 */
257 #ifdef VMS
258 	sprintf (of, "%s-snk", argv[1]);
259 #else
260 	sprintf (of, "%s.snk", argv[1]);
261 #endif
262 	out = fopen (of, "wb");
263 	if (!out) {
264 		fprintf (stderr,
265 			"GD Sink: ERROR - GD Sink Output file does not exist!\n");
266 		exit (1);
267 	}
268 	imgsnk.sink = fwriteWrapper;
269 	imgsnk.context = out;
270 	gdImagePngToSink (im, &imgsnk);
271 	fclose (out);
272 	in = fopen (of, "rb");
273 	if (!in) {
274 		fprintf (stderr,
275 		         "GD Sink: ERROR - GD Sink Output file does not exist!\n");
276 	} else {
277 		im2 = gdImageCreateFromPng (in);
278 		fclose (in);
279 		if (!im2) {
280 			fprintf(stderr, "gdImageCreateFromPng failed.\n");
281 			exit (1);
282 		}
283 		CompareImages ("GD Sink", ref, im2);
284 		gdImageDestroy (im2);
285 	};
286 
287 	unlink (of);
288 
289 	/* */
290 	/*  Test Extraction */
291 	/* */
292 	in = fopen ("test/gdtest_200_300_150_100.png", "rb");
293 	if (!in) {
294 		fprintf(stderr, "gdtest_200_300_150_100.png does not exist!\n");
295 		exit (1);
296 	}
297 	im2 = gdImageCreateFromPng (in);
298 	fclose (in);
299 	if (!im2) {
300 		fprintf(stderr, "gdImageCreateFromPng failed.\n");
301 		exit (1);
302 	}
303 
304 	in = fopen ("test/gdtest.gd2", "rb");
305 	if (!in) {
306 		fprintf(stderr, "gdtest.gd2 does not exist!\n");
307 		exit (1);
308 	}
309 	im3 = gdImageCreateFromGd2Part (in, 200, 300, 150, 100);
310 	fclose (in);
311 	if (!im3) {
312 		fprintf(stderr, "gdImageCreateFromGd2Part failed.\n");
313 		exit (1);
314 	}
315 	CompareImages ("GD2Part (gdtest_200_300_150_100.png, gdtest.gd2(part))",
316 	               im2, im3);
317 
318 	gdImageDestroy (im2);
319 	gdImageDestroy (im3);
320 
321 	/* */
322 	/*  Copy Blend */
323 	/* */
324 	in = fopen ("test/gdtest.png", "rb");
325 	if (!in) {
326 		fprintf(stderr, "gdtest.png does not exist!\n");
327 		exit (1);
328 	}
329 	im2 = gdImageCreateFromPng (in);
330 	fclose (in);
331 	if (!im2) {
332 		fprintf(stderr, "gdImageCreateFromPng failed.\n");
333 		exit (1);
334 	}
335 	im3 = gdImageCreate (100, 60);
336 	if (!im3) {
337 		fprintf(stderr, "gdImageCreate failed.\n");
338 		exit (1);
339 	}
340 	colRed = gdImageColorAllocate (im3, 255, 0, 0);
341 	colBlu = gdImageColorAllocate (im3, 0, 0, 255);
342 	gdImageFilledRectangle (im3, 0, 0, 49, 30, colRed);
343 	gdImageFilledRectangle (im3, 50, 30, 99, 59, colBlu);
344 
345 	gdImageCopyMerge (im2, im3, 150, 200, 10, 10, 90, 50, 50);
346 	gdImageCopyMerge (im2, im3, 180, 70, 10, 10, 90, 50, 50);
347 
348 	gdImageCopyMergeGray (im2, im3, 250, 160, 10, 10, 90, 50, 50);
349 	gdImageCopyMergeGray (im2, im3, 80, 70, 10, 10, 90, 50, 50);
350 
351 	gdImageDestroy (im3);
352 
353 	in = fopen ("test/gdtest_merge.png", "rb");
354 	if (!in) {
355 		fprintf(stderr, "gdtest_merge.png does not exist!\n");
356 		exit (1);
357 	}
358 	im3 = gdImageCreateFromPng (in);
359 	fclose (in);
360 	if (!im3) {
361 		fprintf(stderr, "gdImageCreateFromPng failed.\n");
362 		exit (1);
363 	}
364 	printf ("[Merged Image has %d colours]\n", im2->colorsTotal);
365 	CompareImages ("Merged (gdtest.png, gdtest_merge.png)", im2, im3);
366 
367 	out = fopen ("test/gdtest_merge_out.png", "wb");
368 	gdImagePng(im2, out);
369 	fclose(out);
370 
371 	gdImageDestroy (im2);
372 	gdImageDestroy (im3);
373 
374 #ifdef HAVE_LIBJPEG
375 	out = fopen ("test/gdtest.jpg", "wb");
376 	if (!out) {
377 		fprintf(stderr, "Can't create file test/gdtest.jpg.\n");
378 		exit (1);
379 	}
380 	gdImageJpeg (im, out, -1);
381 	fclose (out);
382 	in = fopen ("test/gdtest.jpg", "rb");
383 	if (!in) {
384 		fprintf(stderr, "Can't open file test/gdtest.jpg.\n");
385 		exit (1);
386 	}
387 	im2 = gdImageCreateFromJpeg (in);
388 	fclose (in);
389 	if (!im2) {
390 		fprintf(stderr, "gdImageCreateFromJpeg failed.\n");
391 		exit (1);
392 	}
393 	gdImageDestroy (im2);
394 	printf ("Created test/gdtest.jpg successfully. Compare this image\n"
395 	        "to the input image manually. Some difference must be\n"
396 	        "expected as JPEG is a lossy file format.\n");
397 #endif /* HAVE_LIBJPEG */
398 	/* Assume the color closest to black is the foreground
399 	   color for the B&W wbmp image. */
400 	fprintf (stderr,
401 	         "NOTE: the WBMP output image will NOT match the original unless the original\n"
402 	         "is also black and white. This is OK!\n");
403 	foreground = gdImageColorClosest (im, 0, 0, 0);
404 	fprintf(stderr, "Foreground index is %d\n", foreground);
405 	if (foreground == -1) {
406 		fprintf(stderr, "Source image has no colors, skipping wbmp test.\n");
407 	} else {
408 		out = fopen ("test/gdtest.wbmp", "wb");
409 		if (!out) {
410 			fprintf(stderr, "Can't create file test/gdtest.wbmp.\n");
411 			exit (1);
412 		}
413 		gdImageWBMP (im, foreground, out);
414 		fclose (out);
415 		in = fopen ("test/gdtest.wbmp", "rb");
416 		if (!in) {
417 			fprintf(stderr, "Can't open file test/gdtest.wbmp.\n");
418 			exit (1);
419 		}
420 		im2 = gdImageCreateFromWBMP (in);
421 		fclose (in);
422 		if (!im2) {
423 			fprintf(stderr, "gdImageCreateFromWBMP failed.\n");
424 			exit (1);
425 		}
426 		fprintf(stderr, "WBMP has %d colors\n", gdImageColorsTotal (im2));
427 		fprintf(stderr, "WBMP colors are:\n");
428 		for (i = 0; (i < gdImageColorsTotal (im2)); i++) {
429 			fprintf(stderr, "%02X%02X%02X\n",
430 			         gdImageRed (im2, i),
431 			         gdImageGreen (im2, i), gdImageBlue (im2, i));
432 		}
433 		CompareImages ("WBMP test (gdtest.png, gdtest.wbmp)", ref, im2);
434 		out = fopen ("test/gdtest_wbmp_to_png.png", "wb");
435 		if (!out) {
436 			fprintf (stderr,
437 			         "Can't create file test/gdtest_wbmp_to_png.png.\n");
438 			exit (1);
439 		}
440 		gdImagePng (im2, out);
441 		fclose (out);
442 		gdImageDestroy (im2);
443 	}
444 	gdImageDestroy (im);
445 	gdImageDestroy (ref);
446 
447 	return 0;
448 }
449 
450 void
CompareImages(char * msg,gdImagePtr im1,gdImagePtr im2)451 CompareImages (char *msg, gdImagePtr im1, gdImagePtr im2)
452 {
453 	int cmpRes;
454 
455 	cmpRes = gdImageCompare (im1, im2);
456 
457 	if (cmpRes & GD_CMP_IMAGE) {
458 		printf ("%%%s: ERROR images differ: BAD\n", msg);
459 	} else if (cmpRes != 0) {
460 		printf ("%%%s: WARNING images differ: WARNING - Probably OK\n", msg);
461 	} else {
462 		printf ("%%%s: OK\n", msg);
463 		return;
464 	}
465 
466 	if (cmpRes & (GD_CMP_SIZE_X + GD_CMP_SIZE_Y)) {
467 		printf ("-%s: INFO image sizes differ\n", msg);
468 	}
469 
470 	if (cmpRes & GD_CMP_NUM_COLORS) {
471 		printf ("-%s: INFO number of palette entries differ %d Vs. %d\n", msg,
472 		        im1->colorsTotal, im2->colorsTotal);
473 	}
474 
475 	if (cmpRes & GD_CMP_COLOR) {
476 		printf ("-%s: INFO actual colours of pixels differ\n", msg);
477 	}
478 }
479 
480 
481 static int
freadWrapper(void * context,char * buf,int len)482 freadWrapper (void *context, char *buf, int len)
483 {
484 	int got = fread (buf, 1, len, (FILE *) context);
485 	return got;
486 }
487 
488 static int
fwriteWrapper(void * context,const char * buffer,int len)489 fwriteWrapper (void *context, const char *buffer, int len)
490 {
491 	return fwrite (buffer, 1, len, (FILE *) context);
492 }
493