1 | #!/usr/bin/php5 |
---|
2 | <?php |
---|
3 | /* |
---|
4 | * img2txt image to text converter |
---|
5 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
6 | * 2007 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
7 | * All Rights Reserved |
---|
8 | * |
---|
9 | * $Id: img2txt.php 3304 2008-11-07 05:48:32Z bsittler $ |
---|
10 | * |
---|
11 | * This program is free software. It comes without any warranty, to |
---|
12 | * the extent permitted by applicable law. You can redistribute it |
---|
13 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
14 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
15 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
16 | */ |
---|
17 | |
---|
18 | if (php_sapi_name() != "cli") { |
---|
19 | die("You have to run this program with php-cli!\n"); |
---|
20 | } |
---|
21 | |
---|
22 | class MygetoptException extends Exception |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | $myoptind = 0; |
---|
27 | |
---|
28 | function mygetopt($shortopts, $longopts) |
---|
29 | { |
---|
30 | global $argc, $argv, $myoptind; |
---|
31 | if($myoptind < 0) |
---|
32 | { |
---|
33 | $myoptind = 0; |
---|
34 | } |
---|
35 | if(($myoptind + 1) >= $argc) |
---|
36 | { |
---|
37 | return NULL; |
---|
38 | } |
---|
39 | $myoptind ++; |
---|
40 | $nextarg = $argv[$myoptind]; |
---|
41 | $ret = NULL; |
---|
42 | if((substr($nextarg, 0, 1) != '-') |
---|
43 | || |
---|
44 | ($nextarg == '-')) |
---|
45 | { |
---|
46 | $myoptind = $argc - 1; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | $skipopt = $myoptind; |
---|
51 | $skipopts = 1; |
---|
52 | if($nextarg == '--') |
---|
53 | { |
---|
54 | $myoptind = $argc - 1; |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | $opt = NULL; |
---|
59 | $arg = NULL; |
---|
60 | foreach($longopts as $longopt) |
---|
61 | { |
---|
62 | $optional = false; |
---|
63 | $hasarg = false; |
---|
64 | if(($longopt != '::') && substr($longopt, -2) == '::') |
---|
65 | { |
---|
66 | $optional = true; |
---|
67 | $hasarg = true; |
---|
68 | $longopt = substr($longopt, 0, -2); |
---|
69 | } |
---|
70 | else if(($longopt != ':') && substr($longopt, -1) == ':') |
---|
71 | { |
---|
72 | $optional = false; |
---|
73 | $hasarg = true; |
---|
74 | $longopt = substr($longopt, 0, -1); |
---|
75 | } |
---|
76 | if($nextarg == ('--' . $longopt)) |
---|
77 | { |
---|
78 | $opt = '--' . $longopt; |
---|
79 | if($hasarg && ! $optional) |
---|
80 | { |
---|
81 | if(($myoptind + 1) < $argc) |
---|
82 | { |
---|
83 | $myoptind ++; |
---|
84 | $skipopts ++; |
---|
85 | $arg = $argv[$myoptind]; |
---|
86 | } |
---|
87 | else |
---|
88 | { |
---|
89 | throw new MygetoptException("option \"$opt\" requires an argument"); |
---|
90 | } |
---|
91 | } |
---|
92 | break; |
---|
93 | } |
---|
94 | else if(substr($nextarg, 0, strlen('--' . $longopt . '=')) |
---|
95 | == |
---|
96 | ('--' . $longopt . '=')) |
---|
97 | { |
---|
98 | $opt = '--' . $longopt; |
---|
99 | $arg = substr($nextarg, strlen($opt . '=')); |
---|
100 | if(! $hasarg) |
---|
101 | { |
---|
102 | throw new MygetoptException("option \"$opt\" does not allow an argument"); |
---|
103 | } |
---|
104 | break; |
---|
105 | } |
---|
106 | } |
---|
107 | if($opt === NULL) |
---|
108 | { |
---|
109 | for($i = 0; $i < strlen($shortopts); $i ++) |
---|
110 | { |
---|
111 | $optional = false; |
---|
112 | $hasarg = false; |
---|
113 | $shortopt = substr($shortopts, $i, 1); |
---|
114 | if(substr($shortopts, $i + 1, 2) == '::') |
---|
115 | { |
---|
116 | $optional = true; |
---|
117 | $hasarg = true; |
---|
118 | $i += 2; |
---|
119 | } |
---|
120 | else if(substr($shortopts, $i + 1, 1) == ':') |
---|
121 | { |
---|
122 | $hasarg = true; |
---|
123 | } |
---|
124 | if($nextarg |
---|
125 | == |
---|
126 | ('-' . $shortopt)) |
---|
127 | { |
---|
128 | $opt = '-' . $shortopt; |
---|
129 | if($hasarg && ! $optional) |
---|
130 | { |
---|
131 | if(($myoptind + 1) < $argc) |
---|
132 | { |
---|
133 | $myoptind ++; |
---|
134 | $skipopts ++; |
---|
135 | $arg = $argv[$myoptind]; |
---|
136 | } |
---|
137 | else |
---|
138 | { |
---|
139 | throw new MygetoptException("option \"$opt\" requires an argument"); |
---|
140 | } |
---|
141 | } |
---|
142 | break; |
---|
143 | } |
---|
144 | else if(substr($nextarg, 0, strlen('-' . $shortopt)) |
---|
145 | == |
---|
146 | ('-' . $shortopt)) |
---|
147 | { |
---|
148 | $opt = '-' . $shortopt; |
---|
149 | if($hasarg) |
---|
150 | { |
---|
151 | $arg = substr($nextarg, strlen($opt)); |
---|
152 | } |
---|
153 | else |
---|
154 | { |
---|
155 | $argv[$myoptind] = '-' . substr($nextarg, strlen($opt)); |
---|
156 | $myoptind --; |
---|
157 | $skipopts = 0; |
---|
158 | } |
---|
159 | } |
---|
160 | } |
---|
161 | } |
---|
162 | if($opt === NULL) |
---|
163 | { |
---|
164 | if(substr($nextarg, 0, strlen('--')) == '--') |
---|
165 | { |
---|
166 | $longopt = substr($nextarg, strlen('--')); |
---|
167 | if(strpos($longopt, '=') !== false) |
---|
168 | { |
---|
169 | $longopt = substr($longopt, 0, strpos($longopt, '=')); |
---|
170 | } |
---|
171 | throw new MygetoptException("option \"--$longopt\" is not recognized"); |
---|
172 | } |
---|
173 | $shortopt = substr($nextarg, strlen('-'), 1); |
---|
174 | throw new MygetoptException("option \"-$shortopt\" is not recognized"); |
---|
175 | } |
---|
176 | $ret = array($opt, $arg); |
---|
177 | } |
---|
178 | if ($skipopts > 0) |
---|
179 | { |
---|
180 | for($i = 0; $i < $argc; $i ++) |
---|
181 | { |
---|
182 | if(($i < $skipopt) || ($i >= ($skipopt + $skipopts))) |
---|
183 | { |
---|
184 | $new_argv[] = $argv[$i]; |
---|
185 | } |
---|
186 | } |
---|
187 | if($myoptind >= $skipopt) |
---|
188 | { |
---|
189 | $myoptind -= $skipopts; |
---|
190 | } |
---|
191 | $argv = $new_argv; |
---|
192 | $argc = count($argv); |
---|
193 | } |
---|
194 | } |
---|
195 | return $ret; |
---|
196 | } |
---|
197 | |
---|
198 | function usage($argc, $argv) |
---|
199 | { |
---|
200 | fprintf(STDERR, "Usage: %s [OPTIONS]... <IMAGE>\n", $argv[0]); |
---|
201 | fprintf(STDERR, "Convert IMAGE to any text based available format.\n"); |
---|
202 | fprintf(STDERR, "Example : %s -w 80 -f ansi ./caca.png\n\n", $argv[0]); |
---|
203 | fprintf(STDERR, "Options:\n"); |
---|
204 | fprintf(STDERR, " -h, --help\t\t\tThis help\n"); |
---|
205 | fprintf(STDERR, " -v, --version\t\t\tVersion of the program\n"); |
---|
206 | fprintf(STDERR, " -W, --width=WIDTH\t\tWidth of resulting image\n"); |
---|
207 | fprintf(STDERR, " -H, --height=HEIGHT\t\tHeight of resulting image\n"); |
---|
208 | fprintf(STDERR, " -x, --font-width=WIDTH\t\tWidth of output font\n"); |
---|
209 | fprintf(STDERR, " -y, --font-height=HEIGHT\t\tHeight of output font\n"); |
---|
210 | fprintf(STDERR, " -b, --brightness=BRIGHTNESS\tBrightness of resulting image\n"); |
---|
211 | fprintf(STDERR, " -c, --contrast=CONTRAST\tContrast of resulting image\n"); |
---|
212 | fprintf(STDERR, " -g, --gamma=GAMMA\t\tGamma of resulting image\n"); |
---|
213 | fprintf(STDERR, " -d, --dither=DITHER\t\tDithering algorithm to use :\n"); |
---|
214 | $list = caca_get_dither_algorithm_list(caca_create_dither(imagecreate(1, 1))); |
---|
215 | foreach($list as $type => $name) |
---|
216 | { |
---|
217 | fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name); |
---|
218 | } |
---|
219 | |
---|
220 | fprintf(STDERR, " -f, --format=FORMAT\t\tFormat of the resulting image :\n"); |
---|
221 | $list = caca_get_export_list(); |
---|
222 | foreach($list as $type => $name) |
---|
223 | { |
---|
224 | fprintf(STDERR, "\t\t\t%s: %s\n", $type, $name); |
---|
225 | } |
---|
226 | } |
---|
227 | |
---|
228 | function version() |
---|
229 | { |
---|
230 | printf( |
---|
231 | "img2txt Copyright 2006-2007 Sam Hocevar and Jean-Yves Lamoureux\n" . |
---|
232 | "Internet: <sam@zoy.org> <jylam@lnxscene.org> Version: %s\n" . |
---|
233 | "\n" . |
---|
234 | "img2txt, along with its documentation, may be freely copied and distributed.\n" . |
---|
235 | "\n" . |
---|
236 | "The latest version of img2txt is available from the web site,\n" . |
---|
237 | " http://caca.zoy.org/wiki/libcaca in the libcaca package.\n" . |
---|
238 | "\n", |
---|
239 | caca_get_version()); |
---|
240 | } |
---|
241 | function main() |
---|
242 | { |
---|
243 | global $argc, $argv; |
---|
244 | $cols = 0; |
---|
245 | $lines = 0; |
---|
246 | $font_width = 6; |
---|
247 | $font_height = 10; |
---|
248 | $format = NULL; |
---|
249 | $dither = NULL; |
---|
250 | $gamma = $brightness = $contrast = -1.0; |
---|
251 | |
---|
252 | $long_options = array( |
---|
253 | "width:" => 'W', |
---|
254 | "height:" => 'H', |
---|
255 | "font-width:" => 'x', |
---|
256 | "font-height:" => 'y', |
---|
257 | "format:" => 'f', |
---|
258 | "dither:" => 'd', |
---|
259 | "gamma:" => 'g', |
---|
260 | "brightness:" => 'b', |
---|
261 | "contrast:" => 'c', |
---|
262 | "help" => 'h', |
---|
263 | "version" => 'v' |
---|
264 | ); |
---|
265 | |
---|
266 | while($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options))) |
---|
267 | { |
---|
268 | $opt = $opt_and_arg[0]; |
---|
269 | $arg = $opt_and_arg[1]; |
---|
270 | if((substr($opt, 0, 2) == '--') |
---|
271 | && |
---|
272 | array_key_exists(substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : ''), $long_options)) |
---|
273 | { |
---|
274 | $opt = '-' . $long_options[substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : '')]; |
---|
275 | } |
---|
276 | switch($opt) |
---|
277 | { |
---|
278 | case '-W': /* --width */ |
---|
279 | $cols = intval($arg); |
---|
280 | break; |
---|
281 | case '-H': /* --height */ |
---|
282 | $lines = intval($arg); |
---|
283 | break; |
---|
284 | case '-x': /* --width */ |
---|
285 | $font_width = intval($arg); |
---|
286 | break; |
---|
287 | case '-y': /* --height */ |
---|
288 | $font_height = intval($arg); |
---|
289 | break; |
---|
290 | case '-f': /* --format */ |
---|
291 | $format = $arg; |
---|
292 | break; |
---|
293 | case '-d': /* --dither */ |
---|
294 | $dither = $arg; |
---|
295 | break; |
---|
296 | case '-g': /* --gamma */ |
---|
297 | $gamma = floatval($arg); |
---|
298 | break; |
---|
299 | case '-b': /* --brightness */ |
---|
300 | $brightness = floatval($arg); |
---|
301 | break; |
---|
302 | case '-c': /* --contrast */ |
---|
303 | $contrast = floatval($arg); |
---|
304 | break; |
---|
305 | case '-h': /* --help */ |
---|
306 | usage($argc, $argv); |
---|
307 | return 0; |
---|
308 | case '-v': /* --version */ |
---|
309 | version(); |
---|
310 | return 0; |
---|
311 | default: |
---|
312 | return 1; |
---|
313 | } |
---|
314 | } |
---|
315 | |
---|
316 | if($argc != 2) |
---|
317 | { |
---|
318 | fprintf(STDERR, "%s: wrong argument count\n", $argv[0]); |
---|
319 | usage($argc, $argv); |
---|
320 | return 1; |
---|
321 | } |
---|
322 | |
---|
323 | $cv = caca_create_canvas(0, 0); |
---|
324 | if(!$cv) |
---|
325 | { |
---|
326 | fprintf(STDERR, "%s: unable to initialise libcaca\n", $argv[0]); |
---|
327 | return 1; |
---|
328 | } |
---|
329 | |
---|
330 | $i_str = file_get_contents($argv[$argc-1]); |
---|
331 | $i = $i_str ? imagecreatefromstring($i_str) : NULL; |
---|
332 | if(!$i) |
---|
333 | { |
---|
334 | fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc-1]); |
---|
335 | return 1; |
---|
336 | } |
---|
337 | |
---|
338 | /* Assume a 6×10 font */ |
---|
339 | if(!$cols && !$lines) |
---|
340 | { |
---|
341 | $cols = 60; |
---|
342 | $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height; |
---|
343 | } |
---|
344 | else if($cols && !$lines) |
---|
345 | { |
---|
346 | $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height; |
---|
347 | } |
---|
348 | else if(!$cols && $lines) |
---|
349 | { |
---|
350 | $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|
354 | caca_set_canvas_size($cv, $cols, $lines); |
---|
355 | caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT); |
---|
356 | caca_clear_canvas($cv); |
---|
357 | $i_dither = caca_create_dither($i); |
---|
358 | if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein")) |
---|
359 | { |
---|
360 | fprintf(STDERR, "%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein"); |
---|
361 | return -1; |
---|
362 | } |
---|
363 | |
---|
364 | if($brightness!=-1) caca_set_dither_brightness ($i_dither, $brightness); |
---|
365 | if($contrast!=-1) caca_set_dither_contrast ($i_dither, $contrast); |
---|
366 | if($gamma!=-1) caca_set_dither_gamma ($i_dither, $gamma); |
---|
367 | |
---|
368 | caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i); |
---|
369 | |
---|
370 | $export = caca_export_string($cv, $format?$format:"ansi"); |
---|
371 | if(!$export) |
---|
372 | { |
---|
373 | fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format); |
---|
374 | } |
---|
375 | else |
---|
376 | { |
---|
377 | echo $export; |
---|
378 | } |
---|
379 | |
---|
380 | return 0; |
---|
381 | } |
---|
382 | exit(main()); |
---|
383 | ?> |
---|