Ignore:
Timestamp:
Oct 26, 2006, 11:04:55 AM (14 years ago)
Author:
Sam Hocevar
Message:
  • Support export of zero-sized canvases.
File:
1 edited

Legend:

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

    r1229 r1237  
    3232#include "cucul_internals.h"
    3333
    34 static void export_caca(cucul_canvas_t *, cucul_buffer_t *);
    35 static void export_ansi(cucul_canvas_t *, cucul_buffer_t *);
    36 static void export_utf8(cucul_canvas_t *, cucul_buffer_t *);
    37 static void export_html(cucul_canvas_t *, cucul_buffer_t *);
    38 static void export_html3(cucul_canvas_t *, cucul_buffer_t *);
    39 static void export_irc(cucul_canvas_t *, cucul_buffer_t *);
    40 static void export_ps(cucul_canvas_t *, cucul_buffer_t *);
    41 static void export_svg(cucul_canvas_t *, cucul_buffer_t *);
    42 static void export_tga(cucul_canvas_t *, cucul_buffer_t *);
     34static int export_caca(cucul_canvas_t *, cucul_buffer_t *);
     35static int export_ansi(cucul_canvas_t *, cucul_buffer_t *);
     36static int export_utf8(cucul_canvas_t *, cucul_buffer_t *);
     37static int export_html(cucul_canvas_t *, cucul_buffer_t *);
     38static int export_html3(cucul_canvas_t *, cucul_buffer_t *);
     39static int export_irc(cucul_canvas_t *, cucul_buffer_t *);
     40static int export_ps(cucul_canvas_t *, cucul_buffer_t *);
     41static int export_svg(cucul_canvas_t *, cucul_buffer_t *);
     42static int export_tga(cucul_canvas_t *, cucul_buffer_t *);
    4343
    4444/** \brief Export a canvas into a foreign format.
     
    6161 *
    6262 *  If an error occurs, NULL is returned and \b errno is set accordingly:
    63  *  - \c EINVAL Invalid format requested.
     63 *  - \c EINVAL Unsupported format requested.
    6464 *  - \c ENOMEM Not enough memory to allocate output buffer.
    6565 *
     
    7171{
    7272    cucul_buffer_t *ex;
     73    int ret = -1;
    7374
    7475    ex = malloc(sizeof(cucul_buffer_t));
     
    8687
    8788    if(!strcasecmp("caca", format))
    88         export_caca(cv, ex);
     89        ret = export_caca(cv, ex);
    8990    else if(!strcasecmp("ansi", format))
    90         export_ansi(cv, ex);
     91        ret = export_ansi(cv, ex);
    9192    else if(!strcasecmp("utf8", format))
    92         export_utf8(cv, ex);
     93        ret = export_utf8(cv, ex);
    9394    else if(!strcasecmp("html", format))
    94         export_html(cv, ex);
     95        ret = export_html(cv, ex);
    9596    else if(!strcasecmp("html3", format))
    96         export_html3(cv, ex);
     97        ret = export_html3(cv, ex);
    9798    else if(!strcasecmp("irc", format))
    98         export_irc(cv, ex);
     99        ret = export_irc(cv, ex);
    99100    else if(!strcasecmp("ps", format))
    100         export_ps(cv, ex);
     101        ret = export_ps(cv, ex);
    101102    else if(!strcasecmp("svg", format))
    102         export_svg(cv, ex);
     103        ret = export_svg(cv, ex);
    103104    else if(!strcasecmp("tga", format))
    104         export_tga(cv, ex);
    105 
    106     if(ex->size == 0)
     105        ret = export_tga(cv, ex);
     106
     107    if(ret < 0)
    107108    {
    108109        free(ex);
     
    151152
    152153/* Generate a native libcaca canvas file. */
    153 static void export_caca(cucul_canvas_t *cv, cucul_buffer_t *ex)
     154static int export_caca(cucul_canvas_t *cv, cucul_buffer_t *ex)
    154155{
    155156    uint32_t *attr = cv->attr;
     
    189190        *cur++ = a & 0xff;
    190191    }
     192
     193    return 0;
    191194}
    192195
    193196/* Generate UTF-8 representation of current canvas. */
    194 static void export_utf8(cucul_canvas_t *cv, cucul_buffer_t *ex)
     197static int export_utf8(cucul_canvas_t *cv, cucul_buffer_t *ex)
    195198{
    196199    static uint8_t const palette[] =
     
    266269    ex->size = (uintptr_t)(cur - ex->data);
    267270    ex->data = realloc(ex->data, ex->size);
     271
     272    return 0;
    268273}
    269274
    270275/* Generate ANSI representation of current canvas. */
    271 static void export_ansi(cucul_canvas_t *cv, cucul_buffer_t *ex)
     276static int export_ansi(cucul_canvas_t *cv, cucul_buffer_t *ex)
    272277{
    273278    static uint8_t const palette[] =
     
    342347    ex->size = (uintptr_t)(cur - ex->data);
    343348    ex->data = realloc(ex->data, ex->size);
     349
     350    return 0;
    344351}
    345352
    346353/* Generate HTML representation of current canvas. */
    347 static void export_html(cucul_canvas_t *cv, cucul_buffer_t *ex)
     354static int export_html(cucul_canvas_t *cv, cucul_buffer_t *ex)
    348355{
    349356    char *cur;
     
    406413    ex->size = strlen(ex->data) + 1;
    407414    ex->data = realloc(ex->data, ex->size);
     415
     416    return 0;
    408417}
    409418
     
    412421 * will not work under gecko (mozilla rendering engine) unless you set a
    413422 * correct header. */
    414 static void export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
     423static int export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
    415424{
    416425    char *cur;
     
    480489    ex->size = (uintptr_t)(cur - ex->data);
    481490    ex->data = realloc(ex->data, ex->size);
     491
     492    return 0;
    482493}
    483494
    484495/* Export a text file with IRC colours */
    485 static void export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
     496static int export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
    486497{
    487498    static uint8_t const palette[] =
     
    579590    ex->size = (uintptr_t)(cur - ex->data);
    580591    ex->data = realloc(ex->data, ex->size);
     592
     593    return 0;
    581594}
    582595
    583596/* Export a PostScript document. */
    584 static void export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
     597static int export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
    585598{
    586599    static char const *ps_header =
     
    613626
    614627    /* 200 is arbitrary but should be ok */
    615     ex->size = strlen(ps_header) + (cv->width * cv->height * 200);
     628    ex->size = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
    616629    ex->data = malloc(ex->size);
    617630
     
    686699    ex->size = (uintptr_t)(cur - ex->data);
    687700    ex->data = realloc(ex->data, ex->size);
     701
     702    return 0;
    688703}
    689704
    690705/* Export an SVG vector image */
    691 static void export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
     706static int export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
    692707{
    693708    static char const svg_header[] =
     
    702717
    703718    /* 200 is arbitrary but should be ok */
    704     ex->size = strlen(svg_header) + (cv->width * cv->height * 200);
     719    ex->size = strlen(svg_header) + 128 + cv->width * cv->height * 200;
    705720    ex->data = malloc(ex->size);
    706721
     
    770785    ex->size = (uintptr_t)(cur - ex->data);
    771786    ex->data = realloc(ex->data, ex->size);
     787
     788    return 0;
    772789}
    773790
    774791/* Export a TGA image */
    775 static void export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
    776 {
    777     char const * const * fontlist;
     792static int export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
     793{
     794    char const * const *fontlist;
    778795    char * cur;
    779796    cucul_font_t *f;
     
    782799    fontlist = cucul_get_font_list();
    783800    if(!fontlist[0])
    784         return;
     801        return -1;
    785802
    786803    f = cucul_load_font(fontlist[0], 0);
     
    826843
    827844    cucul_free_font(f);
    828 }
    829 
     845
     846    return 0;
     847}
     848
Note: See TracChangeset for help on using the changeset viewer.