xref: /PHP-8.3/ext/gd/libgd/testac.c (revision 36db28c8)
1 
2 #include <stdio.h>
3 #include "gd.h"
4 
5 /* If palette is true, we convert from truecolor to palette at the end,
6    to test gdImageTrueColorToPalette and see file size/
7    quality tradeoffs. */
8 
9 void testDrawing (
10 		   gdImagePtr im_in,
11 		   double scale,
12 		   int blending,
13 		   int palette,
14 		   char *filename);
15 
16 int
main(int argc,char * argv[])17 main (int argc, char *argv[])
18 {
19   /* Input and output files */
20   FILE *in;
21   FILE *out;
22 
23   /* Input image */
24   gdImagePtr im_in = 0;
25 
26   /* Colors */
27   int lightBlue;
28 
29   if (argc != 2)
30     {
31       fprintf (stderr, "Usage: testac filename.png\n");
32       exit (1);
33     }
34   /* Load original PNG, which should contain alpha channel
35      information. We will use it in two ways: preserving it
36      literally, for use with compatible browsers, and
37      compositing it ourselves against a background of our
38      choosing (alpha blending). We'll change its size
39      and try creating palette versions of it. */
40   in = fopen (argv[1], "rb");
41   if (!in)
42     {
43       fprintf (stderr, "Can't load %s.\n", argv[1]);
44       exit (1);
45     }
46   else
47     {
48       im_in = gdImageCreateFromPng (in);
49       fclose (in);
50     }
51   testDrawing (im_in, 1.0, 0, 0, "noblending-fullsize-truecolor.png");
52   testDrawing (im_in, 1.0, 1, 0, "blending-fullsize-truecolor.png");
53   testDrawing (im_in, 0.5, 0, 0, "noblending-halfsize-truecolor.png");
54   testDrawing (im_in, 0.5, 1, 0, "blending-halfsize-truecolor.png");
55   testDrawing (im_in, 2.0, 0, 0, "noblending-doublesize-truecolor.png");
56   testDrawing (im_in, 2.0, 1, 0, "blending-doublesize-truecolor.png");
57   testDrawing (im_in, 1.0, 0, 1, "noblending-fullsize-palette.png");
58   testDrawing (im_in, 1.0, 1, 1, "blending-fullsize-palette.png");
59   testDrawing (im_in, 0.5, 0, 1, "noblending-halfsize-palette.png");
60   testDrawing (im_in, 0.5, 1, 1, "blending-halfsize-palette.png");
61   testDrawing (im_in, 2.0, 0, 1, "noblending-doublesize-palette.png");
62   testDrawing (im_in, 2.0, 1, 1, "blending-doublesize-palette.png");
63   gdImageDestroy (im_in);
64   return 0;
65 }
66 
67 /* If palette is true, we convert from truecolor to palette at the end,
68    to test gdImageTrueColorToPalette and see file size/
69    quality tradeoffs. */
70 void
testDrawing(gdImagePtr im_in,double scale,int blending,int palette,char * filename)71 testDrawing (
72 	      gdImagePtr im_in,
73 	      double scale,
74 	      int blending,
75 	      int palette,
76 	      char *filename)
77 {
78   gdImagePtr im_out;
79   FILE *out;
80   /* Create output image. */
81   im_out = gdImageCreateTrueColor ((int) (gdImageSX (im_in) * scale),
82 				   (int) (gdImageSY (im_in) * scale));
83   /*
84      Request alpha blending. This causes future
85      drawing operations to perform alpha channel blending
86      with the background, resulting in an opaque image.
87      Without this call, pixels in the foreground color are
88      copied literally, *including* the alpha channel value,
89      resulting in an output image which is potentially
90      not opaque. This flag can be set and cleared as often
91      as desired. */
92   gdImageAlphaBlending (im_out, blending);
93 
94   /* Flood with light blue. */
95   gdImageFill (im_out, (int) (gdImageSX (im_in) * scale / 2),
96 	       (int) (gdImageSY (im_in) * scale / 2),
97 	       gdTrueColor (192, 192, 255));
98   /* Copy the source image. Alpha blending should result in
99      compositing against red. With blending turned off, the
100      browser or viewer will composite against its preferred
101      background, or, if it does not support an alpha channel,
102      we will see the original colors for the pixels that
103      ought to be transparent or semitransparent. */
104   gdImageCopyResampled (im_out, im_in,
105 			0, 0,
106 			0, 0,
107        (int) (gdImageSX (im_in) * scale), (int) (gdImageSY (im_in) * scale),
108 			gdImageSX (im_in), gdImageSY (im_in));
109   /* Write PNG */
110   out = fopen (filename, "wb");
111 
112   /* If this image is the result of alpha channel blending,
113      it will not contain an interesting alpha channel itself.
114      Save a little file size by not saving the alpha channel.
115      Otherwise the file would typically be slightly larger. */
116   gdImageSaveAlpha (im_out, !blending);
117 
118   /* If requested, convert from truecolor to palette. */
119   if (palette)
120     {
121       /* Dithering, 256 colors. */
122       gdImageTrueColorToPalette (im_out, 1, 256);
123     }
124 
125   gdImagePng (im_out, out);
126   fclose (out);
127 
128   gdImageDestroy (im_out);
129 }
130