xref: /PHP-5.3/ext/gd/libgd/gd2copypal.c (revision b9c0c918)
1 
2 #include <stdio.h>
3 #include "gd.h"
4 #include <stdlib.h>
5 
6 /* A short program which converts a .png file into a .gd file, for
7    your convenience in creating images on the fly from a
8    basis image that must be loaded quickly. The .gd format
9    is not intended to be a general-purpose format. */
10 
11 int
main(int argc,char ** argv)12 main (int argc, char **argv)
13 {
14   gdImagePtr im;
15   gdImagePtr pal;
16   FILE *in, *out;
17   if (argc != 3)
18     {
19       fprintf (stderr, "Usage: gd2copypal palettefile.gd2 filename.gd2\n");
20       exit (1);
21     }
22   in = fopen (argv[1], "rb");
23   if (!in)
24     {
25       fprintf (stderr, "Palette file does not exist!\n");
26       exit (1);
27     }
28   pal = gdImageCreateFromGd2 (in);
29   fclose (in);
30   if (!pal)
31     {
32       fprintf (stderr, "Palette is not in GD2 format!\n");
33       exit (1);
34     }
35 
36   in = fopen (argv[2], "rb");
37   if (!in)
38     {
39       fprintf (stderr, "Input file does not exist!\n");
40       exit (1);
41     }
42   im = gdImageCreateFromGd2 (in);
43   fclose (in);
44   if (!im)
45     {
46       fprintf (stderr, "Input is not in GD2 format!\n");
47       exit (1);
48     }
49 
50   gdImagePaletteCopy (im, pal);
51 
52   out = fopen (argv[2], "wb");
53   if (!out)
54     {
55       fprintf (stderr, "Output file cannot be written to!\n");
56       gdImageDestroy (im);
57       exit (1);
58     }
59   gdImageGd2 (im, out, 128, 2);
60   fclose (out);
61   gdImageDestroy (pal);
62   gdImageDestroy (im);
63 
64   return 0;
65 }
66