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