Initial revision
[rrdtool.git] / libraries / gd1.3 / webgif.c
1 /* Bring in the gd library functions */
2 #include "gd.h"
3
4 /* Bring in standard I/O and string manipulation functions */
5 #include <stdio.h>
6 #include <string.h>
7
8 int main(int argc, char **argv)
9 {
10         FILE *in;
11         FILE *out;
12         char outFn[20];
13         int useStdinStdout=0;
14
15         /* Declare our image pointer */
16         gdImagePtr im = 0;
17         int i;
18        /* We'll clear 'no' once we know the user has made a
19                 reasonable request. */
20         int no = 1;
21         /* We'll set 'write' once we know the user's request
22                 requires that the image be written back to disk. */
23         int write = 0;
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")) {
27                 no = 1; 
28                 goto usage;
29         }
30
31         /* The last argument should be the image. Open the file. */
32         if (strcmp("-", argv[argc-1])==0) { /* - is synonymous with STDIN */
33           useStdinStdout = 1;
34           in = stdin;
35         } else {
36           in = fopen(argv[argc-1], "rb");       
37         }
38         if (!in) {
39                 fprintf(stderr,
40                         "Error: can't open file %s.\n", argv[argc-1]);
41                 exit(1);
42         }
43         /* Now load the image. */       
44         im = gdImageCreateFromGif(in);
45         fclose(in);
46         /* If the load failed, it must not be a GIF file. */
47         if (!im) {
48                 fprintf(stderr,
49                         "Error: %s is not a valid gif file.\n", argv[argc-1]);
50                 exit(1);        
51         }
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! :) */
57                   no = 1;
58                   goto usage;
59                 } else if (!strcmp(argv[i], "-i")) {
60                         if (i == (argc-2)) {
61                                 fprintf(stderr, 
62                                 "Error: -i specified without y or n.\n");
63                                 no = 1;
64                                 goto usage;
65                         }
66                         if (!strcmp(argv[i+1], "y")) {
67                                 /* Set interlace. */
68                                 gdImageInterlace(im, 1);
69                         } else if (!strcmp(argv[i+1], "n")) {
70                                 /* Clear interlace. */
71                                 gdImageInterlace(im, 0);
72                         } else {
73                                 fprintf(stderr,
74                                 "Error: -i specified without y or n.\n");
75                                 no = 1;
76                                 goto usage;
77                         }
78                         i++;
79                         no = 0;
80                         write = 1;
81                 } else if (!strcmp(argv[i], "-t")) {
82                         /* Set transparent index (or none). */
83                         int index;
84                         if (i == (argc-2)) {
85                                 fprintf(stderr,
86                 "Error: -t specified without a color table index.\n");
87                                 no = 1;
88                                 goto usage;
89                         }
90                         if (!strcmp(argv[i+1], "none")) {
91                                 /* -1 means not transparent. */
92                                 gdImageColorTransparent(im, -1);
93                         } else {
94                                 /* OK, get an integer and set the index. */
95                                 index = atoi(argv[i+1]);
96                                 gdImageColorTransparent(im, index);
97                         }
98                         i++;
99                         write = 1;
100                         no = 0;
101                 } else if (!strcmp(argv[i], "-l")) {
102                         /* List the colors in the color table. */
103                         int j;
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",
109                                         j, 
110                                         gdImageRed(im, j),
111                                         gdImageGreen(im, j),
112                                         gdImageBlue(im, j));
113                         }
114                         no = 0;
115                 } else if (!strcmp(argv[i], "-d")) {
116                         /* Output dimensions, etc. */
117                         int t;
118                         printf("Width: %d Height: %d Colors: %d\n",
119                                 gdImageSX(im), gdImageSY(im),
120                                 gdImageColorsTotal(im));
121                         t = gdImageGetTransparent(im);
122                         if (t != (-1)) {
123                                 printf("Transparent index: %d\n", t);
124                         } else {
125                                 /* -1 means the image is not transparent. */
126                                 printf("Transparent index: none\n");
127                         }
128                         if (gdImageGetInterlaced(im)) {
129                                 printf("Interlaced: yes\n");    
130                         } else {
131                                 printf("Interlaced: no\n");     
132                         }
133                         no = 0;
134                 } else {
135                         fprintf(stderr, "Unknown argument: %s\n", argv[i]);
136                         break;  
137                 }
138         }
139 usage:
140         if (no) {
141                 /* If the command failed, output an explanation. */
142                 fprintf(stderr, 
143 "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n"
144
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"
149 "\n"
150 "If you specify '-' as the input file, stdin/stdout will be used input/output.\n"
151 );
152         } 
153         if (write) {
154           if (useStdinStdout) {
155             out = stdout;
156           } else {
157             /* Open a temporary file. */
158
159             /* "temp.tmp" is not good temporary filename. */
160             sprintf(outFn, "webgif.tmp%d", getpid());
161             out = fopen(outFn, "wb"); 
162
163             if (!out) {
164               fprintf(stderr,
165                       "Unable to write to %s -- exiting\n", outFn);
166               exit(1);
167             }
168           }
169
170           /* Write the new gif. */
171           gdImageGif(im, out);
172
173           if (!useStdinStdout) {
174             fclose(out);
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) {
179               perror("rename");
180               exit(1);
181             }
182           }
183         }
184         /* Delete the image from memory. */
185         if (im) {
186                 gdImageDestroy(im);
187         }
188         /* All's well that ends well. */
189         return 0;
190 }
191