Back to index

wims  3.65+svn20090927
webgif.c
Go to the documentation of this file.
00001 /* Bring in the gd library functions */
00002 #include "gd.h"
00003 
00004 /* Bring in standard I/O and string manipulation functions */
00005 #include <stdio.h>
00006 #include <string.h>
00007 
00008 int main(int argc, char **argv)
00009 {
00010        FILE *in;
00011        FILE *out;
00012        /* Declare our image pointer */
00013        gdImagePtr im = 0;
00014        int i;
00015        /* We'll clear 'no' once we know the user has made a
00016               reasonable request. */
00017        int no = 1;
00018        /* We'll set 'write' once we know the user's request
00019               requires that the image be written back to disk. */
00020        int write = 0;
00021        /* C programs always get at least one argument; we want at
00022               least one more (the image), more in practice. */
00023        if (argc < 2) {
00024               no = 1;       
00025               goto usage;
00026        }
00027        /* The last argument should be the image. Open the file. */
00028        in = fopen(argv[argc-1], "rb");    
00029        if (!in) {
00030               fprintf(stderr,
00031                      "Error: can't open file %s.\n", argv[argc-1]);
00032        }
00033        /* Now load the image. */   
00034        im = gdImageCreateFromGif(in);
00035        fclose(in);
00036        /* If the load failed, it must not be a GIF file. */
00037        if (!im) {
00038               fprintf(stderr,
00039                      "Error: %s is not a valid gif file.\n", argv[1]);
00040               exit(1);      
00041        }
00042        /* Consider each argument in turn. */
00043        for (i=1; (i < (argc-1)); i++) {
00044               /* -i turns on and off interlacing. */
00045               if (!strcmp(argv[i], "-i")) {
00046                      if (i == (argc-2)) {
00047                             fprintf(stderr, 
00048                             "Error: -i specified without y or n.\n");
00049                             no = 1;
00050                             goto usage;
00051                      }
00052                      if (!strcmp(argv[i+1], "y")) {
00053                             /* Set interlace. */
00054                             gdImageInterlace(im, 1);
00055                      } else if (!strcmp(argv[i+1], "n")) {
00056                             /* Clear interlace. */
00057                             gdImageInterlace(im, 0);
00058                      } else {
00059                             fprintf(stderr,
00060                             "Error: -i specified without y or n.\n");
00061                             no = 1;
00062                             goto usage;
00063                      }
00064                      i++;
00065                      no = 0;
00066                      write = 1;
00067               } else if (!strcmp(argv[i], "-t")) {
00068                      /* Set transparent index (or none). */
00069                      int index;
00070                      if (i == (argc-2)) {
00071                             fprintf(stderr,
00072               "Error: -t specified without a color table index.\n");
00073                             no = 1;
00074                             goto usage;
00075                      }
00076                      if (!strcmp(argv[i+1], "none")) {
00077                             /* -1 means not transparent. */
00078                             gdImageColorTransparent(im, -1);
00079                      } else {
00080                             /* OK, get an integer and set the index. */
00081                             index = atoi(argv[i+1]);
00082                             gdImageColorTransparent(im, index);
00083                      }
00084                      i++;
00085                      write = 1;
00086                      no = 0;
00087               } else if (!strcmp(argv[i], "-l")) {
00088                      /* List the colors in the color table. */
00089                      int j;
00090                      /* Tabs used below. */
00091                      printf("Index Red    Green  Blue\n");
00092                      for (j=0; (j < gdImageColorsTotal(im)); j++) {
00093                             /* Use access macros to learn colors. */
00094                             printf("%d    %d     %d     %d\n",
00095                                    j, 
00096                                    gdImageRed(im, j),
00097                                    gdImageGreen(im, j),
00098                                    gdImageBlue(im, j));
00099                      }
00100                      no = 0;
00101               } else if (!strcmp(argv[i], "-d")) {
00102                      /* Output dimensions, etc. */
00103                      int t;
00104                      printf("Width: %d Height: %d Colors: %d\n",
00105                             gdImageSX(im), gdImageSY(im),
00106                             gdImageColorsTotal(im));
00107                      t = gdImageGetTransparent(im);
00108                      if (t != (-1)) {
00109                             printf("Transparent index: %d\n", t);
00110                      } else {
00111                             /* -1 means the image is not transparent. */
00112                             printf("Transparent index: none\n");
00113                      }
00114                      if (gdImageGetInterlaced(im)) {
00115                             printf("Interlaced: yes\n");       
00116                      } else {
00117                             printf("Interlaced: no\n"); 
00118                      }
00119                      no = 0;
00120               } else {
00121                      fprintf(stderr, "Unknown argument: %s\n", argv[i]);
00122                      break; 
00123               }
00124        }
00125 usage:
00126        if (no) {
00127               /* If the command failed, output an explanation. */
00128               fprintf(stderr, 
00129        "Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n");
00130               fprintf(stderr, 
00131        "Where -i controls interlace (specify y or n for yes or no),\n");
00132               fprintf(stderr, 
00133        "-l outputs a table of color indexes, -t sets the specified\n");
00134               fprintf(stderr, 
00135        "color index (0-255 or none) to be the transparent color, and\n");
00136               fprintf(stderr,
00137        "-d reports the dimensions and other characteristics of the image.\n");
00138               fprintf(stderr, 
00139        "Note: you may wish to pipe to \"more\" when using the -l option.\n");
00140        } 
00141        if (write) {
00142               /* Open a temporary file. */
00143               out = fopen("temp.tmp", "wb");
00144               if (!out) {
00145                      fprintf(stderr,
00146                             "Unable to write to temp.tmp -- exiting\n");
00147                      exit(1);
00148               }
00149               /* Write the new gif. */
00150               gdImageGif(im, out);
00151               fclose(out);
00152               /* Erase the old gif. */
00153               unlink(argv[argc-1]);
00154               /* Rename the new to the old. */
00155               rename("temp.tmp", argv[argc-1]);
00156        }
00157        /* Delete the image from memory. */
00158        if (im) {
00159               gdImageDestroy(im);
00160        }
00161        /* All's well that ends well. */
00162        return 0;
00163 }
00164