[3239] | 1 | #!/usr/bin/php5 |
---|
| 2 | <?php |
---|
[622] | 3 | /* |
---|
[2821] | 4 | * export libcaca export test program |
---|
[3239] | 5 | * Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com> |
---|
| 6 | * |
---|
| 7 | * This file is a Php port of "examples/export.c" |
---|
| 8 | * which is: |
---|
[622] | 9 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
| 10 | * All Rights Reserved |
---|
| 11 | * |
---|
| 12 | * $Id: export.php 3245 2008-11-03 20:55:12Z bsittler $ |
---|
| 13 | * |
---|
[1462] | 14 | * This program is free software. It comes without any warranty, to |
---|
[1452] | 15 | * the extent permitted by applicable law. You can redistribute it |
---|
| 16 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
| 17 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
[622] | 18 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
| 19 | */ |
---|
| 20 | |
---|
[3239] | 21 | if (php_sapi_name() != "cli") { |
---|
[3244] | 22 | die("You have to run this program with php-cli!\n"); |
---|
[3239] | 23 | } |
---|
[622] | 24 | |
---|
[3239] | 25 | define('WIDTH', 80); |
---|
| 26 | define('HEIGHT', 32); |
---|
[622] | 27 | |
---|
[3239] | 28 | $pixels = imagecreatetruecolor(256, 256); |
---|
[622] | 29 | |
---|
[3239] | 30 | $exports = caca_get_export_list(); |
---|
[622] | 31 | |
---|
[3239] | 32 | if($argc < 2 || $argc > 3) |
---|
| 33 | { |
---|
[3244] | 34 | $msg = ($argv[0] . ": wrong argument count\n" . |
---|
| 35 | "usage: " . $argv[0] . " [file] <format>\n" . |
---|
| 36 | "where <format> is one of:\n"); |
---|
| 37 | foreach($exports as $format => $name) |
---|
| 38 | $msg .= " \"" . $name . "\" (" . $format . ")\n"; |
---|
| 39 | die($msg); |
---|
[3239] | 40 | } |
---|
[622] | 41 | |
---|
[3239] | 42 | if($argc == 2) |
---|
[622] | 43 | { |
---|
[3244] | 44 | $file = NULL; |
---|
| 45 | $format = $argv[1]; |
---|
[3239] | 46 | } |
---|
| 47 | else |
---|
| 48 | { |
---|
[3244] | 49 | $file = $argv[1]; |
---|
| 50 | $format = $argv[2]; |
---|
[3239] | 51 | } |
---|
[622] | 52 | |
---|
[3239] | 53 | if(! array_key_exists($format, $exports)) |
---|
| 54 | { |
---|
[3244] | 55 | $msg = ($argv[0] . ": unknown format `" . $format . "'\n" . |
---|
| 56 | "please use one of:\n"); |
---|
| 57 | foreach($exports as $format => $name) |
---|
| 58 | $msg .= " \"" . $name . "\" (" . $format . ")\n"; |
---|
| 59 | die($msg); |
---|
[3239] | 60 | } |
---|
[788] | 61 | |
---|
[3239] | 62 | if($file) |
---|
| 63 | { |
---|
[3244] | 64 | $cv = caca_create_canvas(0, 0); |
---|
| 65 | if(caca_import_file($cv, $file, "") < 0) |
---|
| 66 | { |
---|
[3245] | 67 | die($argv[0] . ": `" . $file . "' has unknown format\n"); |
---|
[3244] | 68 | } |
---|
[3239] | 69 | } |
---|
| 70 | else |
---|
| 71 | { |
---|
[3244] | 72 | $cv = caca_create_canvas(WIDTH, HEIGHT); |
---|
[622] | 73 | |
---|
[3244] | 74 | for($y = 0; $y < 256; $y++) |
---|
| 75 | { |
---|
| 76 | for($x = 0; $x < 256; $x++) |
---|
| 77 | { |
---|
| 78 | $r = $x; |
---|
| 79 | $g = (255 - $y + $x) / 2; |
---|
| 80 | $b = $y * (255 - $x) / 256; |
---|
| 81 | imagesetpixel($pixels, $x, $y, imagecolorallocate($pixels, $r, $g, $b)); |
---|
| 82 | } |
---|
| 83 | } |
---|
[622] | 84 | |
---|
[3244] | 85 | $dither = caca_create_dither($pixels); |
---|
| 86 | if(($format == "ansi") || ($format == "utf8")) |
---|
| 87 | caca_set_dither_charset($dither, "shades"); |
---|
| 88 | caca_dither_bitmap($cv, 0, 0, caca_get_canvas_width($cv), |
---|
| 89 | caca_get_canvas_height($cv), $dither, $pixels); |
---|
[622] | 90 | |
---|
[3244] | 91 | caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK); |
---|
| 92 | caca_draw_thin_box($cv, 0, 0, WIDTH - 1, HEIGHT - 1); |
---|
[622] | 93 | |
---|
[3244] | 94 | caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE); |
---|
| 95 | caca_fill_ellipse($cv, WIDTH / 2, HEIGHT / 2, |
---|
| 96 | WIDTH / 4, HEIGHT / 4, ord(' ')); |
---|
[622] | 97 | |
---|
[3244] | 98 | caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK); |
---|
| 99 | caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 6, |
---|
| 100 | " lightgray on black "); |
---|
| 101 | caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT); |
---|
| 102 | caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 5, |
---|
| 103 | " default on transparent "); |
---|
| 104 | caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE); |
---|
| 105 | caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 4, |
---|
| 106 | " black on white "); |
---|
[1417] | 107 | |
---|
[3244] | 108 | caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE); |
---|
| 109 | caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]"); |
---|
| 110 | caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]"); |
---|
| 111 | caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ"); |
---|
| 112 | caca_put_str($cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>"); |
---|
[1417] | 113 | |
---|
[3244] | 114 | caca_set_attr($cv, CACA_BOLD, CACA_DEFAULT); |
---|
| 115 | caca_put_str($cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold"); |
---|
| 116 | caca_set_attr($cv, CACA_BLINK, CACA_DEFAULT); |
---|
| 117 | caca_put_str($cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink"); |
---|
| 118 | caca_set_attr($cv, CACA_ITALICS, CACA_DEFAULT); |
---|
| 119 | caca_put_str($cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics"); |
---|
| 120 | caca_set_attr($cv, CACA_UNDERLINE, CACA_DEFAULT); |
---|
| 121 | caca_put_str($cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline"); |
---|
| 122 | caca_set_attr($cv, 0, CACA_DEFAULT); |
---|
[622] | 123 | |
---|
[3244] | 124 | caca_set_color_ansi($cv, CACA_WHITE, CACA_LIGHTBLUE); |
---|
| 125 | caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA "); |
---|
[1263] | 126 | |
---|
[3244] | 127 | for($x = 0; $x < 16; $x++) |
---|
| 128 | { |
---|
| 129 | caca_set_color_argb($cv, 0xff00 | $x, 0xf00f | ($x << 4)); |
---|
| 130 | caca_put_char($cv, WIDTH / 2 - 7 + $x, HEIGHT / 2 + 6, ord('#')); |
---|
| 131 | } |
---|
[3239] | 132 | } |
---|
[788] | 133 | |
---|
[3239] | 134 | echo caca_export_string($cv, $format); |
---|
[622] | 135 | |
---|
[3239] | 136 | ?> |
---|