1 /* Bring in the gd library functions */
4 /* Bring in standard I/O and string manipulation functions */
8 int main(int argc, char **argv)
15 /* Declare our image pointer */
18 /* We'll clear 'no' once we know the user has made a
19 reasonable request. */
21 /* We'll set 'write' once we know the user's request
22 requires that the image be written back to disk. */
24 /* C programs always get at least one argument; we want at
25 least one more (the image), more in practice. */
26 if (argc < 2 || !strcmp(argv[1], "--help")) {
31 /* The last argument should be the image. Open the file. */
32 if (strcmp("-", argv[argc-1])==0) { /* - is synonymous with STDIN */
36 in = fopen(argv[argc-1], "rb");
40 "Error: can't open file %s.\n", argv[argc-1]);
43 /* Now load the image. */
44 im = gdImageCreateFromGif(in);
46 /* If the load failed, it must not be a GIF file. */
49 "Error: %s is not a valid gif file.\n", argv[argc-1]);
52 /* Consider each argument in turn. */
53 for (i=1; (i < (argc-1)); i++) {
54 /* -i turns on and off interlacing. */
55 if (!strcmp(argv[i], "--help")) {
56 /* Every program should use this for help! :) */
59 } else if (!strcmp(argv[i], "-i")) {
62 "Error: -i specified without y or n.\n");
66 if (!strcmp(argv[i+1], "y")) {
68 gdImageInterlace(im, 1);
69 } else if (!strcmp(argv[i+1], "n")) {
70 /* Clear interlace. */
71 gdImageInterlace(im, 0);
74 "Error: -i specified without y or n.\n");
81 } else if (!strcmp(argv[i], "-t")) {
82 /* Set transparent index (or none). */
86 "Error: -t specified without a color table index.\n");
90 if (!strcmp(argv[i+1], "none")) {
91 /* -1 means not transparent. */
92 gdImageColorTransparent(im, -1);
94 /* OK, get an integer and set the index. */
95 index = atoi(argv[i+1]);
96 gdImageColorTransparent(im, index);
101 } else if (!strcmp(argv[i], "-l")) {
102 /* List the colors in the color table. */
104 /* Tabs used below. */
105 printf("Index Red Green Blue\n");
106 for (j=0; (j < gdImageColorsTotal(im)); j++) {
107 /* Use access macros to learn colors. */
108 printf("%d %d %d %d\n",
115 } else if (!strcmp(argv[i], "-d")) {
116 /* Output dimensions, etc. */
118 printf("Width: %d Height: %d Colors: %d\n",
119 gdImageSX(im), gdImageSY(im),
120 gdImageColorsTotal(im));
121 t = gdImageGetTransparent(im);
123 printf("Transparent index: %d\n", t);
125 /* -1 means the image is not transparent. */
126 printf("Transparent index: none\n");
128 if (gdImageGetInterlaced(im)) {
129 printf("Interlaced: yes\n");
131 printf("Interlaced: no\n");
135 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
141 /* If the command failed, output an explanation. */
143 "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n"
145 " -i [y|n] Turns on/off interlace\n"
146 " -l Prints the table of color indexes\n"
147 " -t [index] Set the transparent color to the specified index (0-255 or none)\n"
148 " -d Reports the dimensions and other characteristics of the image.\n"
150 "If you specify '-' as the input file, stdin/stdout will be used input/output.\n"
154 if (useStdinStdout) {
157 /* Open a temporary file. */
159 /* "temp.tmp" is not good temporary filename. */
160 sprintf(outFn, "webgif.tmp%d", getpid());
161 out = fopen(outFn, "wb");
165 "Unable to write to %s -- exiting\n", outFn);
170 /* Write the new gif. */
173 if (!useStdinStdout) {
175 /* Erase the old gif. */
176 unlink(argv[argc-1]);
177 /* Rename the new to the old. */
178 if (rename(outFn, argv[argc-1])!=0) {
184 /* Delete the image from memory. */
188 /* All's well that ends well. */