Ignore:
Timestamp:
Nov 8, 2006, 1:11:07 PM (13 years ago)
Author:
Sam Hocevar
Message:
  • Deprecate buffer management. We're back to standard void* memory areas.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/cucul/export.c

    r1283 r1302  
    4848}
    4949
    50 static int export_caca(cucul_canvas_t *, cucul_buffer_t *);
    51 static int export_ansi(cucul_canvas_t *, cucul_buffer_t *);
    52 static int export_utf8(cucul_canvas_t *, cucul_buffer_t *, int);
    53 static int export_html(cucul_canvas_t *, cucul_buffer_t *);
    54 static int export_html3(cucul_canvas_t *, cucul_buffer_t *);
    55 static int export_irc(cucul_canvas_t *, cucul_buffer_t *);
    56 static int export_ps(cucul_canvas_t *, cucul_buffer_t *);
    57 static int export_svg(cucul_canvas_t *, cucul_buffer_t *);
    58 static int export_tga(cucul_canvas_t *, cucul_buffer_t *);
     50static void *export_caca(cucul_canvas_t *, unsigned long int *);
     51static void *export_ansi(cucul_canvas_t *, unsigned long int *);
     52static void *export_utf8(cucul_canvas_t *, unsigned long int *, int);
     53static void *export_html(cucul_canvas_t *, unsigned long int *);
     54static void *export_html3(cucul_canvas_t *, unsigned long int *);
     55static void *export_irc(cucul_canvas_t *, unsigned long int *);
     56static void *export_ps(cucul_canvas_t *, unsigned long int *);
     57static void *export_svg(cucul_canvas_t *, unsigned long int *);
     58static void *export_tga(cucul_canvas_t *, unsigned long int *);
    5959
    6060/** \brief Export a canvas into a foreign format.
    6161 *
    6262 *  This function exports a libcucul canvas into various foreign formats such
    63  *  as ANSI art, HTML, IRC colours, etc. One should use cucul_get_buffer_data()
    64  *  and cucul_get_buffer_size() to access the buffer contents. The allocated
    65  *  data is valid until cucul_free_buffer() is called.
     63 *  as ANSI art, HTML, IRC colours, etc. The returned pointer should be passed
     64 *  to free() to release the allocated storage when it is no longer needed.
    6665 *
    6766 *  Valid values for \c format are:
     
    8281 *  \param cv A libcucul canvas
    8382 *  \param format A string describing the requested output format.
    84  *  \return A libcucul buffer, or NULL in case of error.
     83 *  \param bytes A pointer to an unsigned long integer where the number of
     84 *         allocated bytes will be written.
     85 *  \return A pointer to the exported memory area, or NULL in case of error.
    8586 */
    86 cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
    87 {
    88     cucul_buffer_t *ex;
    89     int ret = -1;
    90 
    91     ex = malloc(sizeof(cucul_buffer_t));
    92     if(!ex)
    93     {
     87void *cucul_export(cucul_canvas_t *cv, char const *format,
     88                   unsigned long int *bytes)
     89{
     90    if(!strcasecmp("caca", format))
     91        return export_caca(cv, bytes);
     92
     93    if(!strcasecmp("ansi", format))
     94        return export_ansi(cv, bytes);
     95
     96    if(!strcasecmp("utf8", format))
     97        return export_utf8(cv, bytes, 0);
     98
     99    if(!strcasecmp("utf8cr", format))
     100        return export_utf8(cv, bytes, 1);
     101
     102    if(!strcasecmp("html", format))
     103        return export_html(cv, bytes);
     104
     105    if(!strcasecmp("html3", format))
     106        return export_html3(cv, bytes);
     107
     108    if(!strcasecmp("irc", format))
     109        return export_irc(cv, bytes);
     110
     111    if(!strcasecmp("ps", format))
     112        return export_ps(cv, bytes);
     113
     114    if(!strcasecmp("svg", format))
     115        return export_svg(cv, bytes);
     116
     117    if(!strcasecmp("tga", format))
     118        return export_tga(cv, bytes);
     119
    94120#if defined(HAVE_ERRNO_H)
    95         errno = ENOMEM;
     121    errno = EINVAL;
    96122#endif
    97         return NULL;
    98     }
    99 
    100     ex->size = 0;
    101     ex->data = NULL;
    102     ex->user_data = 0;
    103 
    104     if(!strcasecmp("caca", format))
    105         ret = export_caca(cv, ex);
    106     else if(!strcasecmp("ansi", format))
    107         ret = export_ansi(cv, ex);
    108     else if(!strcasecmp("utf8", format))
    109         ret = export_utf8(cv, ex, 0);
    110     else if(!strcasecmp("utf8cr", format))
    111         ret = export_utf8(cv, ex, 1);
    112     else if(!strcasecmp("html", format))
    113         ret = export_html(cv, ex);
    114     else if(!strcasecmp("html3", format))
    115         ret = export_html3(cv, ex);
    116     else if(!strcasecmp("irc", format))
    117         ret = export_irc(cv, ex);
    118     else if(!strcasecmp("ps", format))
    119         ret = export_ps(cv, ex);
    120     else if(!strcasecmp("svg", format))
    121         ret = export_svg(cv, ex);
    122     else if(!strcasecmp("tga", format))
    123         ret = export_tga(cv, ex);
    124 
    125     if(ret < 0)
    126     {
    127         free(ex);
    128 #if defined(HAVE_ERRNO_H)
    129         errno = EINVAL;
    130 #endif
    131         return NULL;
    132     }
    133 
    134     return ex;
     123    return NULL;
    135124}
    136125
     
    171160
    172161/* Generate a native libcaca canvas file. */
    173 static int export_caca(cucul_canvas_t *cv, cucul_buffer_t *ex)
     162static void *export_caca(cucul_canvas_t *cv, unsigned long int *bytes)
    174163{
    175164    uint32_t *attrs = cv->attrs;
    176165    uint32_t *chars = cv->chars;
    177     char *cur;
     166    char *data, *cur;
    178167    unsigned int n;
    179168
     
    183172     *  - 24 bytes for the frame info
    184173     * 8 bytes for each character cell */
    185     ex->size = 44 + 8 * cv->width * cv->height;
    186     ex->data = malloc(ex->size);
    187 
    188     cur = ex->data;
     174    *bytes = 44 + 8 * cv->width * cv->height;
     175    cur = data = malloc(*bytes);
    189176
    190177    /* magic */
     
    213200    }
    214201
    215     return 0;
     202    return data;
    216203}
    217204
     
    273260
    274261/* Generate UTF-8 representation of current canvas. */
    275 static int export_utf8(cucul_canvas_t *cv, cucul_buffer_t *ex, int cr)
     262static void *export_utf8(cucul_canvas_t *cv, unsigned long int *bytes, int cr)
    276263{
    277264    static uint8_t const palette[] =
     
    281268    };
    282269
    283     char *cur;
     270    char *data, *cur;
    284271    unsigned int x, y;
    285272
     
    287274     * 4 max bytes for a UTF-8 character).
    288275     * Add height*9 to that (zeroes color at the end and jump to next line) */
    289     ex->size = (cv->height * 9) + (cv->width * cv->height * 23);
    290     ex->data = malloc(ex->size);
    291 
    292     cur = ex->data;
     276    *bytes = (cv->height * 9) + (cv->width * cv->height * 23);
     277    cur = data = malloc(*bytes);
    293278
    294279    for(y = 0; y < cv->height; y++)
     
    345330
    346331    /* Crop to really used size */
    347     ex->size = (uintptr_t)(cur - ex->data);
    348     ex->data = realloc(ex->data, ex->size);
    349 
    350     return 0;
     332    *bytes = (uintptr_t)(cur - data);
     333    data = realloc(data, *bytes);
     334
     335    return data;
    351336}
    352337
    353338/* Generate ANSI representation of current canvas. */
    354 static int export_ansi(cucul_canvas_t *cv, cucul_buffer_t *ex)
     339static void *export_ansi(cucul_canvas_t *cv, unsigned long int *bytes)
    355340{
    356341    static uint8_t const palette[] =
     
    360345    };
    361346
    362     char *cur;
     347    char *data, *cur;
    363348    unsigned int x, y;
    364349
     
    369354     * 1 byte for a CP437 character).
    370355     * Add height*9 to that (zeroes color at the end and jump to next line) */
    371     ex->size = (cv->height * 9) + (cv->width * cv->height * 16);
    372     ex->data = malloc(ex->size);
    373 
    374     cur = ex->data;
     356    *bytes = (cv->height * 9) + (cv->width * cv->height * 16);
     357    cur = data = malloc(*bytes);
    375358
    376359    for(y = 0; y < cv->height; y++)
     
    423406
    424407    /* Crop to really used size */
    425     ex->size = (uintptr_t)(cur - ex->data);
    426     ex->data = realloc(ex->data, ex->size);
    427 
    428     return 0;
     408    *bytes = (uintptr_t)(cur - data);
     409    data = realloc(data, *bytes);
     410
     411    return data;
    429412}
    430413
    431414/* Generate HTML representation of current canvas. */
    432 static int export_html(cucul_canvas_t *cv, cucul_buffer_t *ex)
    433 {
    434     char *cur;
     415static void *export_html(cucul_canvas_t *cv, unsigned long int *bytes)
     416{
     417    char *data, *cur;
    435418    unsigned int x, y, len;
    436419
     
    441424     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
    442425     *          7 chars for "</span>" */
    443     ex->size = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
    444     ex->data = malloc(ex->size);
    445 
    446     cur = ex->data;
     426    *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
     427    cur = data = malloc(*bytes);
    447428
    448429    /* HTML header */
     
    499480
    500481    /* Crop to really used size */
    501     ex->size = strlen(ex->data) + 1;
    502     ex->data = realloc(ex->data, ex->size);
    503 
    504     return 0;
     482    *bytes = (uintptr_t)(cur - data);
     483    data = realloc(data, *bytes);
     484
     485    return data;
    505486}
    506487
     
    509490 * will not work under gecko (mozilla rendering engine) unless you set a
    510491 * correct header. */
    511 static int export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
    512 {
    513     char *cur;
     492static void *export_html3(cucul_canvas_t *cv, unsigned long int *bytes)
     493{
     494    char *data, *cur;
    514495    unsigned int x, y, len;
    515496
     
    520501     *          up to 9 chars for "&#xxxxxx;", far less for pure ASCII
    521502     *          12 chars for "</font></td>" */
    522     ex->size = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
    523     ex->data = malloc(ex->size);
    524 
    525     cur = ex->data;
     503    *bytes = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
     504    cur = data = malloc(*bytes);
    526505
    527506    /* Table */
     
    594573
    595574    /* Crop to really used size */
    596     ex->size = (uintptr_t)(cur - ex->data);
    597     ex->data = realloc(ex->data, ex->size);
    598 
    599     return 0;
     575    *bytes = (uintptr_t)(cur - data);
     576    data = realloc(data, *bytes);
     577
     578    return data;
    600579}
    601580
    602581/* Export a text file with IRC colours */
    603 static int export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
     582static void *export_irc(cucul_canvas_t *cv, unsigned long int *bytes)
    604583{
    605584    static uint8_t const palette[] =
     
    609588    };
    610589
    611     char *cur;
     590    char *data, *cur;
    612591    unsigned int x, y;
    613592
     
    622601     */
    623602
    624     ex->size = 2 + cv->height * (3 + cv->width * 14);
    625     ex->data = malloc(ex->size);
    626 
    627     cur = ex->data;
     603    *bytes = 2 + cv->height * (3 + cv->width * 14);
     604    cur = data = malloc(*bytes);
    628605
    629606    for(y = 0; y < cv->height; y++)
     
    702679
    703680    /* Crop to really used size */
    704     ex->size = (uintptr_t)(cur - ex->data);
    705     ex->data = realloc(ex->data, ex->size);
    706 
    707     return 0;
     681    *bytes = (uintptr_t)(cur - data);
     682    data = realloc(data, *bytes);
     683
     684    return data;
    708685}
    709686
    710687/* Export a PostScript document. */
    711 static int export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
     688static void *export_ps(cucul_canvas_t *cv, unsigned long int *bytes)
    712689{
    713690    static char const *ps_header =
     
    736713        "6 10 scale\n";
    737714
    738     char *cur;
     715    char *data, *cur;
    739716    unsigned int x, y;
    740717
    741718    /* 200 is arbitrary but should be ok */
    742     ex->size = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
    743     ex->data = malloc(ex->size);
    744 
    745     cur = ex->data;
     719    *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
     720    cur = data = malloc(*bytes);
    746721
    747722    /* Header */
     
    811786
    812787    /* Crop to really used size */
    813     ex->size = (uintptr_t)(cur - ex->data);
    814     ex->data = realloc(ex->data, ex->size);
    815 
    816     return 0;
     788    *bytes = (uintptr_t)(cur - data);
     789    data = realloc(data, *bytes);
     790
     791    return data;
    817792}
    818793
    819794/* Export an SVG vector image */
    820 static int export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
     795static void *export_svg(cucul_canvas_t *cv, unsigned long int *bytes)
    821796{
    822797    static char const svg_header[] =
     
    827802        " xml:space=\"preserve\" version=\"1.1\"  baseProfile=\"full\">\n";
    828803
    829     char *cur;
     804    char *data, *cur;
    830805    unsigned int x, y;
    831806
    832807    /* 200 is arbitrary but should be ok */
    833     ex->size = strlen(svg_header) + 128 + cv->width * cv->height * 200;
    834     ex->data = malloc(ex->size);
    835 
    836     cur = ex->data;
     808    *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200;
     809    cur = data = malloc(*bytes);
    837810
    838811    /* Header */
     
    897870
    898871    /* Crop to really used size */
    899     ex->size = (uintptr_t)(cur - ex->data);
    900     ex->data = realloc(ex->data, ex->size);
    901 
    902     return 0;
     872    *bytes = (uintptr_t)(cur - data);
     873    data = realloc(data, *bytes);
     874
     875    return data;
    903876}
    904877
    905878/* Export a TGA image */
    906 static int export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
     879static void *export_tga(cucul_canvas_t *cv, unsigned long int *bytes)
    907880{
    908881    char const * const *fontlist;
    909     char * cur;
     882    char *data, *cur;
    910883    cucul_font_t *f;
    911884    unsigned int i, w, h;
     
    913886    fontlist = cucul_get_font_list();
    914887    if(!fontlist[0])
    915         return -1;
     888    {
     889#if defined(HAVE_ERRNO_H)
     890        errno = EINVAL;
     891#endif
     892        return NULL;
     893    }
    916894
    917895    f = cucul_load_font(fontlist[0], 0);
     
    920898    h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
    921899
    922     ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
    923     ex->data = malloc(ex->size);
    924 
    925     cur = ex->data;
     900    *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
     901    cur = data = malloc(*bytes);
    926902
    927903    /* ID Length */
     
    958934    cucul_free_font(f);
    959935
    960     return 0;
    961 }
    962 
     936    return data;
     937}
     938
Note: See TracChangeset for help on using the changeset viewer.