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

Last change on this file since 1917 was 1917, checked in by Jean-Yves Lamoureux, 13 years ago
  • Added version switch
  • Property svn:keywords set to Id
File size: 7.0 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 1917 2007-11-07 10:03:50Z 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, "  -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
53    fprintf(stderr, "  -c, --contrast=CONTRAST\tContrast of resulting image\n");
54    fprintf(stderr, "  -g, --gamma=GAMMA\t\tGamma of resulting image\n");
55    fprintf(stderr, "  -d, --dither=DITHER\t\tDithering algorithm to use :\n");
56    fprintf(stderr, "\t\t\tnone     : Nearest color\n");
57    fprintf(stderr, "\t\t\tordered2 : Ordered 2x2\n");
58    fprintf(stderr, "\t\t\tordered4 : Ordered 4x4\n");
59    fprintf(stderr, "\t\t\tordered8 : Ordered 8x8\n");
60    fprintf(stderr, "\t\t\trandom   : Random\n");
61    fprintf(stderr, "\t\t\tfstein   : Floyd Steinberg (default)\n");
62    fprintf(stderr, "  -f, --format=FORMAT\t\tFormat of the resulting image :\n");
63    fprintf(stderr, "\t\t\tansi : coloured ANSI (default)\n");
64    fprintf(stderr, "\t\t\tcaca : internal libcaca format\n");
65    fprintf(stderr, "\t\t\tutf8 : UTF8 with CR\n");
66    fprintf(stderr, "\t\t\tutf8 : UTF8 with CRLF (MS Windows)\n");
67    fprintf(stderr, "\t\t\thtml : HTML with CSS and DIV support\n");
68    fprintf(stderr, "\t\t\thtml3 : Pure HTML3 with tables\n");
69    fprintf(stderr, "\t\t\tirc  : IRC with ctrl-k codes\n");
70    fprintf(stderr, "\t\t\tps   : Postscript\n");
71    fprintf(stderr, "\t\t\tsvg  : Scalable Vector Graphics\n");
72    fprintf(stderr, "\t\t\ttga  : Targa Image\n\n");
73
74#if !defined(USE_IMLIB2)
75    fprintf(stderr, "NOTE: This program has NOT been built with Imlib2 support. Only BMP loading is supported.\n");
76#endif
77}
78
79static void version(void)
80{
81    printf(
82    "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n"
83    "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s, date: %s\n"
84    "\n"
85    "img2txt, along with its documentation, may be freely copied and distributed.\n"
86    "\n"
87    "The latest version of img2txt is available from the web site,\n"
88    "        http://libcaca.zoy.org/ in the libcaca package.\n"
89    "\n"
90    , VERSION, __DATE__);
91}
92int main(int argc, char **argv)
93{
94    /* libcucul context */
95    cucul_canvas_t *cv;
96    void *export;
97    unsigned long int len;
98    struct image *i;
99    unsigned int cols = 0, lines = 0;
100    char *format = NULL;
101    char *dither = NULL;
102    float gamma = -1, brightness = -1, contrast = -1;
103
104    if(argc < 2)
105    {
106        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
107        usage(argc, argv);
108        return 1;
109    }
110
111    for(;;)
112    {
113        int option_index = 0;
114        static struct myoption long_options[] =
115        {
116            { "width",       1, NULL, 'W' },
117            { "height",      1, NULL, 'H' },
118            { "format",      1, NULL, 'f' },
119            { "dither",      1, NULL, 'd' },
120            { "gamma",       1, NULL, 'g' },
121            { "brightness",  1, NULL, 'b' },
122            { "contrast",    1, NULL, 'c' },
123            { "help",        0, NULL, 'h' },
124            { "version",        0, NULL, 'v' },
125        };
126        int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:h:v", long_options, &option_index);
127        if(c == -1)
128            break;
129
130        switch(c)
131        {
132        case 'W': /* --width */
133            cols = atoi(myoptarg);
134            break;
135        case 'H': /* --height */
136            lines = atoi(myoptarg);
137            break;
138        case 'f': /* --format */
139            format = myoptarg;
140            break;
141        case 'd': /* --dither */
142            dither = myoptarg;
143            break;
144        case 'g': /* --gamma */
145            gamma = atof(myoptarg);
146            break;
147        case 'b': /* --brightness */
148            brightness = atof(myoptarg);
149            break;
150        case 'c': /* --contrast */
151            contrast = atof(myoptarg);
152            break;
153        case 'h': /* --help */
154            usage(argc, argv);
155            return 0;
156            break;
157        case 'v': /* --version */
158            version();
159            return 0;
160            break;
161        default:
162            return 1;
163            break;
164        }
165    }
166
167
168    cv = cucul_create_canvas(0, 0);
169    if(!cv)
170    {
171        fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
172        return 1;
173    }
174
175    i = load_image(argv[argc-1]);
176    if(!i)
177    {
178        fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]);
179        cucul_free_canvas(cv);
180        return 1;
181    }
182
183    /* Assume a 6×10 font */
184    if(!cols && !lines)
185    {
186        cols = 60;
187        lines = cols * i->h * 6 / i->w / 10;
188    }
189    else if(cols && !lines)
190    {
191        lines = cols * i->h * 6 / i->w / 10;
192    }
193    else if(!cols && lines)
194    {
195        cols = lines * i->w * 10 / i->h / 6;
196    }
197
198
199    cucul_set_canvas_size(cv, cols, lines);
200    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
201    cucul_clear_canvas(cv);
202    if(cucul_set_dither_algorithm(i->dither, dither?dither:"fstein"))
203    {
204        fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither);
205        unload_image(i);
206        cucul_free_canvas(cv);
207        return -1;
208    }
209
210    if(brightness!=-1) cucul_set_dither_brightness (i->dither, brightness);
211    if(contrast!=-1) cucul_set_dither_contrast (i->dither, contrast);
212    if(gamma!=-1) cucul_set_dither_gamma (i->dither, gamma);
213
214    cucul_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
215
216    unload_image(i);
217
218    export = cucul_export_memory(cv, format?format:"ansi", &len);
219    if(!export)
220    {
221        fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format);
222    }
223    else
224    {
225        fwrite(export, len, 1, stdout);
226        free(export);
227    }
228
229    cucul_free_canvas(cv);
230
231    return 0;
232}
233
Note: See TracBrowser for help on using the repository browser.