Annotation of embedaddon/php/ext/gd/libgd/gdtest.c, revision 1.1.1.1

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>