source: libpipi/trunk/genethumb/genethumb.c @ 2902

Last change on this file since 2902 was 2902, checked in by Sam Hocevar, 11 years ago

Support C99 types on Win32 through the same hacks as in libcaca.

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