home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gd201.zip / gd-2.0.1 / webpng.c < prev   
C/C++ Source or Header  |  2001-04-03  |  6KB  |  255 lines

  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 <stdlib.h>        /* for atoi() */
  7. #include <string.h>
  8.  
  9. #ifdef _WIN32
  10. #include <process.h>
  11. int
  12. getpid ()
  13. {
  14.   return _getpid ();
  15. }
  16. #else
  17. #include <unistd.h>        /* for getpid(), unlink() */
  18. #endif
  19. int
  20. main (int argc, char **argv)
  21. {
  22.   FILE *in;
  23.   FILE *out;
  24.   char outFn[20];
  25.   int useStdinStdout = 0;
  26.  
  27.   /* Declare our image pointer */
  28.   gdImagePtr im = 0;
  29.   int i;
  30.   /* We'll clear 'no' once we know the user has made a
  31.      reasonable request. */
  32.   int no = 1;
  33.   /* We'll set 'write' once we know the user's request
  34.      requires that the image be written back to disk. */
  35.   int write = 0;
  36.   /* C programs always get at least one argument; we want at
  37.      least one more (the image), more in practice. */
  38.   if (argc < 2 || !strcmp (argv[1], "--help"))
  39.     {
  40.       no = 1;
  41.       goto usage;
  42.     }
  43.  
  44.   /* The last argument should be the image. Open the file. */
  45.   if (strcmp ("-", argv[argc - 1]) == 0)
  46.     {                /* - is synonymous with STDIN */
  47.       useStdinStdout = 1;
  48.       in = stdin;
  49.     }
  50.   else
  51.     {
  52.       in = fopen (argv[argc - 1], "rb");
  53.     }
  54.   if (!in)
  55.     {
  56.       fprintf (stderr,
  57.            "Error: can't open file %s.\n", argv[argc - 1]);
  58.       exit (1);
  59.     }
  60.   /* Now load the image. */
  61.   im = gdImageCreateFromPng (in);
  62.   fclose (in);
  63.   /* If the load failed, it must not be a PNG file. */
  64.   if (!im)
  65.     {
  66.       fprintf (stderr,
  67.            "Error: %s is not a valid PNG file.\n", argv[argc - 1]);
  68.       exit (1);
  69.     }
  70.   /* Consider each argument in turn. */
  71.   for (i = 1; (i < (argc - 1)); i++)
  72.     {
  73.       /* -i turns on and off interlacing. */
  74.       if (!strcmp (argv[i], "--help"))
  75.     {
  76.       /* Every program should use this for help! :) */
  77.       no = 1;
  78.       goto usage;
  79.     }
  80.       else if (!strcmp (argv[i], "-i"))
  81.     {
  82.       if (i == (argc - 2))
  83.         {
  84.           fprintf (stderr,
  85.                "Error: -i specified without y or n.\n");
  86.           no = 1;
  87.           goto usage;
  88.         }
  89.       if (!strcmp (argv[i + 1], "y"))
  90.         {
  91.           /* Set interlace. */
  92.           gdImageInterlace (im, 1);
  93.         }
  94.       else if (!strcmp (argv[i + 1], "n"))
  95.         {
  96.           /* Clear interlace. */
  97.           gdImageInterlace (im, 0);
  98.         }
  99.       else
  100.         {
  101.           fprintf (stderr,
  102.                "Error: -i specified without y or n.\n");
  103.           no = 1;
  104.           goto usage;
  105.         }
  106.       i++;
  107.       no = 0;
  108.       write = 1;
  109.     }
  110.       else if (!strcmp (argv[i], "-t"))
  111.     {
  112.       /* Set transparent index (or none). */
  113.       int index;
  114.       if (i == (argc - 2))
  115.         {
  116.           fprintf (stderr,
  117.                "Error: -t specified without a color table index.\n");
  118.           no = 1;
  119.           goto usage;
  120.         }
  121.       if (!strcmp (argv[i + 1], "none"))
  122.         {
  123.           /* -1 means not transparent. */
  124.           gdImageColorTransparent (im, -1);
  125.         }
  126.       else
  127.         {
  128.           /* OK, get an integer and set the index. */
  129.           index = atoi (argv[i + 1]);
  130.           gdImageColorTransparent (im, index);
  131.         }
  132.       i++;
  133.       write = 1;
  134.       no = 0;
  135.     }
  136.       else if (!strcmp (argv[i], "-l"))
  137.     {
  138.       /* List the colors in the color table. */
  139.       int j;
  140.       if (!im->trueColor)
  141.         {
  142.           /* Tabs used below. */
  143.           printf ("Index    Red    Green    Blue Alpha\n");
  144.           for (j = 0; (j < gdImageColorsTotal (im)); j++)
  145.         {
  146.           /* Use access macros to learn colors. */
  147.           printf ("%d    %d    %d    %d    %d\n",
  148.               j,
  149.               gdImageRed (im, j),
  150.               gdImageGreen (im, j),
  151.               gdImageBlue (im, j),
  152.               gdImageAlpha (im, j));
  153.         }
  154.         }
  155.       else
  156.         {
  157.           printf ("Truecolor image, no palette entries to list.\n");
  158.         }
  159.       no = 0;
  160.     }
  161.       else if (!strcmp (argv[i], "-d"))
  162.     {
  163.       /* Output dimensions, etc. */
  164.       int t;
  165.       printf ("Width: %d Height: %d Colors: %d\n",
  166.           gdImageSX (im), gdImageSY (im),
  167.           gdImageColorsTotal (im));
  168.       t = gdImageGetTransparent (im);
  169.       if (t != (-1))
  170.         {
  171.           printf ("First 100% transparent index: %d\n", t);
  172.         }
  173.       else
  174.         {
  175.           /* -1 means the image is not transparent. */
  176.           printf ("First 100% transparent index: none\n");
  177.         }
  178.       if (gdImageGetInterlaced (im))
  179.         {
  180.           printf ("Interlaced: yes\n");
  181.         }
  182.       else
  183.         {
  184.           printf ("Interlaced: no\n");
  185.         }
  186.       no = 0;
  187.     }
  188.       else
  189.     {
  190.       fprintf (stderr, "Unknown argument: %s\n", argv[i]);
  191.       break;
  192.     }
  193.     }
  194. usage:
  195.   if (no)
  196.     {
  197.       /* If the command failed, output an explanation. */
  198.       fprintf (stderr,
  199.       "Usage: webpng [-i y|n ] [-l] [-t index|none ] [-d] pngname.png\n"
  200.  
  201.            "  -i [y|n]   Turns on/off interlace\n"
  202.            "  -l         Prints the table of color indexes\n"
  203.            "  -t [index] Set the transparent color to the specified index (0-255 or \"none\")\n"
  204.            "  -d         Reports the dimensions and other characteristics of the image.\n"
  205.            "\n"
  206.            "If you specify '-' as the input file, stdin/stdout will be used input/output.\n"
  207.     );
  208.     }
  209.   if (write)
  210.     {
  211.       if (useStdinStdout)
  212.     {
  213.       out = stdout;
  214.     }
  215.       else
  216.     {
  217.       /* Open a temporary file. */
  218.  
  219.       /* "temp.tmp" is not good temporary filename. */
  220.       sprintf (outFn, "webpng.tmp%d", getpid ());
  221.       out = fopen (outFn, "wb");
  222.  
  223.       if (!out)
  224.         {
  225.           fprintf (stderr,
  226.                "Unable to write to %s -- exiting\n", outFn);
  227.           exit (1);
  228.         }
  229.     }
  230.  
  231.       /* Write the new PNG. */
  232.       gdImagePng (im, out);
  233.  
  234.       if (!useStdinStdout)
  235.     {
  236.       fclose (out);
  237.       /* Erase the old PNG. */
  238.       unlink (argv[argc - 1]);
  239.       /* Rename the new to the old. */
  240.       if (rename (outFn, argv[argc - 1]) != 0)
  241.         {
  242.           perror ("rename");
  243.           exit (1);
  244.         }
  245.     }
  246.     }
  247.   /* Delete the image from memory. */
  248.   if (im)
  249.     {
  250.       gdImageDestroy (im);
  251.     }
  252.   /* All's well that ends well. */
  253.   return 0;
  254. }
  255.