Changeset 3150 for libcaca


Ignore:
Timestamp:
Oct 30, 2008, 5:16:31 PM (12 years ago)
Author:
nico
Message:
  • Improve php binding of caca_render_canvas
  • Destroy php resource in caca_file_close
  • Improve sample program render.php
Location:
libcaca/trunk/caca-php
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca-php/examples/render.php

    r3149 r3150  
    2626echo caca_export_string($canvas, "utf8");
    2727
    28 $img = imagecreatetruecolor(400, 300);
    2928$font = caca_load_builtin_font("Monospace Bold 12");
     29
     30$width = caca_get_canvas_width($canvas) * caca_get_font_width($font);
     31$height = caca_get_canvas_height($canvas) * caca_get_font_height($font);
     32
     33$img = imagecreatetruecolor($width, $height);
     34
    3035caca_render_canvas($canvas, $font, $img);
    3136
  • libcaca/trunk/caca-php/php_caca.c

    r3149 r3150  
    14011401        }
    14021402
    1403         int pitch = img->sx * sizeof(int);
    1404         void *buffer = malloc(pitch * img->sy);
     1403        int pitch = img->sx * 4;
     1404        uint8_t *buffer = malloc(pitch * img->sy);
    14051405        if (!buffer) {
    14061406                RETURN_FALSE;
    14071407        }
    14081408
    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);
     1409        caca_render_canvas(canvas, font, (void *) buffer, img->sx, img->sy, pitch);
     1410        int i, j;
     1411        for (i = 0; i < img->sy; i++) {
     1412                for (j = 0; j < img->sx; j++) {
     1413                        uint8_t *src = buffer + i * pitch + j * 4;
     1414                        img->tpixels[i][j] = *(src + 3) | (*(src + 2) << 8) | (*(src + 1) << 16) | (*(src + 0) << 24);
     1415                }
     1416        }
    14131417       
    1414         //TODO: fix colors order
    14151418        free(buffer);
    14161419        RETURN_TRUE;
     
    14711474        caca_file_t *file;
    14721475        ZEND_FETCH_RESOURCE(file, caca_file_t*, &_zval, -1, PHP_CACA_FILE_RES_NAME, le_caca_file);
    1473         //TODO: check that file was not already closed
    1474         RETURN_SUCCESS(caca_file_close(file));
     1476
     1477        int res = caca_file_close(file);
     1478
     1479        //Delete php resource
     1480        zend_list_delete(_zval->value.lval);
     1481        RETURN_SUCCESS(res);
    14751482}
    14761483
Note: See TracChangeset for help on using the changeset viewer.