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 DEFAULT_WIDTH 120 |
---|
24 | #define DEFAULT_HEIGHT 90 |
---|
25 | |
---|
26 | #define MOREINFO "Try `%s --help' for more information.\n" |
---|
27 | |
---|
28 | static void usage(void); |
---|
29 | |
---|
30 | int main(int argc, char *argv[]) |
---|
31 | { |
---|
32 | char *srcname = NULL, *dstname = NULL; |
---|
33 | pipi_image_t *src, *dst; |
---|
34 | |
---|
35 | int i, w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT, bpp = 24; |
---|
36 | |
---|
37 | for(;;) |
---|
38 | { |
---|
39 | int option_index = 0; |
---|
40 | static struct myoption long_options[] = |
---|
41 | { |
---|
42 | { "bpp", 1, NULL, 'b' }, |
---|
43 | { "geometry", 1, NULL, 'g' }, |
---|
44 | { "help", 0, NULL, 'h' }, |
---|
45 | { NULL, 0, NULL, 0 }, |
---|
46 | }; |
---|
47 | int c = mygetopt(argc, argv, "b:g:h", long_options, &option_index); |
---|
48 | |
---|
49 | if(c == -1) |
---|
50 | break; |
---|
51 | |
---|
52 | switch(c) |
---|
53 | { |
---|
54 | case 'b': |
---|
55 | bpp = atoi(myoptarg); |
---|
56 | if(bpp != 32 && bpp != 24 && bpp != 16) |
---|
57 | { |
---|
58 | fprintf(stderr, "%s: invalid bpp -- %s\n", argv[0], myoptarg); |
---|
59 | return EXIT_FAILURE; |
---|
60 | } |
---|
61 | break; |
---|
62 | case 'g': |
---|
63 | w = atoi(myoptarg); |
---|
64 | if(strchr(myoptarg, 'x')) |
---|
65 | h = atoi(strchr(myoptarg, 'x') + 1); |
---|
66 | break; |
---|
67 | case 'h': |
---|
68 | usage(); |
---|
69 | return EXIT_SUCCESS; |
---|
70 | default: |
---|
71 | fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); |
---|
72 | printf(MOREINFO, argv[0]); |
---|
73 | return EXIT_FAILURE; |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | for(i = myoptind; i < argc; i++) |
---|
78 | { |
---|
79 | if(!srcname) |
---|
80 | srcname = argv[i]; |
---|
81 | else |
---|
82 | dstname = argv[i]; |
---|
83 | } |
---|
84 | |
---|
85 | if(!srcname || !dstname) |
---|
86 | { |
---|
87 | fprintf(stderr, "%s: too few arguments\n", argv[0]); |
---|
88 | printf(MOREINFO, argv[0]); |
---|
89 | return EXIT_FAILURE; |
---|
90 | } |
---|
91 | |
---|
92 | src = pipi_load(srcname); |
---|
93 | if(!src) |
---|
94 | { |
---|
95 | fprintf(stderr, "%s: could not load `%s'\n", argv[0], srcname); |
---|
96 | return EXIT_FAILURE; |
---|
97 | } |
---|
98 | |
---|
99 | dst = pipi_resize(src, w, h); |
---|
100 | if(bpp == 16) |
---|
101 | pipi_dither_24to16(dst); |
---|
102 | pipi_save(dst, dstname); |
---|
103 | pipi_free(src); |
---|
104 | pipi_free(dst); |
---|
105 | |
---|
106 | return 0; |
---|
107 | } |
---|
108 | |
---|
109 | static void usage(void) |
---|
110 | { |
---|
111 | printf("Usage: genethumb [-b bpp] [-g geometry] SOURCE DESTINATION\n"); |
---|
112 | printf(" genethumb -h | --help\n"); |
---|
113 | printf("Create an image thumbnail.\n"); |
---|
114 | printf("\n"); |
---|
115 | printf("Mandatory arguments to long options are mandatory for short options too.\n"); |
---|
116 | printf(" -b, --bpp <bits> bits per pixel (32 or 16)\n"); |
---|
117 | printf(" -g, --geometry <geometry> target geometry (default %ix%i)\n", |
---|
118 | DEFAULT_WIDTH, DEFAULT_HEIGHT); |
---|
119 | printf(" -h, --help display this help and exit\n"); |
---|
120 | printf("\n"); |
---|
121 | printf("Written by Sam Hocevar. Report bugs to <sam@zoy.org>.\n"); |
---|
122 | } |
---|
123 | |
---|