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

Last change on this file since 1909 was 1909, checked in by Jean-Yves Lamoureux, 13 years ago
  • Added brightness / contrast / gamma support
  • Property svn:keywords set to Id
File size: 6.2 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 1909 2007-11-06 14:09:07Z 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
40static void usage(int argc, char **argv)
41{
42    fprintf(stderr, "Usage: %s [OPTIONS]... <IMAGE>\n", argv[0]);
43    fprintf(stderr, "Convert IMAGE to any text based available format.\n");
44    fprintf(stderr, "Example : %s -w 80 -f ansi ./caca.png\n\n", argv[0]);
45    fprintf(stderr, "Options:\n");
46    fprintf(stderr, "  -h, --help\t\t\tThis help\n");
47    fprintf(stderr, "  -W, --width=WIDTH\t\tWidth of resulting image\n");
48    fprintf(stderr, "  -H, --height=HEIGHT\t\tHeight of resulting image\n");
49    fprintf(stderr, "  -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n");
50    fprintf(stderr, "  -c, --contrast=CONTRAST\tContrast of resulting image\n");
51    fprintf(stderr, "  -g, --gamma=GAMMA\t\tGamma of resulting image\n");
52    fprintf(stderr, "  -d, --dither=DITHER\t\tDithering algorithm to use :\n");
53    fprintf(stderr, "\t\t\tnone     : Nearest color\n");
54    fprintf(stderr, "\t\t\tordered2 : Ordered 2x2\n");
55    fprintf(stderr, "\t\t\tordered4 : Ordered 4x4\n");
56    fprintf(stderr, "\t\t\tordered8 : Ordered 8x8\n");
57    fprintf(stderr, "\t\t\trandom   : Random\n");
58    fprintf(stderr, "\t\t\tfstein   : Floyd Steinberg (default)\n");
59    fprintf(stderr, "  -f, --format=FORMAT\t\tFormat of the resulting image :\n");
60    fprintf(stderr, "\t\t\tansi : coulored ANSI (default)\n");
61    fprintf(stderr, "\t\t\tcaca : internal libcaca format\n");
62    fprintf(stderr, "\t\t\tutf8 : UTF8 with CR\n");
63    fprintf(stderr, "\t\t\tutf8 : UTF8 with CRLF (MS Windows)\n");
64    fprintf(stderr, "\t\t\thtml : HTML with CSS and DIV support\n");
65    fprintf(stderr, "\t\t\thtml : Pure HTML3 with tables\n");
66    fprintf(stderr, "\t\t\tirc  : IRC with ctrl-k codes\n");
67    fprintf(stderr, "\t\t\tps   : Postscript\n");
68    fprintf(stderr, "\t\t\tsvg  : Scalable Vector Graphics\n");
69    fprintf(stderr, "\t\t\ttga  : Targa Image\n\n");
70}
71
72int main(int argc, char **argv)
73{
74    /* libcucul context */
75    cucul_canvas_t *cv;
76    void *export;
77    unsigned long int len;
78    struct image *i;
79    unsigned int cols = 0, lines = 0;
80    char *format = NULL;
81    char *dither = NULL;
82    float gamma = -1, brightness = -1, contrast = -1;
83
84    if(argc < 2)
85    {
86        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
87        usage(argc, argv);
88        return 1;
89    }
90
91    for(;;)
92    {
93        int option_index = 0;
94        static struct myoption long_options[] =
95        {
96            { "width",       1, NULL, 'W' },
97            { "height",      1, NULL, 'H' },
98            { "format",      1, NULL, 'f' },
99            { "dither",      1, NULL, 'd' },
100            { "gamma",       1, NULL, 'g' },
101            { "brightness",  1, NULL, 'b' },
102            { "contrast",    1, NULL, 'c' },
103            { "help",        0, NULL, 'h' },
104        };
105        int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:h", long_options, &option_index);
106        if(c == -1)
107            break;
108
109        switch(c)
110        {
111        case 'W': /* --width */
112            cols = atoi(myoptarg);
113            break;
114        case 'H': /* --height */
115            lines = atoi(myoptarg);
116            break;
117        case 'f': /* --format */
118            format = myoptarg;
119            break;
120        case 'd': /* --dither */
121            dither = myoptarg;
122            break;
123        case 'h': /* --help */
124            usage(argc, argv);
125            return 0;
126            break;
127        case 'g': /* --gamma */
128            gamma = atof(myoptarg);
129            break;
130        case 'b': /* --brightness */
131            brightness = atof(myoptarg);
132            break;
133        case 'c': /* --contrast */
134            contrast = atof(myoptarg);
135            break;
136        default:
137            return 1;
138            break;
139        }
140    }
141
142
143    cv = cucul_create_canvas(0, 0);
144    if(!cv)
145    {
146        fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
147        return 1;
148    }
149
150    i = load_image(argv[argc-1]);
151    if(!i)
152    {
153        fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]);
154        cucul_free_canvas(cv);
155        return 1;
156    }
157
158    /* Assume a 6×10 font */
159    if(!cols && !lines)
160    {
161        cols = 60;
162        lines = cols * i->h * 6 / i->w / 10;
163    }
164    else if(cols && !lines)
165    {
166        lines = cols * i->h * 6 / i->w / 10;
167    }
168    else if(!cols && lines)
169    {
170        cols = lines * i->w * 10 / i->h / 6;
171    }
172
173
174    cucul_set_canvas_size(cv, cols, lines);
175    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
176    cucul_clear_canvas(cv);
177    if(cucul_set_dither_algorithm(i->dither, dither?dither:"fstein"))
178    {
179        fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither);
180        unload_image(i);
181        cucul_free_canvas(cv);
182        return -1;
183    }
184
185    if(brightness!=-1) cucul_set_dither_brightness (i->dither, brightness);
186    if(contrast!=-1) cucul_set_dither_contrast (i->dither, contrast);
187    if(gamma!=-1) cucul_set_dither_gamma (i->dither, gamma);
188
189    cucul_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
190
191    unload_image(i);
192
193    export = cucul_export_memory(cv, format?format:"ansi", &len);
194    if(!export)
195    {
196        fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format);
197    }
198    else
199    {
200        fwrite(export, len, 1, stdout);
201        free(export);
202    }
203
204    cucul_free_canvas(cv);
205
206    return 0;
207}
208
Note: See TracBrowser for help on using the repository browser.