Changeset 826


Ignore:
Timestamp:
Apr 21, 2006, 8:44:04 PM (15 years ago)
Author:
Sam Hocevar
Message:
  • Moved cucul_load_canvas() into cucul/import.c and renamed it into cucul_import_canvas().
  • Renamed cucul_create_export() into cucul_export_canvas() for consistency.
Location:
libcaca/trunk
Files:
11 edited
1 copied

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/driver_raw.c

    r819 r826  
    5656    cucul_buffer_t *buffer;
    5757
    58     buffer = cucul_create_export(dp->cv, "caca");
     58    buffer = cucul_export_canvas(dp->cv, "caca");
    5959    fwrite(cucul_get_buffer_data(buffer),
    6060           cucul_get_buffer_size(buffer), 1, stdout);
  • libcaca/trunk/cpp/cucul++.cpp

    r814 r826  
    351351}
    352352
    353 Cucul::Buffer *         Cucul::create_export (char const *buf)
     353Cucul::Buffer *         Cucul::export_canvas (char const *buf)
    354354{
    355355    Cucul::Buffer *b = new Cucul::Buffer();
    356     b->buffer = cucul_create_export(cv, buf);
     356    b->buffer = cucul_export_canvas(cv, buf);
    357357    return b;
    358358}
  • libcaca/trunk/cpp/cucul++.h

    r814 r826  
    119119    void        render_canvas ( Font *, unsigned char *, unsigned int, unsigned int, unsigned int);
    120120    void        free_font (Font *);
    121     Buffer *    create_export ( char const *);
     121    Buffer *    export_canvas ( char const *);
    122122    char const *const *         get_export_list (void);
    123123
  • libcaca/trunk/cucul/Makefile.am

    r813 r826  
    2929        font_mono9.h \
    3030        font_monobold12.h \
     31        import.c \
    3132        export.c \
    3233        $(NULL)
  • libcaca/trunk/cucul/cucul.c

    r819 r826  
    6868        free(cv);
    6969        return NULL;
    70     }
    71 
    72     return cv;
    73 }
    74 
    75 /** \brief Load a memory area into a canvas.
    76  *
    77  *  This function loads a memory area containing an exported canvas into
    78  *  a new \e libcucul canvas.
    79  *
    80  *  \param data The memory area to be loaded into a canvas.
    81  *  \param size The length of the memory area.
    82  *  \return A libcucul canvas, or NULL in case of error.
    83  */
    84 cucul_canvas_t *cucul_load_canvas(void *data, unsigned int size)
    85 {
    86     cucul_canvas_t *cv;
    87     uint8_t *buf = (uint8_t *)data;
    88     unsigned int width, height, n;
    89 
    90     if(size < 16)
    91         return NULL;
    92 
    93     if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
    94         return NULL;
    95 
    96     if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
    97         return NULL;
    98 
    99     width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
    100            | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
    101     height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
    102             | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
    103 
    104     if(!width || !height)
    105         return NULL;
    106 
    107     if(size != 16 + width * height * 8)
    108         return NULL;
    109 
    110     cv = cucul_create_canvas(width, height);
    111 
    112     if(!cv)
    113         return NULL;
    114 
    115     for(n = height * width; n--; )
    116     {
    117         cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
    118                      | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
    119                      | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
    120                      | (uint32_t)buf[16 + 3 + 8 * n];
    121         cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
    122                     | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
    123                     | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
    124                     | (uint32_t)buf[16 + 7 + 8 * n];
    12570    }
    12671
  • libcaca/trunk/cucul/cucul.h

    r824 r826  
    7575 *  @{ */
    7676cucul_canvas_t * cucul_create_canvas(unsigned int, unsigned int);
    77 cucul_canvas_t * cucul_load_canvas(void *, unsigned int);
    7877void cucul_set_canvas_size(cucul_canvas_t *, unsigned int, unsigned int);
    7978unsigned int cucul_get_canvas_width(cucul_canvas_t *);
     
    206205/*  @} */
    207206
    208 /** \defgroup exporter libcucul exporters to various formats
    209  *
    210  *  These functions export the current canvas to various text formats. It
    211  *  is necessary to call cucul_free_buffer() to dispose of the data.
    212  *
    213  *  @{ */
    214 cucul_buffer_t * cucul_create_export(cucul_canvas_t *, char const *);
     207/** \defgroup importexport libcucul importers/exporters from/to various formats
     208 *
     209 *  These functions import various file formats into a new canvas, or export
     210 *  the current canvas to various text formats.
     211 *
     212 *  @{ */
     213cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *, char const *);
    215214char const * const * cucul_get_export_list(void);
     215cucul_canvas_t * cucul_import_canvas(void const *, unsigned int, char const *);
     216char const * const * cucul_get_import_list(void);
    216217/*  @} */
    217218
  • libcaca/trunk/cucul/export.c

    r825 r826  
    6666 *  \param format A string describing the requested output format.
    6767 */
    68 cucul_buffer_t * cucul_create_export(cucul_canvas_t *cv, char const *format)
     68cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
    6969{
    7070    cucul_buffer_t *ex;
     
    104104 *  Return a list of available export formats. The list is a NULL-terminated
    105105 *  array of strings, interleaving a string containing the internal value for
    106  *  the export format, to be used with cucul_create_export(), and a string
     106 *  the export format, to be used with cucul_export_canvas(), and a string
    107107 *  containing the natural language description for that export format.
    108108 *
  • libcaca/trunk/cucul/import.c

    r823 r826  
    1313
    1414/*
    15  *  This file contains the main functions used by \e libcucul applications
    16  *  to initialise a drawing context.
     15 *  This file contains various import functions.
    1716 */
    1817
     
    2827#include "cucul_internals.h"
    2928
    30 /** \brief Initialise a \e libcucul canvas.
     29static cucul_canvas_t *import_caca(void const *, unsigned int);
     30
     31/** \brief Import a buffer into a canvas
    3132 *
    32  *  This function initialises internal \e libcucul structures and the backend
    33  *  that will be used for subsequent graphical operations. It must be the
    34  *  first \e libcucul function to be called in a function. cucul_free_canvas()
    35  *  should be called at the end of the program to free all allocated resources.
     33 *  This function imports a memory area into an internal libcucul canvas.
    3634 *
    37  *  If one of the desired canvas coordinates is zero, a default canvas size
    38  *  of 80x32 is used instead.
     35 *  Valid values for \c format are:
    3936 *
    40  *  \param width The desired canvas width
    41  *  \param height The desired canvas height
    42  *  \return A libcucul canvas handle upon success, NULL if an error occurred.
    43  */
    44 cucul_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
    45 {
    46     cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
    47 
    48     cv->refcount = 0;
    49 
    50     cv->fgcolor = CUCUL_COLOR_LIGHTGRAY;
    51     cv->bgcolor = CUCUL_COLOR_BLACK;
    52 
    53     cv->width = cv->height = 0;
    54     cv->chars = NULL;
    55     cv->attr = NULL;
    56     cv->empty_line = cv->scratch_line = NULL;
    57 
    58     /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
    59      * default X11 window. When a graphic driver attaches to us, it can set
    60      * a different size. */
    61     if(width && height)
    62         _cucul_set_canvas_size(cv, width, height);
    63     else
    64         _cucul_set_canvas_size(cv, 80, 32);
    65 
    66     if(_cucul_init_dither())
    67     {
    68         free(cv);
    69         return NULL;
    70     }
    71 
    72     return cv;
    73 }
    74 
    75 /** \brief Load a memory area into a canvas.
    76  *
    77  *  This function loads a memory area containing an exported canvas into
    78  *  a new \e libcucul canvas.
     37 *  \li \c "caca": import native libcaca files.
    7938 *
    8039 *  \param data The memory area to be loaded into a canvas.
    8140 *  \param size The length of the memory area.
     41 *  \param format A string describing the input format.
    8242 *  \return A libcucul canvas, or NULL in case of error.
    8343 */
    84 cucul_canvas_t *cucul_load_canvas(void *data, unsigned int size)
     44cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size,
     45                                     char const *format)
     46{
     47    if(!strcasecmp("caca", format))
     48        return import_caca(data, size);
     49
     50    /* FIXME: Try to autodetect */
     51    if(!strcasecmp("", format))
     52        return import_caca(data, size);
     53
     54    return NULL;
     55}
     56
     57/** \brief Get available import formats
     58 *
     59 *  Return a list of available import formats. The list is a NULL-terminated
     60 *  array of strings, interleaving a string containing the internal value for
     61 *  the import format, to be used with cucul_import_canvas(), and a string
     62 *  containing the natural language description for that import format.
     63 *
     64 *  \return An array of strings.
     65 */
     66char const * const * cucul_get_import_list(void)
     67{
     68    static char const * const list[] =
     69    {
     70        "", "autodetect",
     71        "caca", "native libcaca format",
     72        NULL, NULL
     73    };
     74
     75    return list;
     76}
     77
     78/*
     79 * XXX: the following functions are local.
     80 */
     81
     82static cucul_canvas_t *import_caca(void const *data, unsigned int size)
    8583{
    8684    cucul_canvas_t *cv;
     
    128126}
    129127
    130 /** \brief Resize a canvas.
    131  *
    132  *  This function sets the canvas width and height, in character cells.
    133  *
    134  *  The contents of the canvas are preserved to the extent of the new
    135  *  canvas size. Newly allocated character cells at the right and/or at
    136  *  the bottom of the canvas are filled with spaces.
    137  *
    138  *  It is an error to try to resize the canvas if an output driver has
    139  *  been attached to the canvas using caca_create_display(). You need to
    140  *  remove the output driver using caca_free_display() before you can change
    141  *  the  canvas size again. However, the caca output driver can cause a
    142  *  canvas resize through user interaction. See the caca_event() documentation
    143  *  for more about this.
    144  *
    145  *  \param cv A libcucul canvas
    146  *  \param width The desired canvas width
    147  *  \param height The desired canvas height
    148  */
    149 void cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
    150                                                unsigned int height)
    151 {
    152     if(cv->refcount)
    153         return;
    154 
    155     _cucul_set_canvas_size(cv, width, height);
    156 }
    157 
    158 /** \brief Get the canvas width.
    159  *
    160  *  This function returns the current canvas width, in character cells.
    161  *
    162  *  \param cv A libcucul canvas
    163  *  \return The canvas width.
    164  */
    165 unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
    166 {
    167     return cv->width;
    168 }
    169 
    170 /** \brief Get the canvas height.
    171  *
    172  *  This function returns the current canvas height, in character cells.
    173  *
    174  *  \param cv A libcucul canvas
    175  *  \return The canvas height.
    176  */
    177 unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
    178 {
    179     return cv->height;
    180 }
    181 
    182 /** \brief Translate a colour index into the colour's name.
    183  *
    184  *  This function translates a cucul_color enum into a human-readable
    185  *  description string of the associated colour.
    186  *
    187  *  \param color The colour value.
    188  *  \return A static string containing the colour's name.
    189  */
    190 char const *cucul_get_color_name(unsigned int color)
    191 {
    192     static char const *color_names[] =
    193     {
    194         "black",
    195         "blue",
    196         "green",
    197         "cyan",
    198         "red",
    199         "magenta",
    200         "brown",
    201         "light gray",
    202         "dark gray",
    203         "light blue",
    204         "light green",
    205         "light cyan",
    206         "light red",
    207         "light magenta",
    208         "yellow",
    209         "white",
    210     };
    211 
    212     if(color < 0 || color > 15)
    213         return "unknown";
    214 
    215     return color_names[color];
    216 }
    217 
    218 /** \brief Uninitialise \e libcucul.
    219  *
    220  *  This function frees all resources allocated by cucul_create_canvas(). After
    221  *  cucul_free_canvas() has been called, no other \e libcucul functions may be
    222  *  used unless a new call to cucul_create_canvas() is done.
    223  *
    224  *  \param cv A libcucul canvas
    225  */
    226 void cucul_free_canvas(cucul_canvas_t *cv)
    227 {
    228     _cucul_end_dither();
    229 
    230     free(cv->empty_line);
    231     free(cv->scratch_line);
    232 
    233     free(cv->chars);
    234     free(cv->attr);
    235 
    236     free(cv);
    237 }
    238 
    239 /** \brief Generate a random integer within a range.
    240  *
    241  *  \param min The lower bound of the integer range.
    242  *  \param max The upper bound of the integer range.
    243  *  \return A random integer comprised between \p min  and \p max - 1
    244  *  (inclusive).
    245  */
    246 int cucul_rand(int min, int max)
    247 {
    248     return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
    249 }
    250 
    251 /*
    252  * XXX: The following functions are local.
    253  */
    254 
    255 void _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
    256                                                 unsigned int height)
    257 {
    258     unsigned int x, y, old_width, old_height, new_size, old_size;
    259 
    260     old_width = cv->width;
    261     old_height = cv->height;
    262     old_size = old_width * old_height;
    263 
    264     cv->width = width;
    265     cv->height = height;
    266     new_size = width * height;
    267 
    268     /* Step 1: if new area is bigger, resize the memory area now. */
    269     if(new_size > old_size)
    270     {
    271         cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
    272         cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
    273     }
    274 
    275     /* Step 2: move line data if necessary. */
    276     if(width == old_width)
    277     {
    278         /* Width did not change, which means we do not need to move data. */
    279         ;
    280     }
    281     else if(width > old_width)
    282     {
    283         /* New width is bigger than old width, which means we need to
    284          * copy lines starting from the bottom of the screen otherwise
    285          * we will overwrite information. */
    286         for(y = height < old_height ? height : old_height; y--; )
    287         {
    288             for(x = old_width; x--; )
    289             {
    290                 cv->chars[y * width + x] = cv->chars[y * old_width + x];
    291                 cv->attr[y * width + x] = cv->attr[y * old_width + x];
    292             }
    293 
    294             /* Zero the end of the line */
    295             for(x = width - old_width; x--; )
    296                 cv->chars[y * width + old_width + x] = (uint32_t)' ';
    297             memset(cv->attr + y * width + old_width, 0,
    298                    (width - old_width) * 4);
    299         }
    300     }
    301     else
    302     {
    303         /* New width is smaller. Copy as many lines as possible. Ignore
    304          * the first line, it is already in place. */
    305         unsigned int lines = height < old_height ? height : old_height;
    306 
    307         for(y = 1; y < lines; y++)
    308         {
    309             for(x = 0; x < width; x++)
    310             {
    311                 cv->chars[y * width + x] = cv->chars[y * old_width + x];
    312                 cv->attr[y * width + x] = cv->attr[y * old_width + x];
    313             }
    314         }
    315     }
    316 
    317     /* Step 3: fill the bottom of the new screen if necessary. */
    318     if(height > old_height)
    319     {
    320         /* Zero the bottom of the screen */
    321         for(x = (height - old_height) * width; x--; )
    322             cv->chars[old_height * width + x] = (uint32_t)' ';
    323         memset(cv->attr + old_height * width, 0,
    324                (height - old_height) * width * 4);
    325     }
    326 
    327     /* Step 4: if new area is smaller, resize memory area now. */
    328     if(new_size <= old_size)
    329     {
    330         cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
    331         cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
    332     }
    333 
    334     /* Recompute the scratch line and the empty line */
    335     if(width != old_width)
    336     {
    337         cv->empty_line = realloc(cv->empty_line, width + 1);
    338         memset(cv->empty_line, ' ', width);
    339         cv->empty_line[width] = '\0';
    340 
    341         cv->scratch_line = realloc(cv->scratch_line, width + 1);
    342     }
    343 }
    344 
  • libcaca/trunk/src/cacaplay.c

    r819 r826  
    5454    buffer = malloc(statbuf.st_size);
    5555    read(fd, buffer, statbuf.st_size);
    56     cv = cucul_load_canvas(buffer, statbuf.st_size);
     56    cv = cucul_import_canvas(buffer, statbuf.st_size, "caca");
    5757    free(buffer);
    5858
  • libcaca/trunk/src/cacaserver.c

    r818 r826  
    227227            cucul_free_canvas(server->canvas);
    228228
    229         server->canvas = cucul_load_canvas(buf, size);
     229        server->canvas = cucul_import_canvas(buf, size, "caca");
    230230
    231231        if(!server->canvas)
     
    241241        /* Get ANSI representation of the image and skip the end-of buffer
    242242         * linefeed ("\r\n", 2 bytes) */
    243         server->buffer = cucul_create_export(server->canvas, "ansi");
     243        server->buffer = cucul_export_canvas(server->canvas, "ansi");
    244244        server->bufdata = cucul_get_buffer_data(server->buffer);
    245245        server->buflen = cucul_get_buffer_size(server->buffer);
  • libcaca/trunk/src/img2irc.c

    r816 r826  
    5959    unload_image(i);
    6060
    61     export = cucul_create_export(cv, "irc");
     61    export = cucul_export_canvas(cv, "irc");
    6262    fwrite(cucul_get_buffer_data(export),
    6363           cucul_get_buffer_size(export), 1, stdout);
  • libcaca/trunk/test/export.c

    r816 r826  
    102102    }
    103103
    104     buffer = cucul_create_export(cv, argv[1]);
     104    buffer = cucul_export_canvas(cv, argv[1]);
    105105    fwrite(cucul_get_buffer_data(buffer),
    106106           cucul_get_buffer_size(buffer), 1, stdout);
Note: See TracChangeset for help on using the changeset viewer.