- Timestamp:
- Nov 3, 2008, 8:42:38 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcaca/trunk/caca-php/examples/export.php
-
Property
svn:executable
set to
*
r3238 r3239 1 #!/usr/bin/php5 2 <?php 1 3 /* 2 4 * export libcaca export test program 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: 3 9 * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> 4 10 * All Rights Reserved … … 13 19 */ 14 20 15 #include "config.h" 21 if (php_sapi_name() != "cli") { 22 die("You have to run this program with php-cli!\n"); 23 } 16 24 17 #if !defined(__KERNEL__) 18 # include <stdio.h> 19 # include <stdlib.h> 20 # include <string.h> 21 #endif 25 define('WIDTH', 80); 26 define('HEIGHT', 32); 22 27 23 #include "caca.h" 28 $pixels = imagecreatetruecolor(256, 256); 24 29 25 #define WIDTH 80 26 #define HEIGHT 32 30 $exports = caca_get_export_list(); 27 31 28 uint32_t pixels[256*256]; 32 if($argc < 2 || $argc > 3) 33 { 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); 40 } 29 41 30 i nt main(int argc, char *argv[])42 if($argc == 2) 31 43 { 32 caca_canvas_t *cv; 33 caca_dither_t *dither; 34 void *buffer; 35 char *file, *format; 36 char const * const * exports, * const * p; 37 size_t len; 38 int x, y; 44 $file = NULL; 45 $format = $argv[1]; 46 } 47 else 48 { 49 $file = $argv[1]; 50 $format = $argv[2]; 51 } 39 52 40 exports = caca_get_export_list(); 53 if(! array_key_exists($format, $exports)) 54 { 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); 60 } 41 61 42 if(argc < 2 || argc > 3) 62 if($file) 63 { 64 $cv = caca_create_canvas(0, 0); 65 if(caca_import_file($cv, $file, "") < 0) 43 66 { 44 fprintf(stderr, "%s: wrong argument count\n", argv[0]); 45 fprintf(stderr, "usage: %s [file] <format>\n", argv[0]); 46 fprintf(stderr, "where <format> is one of:\n"); 47 for(p = exports; *p; p += 2) 48 fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1)); 49 exit(-1); 67 die($argv[0] . ": `" . $file . "' has unknown $format\n"); 50 68 } 69 } 70 else 71 { 72 $cv = caca_create_canvas(WIDTH, HEIGHT); 51 73 52 if(argc == 2)74 for($y = 0; $y < 256; $y++) 53 75 { 54 file = NULL; 55 format = argv[1]; 56 } 57 else 58 { 59 file = argv[1]; 60 format = argv[2]; 61 } 62 63 for(p = exports; *p; p += 2) 64 if(!strcasecmp(format, *p)) 65 break; 66 67 if(!*p) 68 { 69 fprintf(stderr, "%s: unknown format `%s'\n", argv[0], format); 70 fprintf(stderr, "please use one of:\n"); 71 for(p = exports; *p; p += 2) 72 fprintf(stderr, " \"%s\" (%s)\n", *p, *(p + 1)); 73 exit(-1); 74 } 75 76 if(file) 77 { 78 cv = caca_create_canvas(0, 0); 79 if(caca_import_file(cv, file, "") < 0) 76 for($x = 0; $x < 256; $x++) 80 77 { 81 fprintf(stderr, "%s: `%s' has unknown format\n", argv[0], file); 82 exit(-1); 83 } 84 } 85 else 86 { 87 cv = caca_create_canvas(WIDTH, HEIGHT); 88 89 for(y = 0; y < 256; y++) 90 { 91 for(x = 0; x < 256; x++) 92 { 93 uint32_t r = x; 94 uint32_t g = (255 - y + x) / 2; 95 uint32_t b = y * (255 - x) / 256; 96 pixels[y * 256 + x] = (r << 16) | (g << 8) | (b << 0); 97 } 98 } 99 100 dither = caca_create_dither(32, 256, 256, 4 * 256, 101 0x00ff0000, 0x0000ff00, 0x000000ff, 0x0); 102 if(!strcmp(format, "ansi") || !strcmp(format, "utf8")) 103 caca_set_dither_charset(dither, "shades"); 104 caca_dither_bitmap(cv, 0, 0, caca_get_canvas_width(cv), 105 caca_get_canvas_height(cv), dither, pixels); 106 caca_free_dither(dither); 107 108 caca_set_color_ansi(cv, CACA_WHITE, CACA_BLACK); 109 caca_draw_thin_box(cv, 0, 0, WIDTH - 1, HEIGHT - 1); 110 111 caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); 112 caca_fill_ellipse(cv, WIDTH / 2, HEIGHT / 2, 113 WIDTH / 4, HEIGHT / 4, ' '); 114 115 caca_set_color_ansi(cv, CACA_LIGHTGRAY, CACA_BLACK); 116 caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 6, 117 " lightgray on black "); 118 caca_set_color_ansi(cv, CACA_DEFAULT, CACA_TRANSPARENT); 119 caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 5, 120 " default on transparent "); 121 caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); 122 caca_put_str(cv, WIDTH / 2 - 12, HEIGHT / 2 - 4, 123 " black on white "); 124 125 caca_set_color_ansi(cv, CACA_BLACK, CACA_WHITE); 126 caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 3, "[<><><><> <>--<>]"); 127 caca_put_str(cv, WIDTH / 2 - 8, HEIGHT / 2 - 2, "[ドラゴン ボーレ]"); 128 caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2 + 2, "äβç ░▒▓█▓▒░ ΔЗҒ"); 129 caca_put_str(cv, WIDTH / 2 - 5, HEIGHT / 2 + 4, "(\") \\o/ <&>"); 130 131 caca_set_attr(cv, CACA_BOLD); 132 caca_put_str(cv, WIDTH / 2 - 16, HEIGHT / 2 + 3, "Bold"); 133 caca_set_attr(cv, CACA_BLINK); 134 caca_put_str(cv, WIDTH / 2 - 9, HEIGHT / 2 + 3, "Blink"); 135 caca_set_attr(cv, CACA_ITALICS); 136 caca_put_str(cv, WIDTH / 2 - 1, HEIGHT / 2 + 3, "Italics"); 137 caca_set_attr(cv, CACA_UNDERLINE); 138 caca_put_str(cv, WIDTH / 2 + 8, HEIGHT / 2 + 3, "Underline"); 139 caca_set_attr(cv, 0); 140 141 caca_set_color_ansi(cv, CACA_WHITE, CACA_LIGHTBLUE); 142 caca_put_str(cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA "); 143 144 for(x = 0; x < 16; x++) 145 { 146 caca_set_color_argb(cv, 0xff00 | x, 0xf00f | (x << 4)); 147 caca_put_char(cv, WIDTH / 2 - 7 + x, HEIGHT / 2 + 6, '#'); 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)); 148 82 } 149 83 } 150 84 151 buffer = caca_export_memory(cv, format, &len); 152 fwrite(buffer, len, 1, stdout); 153 free(buffer); 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); 154 90 155 caca_free_canvas(cv); 91 caca_set_color_ansi($cv, CACA_WHITE, CACA_BLACK); 92 caca_draw_thin_box($cv, 0, 0, WIDTH - 1, HEIGHT - 1); 156 93 157 return 0; 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(' ')); 97 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 "); 107 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/ <&>"); 113 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); 123 124 caca_set_color_ansi($cv, CACA_WHITE, CACA_LIGHTBLUE); 125 caca_put_str($cv, WIDTH / 2 - 7, HEIGHT / 2, " LIBCACA "); 126 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 } 158 132 } 159 133 134 echo caca_export_string($cv, $format); 135 136 ?> -
Property
svn:executable
set to
Note: See TracChangeset
for help on using the changeset viewer.