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