gd2copypal.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include <stdio.h>
  2. #include "gd.h"
  3. #include <stdlib.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. gdImagePtr pal;
  13. FILE *in, *out;
  14. if (argc != 3)
  15. {
  16. fprintf (stderr, "Usage: gd2copypal palettefile.gd2 filename.gd2\n");
  17. exit (1);
  18. }
  19. in = fopen (argv[1], "rb");
  20. if (!in)
  21. {
  22. fprintf (stderr, "Palette file does not exist!\n");
  23. exit (1);
  24. }
  25. pal = gdImageCreateFromGd2 (in);
  26. fclose (in);
  27. if (!pal)
  28. {
  29. fprintf (stderr, "Palette is not in GD2 format!\n");
  30. exit (1);
  31. }
  32. in = fopen (argv[2], "rb");
  33. if (!in)
  34. {
  35. fprintf (stderr, "Input file does not exist!\n");
  36. exit (1);
  37. }
  38. im = gdImageCreateFromGd2 (in);
  39. fclose (in);
  40. if (!im)
  41. {
  42. fprintf (stderr, "Input is not in GD2 format!\n");
  43. exit (1);
  44. }
  45. gdImagePaletteCopy (im, pal);
  46. out = fopen (argv[2], "wb");
  47. if (!out)
  48. {
  49. fprintf (stderr, "Output file cannot be written to!\n");
  50. gdImageDestroy (im);
  51. exit (1);
  52. }
  53. gdImageGd2 (im, out, 128, 2);
  54. fclose (out);
  55. gdImageDestroy (pal);
  56. gdImageDestroy (im);
  57. return 0;
  58. }