00001
00002 #include "gd.h"
00003
00004
00005 #include <stdio.h>
00006 #include <string.h>
00007
00008 int main(int argc, char **argv)
00009 {
00010 FILE *in;
00011 FILE *out;
00012 char outFn[20];
00013 int useStdinStdout=0;
00014
00015
00016 gdImagePtr im = 0;
00017 int i;
00018
00019
00020 int no = 1;
00021
00022
00023 int write = 0;
00024
00025
00026 if (argc < 2 || !strcmp(argv[1], "--help")) {
00027 no = 1;
00028 goto usage;
00029 }
00030
00031
00032 if (strcmp("-", argv[argc-1])==0) {
00033 useStdinStdout = 1;
00034 in = stdin;
00035 } else {
00036 in = fopen(argv[argc-1], "rb");
00037 }
00038 if (!in) {
00039 fprintf(stderr,
00040 "Error: can't open file %s.\n", argv[argc-1]);
00041 exit(1);
00042 }
00043
00044 im = gdImageCreateFromGif(in);
00045 fclose(in);
00046
00047 if (!im) {
00048 fprintf(stderr,
00049 "Error: %s is not a valid gif file.\n", argv[argc-1]);
00050 exit(1);
00051 }
00052
00053 for (i=1; (i < (argc-1)); i++) {
00054
00055 if (!strcmp(argv[i], "--help")) {
00056
00057 no = 1;
00058 goto usage;
00059 } else if (!strcmp(argv[i], "-i")) {
00060 if (i == (argc-2)) {
00061 fprintf(stderr,
00062 "Error: -i specified without y or n.\n");
00063 no = 1;
00064 goto usage;
00065 }
00066 if (!strcmp(argv[i+1], "y")) {
00067
00068 gdImageInterlace(im, 1);
00069 } else if (!strcmp(argv[i+1], "n")) {
00070
00071 gdImageInterlace(im, 0);
00072 } else {
00073 fprintf(stderr,
00074 "Error: -i specified without y or n.\n");
00075 no = 1;
00076 goto usage;
00077 }
00078 i++;
00079 no = 0;
00080 write = 1;
00081 } else if (!strcmp(argv[i], "-t")) {
00082
00083 int index;
00084 if (i == (argc-2)) {
00085 fprintf(stderr,
00086 "Error: -t specified without a color table index.\n");
00087 no = 1;
00088 goto usage;
00089 }
00090 if (!strcmp(argv[i+1], "none")) {
00091
00092 gdImageColorTransparent(im, -1);
00093 } else {
00094
00095 index = atoi(argv[i+1]);
00096 gdImageColorTransparent(im, index);
00097 }
00098 i++;
00099 write = 1;
00100 no = 0;
00101 } else if (!strcmp(argv[i], "-l")) {
00102
00103 int j;
00104
00105 printf("Index Red Green Blue\n");
00106 for (j=0; (j < gdImageColorsTotal(im)); j++) {
00107
00108 printf("%d %d %d %d\n",
00109 j,
00110 gdImageRed(im, j),
00111 gdImageGreen(im, j),
00112 gdImageBlue(im, j));
00113 }
00114 no = 0;
00115 } else if (!strcmp(argv[i], "-d")) {
00116
00117 int t;
00118 printf("Width: %d Height: %d Colors: %d\n",
00119 gdImageSX(im), gdImageSY(im),
00120 gdImageColorsTotal(im));
00121 t = gdImageGetTransparent(im);
00122 if (t != (-1)) {
00123 printf("Transparent index: %d\n", t);
00124 } else {
00125
00126 printf("Transparent index: none\n");
00127 }
00128 if (gdImageGetInterlaced(im)) {
00129 printf("Interlaced: yes\n");
00130 } else {
00131 printf("Interlaced: no\n");
00132 }
00133 no = 0;
00134 } else {
00135 fprintf(stderr, "Unknown argument: %s\n", argv[i]);
00136 break;
00137 }
00138 }
00139 usage:
00140 if (no) {
00141
00142 fprintf(stderr,
00143 "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n"
00144
00145 " -i [y|n] Turns on/off interlace\n"
00146 " -l Prints the table of color indexes\n"
00147 " -t [index] Set the transparent color to the specified index (0-255 or none)\n"
00148 " -d Reports the dimensions and other characteristics of the image.\n"
00149 "\n"
00150 "If you specify '-' as the input file, stdin/stdout will be used input/output.\n"
00151 );
00152 }
00153 if (write) {
00154 if (useStdinStdout) {
00155 out = stdout;
00156 } else {
00157
00158
00159
00160 sprintf(outFn, "webgif.tmp%d", getpid());
00161 out = fopen(outFn, "wb");
00162
00163 if (!out) {
00164 fprintf(stderr,
00165 "Unable to write to %s -- exiting\n", outFn);
00166 exit(1);
00167 }
00168 }
00169
00170
00171 gdImageGif(im, out);
00172
00173 if (!useStdinStdout) {
00174 fclose(out);
00175
00176 unlink(argv[argc-1]);
00177
00178 if (rename(outFn, argv[argc-1])!=0) {
00179 perror("rename");
00180 exit(1);
00181 }
00182 }
00183 }
00184
00185 if (im) {
00186 gdImageDestroy(im);
00187 }
00188
00189 return 0;
00190 }
00191