1 #ifdef HAVE_CONFIG_H
2 #include "config.h"
3 #endif
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "gd.h"
8 
9 /* A short program which converts a .png file into a .gd2 file, for
10    your convenience in creating images on the fly from a
11    basis image that must be loaded quickly. The .gd2 format
12    is not intended to be a general-purpose format. */
13 
14 int
main(int argc,char ** argv)15 main (int argc, char **argv)
16 {
17 	gdImagePtr im;
18 	FILE *in, *out;
19 	int x=0, y=0, w=0, h=0;
20 	if ((argc != 3) && (argc != 7)) {
21 		fprintf (stderr,
22 		         "Usage: gd2topng filename.gd2 filename.png [srcx srcy width height]\n");
23 		fprintf (stderr,
24 		         "If the coordinates are absent,t he entire image is converted.\n");
25 		exit (1);
26 	}
27 	if (argc == 7) {
28 		x = atoi (argv[3]);
29 		y = atoi (argv[4]);
30 		w = atoi (argv[5]);
31 		h = atoi (argv[6]);
32 	}
33 	in = fopen (argv[1], "rb");
34 	if (!in) {
35 		fprintf(stderr, "Input file does not exist!\n");
36 		exit (1);
37 	}
38 	if (argc == 7) {
39 		im = gdImageCreateFromGd2Part (in, x, y, w, h);
40 	} else {
41 		im = gdImageCreateFromGd2 (in);
42 	}
43 	fclose (in);
44 	if (!im) {
45 		fprintf(stderr, "Input is not in GD2 format!\n");
46 		exit (1);
47 	}
48 	out = fopen (argv[2], "wb");
49 	if (!out) {
50 		fprintf(stderr, "Output file cannot be written to!\n");
51 		gdImageDestroy (im);
52 		exit (1);
53 	}
54 	gdImagePng (im, out);
55 	fclose (out);
56 	gdImageDestroy (im);
57 
58 	return 0;
59 }
60