Changeset 3149


Ignore:
Timestamp:
Oct 30, 2008, 2:21:26 PM (15 years ago)
Author:
nico
Message:
  • Add new php specific function caca_load_builtin_font(string name)
  • Add php bindings for caca_get_dither_antialias_list, caca_render_canvas
  • Add a new sample program examples/render.php
Location:
libcaca/trunk/caca-php
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca-php/php_caca.c

    r3148 r3149  
    107107        PHP_FE(caca_dither_bitmap, NULL)
    108108        PHP_FE(caca_load_font, NULL)
     109        PHP_FE(caca_load_builtin_font, NULL)
    109110        PHP_FE(caca_get_font_list, NULL)
    110111        PHP_FE(caca_get_font_width, NULL)
     
    361362
    362363void *gd_get_pixels(gdImage *img) {
     364        void *result;
     365        int j, pitch;           
    363366        if (img->trueColor)  {
    364                 int line_size = img->sx * sizeof(int);
    365                 void *result = malloc(img->sy * line_size);
    366                 int j;         
     367                pitch = img->sx * sizeof(int);
     368                result = malloc(img->sy * pitch);
    367369                for (j = 0; j < img->sy; j++)
    368                         memcpy(result + (j * line_size), (const void *) img->tpixels[j], line_size);
    369                 return result;
     370                        memcpy(result + (j * pitch), (const void *) img->tpixels[j], pitch);
    370371        }
    371372        else {
    372                 int line_size = img->sx * sizeof(char);
    373                 void *result = malloc(img->sy * line_size);
    374                 int j;         
     373                pitch = img->sx * sizeof(char);
     374                result = malloc(img->sy * pitch);
    375375                for (j = 0; j < img->sy; j++)
    376                         memcpy(result + (j * line_size), (const void *) img->pixels[j], line_size);
    377                 return result;
    378         }
     376                        memcpy(result + (j * pitch), (const void *) img->pixels[j], pitch);
     377        }
     378        return result;
    379379}
    380380
     
    10381038                dither = caca_create_dither(sizeof(char) * 8, img->sx, img->sy, img->sx * sizeof(char), 0, 0, 0, 0);
    10391039
     1040        if (!dither) {
     1041                RETURN_FALSE;
     1042        }
     1043
    10401044        ZEND_REGISTER_RESOURCE(return_value, dither, le_caca_dither);
    10411045}
     
    11441148
    11451149PHP_FUNCTION(caca_get_dither_antialias_list) {
    1146         //TODO: write
     1150        zval *_zval;
     1151        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &_zval) == FAILURE) {
     1152                RETURN_FALSE;
     1153        }
     1154        caca_dither_t *dither;
     1155        ZEND_FETCH_RESOURCE(dither, caca_dither_t*, &_zval, -1, PHP_CACA_DITHER_RES_NAME, le_caca_dither);
     1156
     1157        char const * const *list = caca_get_dither_antialias_list(dither);
     1158        int i;
     1159        array_init(return_value);       
     1160        for(i = 0; list[i]; i += 1)
     1161                add_next_index_string(return_value, (char*) list[i], 1);
    11471162}
    11481163
     
    13081323        }
    13091324        caca_font_t *font = caca_load_font(str, str_len);
     1325        if (!font) {
     1326                RETURN_FALSE;
     1327        }
     1328        ZEND_REGISTER_RESOURCE(return_value, font, le_caca_font);
     1329}
     1330
     1331PHP_FUNCTION(caca_load_builtin_font) {
     1332        char *str;
     1333        long str_len;
     1334        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &str_len) == FAILURE) {
     1335                RETURN_FALSE;
     1336        }
     1337        caca_font_t *font = caca_load_font(str, 0);
     1338        if (!font) {
     1339                RETURN_FALSE;
     1340        }
    13101341        ZEND_REGISTER_RESOURCE(return_value, font, le_caca_font);
    13111342}
     
    13561387
    13571388PHP_FUNCTION(caca_render_canvas) {
    1358         //TODO: write
     1389        zval *_zval1, *_zval2, *_zval3;
     1390        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rrr", &_zval1, &_zval2, &_zval3) == FAILURE) {
     1391                RETURN_FALSE;
     1392        }
     1393        caca_canvas_t *canvas;
     1394        ZEND_FETCH_RESOURCE(canvas, caca_canvas_t*, &_zval1, -1, PHP_CACA_CANVAS_RES_NAME, le_caca_canvas);
     1395        caca_font_t *font;
     1396        ZEND_FETCH_RESOURCE(font, caca_font_t*, &_zval2, -1, PHP_CACA_FONT_RES_NAME, le_caca_font);
     1397
     1398        gdImage *img = fetch_external_resource(_zval3, "gd");
     1399        if (!img || !img->trueColor) {
     1400                RETURN_FALSE;
     1401        }
     1402
     1403        int pitch = img->sx * sizeof(int);
     1404        void *buffer = malloc(pitch * img->sy);
     1405        if (!buffer) {
     1406                RETURN_FALSE;
     1407        }
     1408
     1409        caca_render_canvas(canvas, font, buffer, img->sx, img->sy, pitch);
     1410        int i;
     1411        for (i = 0; i < img->sy; i++)
     1412                memcpy(img->tpixels[i], buffer + (i * pitch), pitch);
     1413       
     1414        //TODO: fix colors order
     1415        free(buffer);
     1416        RETURN_TRUE;
    13591417}
    13601418
     
    14001458        }
    14011459        caca_file_t *file = caca_file_open(path, mode);
     1460        if (!file) {
     1461                RETURN_FALSE;
     1462        }
    14021463        ZEND_REGISTER_RESOURCE(return_value, file, le_caca_file);
    14031464}
     
    15661627
    15671628        caca_display_t *display = caca_create_display(canvas);
     1629        if (!display) {
     1630                RETURN_FALSE;
     1631        }
    15681632        ZEND_REGISTER_RESOURCE(return_value, display, le_caca_display);
    15691633}
     
    16241688        ZEND_FETCH_RESOURCE(display, caca_display_t*, &_zval, -1, PHP_CACA_DISPLAY_RES_NAME, le_caca_display);
    16251689        caca_canvas_t *canvas = caca_get_canvas(display);
     1690        if (!canvas) {
     1691                RETURN_FALSE;
     1692        }
    16261693        ZEND_REGISTER_RESOURCE(return_value, canvas, le_caca_canvas);
    16271694}
  • libcaca/trunk/caca-php/php_caca.h

    r3148 r3149  
    123123PHP_FUNCTION(caca_dither_bitmap);
    124124PHP_FUNCTION(caca_load_font);
     125PHP_FUNCTION(caca_load_builtin_font);
    125126PHP_FUNCTION(caca_get_font_list);
    126127PHP_FUNCTION(caca_get_font_width);
Note: See TracChangeset for help on using the changeset viewer.