4
int unlink(const char * filename) {
5
return _unlink(filename);
8
#include <unistd.h> /* for getpid(), unlink() */
12
void CompareImages(char *msg, gdImagePtr im1, gdImagePtr im2);
14
static int freadWrapper(void *context, char *buf, int len);
15
static int fwriteWrapper(void *context, const char *buffer, int len);
17
int main(int argc, char **argv)
19
gdImagePtr im, ref, im2, im3;
31
fprintf(stderr, "Usage: gdtest filename.png\n");
34
in = fopen(argv[1], "rb");
36
fprintf(stderr, "Input file does not exist!\n");
39
im = gdImageCreateFromPng(in);
42
ref = gdImageCreateFromPng(in);
46
printf("Reference File has %d Palette entries\n",ref->colorsTotal);
48
CompareImages("Initial Versions", ref, im);
52
/* Send to PNG File then Ptr */
54
sprintf(of, "%s.png", argv[1]);
55
out = fopen(of, "wb");
61
fprintf(stderr, "PNG Output file does not exist!\n");
64
im2 = gdImageCreateFromPng(in);
67
CompareImages("GD->PNG File->GD", ref, im2);
72
iptr = gdImagePngPtr(im,&sz);
73
ctx = gdNewDynamicCtx(sz,iptr);
74
im2 = gdImageCreateFromPngCtx(ctx);
76
CompareImages("GD->PNG ptr->GD", ref, im2);
83
/* Send to GD2 File then Ptr */
85
sprintf(of, "%s.gd2", argv[1]);
86
out = fopen(of, "wb");
87
gdImageGd2(im, out, 128, 2);
92
fprintf(stderr, "GD2 Output file does not exist!\n");
95
im2 = gdImageCreateFromGd2(in);
98
CompareImages("GD->GD2 File->GD", ref, im2);
103
iptr = gdImageGd2Ptr(im,128,2,&sz);
104
/*printf("Got ptr %d (size %d)\n",iptr, sz); */
105
ctx = gdNewDynamicCtx(sz,iptr);
106
/*printf("Got ctx %d\n",ctx); */
107
im2 = gdImageCreateFromGd2Ctx(ctx);
108
/*printf("Got img2 %d\n",im2); */
110
CompareImages("GD->GD2 ptr->GD", ref, im2);
117
/* Send to GD File then Ptr */
119
sprintf(of, "%s.gd", argv[1]);
120
out = fopen(of, "wb");
124
in = fopen(of, "rb");
126
fprintf(stderr, "GD Output file does not exist!\n");
129
im2 = gdImageCreateFromGd(in);
132
CompareImages("GD->GD File->GD", ref, im2);
137
iptr = gdImageGdPtr(im,&sz);
138
/*printf("Got ptr %d (size %d)\n",iptr, sz); */
139
ctx = gdNewDynamicCtx(sz,iptr);
140
/*printf("Got ctx %d\n",ctx); */
141
im2 = gdImageCreateFromGdCtx(ctx);
142
/*printf("Got img2 %d\n",im2); */
144
CompareImages("GD->GD ptr->GD", ref, im2);
150
** Test gdImageCreateFromPngSource'
153
in = fopen(argv[1], "rb");
155
imgsrc.source = freadWrapper;
157
im2 = gdImageCreateFromPngSource(&imgsrc);
161
printf("GD Source: ERROR Null returned by gdImageCreateFromPngSource\n");
163
CompareImages("GD Source", ref, im2);
169
** Test gdImagePngToSink'
172
sprintf(of, "%s.snk", argv[1]);
173
out = fopen(of, "wb");
174
imgsnk.sink = fwriteWrapper;
175
imgsnk.context = out;
176
gdImagePngToSink(im, &imgsnk);
178
in = fopen(of, "rb");
180
fprintf(stderr, "GD Sink: ERROR - GD Sink Output file does not exist!\n");
182
im2 = gdImageCreateFromPng(in);
185
CompareImages("GD Sink", ref, im2);
192
/* Test Extraction */
194
in = fopen("test/gdtest_200_300_150_100.png", "rb");
196
fprintf(stderr, "gdtest_200_300_150_100.png does not exist!\n");
199
im2 = gdImageCreateFromPng(in);
203
in = fopen("test/gdtest.gd2", "rb");
205
fprintf(stderr, "gdtest.gd2 does not exist!\n");
208
im3 = gdImageCreateFromGd2Part(in, 200, 300, 150, 100);
211
CompareImages("GD2Part (gdtest_200_300_150_100.png, gdtest.gd2(part))", im2, im3);
219
in = fopen("test/gdtest.png", "rb");
221
fprintf(stderr, "gdtest.png does not exist!\n");
224
im2 = gdImageCreateFromPng(in);
227
im3 = gdImageCreate(100, 60);
228
colRed = gdImageColorAllocate(im3, 255, 0, 0);
229
colBlu = gdImageColorAllocate(im3, 0, 0, 255);
230
gdImageFilledRectangle(im3, 0, 0, 49, 30, colRed);
231
gdImageFilledRectangle(im3, 50, 30, 99, 59, colBlu);
233
gdImageCopyMerge(im2, im3, 150, 200, 10, 10, 90, 50, 50);
234
gdImageCopyMerge(im2, im3, 180, 70, 10, 10, 90, 50, 50);
236
gdImageCopyMergeGray(im2, im3, 250, 160, 10, 10, 90, 50, 50);
237
gdImageCopyMergeGray(im2, im3, 80, 70, 10, 10, 90, 50, 50);
241
in = fopen("test/gdtest_merge.png", "rb");
243
fprintf(stderr, "gdtest_merge.png does not exist!\n");
246
im3 = gdImageCreateFromPng(in);
249
printf("[Merged Image has %d colours]\n",im2->colorsTotal);
250
CompareImages("Merged (gdtest.png, gdtest_merge.png)", im2, im3);
256
out = fopen("test/gdtest.jpg", "wb");
258
fprintf(stderr, "Can't create file test/gdtest.jpg.\n");
261
gdImageJpeg(im, out, -1);
263
in = fopen("test/gdtest.jpg", "rb");
265
fprintf(stderr, "Can't open file test/gdtest.jpg.\n");
268
im2 = gdImageCreateFromJpeg(in);
271
fprintf(stderr, "gdImageCreateFromJpeg failed.\n");
275
printf("Created test/gdtest.jpg successfully. Compare this image\n"
276
"to the input image manually. Some difference must be\n"
277
"expected as JPEG is a lossy file format.\n");
278
#endif /* HAVE_JPEG */
279
/* Assume the color closest to black is the foreground
280
color for the B&W wbmp image. */
281
fprintf(stderr, "NOTE: the WBMP output image will NOT match the original unless the original\n"
282
"is also black and white. This is OK!\n");
283
foreground = gdImageColorClosest(im, 0, 0, 0);
284
fprintf(stderr, "Foreground index is %d\n", foreground);
285
if (foreground == -1) {
286
fprintf(stderr, "Source image has no colors, skipping wbmp test.\n");
288
out = fopen("test/gdtest.wbmp", "wb");
290
fprintf(stderr, "Can't create file test/gdtest.wbmp.\n");
293
gdImageWBMP(im, foreground, out);
295
in = fopen("test/gdtest.wbmp", "rb");
297
fprintf(stderr, "Can't open file test/gdtest.wbmp.\n");
300
im2 = gdImageCreateFromWBMP(in);
301
fprintf(stderr, "WBMP has %d colors\n", gdImageColorsTotal(im2));
302
fprintf(stderr, "WBMP colors are:\n");
303
for (i = 0; (i < gdImageColorsTotal(im2)); i++) {
304
fprintf(stderr, "%02X%02X%02X\n",
306
gdImageGreen(im2, i),
307
gdImageBlue(im2, i));
311
fprintf(stderr, "gdImageCreateFromWBMP failed.\n");
314
CompareImages("WBMP test (gdtest.png, gdtest.wbmp)", ref, im2);
315
out = fopen("test/gdtest_wbmp_to_png.png", "wb");
317
fprintf(stderr, "Can't create file test/gdtest_wbmp_to_png.png.\n");
320
gdImagePng(im2, out);
330
void CompareImages(char *msg, gdImagePtr im1, gdImagePtr im2)
334
cmpRes = gdImageCompare(im1, im2);
336
if (cmpRes & GD_CMP_IMAGE) {
337
printf("%%%s: ERROR images differ: BAD\n",msg);
338
} else if (cmpRes != 0) {
339
printf("%%%s: WARNING images differ: WARNING - Probably OK\n",msg);
341
printf("%%%s: OK\n",msg);
345
if (cmpRes & (GD_CMP_SIZE_X + GD_CMP_SIZE_Y)) {
346
printf("-%s: INFO image sizes differ\n",msg);
349
if (cmpRes & GD_CMP_NUM_COLORS) {
350
printf("-%s: INFO number of pallette entries differ %d Vs. %d\n",msg,
351
im1->colorsTotal, im2->colorsTotal);
354
if (cmpRes & GD_CMP_COLOR) {
355
printf("-%s: INFO actual colours of pixels differ\n",msg);
360
static int freadWrapper(void *context, char *buf, int len)
362
int got = fread(buf, 1, len, (FILE *) context);
366
static int fwriteWrapper(void *context, const char *buffer, int len)
368
return fwrite(buffer, 1, len, (FILE *) context);