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

Last change on this file since 1902 was 1902, checked in by Jean-Yves Lamoureux, 13 years ago
  • Replaced img2irc / pic2irc occurences, fixed bad c/c in options list
  • Property svn:keywords set to Id
File size: 4.4 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 1902 2007-11-06 12:57:46Z 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\tThis help\n");
47    fprintf(stderr, "  -W, --width=WIDTH\tWidth of resulting image\n");
48    fprintf(stderr, "  -H, --HEIGHT=HEIGHT\tHeight of resulting image\n");
49    fprintf(stderr, "  -f, --format=FORMAT\tFormat of the resulting image :\n");
50    fprintf(stderr, "\t\t\tansi : coulored ANSI\n");
51    fprintf(stderr, "\t\t\tcaca : internal libcaca format\n");
52    fprintf(stderr, "\t\t\tutf8 : UTF8 with CR\n");
53    fprintf(stderr, "\t\t\tutf8 : UTF8 with CRLF (MS Windows)\n");
54    fprintf(stderr, "\t\t\thtml : HTML with CSS and DIV support\n");
55    fprintf(stderr, "\t\t\thtml : Pure HTML3 with tables\n");
56    fprintf(stderr, "\t\t\tirc  : IRC with ctrl-k codes\n");
57    fprintf(stderr, "\t\t\tps   : Postscript\n");
58    fprintf(stderr, "\t\t\tsvg  : Scalable Vector Graphics\n");
59    fprintf(stderr, "\t\t\ttga  : Targa Image\n\n");
60}
61
62int main(int argc, char **argv)
63{
64    /* libcucul context */
65    cucul_canvas_t *cv;
66    void *export;
67    unsigned long int len;
68    struct image *i;
69    unsigned int cols = 0, lines = 0;
70    char *format = NULL;
71
72
73    if(argc < 2)
74    {
75        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
76        usage(argc, argv);
77        return 1;
78    }
79
80    for(;;)
81    {
82        int option_index = 0;
83        static struct myoption long_options[] =
84        {
85            { "width",  1, NULL, 'W' },
86            { "height", 1, NULL, 'H' },
87            { "format", 1, NULL, 'f' },
88            { "help",   0, NULL, 'h' },
89        };
90        int c = mygetopt(argc, argv, "W:H:f:h", long_options, &option_index);
91        if(c == -1)
92            break;
93
94        switch(c)
95        {
96        case 'W': /* --width */
97            cols = atoi(myoptarg);
98            break;
99        case 'H': /* --height */
100            lines = atoi(myoptarg);
101            break;
102        case 'f': /* --format */
103            format = myoptarg;
104            break;
105        case 'h': /* --help */
106            usage(argc, argv);
107            return 0;
108            break;
109        default:
110            return 1;
111            break;
112        }
113    }
114
115
116
117    cv = cucul_create_canvas(0, 0);
118    if(!cv)
119    {
120        fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]);
121        return 1;
122    }
123
124    i = load_image(argv[argc-1]);
125    if(!i)
126    {
127        fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[1]);
128        cucul_free_canvas(cv);
129        return 1;
130    }
131
132    /* Assume a 6×10 font */
133    if(!cols && !lines)
134    {
135        cols = 60;
136        lines = cols * i->h * 6 / i->w / 10;
137    }
138    else if(cols && !lines)
139    {
140        lines = cols * i->h * 6 / i->w / 10;
141    }
142    else if(!cols && lines)
143    {
144        cols = lines * i->w * 10 / i->h / 6;
145    }
146
147
148    cucul_set_canvas_size(cv, cols, lines);
149    cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
150    cucul_clear_canvas(cv);
151    cucul_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels);
152
153    unload_image(i);
154
155    export = cucul_export_memory(cv, format?format:"ansi", &len);
156    if(!export)
157    {
158        fprintf(stderr, "Can't export to format '%s'\n", format);
159    }
160    else
161    {
162        fwrite(export, len, 1, stdout);
163        free(export);
164    }
165
166    cucul_free_canvas(cv);
167
168    return 0;
169}
170
Note: See TracBrowser for help on using the repository browser.