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

  1. #include <stdio.h>
  2. #include <stdlib.h>        /* For atoi */
  3. #include "gd.h"
  4.  
  5. /* A short program which converts a .png file into a .gd file, for
  6.    your convenience in creating images on the fly from a
  7.    basis image that must be loaded quickly. The .gd format
  8.    is not intended to be a general-purpose format. */
  9.  
  10. int
  11. main (int argc, char **argv)
  12. {
  13.   gdImagePtr im;
  14.   FILE *in, *out;
  15.   int x, y, w, h;
  16.  
  17.   if (argc != 7)
  18.     {
  19.       fprintf (stderr, "Usage: gdparttopng filename.gd filename.png x y w h\n");
  20.       exit (1);
  21.     }
  22.   in = fopen (argv[1], "rb");
  23.   if (!in)
  24.     {
  25.       fprintf (stderr, "Input file does not exist!\n");
  26.       exit (1);
  27.     }
  28.  
  29.   x = atoi (argv[3]);
  30.   y = atoi (argv[4]);
  31.   w = atoi (argv[5]);
  32.   h = atoi (argv[6]);
  33.  
  34.   printf ("Extracting from (%d, %d), size is %dx%d\n", x, y, w, h);
  35.  
  36.   im = gdImageCreateFromGd2Part (in, x, y, w, h);
  37.   fclose (in);
  38.   if (!im)
  39.     {
  40.       fprintf (stderr, "Input is not in PNG format!\n");
  41.       exit (1);
  42.     }
  43.   out = fopen (argv[2], "wb");
  44.   if (!out)
  45.     {
  46.       fprintf (stderr, "Output file cannot be written to!\n");
  47.       gdImageDestroy (im);
  48.       exit (1);
  49.     }
  50.   gdImagePng (im, out);
  51.   fclose (out);
  52.   gdImageDestroy (im);
  53.  
  54.   return 0;
  55. }
  56.