You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

genethumb.c 2.1 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include "config.h"
  2. #include "common.h"
  3. #if !defined HAVE_GETOPT_LONG
  4. # include "mygetopt.h"
  5. #elif defined HAVE_GETOPT_H
  6. # include <getopt.h>
  7. #endif
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <pipi.h>
  12. #if defined HAVE_GETOPT_LONG
  13. # define mygetopt getopt_long
  14. # define myoptind optind
  15. # define myoptarg optarg
  16. # define myoption option
  17. #endif
  18. #define MOREINFO "Try `%s --help' for more information.\n"
  19. int main(int argc, char *argv[])
  20. {
  21. char *srcname = NULL, *dstname = NULL;
  22. pipi_image_t *src, *dst;
  23. int i, w = 0, h = 0, bpp = 24;
  24. for(;;)
  25. {
  26. int option_index = 0;
  27. static struct myoption long_options[] =
  28. {
  29. { "geometry", 1, NULL, 'g' },
  30. { "bpp", 1, NULL, 'b' },
  31. };
  32. int c = mygetopt(argc, argv, "g:b:", long_options, &option_index);
  33. if(c == -1)
  34. break;
  35. switch(c)
  36. {
  37. case 'b':
  38. bpp = atoi(myoptarg);
  39. if(bpp != 32 && bpp != 24 && bpp != 16)
  40. {
  41. fprintf(stderr, "%s: invalid bpp -- %s\n", argv[0], myoptarg);
  42. return EXIT_FAILURE;
  43. }
  44. break;
  45. case 'g':
  46. w = atoi(myoptarg);
  47. if(strchr(myoptarg, 'x'))
  48. h = atoi(strchr(myoptarg, 'x') + 1);
  49. break;
  50. default:
  51. fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c);
  52. printf(MOREINFO, argv[0]);
  53. return EXIT_FAILURE;
  54. }
  55. }
  56. for(i = myoptind; i < argc; i++)
  57. {
  58. if(!srcname)
  59. srcname = argv[i];
  60. else
  61. dstname = argv[i];
  62. }
  63. if(!srcname || !dstname)
  64. {
  65. fprintf(stderr, "%s: too few arguments\n", argv[0]);
  66. printf(MOREINFO, argv[0]);
  67. return EXIT_FAILURE;
  68. }
  69. src = pipi_load(srcname);
  70. if(!src)
  71. {
  72. fprintf(stderr, "%s: could not load `%s'\n", argv[0], srcname);
  73. return EXIT_FAILURE;
  74. }
  75. dst = pipi_resize(src, w, h);
  76. if(bpp == 16)
  77. pipi_dither_24to16(dst);
  78. pipi_save(dst, dstname);
  79. pipi_free(src);
  80. pipi_free(dst);
  81. return 0;
  82. }