webgif.cpp File Reference

#include "gd.h"
#include <stdio.h>
#include <string.h>

Go to the source code of this file.

Functions

int main (int argc, char **argv)


Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 8 of file webgif.cpp.

References gdImageBlue, gdImageColorsTotal, gdImageColorTransparent(), gdImageCreateFromGif(), gdImageDestroy(), gdImageGetInterlaced, gdImageGetTransparent, gdImageGif(), gdImageGreen, gdImageInterlace(), gdImageRed, gdImageSX, and gdImageSY.

00009 {
00010         FILE *in;
00011         FILE *out;
00012         char outFn[20];
00013         int useStdinStdout=0;
00014 
00015         /* Declare our image pointer */
00016         gdImagePtr im = 0;
00017         int i;
00018        /* We'll clear 'no' once we know the user has made a
00019                 reasonable request. */
00020         int no = 1;
00021         /* We'll set 'write' once we know the user's request
00022                 requires that the image be written back to disk. */
00023         int write = 0;
00024         /* C programs always get at least one argument; we want at
00025                 least one more (the image), more in practice. */
00026         if (argc < 2 || !strcmp(argv[1], "--help")) {
00027                 no = 1; 
00028                 goto usage;
00029         }
00030 
00031         /* The last argument should be the image. Open the file. */
00032         if (strcmp("-", argv[argc-1])==0) { /* - is synonymous with STDIN */
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         /* Now load the image. */       
00044         im = gdImageCreateFromGif(in);
00045         fclose(in);
00046         /* If the load failed, it must not be a GIF file. */
00047         if (!im) {
00048                 fprintf(stderr,
00049                         "Error: %s is not a valid gif file.\n", argv[argc-1]);
00050                 exit(1);        
00051         }
00052         /* Consider each argument in turn. */
00053         for (i=1; (i < (argc-1)); i++) {
00054                 /* -i turns on and off interlacing. */
00055                 if (!strcmp(argv[i], "--help")) { 
00056                   /* Every program should use this for help! :) */
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                                 /* Set interlace. */
00068                                 gdImageInterlace(im, 1);
00069                         } else if (!strcmp(argv[i+1], "n")) {
00070                                 /* Clear interlace. */
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                         /* Set transparent index (or none). */
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                                 /* -1 means not transparent. */
00092                                 gdImageColorTransparent(im, -1);
00093                         } else {
00094                                 /* OK, get an integer and set the index. */
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                         /* List the colors in the color table. */
00103                         int j;
00104                         /* Tabs used below. */
00105                         printf("Index   Red     Green   Blue\n");
00106                         for (j=0; (j < gdImageColorsTotal(im)); j++) {
00107                                 /* Use access macros to learn colors. */
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                         /* Output dimensions, etc. */
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                                 /* -1 means the image is not transparent. */
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                 /* If the command failed, output an explanation. */
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             /* Open a temporary file. */
00158 
00159             /* "temp.tmp" is not good temporary filename. */
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           /* Write the new gif. */
00171           gdImageGif(im, out);
00172 
00173           if (!useStdinStdout) {
00174             fclose(out);
00175             /* Erase the old gif. */
00176             unlink(argv[argc-1]);
00177             /* Rename the new to the old. */
00178             if (rename(outFn, argv[argc-1])!=0) {
00179               perror("rename");
00180               exit(1);
00181             }
00182           }
00183         }
00184         /* Delete the image from memory. */
00185         if (im) {
00186                 gdImageDestroy(im);
00187         }
00188         /* All's well that ends well. */
00189         return 0;
00190 }


Generated on Sat Dec 15 00:01:27 2007 for BulmaGes by  doxygen 1.5.1