浏览代码

* Remove "_gd" suffix from dithering functions

* caca_dithering_bitmap can load palette from gd resource
* Remove caca_set_dither_palette_gd function
* Adapt sample program "dithering.php"
tags/v0.99.beta17
Nicolas Vion nico 16 年前
父节点
当前提交
0d8c061895
共有 3 个文件被更改,包括 29 次插入46 次删除
  1. +2
    -3
      caca-php/examples/dithering.php
  2. +26
    -40
      caca-php/php_caca.c
  3. +1
    -3
      caca-php/php_caca.h

+ 2
- 3
caca-php/examples/dithering.php 查看文件

@@ -5,7 +5,7 @@ $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($img);
if (!$dither)
die("Can not create dither. Maybe this image is not truecolor.\n");

@@ -14,8 +14,7 @@ $display = caca_create_display($canvas);
if (!$display)
die("Can not create display.\n");

//caca_set_dither_palette_gd($dither, $img);
caca_dither_bitmap_gd($canvas, 0, 0, caca_get_canvas_width($canvas), caca_get_canvas_height($canvas), $dither, $img);
caca_dither_bitmap($canvas, 0, 0, caca_get_canvas_width($canvas), caca_get_canvas_height($canvas), $dither, $img);
caca_refresh_display($display);

sleep(5);


+ 26
- 40
caca-php/php_caca.c 查看文件

@@ -85,9 +85,7 @@ static function_entry caca_functions[] = {
PHP_FE(caca_create_frame, NULL)
PHP_FE(caca_free_frame, NULL)
PHP_FE(caca_create_dither, NULL)
PHP_FE(caca_create_dither_gd, NULL)
PHP_FE(caca_set_dither_palette, NULL)
PHP_FE(caca_set_dither_palette_gd, NULL)
PHP_FE(caca_set_dither_brightness, NULL)
PHP_FE(caca_get_dither_brightness, NULL)
PHP_FE(caca_set_dither_gamma, NULL)
@@ -106,7 +104,7 @@ static function_entry caca_functions[] = {
PHP_FE(caca_set_dither_algorithm, NULL)
PHP_FE(caca_get_dither_algorithm_list, NULL)
PHP_FE(caca_get_dither_algorithm, NULL)
PHP_FE(caca_dither_bitmap_gd, NULL)
PHP_FE(caca_dither_bitmap, NULL)
PHP_FE(caca_load_font, NULL)
PHP_FE(caca_get_font_list, NULL)
PHP_FE(caca_get_font_width, NULL)
@@ -380,6 +378,23 @@ void *gd_get_pixels(gdImage *img) {
}
}

int gd_load_palette(gdImage *img, caca_dither_t *dither) {
if (!img || img->trueColor || gdMaxColors != 256) {
return -1;
}

uint32_t r[256], g[256], b[256], a[256];
int i;
for (i = 0; i < 256; i++) {
r[i] = img->red[i] << 4;
g[i] = img->green[i] << 4;
b[i] = img->blue[i] << 4;
a[i] = img->alpha[i] << 4;
}

return caca_set_dither_palette(dither, &r[0], &g[0], &b[0], &a[0]);
}

//------- Caca's functions ----------------//

PHP_FUNCTION(caca_create_canvas) {
@@ -1006,15 +1021,6 @@ PHP_FUNCTION(caca_free_frame) {
}

PHP_FUNCTION(caca_create_dither) {
long bpp, w, h, pitch, rmask, gmask, bmask, amask = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "llllllll", &bpp, &w, &h, &pitch, &rmask, &gmask, &bmask, &amask) == FAILURE) {
RETURN_FALSE;
}
caca_dither_t *dither = caca_create_dither(bpp, w, h, pitch, rmask, gmask, bmask, amask);
ZEND_REGISTER_RESOURCE(return_value, dither, le_caca_dither);
}

PHP_FUNCTION(caca_create_dither_gd) {
zval *_zval;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
RETURN_FALSE;
@@ -1061,32 +1067,6 @@ PHP_FUNCTION(caca_set_dither_palette) {
RETURN_SUCCESS(caca_set_dither_palette(dither, tbl[0], tbl[1], tbl[2], tbl[3]));
}

PHP_FUNCTION(caca_set_dither_palette_gd) {
zval *_zval1, *_zval2;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rr", &_zval1, &_zval2) == FAILURE) {
RETURN_FALSE;
}

caca_dither_t *dither;
ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_dither);

gdImage *img = fetch_external_resource(_zval2, "gd");
if (!img || img->trueColor || gdMaxColors != 256) {
RETURN_FALSE;
}

uint32_t r[256], g[256], b[256], a[256];
int i;
for (i = 0; i < 256; i++) {
r[i] = img->red[i] << 4;
g[i] = img->green[i] << 4;
b[i] = img->blue[i] << 4;
a[i] = img->alpha[i] << 4;
}

RETURN_SUCCESS(caca_set_dither_palette(dither, &r[0], &g[0], &b[0], &a[0]));
}

PHP_FUNCTION(caca_set_dither_brightness) {
zval *_zval;
double value;
@@ -1287,10 +1267,11 @@ PHP_FUNCTION(caca_get_dither_algorithm) {
RETURN_STRING((char *) caca_get_dither_algorithm(dither), 1);
}

PHP_FUNCTION(caca_dither_bitmap_gd) {
PHP_FUNCTION(caca_dither_bitmap) {
zval *_zval1, *_zval2, *_zval3;
long x, y, w, h = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllrr", &_zval1, &x, &y, &w, &h, &_zval2, &_zval3) == FAILURE) {
zend_bool load_palette = 1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rllllrr|b", &_zval1, &x, &y, &w, &h, &_zval2, &_zval3, &load_palette) == FAILURE) {
RETURN_FALSE;
}

@@ -1309,6 +1290,11 @@ PHP_FUNCTION(caca_dither_bitmap_gd) {
RETURN_FALSE;
}

//load palette if image is not true color
if (load_palette && !img->trueColor && gd_load_palette(img, dither) != 0) {
RETURN_FALSE;
}

caca_dither_bitmap(canvas, x, y, w, h, dither, pixels);
free(pixels);
RETURN_TRUE;


+ 1
- 3
caca-php/php_caca.h 查看文件

@@ -101,9 +101,7 @@ PHP_FUNCTION(caca_set_frame_name);
PHP_FUNCTION(caca_create_frame);
PHP_FUNCTION(caca_free_frame);
PHP_FUNCTION(caca_create_dither);
PHP_FUNCTION(caca_create_dither_gd);
PHP_FUNCTION(caca_set_dither_palette);
PHP_FUNCTION(caca_set_dither_palette_gd);
PHP_FUNCTION(caca_set_dither_brightness);
PHP_FUNCTION(caca_get_dither_brightness);
PHP_FUNCTION(caca_set_dither_gamma);
@@ -122,7 +120,7 @@ PHP_FUNCTION(caca_get_dither_charset);
PHP_FUNCTION(caca_set_dither_algorithm);
PHP_FUNCTION(caca_get_dither_algorithm_list);
PHP_FUNCTION(caca_get_dither_algorithm);
PHP_FUNCTION(caca_dither_bitmap_gd);
PHP_FUNCTION(caca_dither_bitmap);
PHP_FUNCTION(caca_load_font);
PHP_FUNCTION(caca_get_font_list);
PHP_FUNCTION(caca_get_font_width);


正在加载...
取消
保存