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

Last change on this file since 3428 was 2833, checked in by Sam Hocevar, 12 years ago

Change the website name to caca.zoy.org.

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