Back to index

php5  5.3.10
testac.c
Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include "gd.h"
00004 
00005 /* If palette is true, we convert from truecolor to palette at the end,
00006    to test gdImageTrueColorToPalette and see file size/
00007    quality tradeoffs. */
00008 
00009 void testDrawing (
00010                  gdImagePtr im_in,
00011                  double scale,
00012                  int blending,
00013                  int palette,
00014                  char *filename);
00015 
00016 int
00017 main (int argc, char *argv[])
00018 {
00019   /* Input and output files */
00020   FILE *in;
00021   FILE *out;
00022 
00023   /* Input image */
00024   gdImagePtr im_in = 0;
00025 
00026   /* Colors */
00027   int lightBlue;
00028 
00029   if (argc != 2)
00030     {
00031       fprintf (stderr, "Usage: testac filename.png\n");
00032       exit (1);
00033     }
00034   /* Load original PNG, which should contain alpha channel
00035      information. We will use it in two ways: preserving it
00036      literally, for use with compatible browsers, and
00037      compositing it ourselves against a background of our
00038      choosing (alpha blending). We'll change its size
00039      and try creating palette versions of it. */
00040   in = fopen (argv[1], "rb");
00041   if (!in)
00042     {
00043       fprintf (stderr, "Can't load %s.\n", argv[1]);
00044       exit (1);
00045     }
00046   else
00047     {
00048       im_in = gdImageCreateFromPng (in);
00049       fclose (in);
00050     }
00051   testDrawing (im_in, 1.0, 0, 0, "noblending-fullsize-truecolor.png");
00052   testDrawing (im_in, 1.0, 1, 0, "blending-fullsize-truecolor.png");
00053   testDrawing (im_in, 0.5, 0, 0, "noblending-halfsize-truecolor.png");
00054   testDrawing (im_in, 0.5, 1, 0, "blending-halfsize-truecolor.png");
00055   testDrawing (im_in, 2.0, 0, 0, "noblending-doublesize-truecolor.png");
00056   testDrawing (im_in, 2.0, 1, 0, "blending-doublesize-truecolor.png");
00057   testDrawing (im_in, 1.0, 0, 1, "noblending-fullsize-palette.png");
00058   testDrawing (im_in, 1.0, 1, 1, "blending-fullsize-palette.png");
00059   testDrawing (im_in, 0.5, 0, 1, "noblending-halfsize-palette.png");
00060   testDrawing (im_in, 0.5, 1, 1, "blending-halfsize-palette.png");
00061   testDrawing (im_in, 2.0, 0, 1, "noblending-doublesize-palette.png");
00062   testDrawing (im_in, 2.0, 1, 1, "blending-doublesize-palette.png");
00063   gdImageDestroy (im_in);
00064   return 0;
00065 }
00066 
00067 /* If palette is true, we convert from truecolor to palette at the end,
00068    to test gdImageTrueColorToPalette and see file size/
00069    quality tradeoffs. */
00070 void
00071 testDrawing (
00072              gdImagePtr im_in,
00073              double scale,
00074              int blending,
00075              int palette,
00076              char *filename)
00077 {
00078   gdImagePtr im_out;
00079   FILE *out;
00080   /* Create output image. */
00081   im_out = gdImageCreateTrueColor ((int) (gdImageSX (im_in) * scale),
00082                                (int) (gdImageSY (im_in) * scale));
00083   /*
00084      Request alpha blending. This causes future
00085      drawing operations to perform alpha channel blending
00086      with the background, resulting in an opaque image.
00087      Without this call, pixels in the foreground color are
00088      copied literally, *including* the alpha channel value,
00089      resulting in an output image which is potentially
00090      not opaque. This flag can be set and cleared as often
00091      as desired. */
00092   gdImageAlphaBlending (im_out, blending);
00093 
00094   /* Flood with light blue. */
00095   gdImageFill (im_out, (int) (gdImageSX (im_in) * scale / 2),
00096               (int) (gdImageSY (im_in) * scale / 2),
00097               gdTrueColor (192, 192, 255));
00098   /* Copy the source image. Alpha blending should result in
00099      compositing against red. With blending turned off, the
00100      browser or viewer will composite against its preferred
00101      background, or, if it does not support an alpha channel,
00102      we will see the original colors for the pixels that
00103      ought to be transparent or semitransparent. */
00104   gdImageCopyResampled (im_out, im_in,
00105                      0, 0,
00106                      0, 0,
00107        (int) (gdImageSX (im_in) * scale), (int) (gdImageSY (im_in) * scale),
00108                      gdImageSX (im_in), gdImageSY (im_in));
00109   /* Write PNG */
00110   out = fopen (filename, "wb");
00111 
00112   /* If this image is the result of alpha channel blending,
00113      it will not contain an interesting alpha channel itself.
00114      Save a little file size by not saving the alpha channel.
00115      Otherwise the file would typically be slightly larger. */
00116   gdImageSaveAlpha (im_out, !blending);
00117 
00118   /* If requested, convert from truecolor to palette. */
00119   if (palette)
00120     {
00121       /* Dithering, 256 colors. */
00122       gdImageTrueColorToPalette (im_out, 1, 256);
00123     }
00124 
00125   gdImagePng (im_out, out);
00126   fclose (out);
00127 
00128   gdImageDestroy (im_out);
00129 }