Debian Codegraph @

caca-php/examples/img2txt.php

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