| 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@zoy.org> |
|---|
| 10 | * 2007 Jean-Yves Lamoureux <jylam@lnxscene.org> |
|---|
| 11 | * All Rights Reserved |
|---|
| 12 | * |
|---|
| 13 | * $Id$ |
|---|
| 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 | "Internet: <sam@zoy.org> <jylam@lnxscene.org> 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 | while($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options))) |
|---|
| 271 | { |
|---|
| 272 | $opt = $opt_and_arg[0]; |
|---|
| 273 | $arg = $opt_and_arg[1]; |
|---|
| 274 | if((substr($opt, 0, 2) == '--') |
|---|
| 275 | && |
|---|
| 276 | array_key_exists(substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : ''), $long_options)) |
|---|
| 277 | { |
|---|
| 278 | $opt = '-' . $long_options[substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : '')]; |
|---|
| 279 | } |
|---|
| 280 | switch($opt) |
|---|
| 281 | { |
|---|
| 282 | case '-W': /* --width */ |
|---|
| 283 | $cols = intval($arg); |
|---|
| 284 | break; |
|---|
| 285 | case '-H': /* --height */ |
|---|
| 286 | $lines = intval($arg); |
|---|
| 287 | break; |
|---|
| 288 | case '-x': /* --width */ |
|---|
| 289 | $font_width = intval($arg); |
|---|
| 290 | break; |
|---|
| 291 | case '-y': /* --height */ |
|---|
| 292 | $font_height = intval($arg); |
|---|
| 293 | break; |
|---|
| 294 | case '-f': /* --format */ |
|---|
| 295 | $format = $arg; |
|---|
| 296 | break; |
|---|
| 297 | case '-d': /* --dither */ |
|---|
| 298 | $dither = $arg; |
|---|
| 299 | break; |
|---|
| 300 | case '-g': /* --gamma */ |
|---|
| 301 | $gamma = floatval($arg); |
|---|
| 302 | break; |
|---|
| 303 | case '-b': /* --brightness */ |
|---|
| 304 | $brightness = floatval($arg); |
|---|
| 305 | break; |
|---|
| 306 | case '-c': /* --contrast */ |
|---|
| 307 | $contrast = floatval($arg); |
|---|
| 308 | break; |
|---|
| 309 | case '-h': /* --help */ |
|---|
| 310 | usage($argc, $argv); |
|---|
| 311 | return 0; |
|---|
| 312 | case '-v': /* --version */ |
|---|
| 313 | version(); |
|---|
| 314 | return 0; |
|---|
| 315 | default: |
|---|
| 316 | return 1; |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | if($argc != 2) |
|---|
| 321 | { |
|---|
| 322 | fprintf(STDERR, "%s: wrong argument count\n", $argv[0]); |
|---|
| 323 | usage($argc, $argv); |
|---|
| 324 | return 1; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | $cv = caca_create_canvas(0, 0); |
|---|
| 328 | if(!$cv) |
|---|
| 329 | { |
|---|
| 330 | fprintf(STDERR, "%s: unable to initialise libcaca\n", $argv[0]); |
|---|
| 331 | return 1; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | $i_str = file_get_contents($argv[$argc-1]); |
|---|
| 335 | $i = $i_str ? imagecreatefromstring($i_str) : NULL; |
|---|
| 336 | if(!$i) |
|---|
| 337 | { |
|---|
| 338 | fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc-1]); |
|---|
| 339 | return 1; |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | /* Assume a 6×10 font */ |
|---|
| 343 | if(!$cols && !$lines) |
|---|
| 344 | { |
|---|
| 345 | $cols = 60; |
|---|
| 346 | $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height; |
|---|
| 347 | } |
|---|
| 348 | else if($cols && !$lines) |
|---|
| 349 | { |
|---|
| 350 | $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height; |
|---|
| 351 | } |
|---|
| 352 | else if(!$cols && $lines) |
|---|
| 353 | { |
|---|
| 354 | $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width; |
|---|
| 355 | } |
|---|
| 356 | |
|---|
| 357 | |
|---|
| 358 | caca_set_canvas_size($cv, $cols, $lines); |
|---|
| 359 | caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT); |
|---|
| 360 | caca_clear_canvas($cv); |
|---|
| 361 | $i_dither = caca_create_dither($i); |
|---|
| 362 | if(! caca_set_dither_algorithm($i_dither, $dither?$dither:"fstein")) |
|---|
| 363 | { |
|---|
| 364 | fprintf(STDERR, "%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither?$dither:"fstein"); |
|---|
| 365 | return -1; |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | if($brightness!=-1) caca_set_dither_brightness ($i_dither, $brightness); |
|---|
| 369 | if($contrast!=-1) caca_set_dither_contrast ($i_dither, $contrast); |
|---|
| 370 | if($gamma!=-1) caca_set_dither_gamma ($i_dither, $gamma); |
|---|
| 371 | |
|---|
| 372 | caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i); |
|---|
| 373 | |
|---|
| 374 | $export = caca_export_string($cv, $format?$format:"ansi"); |
|---|
| 375 | if(!$export) |
|---|
| 376 | { |
|---|
| 377 | fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format); |
|---|
| 378 | return -1; |
|---|
| 379 | } |
|---|
| 380 | else |
|---|
| 381 | { |
|---|
| 382 | echo $export; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | return 0; |
|---|
| 386 | } |
|---|
| 387 | exit(main()); |
|---|
| 388 | ?> |
|---|