Browse Source

* Add php bindings for 9 functions

* Modify php bloc headers in samples programs
* Add list of import/export formats in examples/drivers.php
tags/v0.99.beta17
Nicolas Vion nico 16 years ago
parent
commit
f3cf74d37f
6 changed files with 96 additions and 34 deletions
  1. +1
    -1
      caca-php/examples/demo.php
  2. +5
    -1
      caca-php/examples/dithering.php
  3. +10
    -5
      caca-php/examples/drivers.php
  4. +1
    -1
      caca-php/examples/example1.php
  5. +78
    -24
      caca-php/php_caca.c
  6. +1
    -2
      caca-php/php_caca.h

+ 1
- 1
caca-php/examples/demo.php View File

@@ -1,5 +1,5 @@
#!/usr/bin/php5 #!/usr/bin/php5
<?
<?php
/* /*
* demo.php demo for libcaca php binding * demo.php demo for libcaca php binding
* Copyright (c) 2008 Nicolas Vion <nico@yojik.eu> * Copyright (c) 2008 Nicolas Vion <nico@yojik.eu>


+ 5
- 1
caca-php/examples/dithering.php View File

@@ -1,6 +1,10 @@
#!/usr/bin/php5 #!/usr/bin/php5
<?
<?php

$img = imagecreatefrompng(dirname(__FILE__)."/logo-caca.png"); $img = imagecreatefrompng(dirname(__FILE__)."/logo-caca.png");
if (!$img)
die("Can not open image.\n");

$dither = caca_create_dither_gd($img); $dither = caca_create_dither_gd($img);
if (!$dither) if (!$dither)
die("Can not create dither. Maybe this image is not truecolor.\n"); die("Can not create dither. Maybe this image is not truecolor.\n");


+ 10
- 5
caca-php/examples/drivers.php View File

@@ -1,6 +1,5 @@
#!/usr/bin/php5 #!/usr/bin/php5
<?

<?php


//--- Just for fun ---// //--- Just for fun ---//


@@ -44,10 +43,16 @@ foreach($list as $type => $name)
echo "* $name ($type)\n"; echo "* $name ($type)\n";
echo "\n"; echo "\n";


echo "Available export modules:\n";
echo "Available import formats:\n";
$list = caca_get_import_list();
foreach($list as $format => $name)
echo "* $name ($format)\n";
echo "\n";

echo "Available export formats:\n";
$list = caca_get_export_list(); $list = caca_get_export_list();
foreach($list as $type => $name)
echo "* $name ($type)\n";
foreach($list as $format => $name)
echo "* $name ($format)\n";
echo "\n"; echo "\n";


echo "Available caca fonts:\n"; echo "Available caca fonts:\n";


+ 1
- 1
caca-php/examples/example1.php View File

@@ -1,5 +1,5 @@
#!/usr/bin/php5 #!/usr/bin/php5
<?
<?php


$pig_str = <<<EOT $pig_str = <<<EOT


+ 78
- 24
caca-php/php_caca.c View File

@@ -21,8 +21,6 @@
static function_entry caca_functions[] = { static function_entry caca_functions[] = {
PHP_FE(caca_create_event, NULL) PHP_FE(caca_create_event, NULL)
PHP_FE(caca_create_canvas, NULL) PHP_FE(caca_create_canvas, NULL)
PHP_FE(caca_manage_canvas, NULL)
PHP_FE(caca_unmanage_canvas, NULL)
PHP_FE(caca_set_canvas_size, NULL) PHP_FE(caca_set_canvas_size, NULL)
PHP_FE(caca_get_canvas_width, NULL) PHP_FE(caca_get_canvas_width, NULL)
PHP_FE(caca_get_canvas_height, NULL) PHP_FE(caca_get_canvas_height, NULL)
@@ -116,6 +114,7 @@ static function_entry caca_functions[] = {
PHP_FE(caca_canvas_set_figfont, NULL) PHP_FE(caca_canvas_set_figfont, NULL)
PHP_FE(caca_put_figchar, NULL) PHP_FE(caca_put_figchar, NULL)
PHP_FE(caca_flush_figlet, NULL) PHP_FE(caca_flush_figlet, NULL)
PHP_FE(caca_file_open, NULL)
PHP_FE(caca_file_close, NULL) PHP_FE(caca_file_close, NULL)
PHP_FE(caca_file_tell, NULL) PHP_FE(caca_file_tell, NULL)
PHP_FE(caca_file_read, NULL) PHP_FE(caca_file_read, NULL)
@@ -361,18 +360,16 @@ void *fetch_external_resource(zval *_zval, char const *type_name) {
//Fetch buffer of pixels from gdImage //Fetch buffer of pixels from gdImage


void *gd_get_pixels(gdImage *img) { void *gd_get_pixels(gdImage *img) {
if (img->trueColor) {
int line_size = img->sx * sizeof(int);
void *result = malloc(img->sy * line_size);
int j;
for (j = 0; j < img->sy; j++)
memcpy(result + (j * line_size), (const void *) img->tpixels[j], line_size);
return result;
}
return NULL;
}

if (!img->trueColor)
return NULL;


int line_size = img->sx * sizeof(int);
void *result = malloc(img->sy * line_size);
int j;
for (j = 0; j < img->sy; j++)
memcpy(result + (j * line_size), (const void *) img->tpixels[j], line_size);
return result;
}


//------- PHP Binding's specific functions ----------// //------- PHP Binding's specific functions ----------//


@@ -396,12 +393,6 @@ PHP_FUNCTION(caca_create_canvas) {
ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas); ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas);
} }


PHP_FUNCTION(caca_manage_canvas) {
}

PHP_FUNCTION(caca_unmanage_canvas) {
}

PHP_FUNCTION(caca_set_canvas_size) { PHP_FUNCTION(caca_set_canvas_size) {
zval *_zval; zval *_zval;
long width, height = 0; long width, height = 0;
@@ -1239,18 +1230,59 @@ PHP_FUNCTION(caca_render_canvas) {
} }


PHP_FUNCTION(caca_canvas_set_figfont) { PHP_FUNCTION(caca_canvas_set_figfont) {
zval *_zval;
char *font;
long font_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rs", &_zval, &font, &font_len) == FAILURE) {
RETURN_FALSE;
}
caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
RETURN_SUCCESS(caca_canvas_set_figfont(canvas, font));
} }


PHP_FUNCTION(caca_put_figchar) { PHP_FUNCTION(caca_put_figchar) {
} }


PHP_FUNCTION(caca_flush_figlet) { PHP_FUNCTION(caca_flush_figlet) {
zval *_zval;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
RETURN_FALSE;
}
caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
RETURN_SUCCESS(caca_flush_figlet(canvas));
}

PHP_FUNCTION(caca_file_open) {
char *path, *mode;
long path_len, mode_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &path, &path_len, &mode, &mode_len) == FAILURE) {
RETURN_FALSE;
}
caca_file_t *file = caca_file_open(path, mode);
ZEND_REGISTER_RESOURCE(return_value, file, le_caca_file);
} }


PHP_FUNCTION(caca_file_close) { PHP_FUNCTION(caca_file_close) {
zval *_zval;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
RETURN_FALSE;
}
caca_file_t *file;
ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
//TODO: check that file was not already closed
RETURN_SUCCESS(caca_file_close(file));
} }


PHP_FUNCTION(caca_file_tell) { PHP_FUNCTION(caca_file_tell) {
zval *_zval;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
RETURN_FALSE;
}
caca_file_t *file;
ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
RETURN_LONG(caca_file_tell(file));
} }


PHP_FUNCTION(caca_file_read) { PHP_FUNCTION(caca_file_read) {
@@ -1263,25 +1295,47 @@ PHP_FUNCTION(caca_file_gets) {
} }


PHP_FUNCTION(caca_file_eof) { PHP_FUNCTION(caca_file_eof) {
zval *_zval;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
RETURN_FALSE;
}
caca_file_t *file;
ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
RETURN_BOOL(caca_file_eof(file) != 0);
} }


PHP_FUNCTION(caca_import_string) { PHP_FUNCTION(caca_import_string) {
zval *_zval; zval *_zval;
char *src, *type;
long src_len, type_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &src, &src_len, &type, &type_len) == FAILURE) {
char *src, *format;
long src_len, format_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &src, &src_len, &format, &format_len) == FAILURE) {
RETURN_FALSE; RETURN_FALSE;
} }
caca_canvas_t *canvas; caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas); ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);


RETURN_LONG(caca_import_memory(canvas, src, src_len, type));
RETURN_LONG(caca_import_memory(canvas, src, src_len, format));
} }


PHP_FUNCTION(caca_import_file) { PHP_FUNCTION(caca_import_file) {
zval *_zval;
char *filename, *format;
long filename_len, format_len = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rss", &_zval, &filename, &filename_len, &format, &format_len) == FAILURE) {
RETURN_FALSE;
}
caca_canvas_t *canvas;
ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);

RETURN_LONG(caca_import_file(canvas, filename, format));
} }


PHP_FUNCTION(caca_get_import_list) { PHP_FUNCTION(caca_get_import_list) {
char const * const *list = caca_get_import_list();
int i;
array_init(return_value);
for(i = 0; list[i]; i += 2)
add_assoc_string(return_value, (char*) list[i], (char*) list[i + 1], 1);
} }


PHP_FUNCTION(caca_export_string) { PHP_FUNCTION(caca_export_string) {
@@ -1401,7 +1455,7 @@ PHP_FUNCTION(caca_set_display_time) {
PHP_FUNCTION(caca_get_display_time) { PHP_FUNCTION(caca_get_display_time) {
caca_display_t *display; caca_display_t *display;
FETCH_DISPLAY(display); FETCH_DISPLAY(display);
RETURN_LONG(caca_get_display_time(display)); //TODO: check return value
RETURN_LONG(caca_get_display_time(display));
} }


PHP_FUNCTION(caca_get_display_width) { PHP_FUNCTION(caca_get_display_width) {


+ 1
- 2
caca-php/php_caca.h View File

@@ -38,8 +38,6 @@ PHP_MINFO_FUNCTION(caca);
PHP_FUNCTION(caca_create_event); PHP_FUNCTION(caca_create_event);


PHP_FUNCTION(caca_create_canvas); PHP_FUNCTION(caca_create_canvas);
PHP_FUNCTION(caca_manage_canvas);
PHP_FUNCTION(caca_unmanage_canvas);
PHP_FUNCTION(caca_set_canvas_size); PHP_FUNCTION(caca_set_canvas_size);
PHP_FUNCTION(caca_get_canvas_width); PHP_FUNCTION(caca_get_canvas_width);
PHP_FUNCTION(caca_get_canvas_height); PHP_FUNCTION(caca_get_canvas_height);
@@ -133,6 +131,7 @@ PHP_FUNCTION(caca_render_canvas);
PHP_FUNCTION(caca_canvas_set_figfont); PHP_FUNCTION(caca_canvas_set_figfont);
PHP_FUNCTION(caca_put_figchar); PHP_FUNCTION(caca_put_figchar);
PHP_FUNCTION(caca_flush_figlet); PHP_FUNCTION(caca_flush_figlet);
PHP_FUNCTION(caca_file_open);
PHP_FUNCTION(caca_file_close); PHP_FUNCTION(caca_file_close);
PHP_FUNCTION(caca_file_tell); PHP_FUNCTION(caca_file_tell);
PHP_FUNCTION(caca_file_read); PHP_FUNCTION(caca_file_read);


Loading…
Cancel
Save