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

Last change on this file since 2104 was 2104, checked in by Jean-Yves Lamoureux, 12 years ago
  • Added BBCode (French) format to img2txt's help and doc
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1/*
2 *  img2txt       image to text converter
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                2007 Jean-Yves Lamoureux <jylam@lnxscene.org>
5 *                All Rights Reserved
6 *
7 *  $Id: img2txt.c 2104 2007-11-30 23:59:57Z jylam $
8 *
9 *  This program is free software. It comes without any warranty, to
10 *  the extent permitted by applicable law. You can redistribute it
11 *  and/or modify it under the terms of the Do What The Fuck You Want
12 *  To Public License, Version 2, as published by Sam Hocevar. See
13 *  http://sam.zoy.org/wtfpl/COPYING for more details.
14 */
15
16#include "config.h"
17#include "common.h"
18
19#if !defined(__KERNEL__)
20#   include <stdio.h>
21#   include <string.h>
22#   include <stdlib.h>
23#endif
24
25#if !defined HAVE_GETOPT_LONG
26#   include "mygetopt.h"
27#elif defined HAVE_GETOPT_H
28#   include <getopt.h>
29#endif
30#if defined HAVE_GETOPT_LONG
31#   define mygetopt getopt_long
32#   define myoptind optind
33#   define myoptarg optarg
34#   define myoption option
35#endif
36
37#include "cucul.h"
38#include "common-image.h"
39
40#define IMG2TXTVERSION "0.1"
41
42static void usage(int argc, char **argv)
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    fprintf(stderr, "\t\t\tnone     : Nearest color\n");
59    fprintf(stderr, "\t\t\tordered2 : Ordered 2x2\n");
60    fprintf(stderr, "\t\t\tordered4 : Ordered 4x4\n");
61    fprintf(stderr, "\t\t\tordered8 : Ordered 8x8\n");
62    fprintf(stderr, "\t\t\trandom   : Random\n");
63    fprintf(stderr, "\t\t\tfstein   : Floyd Steinberg (default)\n");
64    fprintf(stderr, "  -f, --format=FORMAT\t\tFormat of the resulting image :\n");
65    fprintf(stderr, "\t\t\tansi  : coloured ANSI (default)\n");
66    fprintf(stderr, "\t\t\tcaca  : internal libcaca format\n");
67    fprintf(stderr, "\t\t\tutf8  : UTF8 with CR\n");
68    fprintf(stderr, "\t\t\tutf8  : UTF8 with CRLF (MS Windows)\n");
69    fprintf(stderr, "\t\t\thtml  : HTML with CSS and DIV support\n");
70    fprintf(stderr, "\t\t\thtml3 : Pure HTML3 with tables\n");
71    fprintf(stderr, "\t\t\tirc   : IRC with ctrl-k codes\n");
72    fprintf(stderr, "\t\t\bbfr   : BBCode (French)\n");
73    fprintf(stderr, "\t\t\tps    : Postscript\n");
74    fprintf(stderr, "\t\t\tsvg   : Scalable Vector Graphics\n");
75    fprintf(stderr, "\t\t\ttga   : Targa Image\n\n");
76
77#if !defined(USE_IMLIB2)
78    fprintf(stderr, "NOTE: This program has NOT been built with Imlib2 support. Only BMP loading is supported.\n");
79#endif
80}
81
82static void version(void)
83{
84    printf(
85    "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n"
86    "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s, date: %s\n"
87    "\n"
88    "img2txt, along with its documentation, may be freely copied and distributed.\n"
89    "\n"
90    "The latest version of img2txt is available from the web site,\n"
91    "        http://libcaca.zoy.org/ in the libcaca package.\n"
92    "\n",
93    cucul_get_version(), __DATE__);
94}
95int main(int argc, char **argv)
96{
97    /* libcucul context */
98    cucul_canvas_t *cv;
99    void *export;
100    unsigned long int len;
101    struct image *i;
102    unsigned int cols = 0, lines = 0, font_width = 6, font_height = 10;
103    char *format = NULL;
104    char *dither = NULL;
105    float gamma = -1, brightness = -1, contrast = -1;
106
107    if(argc < 2)
108    {
109        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
110        usage(argc, argv);
111        return 1;
112    }
113
114    for(;;)
115    {
116        int option_index = 0;
117        static struct myoption long_options[] =
118        {
119            { "width",       1, NULL, 'W' },
120            { "height",      1, NULL, 'H' },
121            { "font-width",  1, NULL, 'x' },
122            { "font-height", 1, NULL, 'y' },
123            { "format",      1, NULL, 'f' },
124            { "dither",      1, NULL, 'd' },
125            { "gamma",       1, NULL, 'g' },
126            { "brightness",  1, NULL, 'b' },
127            { "contrast",    1, NULL, 'c' },
128            { "help",        0, NULL, 'h' },
129            { "version",     0, NULL, 'v' },
130        };
131        int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:h:v:x:y:", long_options, &option_index);
132        if(c == -1)
133            break;
134
135        switch(c)
136        {
137        case 'W': /* --width */
138            cols = atoi(myoptarg);
139            break;
140        case 'H': /* --height */
141            lines = atoi(myoptarg);
142            break;
143        case 'x': /* --width */
144            font_width = atoi(myoptarg);
145            break;
146        case 'y': /* --height */
147            font_height = atoi(myoptarg);
148            break;
149        case 'f': /* --format */
150            format = myoptarg;
151            break;
152        case 'd': /* --dither */
153            dither = myoptarg;
154            break;
155        case 'g': /* --gamma */
156            gamma = atof(myoptarg);
157            break;
158        case 'b': /* --brightness */
159            brightness = atof(myoptarg);
160            break;
161        case 'c': /* --contrast */
162            contrast = atof(myoptarg);
163            break;
164        case 'h': /* --help */
165            usage(argc, argv);
166            return 0;
167            break;
168        case 'v': /* --version */
169            version();
170            return 0;
171            break;
172        default:
173            return 1;
174            break;
175        }
176    }
177
178
179    cv = cucul_create_canvas(0, 0);
180    if(!cv)
181    {
182        fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
183        return 1;
184    }
185
186    i = load_image(argv[argc-1]);
187    if(!i)
188    {
189        fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]);
190        cucul_free_canvas(cv);
191        return 1;
192    }
193
194    /* Assume a 6×10 font */
195    if(!cols && !lines)
196    {
197        cols = 60;
198        lines = cols * i->h * font_width / i->w / font_height;
199    }
200    else if(cols && !lines)
201    {
202        lines = cols * i->h * font_width / i->w / font_height;
203    }
204    else if(!cols && lines)
205    {
206        cols = lines * i->w * font_height / i->h / font_width;
207    }
208
209
210    cucul_set_canvas_size(cv, cols, lines);
211    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
212    cucul_clear_canvas(cv);
213    if(cucul_set_dither_algorithm(i->dither, dither?dither:"fstein"))
214    {
215        fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither);
216        unload_image(i);
217        cucul_free_canvas(cv);
218        return -1;
219    }
220
221    if(brightness!=-1) cucul_set_dither_brightness (i->dither, brightness);
222    if(contrast!=-1) cucul_set_dither_contrast (i->dither, contrast);
223    if(gamma!=-1) cucul_set_dither_gamma (i->dither, gamma);
224
225    cucul_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
226
227    unload_image(i);
228
229    export = cucul_export_memory(cv, format?format:"ansi", &len);
230    if(!export)
231    {
232        fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format);
233    }
234    else
235    {
236        fwrite(export, len, 1, stdout);
237        free(export);
238    }
239
240    cucul_free_canvas(cv);
241
242    return 0;
243}
244
Note: See TracBrowser for help on using the repository browser.