Back to index

php5  5.3.10
gdtest.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #ifdef _WIN32
00003 #include <process.h>
00004 int
00005 unlink (const char *filename)
00006 {
00007   return _unlink (filename);
00008 }
00009 #else
00010 #include <unistd.h>         /* for getpid(), unlink() */
00011 #endif
00012 #include "gd.h"
00013 
00014 void CompareImages (char *msg, gdImagePtr im1, gdImagePtr im2);
00015 
00016 static int freadWrapper (void *context, char *buf, int len);
00017 static int fwriteWrapper (void *context, const char *buffer, int len);
00018 
00019 int
00020 main (int argc, char **argv)
00021 {
00022   gdImagePtr im, ref, im2, im3;
00023   FILE *in, *out;
00024   void *iptr;
00025   int sz;
00026   gdIOCtxPtr ctx;
00027   char of[256];
00028   int colRed, colBlu;
00029   gdSource imgsrc;
00030   gdSink imgsnk;
00031   int foreground;
00032   int i;
00033   if (argc != 2)
00034     {
00035       fprintf (stderr, "Usage: gdtest filename.png\n");
00036       exit (1);
00037     }
00038   in = fopen (argv[1], "rb");
00039   if (!in)
00040     {
00041       fprintf (stderr, "Input file does not exist!\n");
00042       exit (1);
00043     }
00044   im = gdImageCreateFromPng (in);
00045 
00046   rewind (in);
00047   ref = gdImageCreateFromPng (in);
00048 
00049   fclose (in);
00050 
00051   printf ("Reference File has %d Palette entries\n", ref->colorsTotal);
00052 
00053   CompareImages ("Initial Versions", ref, im);
00054 
00055 
00056   /* */
00057   /* Send to PNG File then Ptr */
00058   /* */
00059   snprintf (of, sizeof(of), "%s.png", argv[1]);
00060   out = fopen (of, "wb");
00061   gdImagePng (im, out);
00062   fclose (out);
00063 
00064   in = fopen (of, "rb");
00065   if (!in)
00066     {
00067       fprintf (stderr, "PNG Output file does not exist!\n");
00068       exit (1);
00069     }
00070   im2 = gdImageCreateFromPng (in);
00071   fclose (in);
00072 
00073   CompareImages ("GD->PNG File->GD", ref, im2);
00074 
00075   unlink (of);
00076   gdImageDestroy (im2);
00077 
00078   iptr = gdImagePngPtr (im, &sz);
00079   ctx = gdNewDynamicCtx (sz, iptr);
00080   im2 = gdImageCreateFromPngCtx (ctx);
00081 
00082   CompareImages ("GD->PNG ptr->GD", ref, im2);
00083 
00084   gdImageDestroy (im2);
00085   ctx->gd_free (ctx);
00086 
00087 
00088   /* */
00089   /* Send to GD2 File then Ptr */
00090   /* */
00091   snprintf (of, sizeof(of), "%s.gd2", argv[1]);
00092   out = fopen (of, "wb");
00093   gdImageGd2 (im, out, 128, 2);
00094   fclose (out);
00095 
00096   in = fopen (of, "rb");
00097   if (!in)
00098     {
00099       fprintf (stderr, "GD2 Output file does not exist!\n");
00100       exit (1);
00101     }
00102   im2 = gdImageCreateFromGd2 (in);
00103   fclose (in);
00104 
00105   CompareImages ("GD->GD2 File->GD", ref, im2);
00106 
00107   unlink (of);
00108   gdImageDestroy (im2);
00109 
00110   iptr = gdImageGd2Ptr (im, 128, 2, &sz);
00111   /*printf("Got ptr %d (size %d)\n",iptr, sz); */
00112   ctx = gdNewDynamicCtx (sz, iptr);
00113   /*printf("Got ctx %d\n",ctx); */
00114   im2 = gdImageCreateFromGd2Ctx (ctx);
00115   /*printf("Got img2 %d\n",im2); */
00116 
00117   CompareImages ("GD->GD2 ptr->GD", ref, im2);
00118 
00119   gdImageDestroy (im2);
00120   ctx->gd_free (ctx);
00121 
00122 
00123   /* */
00124   /* Send to GD File then Ptr */
00125   /* */
00126   snprintf (of, sizeof(of), "%s.gd", argv[1]);
00127   out = fopen (of, "wb");
00128   gdImageGd (im, out);
00129   fclose (out);
00130 
00131   in = fopen (of, "rb");
00132   if (!in)
00133     {
00134       fprintf (stderr, "GD Output file does not exist!\n");
00135       exit (1);
00136     }
00137   im2 = gdImageCreateFromGd (in);
00138   fclose (in);
00139 
00140   CompareImages ("GD->GD File->GD", ref, im2);
00141 
00142   unlink (of);
00143   gdImageDestroy (im2);
00144 
00145   iptr = gdImageGdPtr (im, &sz);
00146   /*printf("Got ptr %d (size %d)\n",iptr, sz); */
00147   ctx = gdNewDynamicCtx (sz, iptr);
00148   /*printf("Got ctx %d\n",ctx); */
00149   im2 = gdImageCreateFromGdCtx (ctx);
00150   /*printf("Got img2 %d\n",im2); */
00151 
00152   CompareImages ("GD->GD ptr->GD", ref, im2);
00153 
00154   gdImageDestroy (im2);
00155   ctx->gd_free (ctx);
00156 
00157   /*
00158      ** Test gdImageCreateFromPngSource'
00159      * */
00160 
00161   in = fopen (argv[1], "rb");
00162 
00163   imgsrc.source = freadWrapper;
00164   imgsrc.context = in;
00165   im2 = gdImageCreateFromPngSource (&imgsrc);
00166   fclose (in);
00167 
00168   if (im2 == NULL)
00169     {
00170       printf ("GD Source: ERROR Null returned by gdImageCreateFromPngSource\n");
00171     }
00172   else
00173     {
00174       CompareImages ("GD Source", ref, im2);
00175       gdImageDestroy (im2);
00176     };
00177 
00178 
00179   /*
00180      ** Test gdImagePngToSink'
00181      * */
00182 
00183   snprintf (of, sizeof(of), "%s.snk", argv[1]);
00184   out = fopen (of, "wb");
00185   imgsnk.sink = fwriteWrapper;
00186   imgsnk.context = out;
00187   gdImagePngToSink (im, &imgsnk);
00188   fclose (out);
00189   in = fopen (of, "rb");
00190   if (!in)
00191     {
00192       fprintf (stderr, "GD Sink: ERROR - GD Sink Output file does not exist!\n");
00193     }
00194   else
00195     {
00196       im2 = gdImageCreateFromPng (in);
00197       fclose (in);
00198 
00199       CompareImages ("GD Sink", ref, im2);
00200       gdImageDestroy (im2);
00201     };
00202 
00203   unlink (of);
00204 
00205   /* */
00206   /*  Test Extraction */
00207   /* */
00208   in = fopen ("test/gdtest_200_300_150_100.png", "rb");
00209   if (!in)
00210     {
00211       fprintf (stderr, "gdtest_200_300_150_100.png does not exist!\n");
00212       exit (1);
00213     }
00214   im2 = gdImageCreateFromPng (in);
00215   fclose (in);
00216 
00217 
00218   in = fopen ("test/gdtest.gd2", "rb");
00219   if (!in)
00220     {
00221       fprintf (stderr, "gdtest.gd2 does not exist!\n");
00222       exit (1);
00223     }
00224   im3 = gdImageCreateFromGd2Part (in, 200, 300, 150, 100);
00225   fclose (in);
00226 
00227   CompareImages ("GD2Part (gdtest_200_300_150_100.png, gdtest.gd2(part))", im2, im3);
00228 
00229   gdImageDestroy (im2);
00230   gdImageDestroy (im3);
00231 
00232   /* */
00233   /*  Copy Blend */
00234   /* */
00235   in = fopen ("test/gdtest.png", "rb");
00236   if (!in)
00237     {
00238       fprintf (stderr, "gdtest.png does not exist!\n");
00239       exit (1);
00240     }
00241   im2 = gdImageCreateFromPng (in);
00242   fclose (in);
00243 
00244   im3 = gdImageCreate (100, 60);
00245   colRed = gdImageColorAllocate (im3, 255, 0, 0);
00246   colBlu = gdImageColorAllocate (im3, 0, 0, 255);
00247   gdImageFilledRectangle (im3, 0, 0, 49, 30, colRed);
00248   gdImageFilledRectangle (im3, 50, 30, 99, 59, colBlu);
00249 
00250   gdImageCopyMerge (im2, im3, 150, 200, 10, 10, 90, 50, 50);
00251   gdImageCopyMerge (im2, im3, 180, 70, 10, 10, 90, 50, 50);
00252 
00253   gdImageCopyMergeGray (im2, im3, 250, 160, 10, 10, 90, 50, 50);
00254   gdImageCopyMergeGray (im2, im3, 80, 70, 10, 10, 90, 50, 50);
00255 
00256   gdImageDestroy (im3);
00257 
00258   in = fopen ("test/gdtest_merge.png", "rb");
00259   if (!in)
00260     {
00261       fprintf (stderr, "gdtest_merge.png does not exist!\n");
00262       exit (1);
00263     }
00264   im3 = gdImageCreateFromPng (in);
00265   fclose (in);
00266 
00267   printf ("[Merged Image has %d colours]\n", im2->colorsTotal);
00268   CompareImages ("Merged (gdtest.png, gdtest_merge.png)", im2, im3);
00269 
00270   gdImageDestroy (im2);
00271   gdImageDestroy (im3);
00272 
00273 #ifdef HAVE_JPEG
00274   out = fopen ("test/gdtest.jpg", "wb");
00275   if (!out)
00276     {
00277       fprintf (stderr, "Can't create file test/gdtest.jpg.\n");
00278       exit (1);
00279     }
00280   gdImageJpeg (im, out, -1);
00281   fclose (out);
00282   in = fopen ("test/gdtest.jpg", "rb");
00283   if (!in)
00284     {
00285       fprintf (stderr, "Can't open file test/gdtest.jpg.\n");
00286       exit (1);
00287     }
00288   im2 = gdImageCreateFromJpeg (in);
00289   fclose (in);
00290   if (!im2)
00291     {
00292       fprintf (stderr, "gdImageCreateFromJpeg failed.\n");
00293       exit (1);
00294     }
00295   gdImageDestroy (im2);
00296   printf ("Created test/gdtest.jpg successfully. Compare this image\n"
00297          "to the input image manually. Some difference must be\n"
00298          "expected as JPEG is a lossy file format.\n");
00299 #endif /* HAVE_JPEG */
00300   /* Assume the color closest to black is the foreground
00301      color for the B&W wbmp image. */
00302   fprintf (stderr, "NOTE: the WBMP output image will NOT match the original unless the original\n"
00303           "is also black and white. This is OK!\n");
00304   foreground = gdImageColorClosest (im, 0, 0, 0);
00305   fprintf (stderr, "Foreground index is %d\n", foreground);
00306   if (foreground == -1)
00307     {
00308       fprintf (stderr, "Source image has no colors, skipping wbmp test.\n");
00309     }
00310   else
00311     {
00312       out = fopen ("test/gdtest.wbmp", "wb");
00313       if (!out)
00314        {
00315          fprintf (stderr, "Can't create file test/gdtest.wbmp.\n");
00316          exit (1);
00317        }
00318       gdImageWBMP (im, foreground, out);
00319       fclose (out);
00320       in = fopen ("test/gdtest.wbmp", "rb");
00321       if (!in)
00322        {
00323          fprintf (stderr, "Can't open file test/gdtest.wbmp.\n");
00324          exit (1);
00325        }
00326       im2 = gdImageCreateFromWBMP (in);
00327       fprintf (stderr, "WBMP has %d colors\n", gdImageColorsTotal (im2));
00328       fprintf (stderr, "WBMP colors are:\n");
00329       for (i = 0; (i < gdImageColorsTotal (im2)); i++)
00330        {
00331          fprintf (stderr, "%02X%02X%02X\n",
00332                  gdImageRed (im2, i),
00333                  gdImageGreen (im2, i),
00334                  gdImageBlue (im2, i));
00335        }
00336       fclose (in);
00337       if (!im2)
00338        {
00339          fprintf (stderr, "gdImageCreateFromWBMP failed.\n");
00340          exit (1);
00341        }
00342       CompareImages ("WBMP test (gdtest.png, gdtest.wbmp)", ref, im2);
00343       out = fopen ("test/gdtest_wbmp_to_png.png", "wb");
00344       if (!out)
00345        {
00346          fprintf (stderr, "Can't create file test/gdtest_wbmp_to_png.png.\n");
00347          exit (1);
00348        }
00349       gdImagePng (im2, out);
00350       fclose (out);
00351       gdImageDestroy (im2);
00352     }
00353   gdImageDestroy (im);
00354   gdImageDestroy (ref);
00355 
00356   return 0;
00357 }
00358 
00359 void
00360 CompareImages (char *msg, gdImagePtr im1, gdImagePtr im2)
00361 {
00362   int cmpRes;
00363 
00364   cmpRes = gdImageCompare (im1, im2);
00365 
00366   if (cmpRes & GD_CMP_IMAGE)
00367     {
00368       printf ("%%%s: ERROR images differ: BAD\n", msg);
00369     }
00370   else if (cmpRes != 0)
00371     {
00372       printf ("%%%s: WARNING images differ: WARNING - Probably OK\n", msg);
00373     }
00374   else
00375     {
00376       printf ("%%%s: OK\n", msg);
00377       return;
00378     }
00379 
00380   if (cmpRes & (GD_CMP_SIZE_X + GD_CMP_SIZE_Y))
00381     {
00382       printf ("-%s: INFO image sizes differ\n", msg);
00383     }
00384 
00385   if (cmpRes & GD_CMP_NUM_COLORS)
00386     {
00387       printf ("-%s: INFO number of pallette entries differ %d Vs. %d\n", msg,
00388              im1->colorsTotal, im2->colorsTotal);
00389     }
00390 
00391   if (cmpRes & GD_CMP_COLOR)
00392     {
00393       printf ("-%s: INFO actual colours of pixels differ\n", msg);
00394     }
00395 }
00396 
00397 
00398 static int
00399 freadWrapper (void *context, char *buf, int len)
00400 {
00401   int got = fread (buf, 1, len, (FILE *) context);
00402   return got;
00403 }
00404 
00405 static int
00406 fwriteWrapper (void *context, const char *buffer, int len)
00407 {
00408   return fwrite (buffer, 1, len, (FILE *) context);
00409 }