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 1906 2007-11-06 13:45:18Z 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 | static 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, " -d, --dither=DITHER\tDithering algorithm to use :\n"); |
---|
50 | fprintf(stderr, "\t\t\tnone : Nearest color\n"); |
---|
51 | fprintf(stderr, "\t\t\tordered2 : Ordered 2x2\n"); |
---|
52 | fprintf(stderr, "\t\t\tordered4 : Ordered 4x4\n"); |
---|
53 | fprintf(stderr, "\t\t\tordered8 : Ordered 8x8\n"); |
---|
54 | fprintf(stderr, "\t\t\trandom : Random\n"); |
---|
55 | fprintf(stderr, "\t\t\tfstein : Floyd Steinberg (default)\n"); |
---|
56 | fprintf(stderr, " -f, --format=FORMAT\tFormat of the resulting image :\n"); |
---|
57 | fprintf(stderr, "\t\t\tansi : coulored ANSI (default)\n"); |
---|
58 | fprintf(stderr, "\t\t\tcaca : internal libcaca format\n"); |
---|
59 | fprintf(stderr, "\t\t\tutf8 : UTF8 with CR\n"); |
---|
60 | fprintf(stderr, "\t\t\tutf8 : UTF8 with CRLF (MS Windows)\n"); |
---|
61 | fprintf(stderr, "\t\t\thtml : HTML with CSS and DIV support\n"); |
---|
62 | fprintf(stderr, "\t\t\thtml : Pure HTML3 with tables\n"); |
---|
63 | fprintf(stderr, "\t\t\tirc : IRC with ctrl-k codes\n"); |
---|
64 | fprintf(stderr, "\t\t\tps : Postscript\n"); |
---|
65 | fprintf(stderr, "\t\t\tsvg : Scalable Vector Graphics\n"); |
---|
66 | fprintf(stderr, "\t\t\ttga : Targa Image\n\n"); |
---|
67 | } |
---|
68 | |
---|
69 | int main(int argc, char **argv) |
---|
70 | { |
---|
71 | /* libcucul context */ |
---|
72 | cucul_canvas_t *cv; |
---|
73 | void *export; |
---|
74 | unsigned long int len; |
---|
75 | struct image *i; |
---|
76 | unsigned int cols = 0, lines = 0; |
---|
77 | char *format = NULL; |
---|
78 | char *dither = NULL; |
---|
79 | |
---|
80 | |
---|
81 | if(argc < 2) |
---|
82 | { |
---|
83 | fprintf(stderr, "%s: wrong argument count\n", argv[0]); |
---|
84 | usage(argc, argv); |
---|
85 | return 1; |
---|
86 | } |
---|
87 | |
---|
88 | for(;;) |
---|
89 | { |
---|
90 | int option_index = 0; |
---|
91 | static struct myoption long_options[] = |
---|
92 | { |
---|
93 | { "width", 1, NULL, 'W' }, |
---|
94 | { "height", 1, NULL, 'H' }, |
---|
95 | { "format", 1, NULL, 'f' }, |
---|
96 | { "dither", 1, NULL, 'd' }, |
---|
97 | { "help", 0, NULL, 'h' }, |
---|
98 | }; |
---|
99 | int c = mygetopt(argc, argv, "W:H:f:d:h", long_options, &option_index); |
---|
100 | if(c == -1) |
---|
101 | break; |
---|
102 | |
---|
103 | switch(c) |
---|
104 | { |
---|
105 | case 'W': /* --width */ |
---|
106 | cols = atoi(myoptarg); |
---|
107 | break; |
---|
108 | case 'H': /* --height */ |
---|
109 | lines = atoi(myoptarg); |
---|
110 | break; |
---|
111 | case 'f': /* --format */ |
---|
112 | format = myoptarg; |
---|
113 | break; |
---|
114 | case 'd': /* --dither */ |
---|
115 | dither = myoptarg; |
---|
116 | break; |
---|
117 | case 'h': /* --help */ |
---|
118 | usage(argc, argv); |
---|
119 | return 0; |
---|
120 | break; |
---|
121 | default: |
---|
122 | return 1; |
---|
123 | break; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | cv = cucul_create_canvas(0, 0); |
---|
129 | if(!cv) |
---|
130 | { |
---|
131 | fprintf(stderr, "%s: unable to initialise libcucul\n", argv[0]); |
---|
132 | return 1; |
---|
133 | } |
---|
134 | |
---|
135 | i = load_image(argv[argc-1]); |
---|
136 | if(!i) |
---|
137 | { |
---|
138 | fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[1]); |
---|
139 | cucul_free_canvas(cv); |
---|
140 | return 1; |
---|
141 | } |
---|
142 | |
---|
143 | /* Assume a 6×10 font */ |
---|
144 | if(!cols && !lines) |
---|
145 | { |
---|
146 | cols = 60; |
---|
147 | lines = cols * i->h * 6 / i->w / 10; |
---|
148 | } |
---|
149 | else if(cols && !lines) |
---|
150 | { |
---|
151 | lines = cols * i->h * 6 / i->w / 10; |
---|
152 | } |
---|
153 | else if(!cols && lines) |
---|
154 | { |
---|
155 | cols = lines * i->w * 10 / i->h / 6; |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | cucul_set_canvas_size(cv, cols, lines); |
---|
160 | cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT); |
---|
161 | cucul_clear_canvas(cv); |
---|
162 | cucul_set_dither_algorithm(i->dither, dither?dither:"fstein"); |
---|
163 | cucul_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels); |
---|
164 | |
---|
165 | unload_image(i); |
---|
166 | |
---|
167 | export = cucul_export_memory(cv, format?format:"ansi", &len); |
---|
168 | if(!export) |
---|
169 | { |
---|
170 | fprintf(stderr, "Can't export to format '%s'\n", format); |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | fwrite(export, len, 1, stdout); |
---|
175 | free(export); |
---|
176 | } |
---|
177 | |
---|
178 | cucul_free_canvas(cv); |
---|
179 | |
---|
180 | return 0; |
---|
181 | } |
---|
182 | |
---|