gdparttopng.c 1.3 KB

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