source: libcaca/trunk/src/img2txt.c @ 4333

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

  • Property svn:keywords set to Id
File size: 6.9 KB
Line 
1/*
2 *  img2txt       image to text converter
3 *  Copyright (c) 2006-2009 Sam Hocevar <sam@hocevar.net>
4 *                2007 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  This program is free software. It comes without any warranty, to
8 *  the extent permitted by applicable law. You can redistribute it
9 *  and/or modify it under the terms of the Do What The Fuck You Want
10 *  To Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 */
13
14#include "config.h"
15
16#if !defined(__KERNEL__)
17#   include <stdio.h>
18#   include <string.h>
19#   include <stdlib.h>
20#endif
21
22#if !defined HAVE_GETOPT_LONG
23#   include "mygetopt.h"
24#elif defined HAVE_GETOPT_H
25#   include <getopt.h>
26#endif
27#if defined HAVE_GETOPT_LONG
28#   define mygetopt getopt_long
29#   define myoptind optind
30#   define myoptarg optarg
31#   define myoption option
32#endif
33
34#include "caca.h"
35
36#include "common-image.h"
37
38#define IMG2TXTVERSION "0.1"
39
40static void usage(int argc, char **argv)
41{
42    char const * const * list;
43
44    fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]);
45    fprintf(stderr, "Convert IMAGE to any text based available format.\n");
46    fprintf(stderr, "Example : %s -w 80 -f ansi ./caca.png\n\n", argv[0]);
47    fprintf(stderr, "Options:\n");
48    fprintf(stderr, "  -h, --help\t\t\tThis help\n");
49    fprintf(stderr, "  -v, --version\t\t\tVersion of the program\n");
50    fprintf(stderr, "  -W, --width=WIDTH\t\tWidth of resulting image\n");
51    fprintf(stderr, "  -H, --height=HEIGHT\t\tHeight of resulting image\n");
52    fprintf(stderr, "  -x, --font-width=WIDTH\t\tWidth of output font\n");
53    fprintf(stderr, "  -y, --font-height=HEIGHT\t\tHeight of output font\n");
54    fprintf(stderr, "  -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
55    fprintf(stderr, "  -c, --contrast=CONTRAST\tContrast of resulting image\n");
56    fprintf(stderr, "  -g, --gamma=GAMMA\t\tGamma of resulting image\n");
57    fprintf(stderr, "  -d, --dither=DITHER\t\tDithering algorithm to use :\n");
58    list = caca_get_dither_algorithm_list(NULL);
59    while(*list)
60    {
61        fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]);
62        list += 2;
63    }
64
65    fprintf(stderr, "  -f, --format=FORMAT\t\tFormat of the resulting image :\n");
66    list = caca_get_export_list();
67    while(*list)
68    {
69        fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]);
70        list += 2;
71    }
72
73#if !defined(USE_IMLIB2)
74    fprintf(stderr, "NOTE: This program has NOT been built with Imlib2 support. Only BMP loading is supported.\n");
75#endif
76}
77
78static void version(void)
79{
80    printf(
81    "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n"
82    "Internet: <sam@hocevar.net> <jylam@lnxscene.org> Version: %s, date: %s\n"
83    "\n"
84    "img2txt, along with its documentation, may be freely copied and distributed.\n"
85    "\n"
86    "The latest version of img2txt is available from the web site,\n"
87    "        http://caca.zoy.org/wiki/libcaca in the libcaca package.\n"
88    "\n",
89    caca_get_version(), __DATE__);
90}
91int main(int argc, char **argv)
92{
93    /* libcaca context */
94    caca_canvas_t *cv;
95    void *export;
96    size_t len;
97    struct image *i;
98    unsigned int cols = 0, lines = 0, font_width = 6, font_height = 10;
99    char *format = NULL;
100    char *dither = NULL;
101    float gamma = -1, brightness = -1, contrast = -1;
102
103    if(argc < 2)
104    {
105        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
106        usage(argc, argv);
107        return 1;
108    }
109
110    for(;;)
111    {
112        int option_index = 0;
113        static struct myoption long_options[] =
114        {
115            { "width",       1, NULL, 'W' },
116            { "height",      1, NULL, 'H' },
117            { "font-width",  1, NULL, 'x' },
118            { "font-height", 1, NULL, 'y' },
119            { "format",      1, NULL, 'f' },
120            { "dither",      1, NULL, 'd' },
121            { "gamma",       1, NULL, 'g' },
122            { "brightness",  1, NULL, 'b' },
123            { "contrast",    1, NULL, 'c' },
124            { "help",        0, NULL, 'h' },
125            { "version",     0, NULL, 'v' },
126        };
127        int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:hvx:y:", long_options, &option_index);
128        if(c == -1)
129            break;
130
131        switch(c)
132        {
133        case 'W': /* --width */
134            cols = atoi(myoptarg);
135            break;
136        case 'H': /* --height */
137            lines = atoi(myoptarg);
138            break;
139        case 'x': /* --width */
140            font_width = atoi(myoptarg);
141            break;
142        case 'y': /* --height */
143            font_height = atoi(myoptarg);
144            break;
145        case 'f': /* --format */
146            format = myoptarg;
147            break;
148        case 'd': /* --dither */
149            dither = myoptarg;
150            break;
151        case 'g': /* --gamma */
152            gamma = atof(myoptarg);
153            break;
154        case 'b': /* --brightness */
155            brightness = atof(myoptarg);
156            break;
157        case 'c': /* --contrast */
158            contrast = atof(myoptarg);
159            break;
160        case 'h': /* --help */
161            usage(argc, argv);
162            return 0;
163            break;
164        case 'v': /* --version */
165            version();
166            return 0;
167            break;
168        default:
169            return 1;
170            break;
171        }
172    }
173
174
175    cv = caca_create_canvas(0, 0);
176    if(!cv)
177    {
178        fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]);
179        return 1;
180    }
181
182    i = load_image(argv[argc-1]);
183    if(!i)
184    {
185        fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]);
186        caca_free_canvas(cv);
187        return 1;
188    }
189
190    /* Assume a 6×10 font */
191    if(!cols && !lines)
192    {
193        cols = 60;
194        lines = cols * i->h * font_width / i->w / font_height;
195    }
196    else if(cols && !lines)
197    {
198        lines = cols * i->h * font_width / i->w / font_height;
199    }
200    else if(!cols && lines)
201    {
202        cols = lines * i->w * font_height / i->h / font_width;
203    }
204
205
206    caca_set_canvas_size(cv, cols, lines);
207    caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT);
208    caca_clear_canvas(cv);
209    if(caca_set_dither_algorithm(i->dither, dither?dither:"fstein"))
210    {
211        fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither);
212        unload_image(i);
213        caca_free_canvas(cv);
214        return -1;
215    }
216
217    if(brightness!=-1) caca_set_dither_brightness (i->dither, brightness);
218    if(contrast!=-1) caca_set_dither_contrast (i->dither, contrast);
219    if(gamma!=-1) caca_set_dither_gamma (i->dither, gamma);
220
221    caca_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
222
223    unload_image(i);
224
225    export = caca_export_canvas_to_memory(cv, format?format:"ansi", &len);
226    if(!export)
227    {
228        fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format);
229    }
230    else
231    {
232        fwrite(export, len, 1, stdout);
233        free(export);
234    }
235
236    caca_free_canvas(cv);
237
238    return 0;
239}
240
Note: See TracBrowser for help on using the repository browser.