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.php 3297 2008-11-06 17:04:21Z bsittler $ |
---|
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 | |
---|
18 | #if !defined(__KERNEL__) |
---|
19 | # include <stdio.h> |
---|
20 | # include <string.h> |
---|
21 | # include <stdlib.h> |
---|
22 | #endif |
---|
23 | |
---|
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 | |
---|
36 | #include "caca.h" |
---|
37 | |
---|
38 | #include "common-image.h" |
---|
39 | |
---|
40 | #define IMG2TXTVERSION "0.1" |
---|
41 | |
---|
42 | static void usage(int argc, char **argv) |
---|
43 | { |
---|
44 | char const * const * list; |
---|
45 | |
---|
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"); |
---|
50 | fprintf(stderr, " -h, --help\t\t\tThis help\n"); |
---|
51 | fprintf(stderr, " -v, --version\t\t\tVersion of the program\n"); |
---|
52 | fprintf(stderr, " -W, --width=WIDTH\t\tWidth of resulting image\n"); |
---|
53 | fprintf(stderr, " -H, --height=HEIGHT\t\tHeight of resulting image\n"); |
---|
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"); |
---|
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"); |
---|
60 | list = caca_get_dither_algorithm_list(NULL); |
---|
61 | while(*list) |
---|
62 | { |
---|
63 | fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]); |
---|
64 | list += 2; |
---|
65 | } |
---|
66 | |
---|
67 | fprintf(stderr, " -f, --format=FORMAT\t\tFormat of the resulting image :\n"); |
---|
68 | list = caca_get_export_list(); |
---|
69 | while(*list) |
---|
70 | { |
---|
71 | fprintf(stderr, "\t\t\t%s: %s\n", list[0], list[1]); |
---|
72 | list += 2; |
---|
73 | } |
---|
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 |
---|
78 | } |
---|
79 | |
---|
80 | static 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" |
---|
89 | " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" |
---|
90 | "\n", |
---|
91 | caca_get_version(), __DATE__); |
---|
92 | } |
---|
93 | int main(int argc, char **argv) |
---|
94 | { |
---|
95 | /* libcaca context */ |
---|
96 | caca_canvas_t *cv; |
---|
97 | void *export; |
---|
98 | size_t len; |
---|
99 | struct image *i; |
---|
100 | unsigned int cols = 0, lines = 0, font_width = 6, font_height = 10; |
---|
101 | char *format = NULL; |
---|
102 | char *dither = NULL; |
---|
103 | float gamma = -1, brightness = -1, contrast = -1; |
---|
104 | |
---|
105 | if(argc < 2) |
---|
106 | { |
---|
107 | fprintf(stderr, "%s: wrong argument count\n", argv[0]); |
---|
108 | usage(argc, argv); |
---|
109 | return 1; |
---|
110 | } |
---|
111 | |
---|
112 | for(;;) |
---|
113 | { |
---|
114 | int option_index = 0; |
---|
115 | static struct myoption long_options[] = |
---|
116 | { |
---|
117 | { "width", 1, NULL, 'W' }, |
---|
118 | { "height", 1, NULL, 'H' }, |
---|
119 | { "font-width", 1, NULL, 'x' }, |
---|
120 | { "font-height", 1, NULL, 'y' }, |
---|
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' }, |
---|
127 | { "version", 0, NULL, 'v' }, |
---|
128 | }; |
---|
129 | int c = mygetopt(argc, argv, "W:H:f:d:g:b:c:hvx:y:", long_options, &option_index); |
---|
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; |
---|
141 | case 'x': /* --width */ |
---|
142 | font_width = atoi(myoptarg); |
---|
143 | break; |
---|
144 | case 'y': /* --height */ |
---|
145 | font_height = atoi(myoptarg); |
---|
146 | break; |
---|
147 | case 'f': /* --format */ |
---|
148 | format = myoptarg; |
---|
149 | break; |
---|
150 | case 'd': /* --dither */ |
---|
151 | dither = myoptarg; |
---|
152 | break; |
---|
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; |
---|
162 | case 'h': /* --help */ |
---|
163 | usage(argc, argv); |
---|
164 | return 0; |
---|
165 | break; |
---|
166 | case 'v': /* --version */ |
---|
167 | version(); |
---|
168 | return 0; |
---|
169 | break; |
---|
170 | default: |
---|
171 | return 1; |
---|
172 | break; |
---|
173 | } |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | cv = caca_create_canvas(0, 0); |
---|
178 | if(!cv) |
---|
179 | { |
---|
180 | fprintf(stderr, "%s: unable to initialise libcaca\n", argv[0]); |
---|
181 | return 1; |
---|
182 | } |
---|
183 | |
---|
184 | i = load_image(argv[argc-1]); |
---|
185 | if(!i) |
---|
186 | { |
---|
187 | fprintf(stderr, "%s: unable to load %s\n", argv[0], argv[argc-1]); |
---|
188 | caca_free_canvas(cv); |
---|
189 | return 1; |
---|
190 | } |
---|
191 | |
---|
192 | /* Assume a 6×10 font */ |
---|
193 | if(!cols && !lines) |
---|
194 | { |
---|
195 | cols = 60; |
---|
196 | lines = cols * i->h * font_width / i->w / font_height; |
---|
197 | } |
---|
198 | else if(cols && !lines) |
---|
199 | { |
---|
200 | lines = cols * i->h * font_width / i->w / font_height; |
---|
201 | } |
---|
202 | else if(!cols && lines) |
---|
203 | { |
---|
204 | cols = lines * i->w * font_height / i->h / font_width; |
---|
205 | } |
---|
206 | |
---|
207 | |
---|
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")) |
---|
212 | { |
---|
213 | fprintf(stderr, "%s: Can't dither image with algorithm '%s'\n", argv[0], dither); |
---|
214 | unload_image(i); |
---|
215 | caca_free_canvas(cv); |
---|
216 | return -1; |
---|
217 | } |
---|
218 | |
---|
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); |
---|
222 | |
---|
223 | caca_dither_bitmap(cv, 0, 0, cols, lines, i->dither, i->pixels); |
---|
224 | |
---|
225 | unload_image(i); |
---|
226 | |
---|
227 | export = caca_export_memory(cv, format?format:"ansi", &len); |
---|
228 | if(!export) |
---|
229 | { |
---|
230 | fprintf(stderr, "%s: Can't export to format '%s'\n", argv[0], format); |
---|
231 | } |
---|
232 | else |
---|
233 | { |
---|
234 | fwrite(export, len, 1, stdout); |
---|
235 | free(export); |
---|
236 | } |
---|
237 | |
---|
238 | caca_free_canvas(cv); |
---|
239 | |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|