浏览代码

export.php is now actually php, and appears to work identically to its

C predecessor.
tags/v0.99.beta17
Ben Wiley Sittler bsittler 16 年前
父节点
当前提交
ea2f4ff159
共有 1 个文件被更改,包括 103 次插入126 次删除
  1. +103
    -126
      caca-php/examples/export.php

+ 103
- 126
caca-php/examples/export.php 查看文件

@@ -1,5 +1,11 @@
#!/usr/bin/php5
<?php
/* /*
* export libcaca export test program * export libcaca export test program
* Copyright (c) 2008 Benjamin C. Wiley Sittler <bsittler@gmail.com>
*
* This file is a Php port of "examples/export.c"
* which is:
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org> * Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* All Rights Reserved * All Rights Reserved
* *
@@ -12,148 +18,119 @@
* http://sam.zoy.org/wtfpl/COPYING for more details. * http://sam.zoy.org/wtfpl/COPYING for more details.
*/ */


#include "config.h" if (php_sapi_name() != "cli") {

die("You have to run this program with php-cli!\n");
#if !defined(__KERNEL__) }
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
#endif


#include "caca.h" define('WIDTH', 80);
define('HEIGHT', 32);


#define WIDTH 80 $pixels = imagecreatetruecolor(256, 256);
#define HEIGHT 32


uint32_t pixels[256*256]; $exports = caca_get_export_list();


int main(int argc, char *argv[]) if($argc < 2 || $argc > 3)
{ {
caca_canvas_t *cv; $msg = ($argv[0] . ": wrong argument count\n" .
caca_dither_t *dither; "usage: " . $argv[0] . " [file] <format>\n" .
void *buffer; "where <format> is one of:\n");
char *file, *format; foreach($exports as $format => $name)
char const * const * exports, * const * p; $msg .= " \"" . $name . "\" (" . $format . ")\n";
size_t len; die($msg);
int x, y; }

exports = caca_get_export_list();

if(argc < 2 || argc > 3)
{
fprintf(stderr, "%s: wrong argument count\n", argv[0]);
fprintf(stderr, "usage: %s [file] <format>\n", argv[0]);
fprintf(stderr, "where <format> is one of:\n");
for(p = exports; *p; p += 2)
fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1));
exit(-1);
}


if(argc == 2) if($argc == 2)
{ {
file = NULL; $file = NULL;
format = argv[1]; $format = $argv[1];
} }
else else
{ {
file = argv[1]; $file = $argv[1];
format = argv[2]; $format = $argv[2];
} }


for(p = exports; *p; p += 2) if(! array_key_exists($format, $exports))
if(!strcasecmp(format, *p)) {
break; $msg = ($argv[0] . ": unknown $format `" . $format . "'\n" .
"please use one of:\n");
foreach($exports as $format => $name)
$msg .= " \"" . $name . "\" (" . $format . ")\n";
die($msg);
}


if(!*p) if($file)
{
$cv = caca_create_canvas(0, 0);
if(caca_import_file($cv, $file, "") < 0)
{ {
fprintf(stderr, "%s: unknown format `%s'\n", argv[0], format); die($argv[0] . ": `" . $file . "' has unknown $format\n");
fprintf(stderr, "please use one of:\n");
for(p = exports; *p; p += 2)
fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1));
exit(-1);
} }
}
else
{
$cv = caca_create_canvas(WIDTH, HEIGHT);


if(file) for($y = 0; $y < 256; $y++)
{ {
cv = caca_create_canvas(0, 0); for($x = 0; $x < 256; $x++)
if(caca_import_file(cv, file, "") < 0)
{ {
fprintf(stderr, "%s: `%s' has unknown format\n", argv[0], file); $r = $x;
exit(-1); $g = (255 - $y + $x) / 2;
$b = $y * (255 - $x) / 256;
imagesetpixel($pixels, $x, $y, imagecolorallocate($pixels, $r, $g, $b));
} }
} }
else
{
cv = caca_create_canvas(WIDTH, HEIGHT);

for(y = 0; y < 256; y++)
{
for(x = 0; x < 256; x++)
{
uint32_t r = x;
uint32_t g = (255 - y + x) / 2;
uint32_t b = y * (255 - x) / 256;
pixels[y * 256 + x] = (r << 16) | (g << 8) | (b << 0);
}
}


dither = caca_create_dither(32, 256, 256, 4 * 256, $dither = caca_create_dither($pixels);
0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); if(($format == "ansi") || ($format == "utf8"))
if(!strcmp(format, "ansi") || !strcmp(format, "utf8")) caca_set_dither_charset($dither, "shades");
caca_set_dither_charset(dither, "shades"); caca_dither_bitmap($cv, 0, 0, caca_get_canvas_width($cv),
caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), caca_get_canvas_height($cv), $dither, $pixels);
caca_get_canvas_height(cv), dither, pixels); caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK);
caca_free_dither(dither); caca_draw_thin_box($cv, 0, 0, WIDTH - 1, HEIGHT - 1);

caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK); caca_fill_ellipse($cv, WIDTH / 2, HEIGHT / 2,
caca_draw_thin_box(cv, 0, 0, WIDTH - 1, HEIGHT - 1); WIDTH / 4, HEIGHT / 4, ord(' '));

caca_set_color_ansi($cv, CACA_LIGHTGRAY, CACA_BLACK);
caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 6,
caca_fill_ellipse(cv, WIDTH / 2, HEIGHT / 2, " lightgray on black ");
WIDTH / 4, HEIGHT / 4, ' '); caca_set_color_ansi($cv, CACA_DEFAULT, CACA_TRANSPARENT);

caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 5,
caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK); " default on transparent ");
caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 6, caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
" lightgray on black "); caca_put_str($cv, WIDTH / 2 - 12, HEIGHT / 2 - 4,
caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT); " black on white ");
caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 5, caca_set_color_ansi($cv, CACA_BLACK, CACA_WHITE);
" default on transparent "); caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]");
caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); caca_put_str($cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]");
caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 4, caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ");
" black on white "); caca_put_str($cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>");

caca_set_attr($cv, CACA_BOLD, CACA_DEFAULT);
caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); caca_put_str($cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold");
caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]"); caca_set_attr($cv, CACA_BLINK, CACA_DEFAULT);
caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]"); caca_put_str($cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink");
caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ"); caca_set_attr($cv, CACA_ITALICS, CACA_DEFAULT);
caca_put_str(cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>"); caca_put_str($cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics");

caca_set_attr($cv, CACA_UNDERLINE, CACA_DEFAULT);
caca_set_attr(cv, CACA_BOLD); caca_put_str($cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline");
caca_put_str(cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold"); caca_set_attr($cv, 0, CACA_DEFAULT);
caca_set_attr(cv, CACA_BLINK); caca_set_color_ansi($cv, CACA_WHITE, CACA_LIGHTBLUE);
caca_put_str(cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink"); caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA ");
caca_set_attr(cv, CACA_ITALICS); for($x = 0; $x < 16; $x++)
caca_put_str(cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics"); {
caca_set_attr(cv, CACA_UNDERLINE); caca_set_color_argb($cv, 0xff00 | $x, 0xf00f | ($x << 4));
caca_put_str(cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline"); caca_put_char($cv, WIDTH / 2 - 7 + $x, HEIGHT / 2 + 6, ord('#'));
caca_set_attr(cv, 0);

caca_set_color_ansi(cv, CACA_WHITE, CACA_LIGHTBLUE);
caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA ");

for(x = 0; x < 16; x++)
{
caca_set_color_argb(cv, 0xff00 | x, 0xf00f | (x << 4));
caca_put_char(cv, WIDTH / 2 - 7 + x, HEIGHT / 2 + 6, '#');
}
} }

buffer = caca_export_memory(cv, format, &len);
fwrite(buffer, len, 1, stdout);
free(buffer);

caca_free_canvas(cv);

return 0;
} }


echo caca_export_string($cv, $format);

?>

||||||
x
 
000:0
正在加载...
取消
保存