1 | /* Bring in the gd library functions */
|
---|
2 | #include <stdlib.h>
|
---|
3 | #include "gd.h"
|
---|
4 |
|
---|
5 | /* Bring in standard I/O and string manipulation functions */
|
---|
6 | #include <stdio.h>
|
---|
7 | #include <string.h>
|
---|
8 |
|
---|
9 | int main(int argc, char **argv)
|
---|
10 | {
|
---|
11 | FILE *in;
|
---|
12 | FILE *out;
|
---|
13 | /* Declare our image pointer */
|
---|
14 | gdImagePtr im = 0;
|
---|
15 | int i;
|
---|
16 | /* We'll clear 'no' once we know the user has made a
|
---|
17 | reasonable request. */
|
---|
18 | int no = 1;
|
---|
19 | /* We'll set 'write' once we know the user's request
|
---|
20 | requires that the image be written back to disk. */
|
---|
21 | int write = 0;
|
---|
22 | /* C programs always get at least one argument; we want at
|
---|
23 | least one more (the image), more in practice. */
|
---|
24 | if (argc < 2) {
|
---|
25 | no = 1;
|
---|
26 | goto usage;
|
---|
27 | }
|
---|
28 | /* The last argument should be the image. Open the file. */
|
---|
29 | in = fopen(argv[argc-1], "rb");
|
---|
30 | if (!in) {
|
---|
31 | fprintf(stderr,
|
---|
32 | "Error: can't open file %s.\n", argv[argc-1]);
|
---|
33 | }
|
---|
34 | /* Now load the image. */
|
---|
35 | im = gdImageCreateFromGif(in);
|
---|
36 | fclose(in);
|
---|
37 | /* If the load failed, it must not be a GIF file. */
|
---|
38 | if (!im) {
|
---|
39 | fprintf(stderr,
|
---|
40 | "Error: %s is not a valid gif file.\n", argv[1]);
|
---|
41 | exit(1);
|
---|
42 | }
|
---|
43 | /* Consider each argument in turn. */
|
---|
44 | for (i=1; (i < (argc-1)); i++) {
|
---|
45 | /* -i turns on and off interlacing. */
|
---|
46 | if (!strcmp(argv[i], "-i")) {
|
---|
47 | if (i == (argc-2)) {
|
---|
48 | fprintf(stderr,
|
---|
49 | "Error: -i specified without y or n.\n");
|
---|
50 | no = 1;
|
---|
51 | goto usage;
|
---|
52 | }
|
---|
53 | if (!strcmp(argv[i+1], "y")) {
|
---|
54 | /* Set interlace. */
|
---|
55 | gdImageInterlace(im, 1);
|
---|
56 | } else if (!strcmp(argv[i+1], "n")) {
|
---|
57 | /* Clear interlace. */
|
---|
58 | gdImageInterlace(im, 0);
|
---|
59 | } else {
|
---|
60 | fprintf(stderr,
|
---|
61 | "Error: -i specified without y or n.\n");
|
---|
62 | no = 1;
|
---|
63 | goto usage;
|
---|
64 | }
|
---|
65 | i++;
|
---|
66 | no = 0;
|
---|
67 | write = 1;
|
---|
68 | } else if (!strcmp(argv[i], "-t")) {
|
---|
69 | /* Set transparent index (or none). */
|
---|
70 | int index;
|
---|
71 | if (i == (argc-2)) {
|
---|
72 | fprintf(stderr,
|
---|
73 | "Error: -t specified without a color table index.\n");
|
---|
74 | no = 1;
|
---|
75 | goto usage;
|
---|
76 | }
|
---|
77 | if (!strcmp(argv[i+1], "none")) {
|
---|
78 | /* -1 means not transparent. */
|
---|
79 | gdImageColorTransparent(im, -1);
|
---|
80 | } else {
|
---|
81 | /* OK, get an integer and set the index. */
|
---|
82 | index = atoi(argv[i+1]);
|
---|
83 | gdImageColorTransparent(im, index);
|
---|
84 | }
|
---|
85 | i++;
|
---|
86 | write = 1;
|
---|
87 | no = 0;
|
---|
88 | } else if (!strcmp(argv[i], "-l")) {
|
---|
89 | /* List the colors in the color table. */
|
---|
90 | int j;
|
---|
91 | /* Tabs used below. */
|
---|
92 | printf("Index Red Green Blue\n");
|
---|
93 | for (j=0; (j < gdImageColorsTotal(im)); j++) {
|
---|
94 | /* Use access macros to learn colors. */
|
---|
95 | printf("%d %d %d %d\n",
|
---|
96 | j,
|
---|
97 | gdImageRed(im, j),
|
---|
98 | gdImageGreen(im, j),
|
---|
99 | gdImageBlue(im, j));
|
---|
100 | }
|
---|
101 | no = 0;
|
---|
102 | } else if (!strcmp(argv[i], "-d")) {
|
---|
103 | /* Output dimensions, etc. */
|
---|
104 | int t;
|
---|
105 | printf("Width: %d Height: %d Colors: %d\n",
|
---|
106 | gdImageSX(im), gdImageSY(im),
|
---|
107 | gdImageColorsTotal(im));
|
---|
108 | t = gdImageGetTransparent(im);
|
---|
109 | if (t != (-1)) {
|
---|
110 | printf("Transparent index: %d\n", t);
|
---|
111 | } else {
|
---|
112 | /* -1 means the image is not transparent. */
|
---|
113 | printf("Transparent index: none\n");
|
---|
114 | }
|
---|
115 | if (gdImageGetInterlaced(im)) {
|
---|
116 | printf("Interlaced: yes\n");
|
---|
117 | } else {
|
---|
118 | printf("Interlaced: no\n");
|
---|
119 | }
|
---|
120 | no = 0;
|
---|
121 | } else {
|
---|
122 | fprintf(stderr, "Unknown argument: %s\n", argv[i]);
|
---|
123 | break;
|
---|
124 | }
|
---|
125 | }
|
---|
126 | usage:
|
---|
127 | if (no) {
|
---|
128 | /* If the command failed, output an explanation. */
|
---|
129 | fprintf(stderr,
|
---|
130 | "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n");
|
---|
131 | fprintf(stderr,
|
---|
132 | "Where -i controls interlace (specify y or n for yes or no),\n");
|
---|
133 | fprintf(stderr,
|
---|
134 | "-l outputs a table of color indexes, -t sets the specified\n");
|
---|
135 | fprintf(stderr,
|
---|
136 | "color index (0-255 or none) to be the transparent color, and\n");
|
---|
137 | fprintf(stderr,
|
---|
138 | "-d reports the dimensions and other characteristics of the image.\n");
|
---|
139 | fprintf(stderr,
|
---|
140 | "Note: you may wish to pipe to \"more\" when using the -l option.\n");
|
---|
141 | }
|
---|
142 | if (write) {
|
---|
143 | /* Open a temporary file. */
|
---|
144 | out = fopen("temp.tmp", "wb");
|
---|
145 | if (!out) {
|
---|
146 | fprintf(stderr,
|
---|
147 | "Unable to write to temp.tmp -- exiting\n");
|
---|
148 | exit(1);
|
---|
149 | }
|
---|
150 | /* Write the new gif. */
|
---|
151 | gdImageGif(im, out);
|
---|
152 | fclose(out);
|
---|
153 | /* Erase the old gif. */
|
---|
154 | unlink(argv[argc-1]);
|
---|
155 | /* Rename the new to the old. */
|
---|
156 | rename("temp.tmp", argv[argc-1]);
|
---|
157 | }
|
---|
158 | /* Delete the image from memory. */
|
---|
159 | if (im) {
|
---|
160 | gdImageDestroy(im);
|
---|
161 | }
|
---|
162 | /* All's well that ends well. */
|
---|
163 | return 0;
|
---|
164 | }
|
---|
165 |
|
---|