~ubuntu-branches/ubuntu/maverick/ntop/maverick

« back to all changes in this revision

Viewing changes to gdchart0.94c/gd-1.8.3/gdtest.c

  • Committer: Bazaar Package Importer
  • Author(s): Dennis Schoen
  • Date: 2002-04-12 11:38:47 UTC
  • Revision ID: james.westby@ubuntu.com-20020412113847-4k4yydw0pzybc6g8
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#ifdef _WIN32
 
3
#include <process.h>
 
4
int unlink(const char * filename) { 
 
5
        return _unlink(filename);
 
6
}
 
7
#else 
 
8
#include <unistd.h>   /* for getpid(), unlink() */
 
9
#endif
 
10
#include "gd.h"
 
11
 
 
12
void CompareImages(char *msg, gdImagePtr im1, gdImagePtr im2);
 
13
 
 
14
static int freadWrapper(void *context, char *buf, int len);
 
15
static int fwriteWrapper(void *context, const char *buffer, int len);
 
16
 
 
17
int main(int argc, char **argv)
 
18
{
 
19
        gdImagePtr      im, ref, im2, im3;
 
20
        FILE            *in, *out;
 
21
        void            *iptr;
 
22
        int             sz;
 
23
        gdIOCtxPtr      ctx;
 
24
        char            of[256];
 
25
        int             colRed, colBlu;
 
26
        gdSource        imgsrc;
 
27
        gdSink          imgsnk;
 
28
        int             foreground;
 
29
        int             i;
 
30
        if (argc != 2) {
 
31
                fprintf(stderr, "Usage: gdtest filename.png\n");
 
32
                exit(1);
 
33
        }
 
34
        in = fopen(argv[1], "rb");
 
35
        if (!in) {
 
36
                fprintf(stderr, "Input file does not exist!\n");
 
37
                exit(1);
 
38
        }
 
39
        im = gdImageCreateFromPng(in);
 
40
 
 
41
        rewind(in);
 
42
        ref = gdImageCreateFromPng(in);
 
43
 
 
44
        fclose(in);
 
45
 
 
46
        printf("Reference File has %d Palette entries\n",ref->colorsTotal);
 
47
 
 
48
        CompareImages("Initial Versions", ref, im);
 
49
 
 
50
 
 
51
        /* */
 
52
        /* Send to PNG File then Ptr */
 
53
        /* */
 
54
        sprintf(of, "%s.png", argv[1]);
 
55
        out = fopen(of, "wb");
 
56
        gdImagePng(im, out);
 
57
        fclose(out);
 
58
 
 
59
        in = fopen(of, "rb");
 
60
        if (!in) {
 
61
                fprintf(stderr, "PNG Output file does not exist!\n");
 
62
                exit(1);
 
63
        }
 
64
        im2 = gdImageCreateFromPng(in);
 
65
        fclose(in);
 
66
 
 
67
        CompareImages("GD->PNG File->GD", ref, im2);
 
68
 
 
69
        unlink(of);
 
70
        gdImageDestroy(im2);
 
71
 
 
72
        iptr = gdImagePngPtr(im,&sz);
 
73
        ctx = gdNewDynamicCtx(sz,iptr);
 
74
        im2 = gdImageCreateFromPngCtx(ctx);
 
75
 
 
76
        CompareImages("GD->PNG ptr->GD", ref, im2);
 
77
 
 
78
        gdImageDestroy(im2);
 
79
        ctx->free(ctx);
 
80
 
 
81
 
 
82
        /* */
 
83
        /* Send to GD2 File then Ptr */
 
84
        /* */
 
85
        sprintf(of, "%s.gd2", argv[1]);
 
86
        out = fopen(of, "wb");
 
87
        gdImageGd2(im, out, 128, 2);
 
88
        fclose(out);
 
89
 
 
90
        in = fopen(of, "rb");
 
91
        if (!in) {
 
92
                fprintf(stderr, "GD2 Output file does not exist!\n");
 
93
                exit(1);
 
94
        }
 
95
        im2 = gdImageCreateFromGd2(in);
 
96
        fclose(in);
 
97
 
 
98
        CompareImages("GD->GD2 File->GD", ref, im2);
 
99
 
 
100
        unlink(of);
 
101
        gdImageDestroy(im2);
 
102
 
 
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); */
 
109
 
 
110
        CompareImages("GD->GD2 ptr->GD", ref, im2);
 
111
 
 
112
        gdImageDestroy(im2);
 
113
        ctx->free(ctx);
 
114
 
 
115
 
 
116
        /* */
 
117
        /* Send to GD File then Ptr */
 
118
        /* */
 
119
        sprintf(of, "%s.gd", argv[1]);
 
120
        out = fopen(of, "wb");
 
121
        gdImageGd(im, out);
 
122
        fclose(out);
 
123
 
 
124
        in = fopen(of, "rb");
 
125
        if (!in) {
 
126
                fprintf(stderr, "GD Output file does not exist!\n");
 
127
                exit(1);
 
128
        }
 
129
        im2 = gdImageCreateFromGd(in);
 
130
        fclose(in);
 
131
 
 
132
        CompareImages("GD->GD File->GD", ref, im2);
 
133
 
 
134
        unlink(of);
 
135
        gdImageDestroy(im2);
 
136
 
 
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); */
 
143
 
 
144
        CompareImages("GD->GD ptr->GD", ref, im2);
 
145
 
 
146
        gdImageDestroy(im2);
 
147
        ctx->free(ctx);
 
148
 
 
149
        /*
 
150
        ** Test gdImageCreateFromPngSource'
 
151
        **/
 
152
 
 
153
        in = fopen(argv[1], "rb");
 
154
 
 
155
        imgsrc.source = freadWrapper;
 
156
        imgsrc.context = in;
 
157
        im2 = gdImageCreateFromPngSource(&imgsrc);
 
158
        fclose(in);
 
159
 
 
160
        if (im2 == NULL) {
 
161
                printf("GD Source: ERROR Null returned by gdImageCreateFromPngSource\n");
 
162
        } else {
 
163
                CompareImages("GD Source", ref, im2);
 
164
                gdImageDestroy(im2);
 
165
        };
 
166
 
 
167
 
 
168
        /*
 
169
        ** Test gdImagePngToSink'
 
170
        **/
 
171
 
 
172
        sprintf(of, "%s.snk", argv[1]);
 
173
        out = fopen(of, "wb");
 
174
        imgsnk.sink = fwriteWrapper;
 
175
        imgsnk.context = out;
 
176
        gdImagePngToSink(im, &imgsnk);
 
177
        fclose(out);
 
178
        in = fopen(of, "rb");
 
179
        if (!in) {
 
180
                fprintf(stderr, "GD Sink: ERROR - GD Sink Output file does not exist!\n");
 
181
        } else {
 
182
                im2 = gdImageCreateFromPng(in);
 
183
                fclose(in);
 
184
 
 
185
                CompareImages("GD Sink", ref, im2);
 
186
                gdImageDestroy(im2);
 
187
        };
 
188
 
 
189
        unlink(of);
 
190
 
 
191
        /* */
 
192
        /*  Test Extraction */
 
193
        /* */
 
194
        in = fopen("test/gdtest_200_300_150_100.png", "rb");
 
195
        if (!in) {
 
196
                fprintf(stderr, "gdtest_200_300_150_100.png does not exist!\n");
 
197
                exit(1);
 
198
        }
 
199
        im2 = gdImageCreateFromPng(in);
 
200
        fclose(in);
 
201
 
 
202
 
 
203
        in = fopen("test/gdtest.gd2", "rb");
 
204
        if (!in) {
 
205
                fprintf(stderr, "gdtest.gd2 does not exist!\n");
 
206
                exit(1);
 
207
        }
 
208
        im3 = gdImageCreateFromGd2Part(in, 200, 300, 150, 100);
 
209
        fclose(in);
 
210
 
 
211
        CompareImages("GD2Part (gdtest_200_300_150_100.png, gdtest.gd2(part))", im2, im3);
 
212
 
 
213
        gdImageDestroy(im2);
 
214
        gdImageDestroy(im3);
 
215
 
 
216
        /* */
 
217
        /*  Copy Blend */
 
218
        /* */
 
219
        in = fopen("test/gdtest.png", "rb");
 
220
        if (!in) {
 
221
                fprintf(stderr, "gdtest.png does not exist!\n");
 
222
                exit(1);
 
223
        }
 
224
        im2 = gdImageCreateFromPng(in);
 
225
        fclose(in);
 
226
 
 
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);
 
232
 
 
233
        gdImageCopyMerge(im2, im3, 150, 200, 10, 10, 90, 50, 50);
 
234
        gdImageCopyMerge(im2, im3, 180, 70, 10, 10, 90, 50, 50);
 
235
 
 
236
        gdImageCopyMergeGray(im2, im3, 250, 160, 10, 10, 90, 50, 50);
 
237
        gdImageCopyMergeGray(im2, im3, 80, 70, 10, 10, 90, 50, 50);
 
238
 
 
239
        gdImageDestroy(im3);
 
240
 
 
241
        in = fopen("test/gdtest_merge.png", "rb");
 
242
        if (!in) {
 
243
                fprintf(stderr, "gdtest_merge.png does not exist!\n");
 
244
                exit(1);
 
245
        }
 
246
        im3 = gdImageCreateFromPng(in);
 
247
        fclose(in);
 
248
 
 
249
        printf("[Merged Image has %d colours]\n",im2->colorsTotal);
 
250
        CompareImages("Merged (gdtest.png, gdtest_merge.png)", im2, im3);
 
251
 
 
252
        gdImageDestroy(im2);
 
253
        gdImageDestroy(im3);
 
254
 
 
255
#ifdef HAVE_JPEG
 
256
        out = fopen("test/gdtest.jpg", "wb");
 
257
        if (!out) {
 
258
                fprintf(stderr, "Can't create file test/gdtest.jpg.\n");
 
259
                exit(1);
 
260
        }
 
261
        gdImageJpeg(im, out, -1);       
 
262
        fclose(out);
 
263
        in = fopen("test/gdtest.jpg", "rb");
 
264
        if (!in) {
 
265
                fprintf(stderr, "Can't open file test/gdtest.jpg.\n");
 
266
                exit(1);
 
267
        }
 
268
        im2 = gdImageCreateFromJpeg(in);
 
269
        fclose(in);
 
270
        if (!im2) {
 
271
                fprintf(stderr, "gdImageCreateFromJpeg failed.\n");
 
272
                exit(1);
 
273
        }
 
274
        gdImageDestroy(im2);    
 
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");
 
287
        } else {
 
288
                out = fopen("test/gdtest.wbmp", "wb");
 
289
                if (!out) {
 
290
                        fprintf(stderr, "Can't create file test/gdtest.wbmp.\n");
 
291
                        exit(1);
 
292
                }
 
293
                gdImageWBMP(im, foreground, out);       
 
294
                fclose(out);
 
295
                in = fopen("test/gdtest.wbmp", "rb");
 
296
                if (!in) {
 
297
                        fprintf(stderr, "Can't open file test/gdtest.wbmp.\n");
 
298
                        exit(1);
 
299
                }
 
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",
 
305
                                gdImageRed(im2, i), 
 
306
                                gdImageGreen(im2, i),
 
307
                                gdImageBlue(im2, i)); 
 
308
                }
 
309
                fclose(in);
 
310
                if (!im2) {
 
311
                        fprintf(stderr, "gdImageCreateFromWBMP failed.\n");
 
312
                        exit(1);
 
313
                }
 
314
                CompareImages("WBMP test (gdtest.png, gdtest.wbmp)", ref, im2);
 
315
                out = fopen("test/gdtest_wbmp_to_png.png", "wb");
 
316
                if (!out) {
 
317
                        fprintf(stderr, "Can't create file test/gdtest_wbmp_to_png.png.\n");
 
318
                        exit(1);
 
319
                }
 
320
                gdImagePng(im2, out);   
 
321
                fclose(out);
 
322
                gdImageDestroy(im2);    
 
323
        }
 
324
        gdImageDestroy(im);
 
325
        gdImageDestroy(ref);
 
326
 
 
327
        return 0;
 
328
}
 
329
 
 
330
void CompareImages(char *msg, gdImagePtr im1, gdImagePtr im2)
 
331
{
 
332
        int cmpRes;
 
333
 
 
334
        cmpRes = gdImageCompare(im1, im2);
 
335
 
 
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);
 
340
        } else {
 
341
                printf("%%%s: OK\n",msg);
 
342
                return;
 
343
        }
 
344
 
 
345
        if (cmpRes & (GD_CMP_SIZE_X + GD_CMP_SIZE_Y)) {
 
346
                printf("-%s: INFO image sizes differ\n",msg);
 
347
        }
 
348
 
 
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);
 
352
        }
 
353
 
 
354
        if (cmpRes & GD_CMP_COLOR) {
 
355
                printf("-%s: INFO actual colours of pixels differ\n",msg);
 
356
        }
 
357
}
 
358
 
 
359
 
 
360
static int freadWrapper(void *context, char *buf, int len)
 
361
{
 
362
        int got = fread(buf, 1, len, (FILE *) context);
 
363
        return got;
 
364
}
 
365
 
 
366
static int fwriteWrapper(void *context, const char *buffer, int len)
 
367
{
 
368
        return fwrite(buffer, 1, len, (FILE *) context);
 
369
}
 
370