示例#1
0
文件: img2txt.php 项目: dns/libcaca
function main()
{
    global $argc, $argv;
    $cols = 0;
    $lines = 0;
    $font_width = 6;
    $font_height = 10;
    $format = NULL;
    $dither = NULL;
    $gamma = $brightness = $contrast = -1.0;
    $long_options = array("width:" => 'W', "height:" => 'H', "font-width:" => 'x', "font-height:" => 'y', "format:" => 'f', "dither:" => 'd', "gamma:" => 'g', "brightness:" => 'b', "contrast:" => 'c', "help" => 'h', "version" => 'v');
    try {
        while ($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options))) {
            $opt = $opt_and_arg[0];
            $arg = $opt_and_arg[1];
            if (substr($opt, 0, 2) == '--' && array_key_exists(substr($opt, strlen('--')) . ($arg !== NULL ? ':' : ''), $long_options)) {
                $opt = '-' . $long_options[substr($opt, strlen('--')) . ($arg !== NULL ? ':' : '')];
            }
            switch ($opt) {
                case '-W':
                    /* --width */
                    $cols = intval($arg);
                    break;
                case '-H':
                    /* --height */
                    $lines = intval($arg);
                    break;
                case '-x':
                    /* --width */
                    $font_width = intval($arg);
                    break;
                case '-y':
                    /* --height */
                    $font_height = intval($arg);
                    break;
                case '-f':
                    /* --format */
                    $format = $arg;
                    break;
                case '-d':
                    /* --dither */
                    $dither = $arg;
                    break;
                case '-g':
                    /* --gamma */
                    $gamma = floatval($arg);
                    break;
                case '-b':
                    /* --brightness */
                    $brightness = floatval($arg);
                    break;
                case '-c':
                    /* --contrast */
                    $contrast = floatval($arg);
                    break;
                case '-h':
                    /* --help */
                    usage($argc, $argv);
                    return 0;
                case '-v':
                    /* --version */
                    version();
                    return 0;
                default:
                    return 1;
            }
        }
    } catch (MygetoptException $e) {
        fprintf(STDERR, "%s", $argv[0] . ": " . $e->getMessage() . "\n");
        usage($argc, $argv);
        return 2;
    }
    if ($argc != 2) {
        fprintf(STDERR, "%s: wrong argument count\n", $argv[0]);
        usage($argc, $argv);
        return 1;
    }
    $cv = caca_create_canvas(0, 0);
    if (!$cv) {
        fprintf(STDERR, "%s: unable to initialise libcaca\n", $argv[0]);
        return 1;
    }
    $i_str = file_get_contents($argv[$argc - 1]);
    $i = $i_str ? imagecreatefromstring($i_str) : NULL;
    if (!$i) {
        fprintf(STDERR, "%s: unable to load %s\n", $argv[0], $argv[$argc - 1]);
        return 1;
    }
    /* Assume a 6×10 font */
    if (!$cols && !$lines) {
        $cols = 60;
        $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
    } else {
        if ($cols && !$lines) {
            $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
        } else {
            if (!$cols && $lines) {
                $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width;
            }
        }
    }
    caca_set_canvas_size($cv, $cols, $lines);
    caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
    caca_clear_canvas($cv);
    $i_dither = caca_create_dither($i);
    if (!caca_set_dither_algorithm($i_dither, $dither ? $dither : "fstein")) {
        fprintf(STDERR, "%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither ? $dither : "fstein");
        return -1;
    }
    if ($brightness != -1) {
        caca_set_dither_brightness($i_dither, $brightness);
    }
    if ($contrast != -1) {
        caca_set_dither_contrast($i_dither, $contrast);
    }
    if ($gamma != -1) {
        caca_set_dither_gamma($i_dither, $gamma);
    }
    caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i);
    $export = caca_export_string($cv, $format ? $format : "ansi");
    if (!$export) {
        fprintf(STDERR, "%s: Can't export to format '%s'\n", $argv[0], $format ? $format : "ansi");
        return -1;
    } else {
        echo $export;
    }
    return 0;
}
示例#2
0
文件: caca.php 项目: dns/libcaca
 function Clear()
 {
     return caca_clear_canvas($this->cv);
 }
示例#3
0
 *  to Public License, Version 2, as published by Sam Hocevar. See
 *  http://www.wtfpl.net/ for more details.
 */
?>
<head>
<title>display all possible libcaca colour pairs</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body text="silver" bgcolor="black">
<?php 
$cv = caca_create_canvas(80, 24);
if (!$cv) {
    die("Failed to create canvas\n");
}
caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
caca_clear_canvas($cv);
for ($i = 0; $i < 16; $i++) {
    caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
    caca_put_str($cv, 3, $i + ($i >= 8 ? 3 : 2), "ANSI " . $i);
    for ($j = 0; $j < 16; $j++) {
        caca_set_color_ansi($cv, $i, $j);
        caca_put_str($cv, ($j >= 8 ? 13 : 12) + $j * 4, $i + ($i >= 8 ? 3 : 2), "Aaホ");
    }
}
caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
caca_put_str($cv, 3, 20, "This is bold    This is blink    This is italics    This is underline");
caca_set_attr($cv, CACA_BOLD);
caca_put_str($cv, 3 + 8, 20, "bold");
caca_set_attr($cv, CACA_BLINK);
caca_put_str($cv, 3 + 24, 20, "blink");
caca_set_attr($cv, CACA_ITALICS);
示例#4
0
文件: img2txt.php 项目: dns/libcaca
function main()
{
    global $file, $filename;
    global $argc, $argv;
    global $stderr;
    $cols = 0;
    $lines = 0;
    $font_width = 6;
    $font_height = 10;
    $format = NULL;
    $dither = NULL;
    $gamma = $brightness = $contrast = -1.0;
    $long_options = array("width:" => 'W', "height:" => 'H', "font-width:" => 'x', "font-height:" => 'y', "format:" => 'f', "dither:" => 'd', "gamma:" => 'g', "brightness:" => 'b', "contrast:" => 'c', "help" => 'h', "version" => 'v');
    try {
        while ($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options))) {
            $opt = $opt_and_arg[0];
            $arg = $opt_and_arg[1];
            if (substr($opt, 0, 2) == '--' && array_key_exists(substr($opt, strlen('--')) . ($arg !== NULL ? ':' : ''), $long_options)) {
                $opt = '-' . $long_options[substr($opt, strlen('--')) . ($arg !== NULL ? ':' : '')];
            }
            switch ($opt) {
                case '-W':
                    /* --width */
                    $cols = intval($arg);
                    break;
                case '-H':
                    /* --height */
                    $lines = intval($arg);
                    break;
                case '-x':
                    /* --width */
                    $font_width = intval($arg);
                    break;
                case '-y':
                    /* --height */
                    $font_height = intval($arg);
                    break;
                case '-f':
                    /* --format */
                    $format = $arg;
                    break;
                case '-d':
                    /* --dither */
                    $dither = $arg;
                    break;
                case '-g':
                    /* --gamma */
                    $gamma = floatval($arg);
                    break;
                case '-b':
                    /* --brightness */
                    $brightness = floatval($arg);
                    break;
                case '-c':
                    /* --contrast */
                    $contrast = floatval($arg);
                    break;
                case '-h':
                    /* --help */
                    usage($argc, $argv);
                    return 0;
                case '-v':
                    /* --version */
                    version();
                    return 0;
                default:
                    return 1;
            }
        }
    } catch (MygetoptException $e) {
        $stderr .= $argv[0] . ": " . $e->getMessage() . "\n";
        usage($argc, $argv);
        return 2;
    }
    if (!$file && $argc == 2 && $argv[1] == 'logo-caca.png') {
        $file = 'logo-caca.png';
        $argc = 1;
    } else {
        if ($filename && $file && $argc == 2 && strtolower(basename($argv[1])) == strtolower(basename($filename))) {
            $argc = 1;
        }
    }
    if ($argc == 2) {
        $stderr .= sprintf("%s: image not found\n", $argv[1]);
        return 1;
    }
    if ($argc > 2) {
        $stderr .= sprintf("%s: too many arguments\n", $argv[0]);
        usage($argc, $argv);
        return 1;
    }
    if (!$file) {
        $stderr .= sprintf("%s: no image was provided\n", $argv[0]);
        usage($argc, $argv);
        return 1;
    }
    $cv = caca_create_canvas(0, 0);
    if (!$cv) {
        $stderr .= sprintf("%s: unable to initialise libcaca\n", $argv[0]);
        return 1;
    }
    $i_str = $file ? file_get_contents($file) : NULL;
    $i = $i_str ? imagecreatefromstring($i_str) : NULL;
    if (!$i) {
        $stderr .= sprintf("%s: unable to load %s\n", $argv[0], $filename);
        return 1;
    }
    /* Assume a 6×10 font */
    if (!$cols && !$lines) {
        $cols = 60;
        $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
    } else {
        if ($cols && !$lines) {
            $lines = $cols * imagesy($i) * $font_width / imagesx($i) / $font_height;
        } else {
            if (!$cols && $lines) {
                $cols = $lines * imagesx($i) * $font_height / imagesy($i) / $font_width;
            }
        }
    }
    caca_set_canvas_size($cv, $cols, $lines);
    caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);
    caca_clear_canvas($cv);
    $i_dither = caca_create_dither($i);
    if (!caca_set_dither_algorithm($i_dither, $dither ? $dither : "fstein")) {
        $stderr .= sprintf("%s: Can't dither image with algorithm '%s'\n", $argv[0], $dither ? $dither : "fstein");
        return -1;
    }
    if ($brightness != -1) {
        caca_set_dither_brightness($i_dither, $brightness);
    }
    if ($contrast != -1) {
        caca_set_dither_contrast($i_dither, $contrast);
    }
    if ($gamma != -1) {
        caca_set_dither_gamma($i_dither, $gamma);
    }
    caca_dither_bitmap($cv, 0, 0, $cols, $lines, $i_dither, $i);
    $format = $format ? $format : 'html';
    $export = caca_export_string($cv, $format);
    if (!$export) {
        $stderr .= sprintf("%s: Can't export to format '%s'\n", $argv[0], $format);
        return -1;
    } else {
        $content_type_map = array('ansi' => 'text/plain; charset=CP437', 'utf8' => 'text/plain; charset=UTF-8', 'utf8cr' => 'text/plain; charset=UTF-8', 'html' => 'text/html; charset=UTF-8', 'html3' => 'text/html; charset=UTF-8', 'bbfr' => 'text/plain; charset=UTF-8', 'irc' => 'text/plain; charset=UTF-8', 'ps' => 'application/postscript', 'svg' => 'image/svg+xml', 'tga' => 'image/x-targa');
        $download_extension_map = array('caca' => 'caca', 'ansi' => 'txt', 'utf8' => 'txt', 'utf8cr' => 'txt', 'irc' => 'txt', 'tga' => 'tga');
        $inline_extension_map = array('bbfr' => 'txt', 'ps' => 'ps', 'svg' => 'svg');
        if (!array_key_exists($format, $content_type_map)) {
            $content_type = 'application/octet-stream';
        } else {
            $content_type = $content_type_map[$format];
        }
        header('Content-Type: ' . $content_type);
        if (array_key_exists($format, $download_extension_map)) {
            header('Content-Disposition: attachment; filename=export.' . $download_extension_map[$format]);
        } else {
            if (array_key_exists($format, $inline_extension_map)) {
                header('Content-Disposition: inline; filename=export.' . $inline_extension_map[$format]);
            }
        }
        echo $export;
    }
    return 0;
}
示例#5
0
文件: demo.php 项目: dns/libcaca
function demo_all($cv, $bounds, $outline)
{
    global $i;
    $i++;
    caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
    caca_clear_canvas($cv);
    /* Draw the sun */
    caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK);
    $xo = caca_get_canvas_width($cv) / 4;
    $yo = caca_get_canvas_height($cv) / 4 + 5 * sin(0.03 * $i);
    for ($j = 0; $j < 16; $j++) {
        $xa = $xo - (30 + sin(0.03 * $i) * 8) * sin(0.03 * $i + M_PI * $j / 8);
        $ya = $yo + (15 + sin(0.03 * $i) * 4) * cos(0.03 * $i + M_PI * $j / 8);
        caca_draw_thin_line($cv, $xo, $yo, $xa, $ya);
    }
    $j = 15 + sin(0.03 * $i) * 8;
    caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK);
    caca_fill_ellipse($cv, $xo, $yo, $j, $j / 2, ord('#'));
    caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK);
    caca_draw_ellipse($cv, $xo, $yo, $j, $j / 2, ord('#'));
    /* Draw the pyramid */
    $xo = caca_get_canvas_width($cv) * 5 / 8;
    $yo = 2;
    $xa = caca_get_canvas_width($cv) / 8 + sin(0.03 * $i) * 5;
    $ya = caca_get_canvas_height($cv) / 2 + cos(0.03 * $i) * 5;
    $xb = caca_get_canvas_width($cv) - 10 - cos(0.02 * $i) * 10;
    $yb = caca_get_canvas_height($cv) * 3 / 4 - 5 + sin(0.02 * $i) * 5;
    $xc = caca_get_canvas_width($cv) / 4 - sin(0.02 * $i) * 5;
    $yc = caca_get_canvas_height($cv) * 3 / 4 + cos(0.02 * $i) * 5;
    caca_set_color_ansi($cv, CACA_GREEN, CACA_BLACK);
    caca_fill_triangle($cv, $xo, $yo, $xb, $yb, $xa, $ya, ord('%'));
    caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK);
    caca_draw_thin_triangle($cv, $xo, $yo, $xb, $yb, $xa, $ya);
    caca_set_color_ansi($cv, CACA_RED, CACA_BLACK);
    caca_fill_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc, ord('#'));
    caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK);
    caca_draw_thin_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc);
    caca_set_color_ansi($cv, CACA_BLUE, CACA_BLACK);
    caca_fill_triangle($cv, $xo, $yo, $xb, $yb, $xc, $yc, ord('%'));
    caca_set_color_ansi($cv, CACA_YELLOW, CACA_BLACK);
    caca_draw_thin_triangle($cv, $xo, $yo, $xb, $yb, $xc, $yc);
    /* Draw a background triangle */
    $xa = 2;
    $ya = 2;
    $xb = caca_get_canvas_width($cv) - 3;
    $yb = caca_get_canvas_height($cv) / 2;
    $xc = caca_get_canvas_width($cv) / 3;
    $yc = caca_get_canvas_height($cv) - 3;
    caca_set_color_ansi($cv, CACA_CYAN, CACA_BLACK);
    caca_draw_thin_triangle($cv, $xa, $ya, $xb, $yb, $xc, $yc);
    $xo = caca_get_canvas_width($cv) / 2 + cos(0.027 * $i) * caca_get_canvas_width($cv) / 3;
    $yo = caca_get_canvas_height($cv) / 2 - sin(0.027 * $i) * caca_get_canvas_height($cv) / 2;
    caca_draw_thin_line($cv, $xa, $ya, $xo, $yo);
    caca_draw_thin_line($cv, $xb, $yb, $xo, $yo);
    caca_draw_thin_line($cv, $xc, $yc, $xo, $yo);
    /* Draw a trail behind the foreground sprite */
    for ($j = $i - 60; $j < $i; $j++) {
        $delta = caca_rand(-5, 6);
        caca_set_color_ansi($cv, caca_rand(0, 16), caca_rand(0, 16));
        caca_put_char($cv, caca_get_canvas_width($cv) / 2 + cos(0.02 * $j) * ($delta + caca_get_canvas_width($cv) / 4), caca_get_canvas_height($cv) / 2 + sin(0.02 * $j) * ($delta + caca_get_canvas_height($cv) / 3), ord('#'));
    }
}