Changeset 3495 for libcaca/trunk


Ignore:
Timestamp:
May 21, 2009, 10:55:21 PM (10 years ago)
Author:
Sam Hocevar
Message:

Add caca_export_area_to_memory() to export only a selected part of the
current canvas. This is useful to export dirty rectangles. Accordingly,
add caca_import_area_from_memory() and caca_import_area_from_file().

Location:
libcaca/trunk
Files:
24 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca/caca.h

    r3494 r3495  
    426426 *
    427427 *  @{ */
    428 __extern ssize_t caca_import_memory(caca_canvas_t *, void const *,
    429                                      size_t, char const *);
    430 __extern ssize_t caca_import_file(caca_canvas_t *, char const *,
    431                                    char const *);
     428__extern ssize_t caca_import_canvas_from_memory(caca_canvas_t *, void const *,
     429                                                size_t, char const *);
     430__extern ssize_t caca_import_canvas_from_file(caca_canvas_t *, char const *,
     431                                              char const *);
     432__extern ssize_t caca_import_area_from_memory(caca_canvas_t *, int, int,
     433                                              void const *, size_t,
     434                                              char const *);
     435__extern ssize_t caca_import_area_from_file(caca_canvas_t *, int, int,
     436                                            char const *, char const *);
    432437__extern char const * const * caca_get_import_list(void);
    433 __extern void *caca_export_memory(caca_canvas_t const *, char const *,
    434                                    size_t *);
     438__extern void *caca_export_canvas_to_memory(caca_canvas_t const *,
     439                                            char const *, size_t *);
     440__extern void *caca_export_area_to_memory(caca_canvas_t const *, int, int,
     441                                          int, int, char const *, size_t *);
    435442__extern char const * const * caca_get_export_list(void);
    436443/*  @} */
     
    523530__extern caca_canvas_t * cucul_import_canvas(cucul_buffer_t *,
    524531                                              char const *) CACA_DEPRECATED;
     532__extern ssize_t caca_import_memory(caca_canvas_t *, void const *, size_t,
     533                                    char const *) CACA_DEPRECATED;
     534__extern ssize_t caca_import_file(caca_canvas_t *, char const *,
     535                                  char const *) CACA_DEPRECATED;
     536__extern void *caca_export_memory(caca_canvas_t const *, char const *,
     537                                  size_t *) CACA_DEPRECATED;
    525538__extern int cucul_rotate(caca_canvas_t *) CACA_DEPRECATED;
    526539__extern int cucul_set_dither_invert(caca_dither_t *, int) CACA_DEPRECATED;
  • libcaca/trunk/caca/caca0.c

    r2858 r3495  
    11/*
    22 *  libcaca       Colour ASCII-Art library
    3  *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
     3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
    44 *                All Rights Reserved
    55 *
     
    216216
    217217    cv = caca_create_canvas(0, 0);;
    218     if(caca_import_file(cv, file, "") < 0)
     218    if(caca_import_canvas_from_file(cv, file, "") < 0)
    219219    {
    220220        caca_free_canvas(cv);
  • libcaca/trunk/caca/codec/export.c

    r3492 r3495  
    8181 *  \return A pointer to the exported memory area, or NULL in case of error.
    8282 */
    83 void *caca_export_memory(caca_canvas_t const *cv, char const *format,
    84                           size_t *bytes)
     83void *caca_export_canvas_to_memory(caca_canvas_t const *cv, char const *format,
     84                                   size_t *bytes)
    8585{
    8686    if(!strcasecmp("caca", format))
     
    119119    seterrno(EINVAL);
    120120    return NULL;
     121}
     122
     123/** \brief Export a canvas portion into a foreign format.
     124 *
     125 *  This function exports a portion of a \e libcaca canvas into various
     126 *  formats. For more information, see caca_export_canvas_to_memory().
     127 *
     128 *  If an error occurs, NULL is returned and \b errno is set accordingly:
     129 *  - \c EINVAL Unsupported format requested or invalid coordinates.
     130 *  - \c ENOMEM Not enough memory to allocate output buffer.
     131 *
     132 *  \param cv A libcaca canvas
     133 *  \param x The leftmost coordinate of the area to export.
     134 *  \param y The topmost coordinate of the area to export.
     135 *  \param w The width of the area to export.
     136 *  \param h The height of the area to export.
     137 *  \param format A string describing the requested output format.
     138 *  \param bytes A pointer to a size_t where the number of allocated bytes
     139 *         will be written.
     140 *  \return A pointer to the exported memory area, or NULL in case of error.
     141 */
     142void *caca_export_area_to_memory(caca_canvas_t const *cv, int x, int y, int w,
     143                                 int h, char const *format, size_t *bytes)
     144{
     145    caca_canvas_t *tmp;
     146    void *ret;
     147
     148    if(w < 0 || h < 0 || x < 0 || y < 0
     149        || x + w >= cv->width || y + h >= cv->height)
     150    {
     151        seterrno(EINVAL);
     152        return NULL;
     153    }
     154
     155    /* TODO: we need to spare the blit here by exporting the area we want. */
     156    tmp = caca_create_canvas(w, h);
     157    caca_blit(tmp, -x, -y, cv, NULL);
     158
     159    ret = caca_export_canvas_to_memory(tmp, format, bytes);
     160
     161    caca_free_canvas(tmp);
     162
     163    return ret;
    121164}
    122165
     
    938981
    939982void *cucul_export_memory(cucul_canvas_t const *, char const *,
    940                           size_t *) CACA_ALIAS(caca_export_memory);
     983                          size_t *) CACA_ALIAS(caca_export_canvas_to_memory);
     984void *caca_export_memory(caca_canvas_t const *, char const *,
     985                         size_t *) CACA_ALIAS(caca_export_canvas_to_memory);
    941986char const * const * cucul_get_export_list(void)
    942987         CACA_ALIAS(caca_get_export_list);
  • libcaca/trunk/caca/codec/import.c

    r3492 r3495  
    7272 *  or -1 if an error occurred.
    7373 */
    74 ssize_t caca_import_memory(caca_canvas_t *cv, void const *data,
    75                             size_t len, char const *format)
     74ssize_t caca_import_canvas_from_memory(caca_canvas_t *cv, void const *data,
     75                                       size_t len, char const *format)
    7676{
    7777    if(!strcasecmp("caca", format))
     
    137137 *  or -1 if an error occurred.
    138138 */
    139 ssize_t caca_import_file(caca_canvas_t *cv, char const *filename,
    140                           char const *format)
     139ssize_t caca_import_canvas_from_file(caca_canvas_t *cv, char const *filename,
     140                                     char const *format)
    141141{
    142142#if defined __KERNEL__
     
    168168    caca_file_close(f);
    169169
    170     ret = caca_import_memory(cv, data, size, format);
     170    ret = caca_import_canvas_from_memory(cv, data, size, format);
    171171    free(data);
    172172
    173173    return ret;
    174174#endif
     175}
     176
     177/** \brief Import a memory buffer into a canvas area
     178 *
     179 *  Import a memory buffer into the given libcaca canvas's current
     180 *  frame, at the specified position. For more information, see
     181 *  caca_import_canvas_from_memory().
     182 *
     183 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     184 *  - \c EINVAL Unsupported format requested or invalid coordinates.
     185 *  - \c ENOMEM Not enough memory to allocate canvas.
     186 *
     187 *  \param cv A libcaca canvas in which to import the file.
     188 *  \param x The leftmost coordinate of the area to import to.
     189 *  \param y The topmost coordinate of the area to import to.
     190 *  \param data A memory area containing the data to be loaded into the canvas.
     191 *  \param len The size in bytes of the memory area.
     192 *  \param format A string describing the input format.
     193 *  \return The number of bytes read, or 0 if there was not enough data,
     194 *  or -1 if an error occurred.
     195 */
     196ssize_t caca_import_area_from_memory(caca_canvas_t *cv, int x, int y,
     197                                     void const *data, size_t len,
     198                                     char const *format)
     199{
     200    caca_canvas_t *tmp;
     201    ssize_t ret;
     202
     203    tmp = caca_create_canvas(0, 0);
     204    ret = caca_import_canvas_from_memory(tmp, data, len, format);
     205
     206    if(ret > 0)
     207        caca_blit(cv, x, y, tmp, NULL);
     208
     209    caca_free_canvas(tmp);
     210
     211    return ret;
     212}
     213
     214/** \brief Import a file into a canvas area
     215 *
     216 *  Import a file into the given libcaca canvas's current frame, at the
     217 *  specified position. For more information, see
     218 *  caca_import_canvas_from_file().
     219 *
     220 *  If an error occurs, -1 is returned and \b errno is set accordingly:
     221 *  - \c ENOSYS File access is not implemented on this system.
     222 *  - \c ENOMEM Not enough memory to allocate canvas.
     223 *  - \c EINVAL Unsupported format requested or invalid coordinates.
     224 *  caca_import_file() may also fail and set \b errno for any of the
     225 *  errors specified for the routine fopen().
     226 *
     227 *  \param cv A libcaca canvas in which to import the file.
     228 *  \param x The leftmost coordinate of the area to import to.
     229 *  \param y The topmost coordinate of the area to import to.
     230 *  \param filename The name of the file to load.
     231 *  \param format A string describing the input format.
     232 *  \return The number of bytes read, or 0 if there was not enough data,
     233 *  or -1 if an error occurred.
     234 */
     235ssize_t caca_import_area_from_file(caca_canvas_t *cv, int x, int y,
     236                                   char const *filename, char const *format)
     237{
     238    caca_canvas_t *tmp;
     239    ssize_t ret;
     240
     241    tmp = caca_create_canvas(0, 0);
     242    ret = caca_import_canvas_from_file(tmp, filename, format);
     243
     244    if(ret > 0)
     245        caca_blit(cv, x, y, tmp, NULL);
     246
     247    caca_free_canvas(tmp);
     248
     249    return ret;
    175250}
    176251
     
    330405ssize_t cucul_import_file(cucul_canvas_t *, char const *,
    331406                          char const *) CACA_ALIAS(caca_import_file);
     407ssize_t caca_import_memory(caca_canvas_t *, void const *, size_t, char const *)
     408                                  CACA_ALIAS(caca_import_canvas_from_memory);
     409ssize_t caca_import_file(caca_canvas_t *, char const *, char const *)
     410                                  CACA_ALIAS(caca_import_canvas_from_file);
    332411char const * const * cucul_get_import_list(void)
    333412         CACA_ALIAS(caca_get_import_list);
  • libcaca/trunk/caca/driver/raw.c

    r2962 r3495  
    11/*
    22 *  libcaca       Colour ASCII-Art library
    3  *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
     3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
    44 *                All Rights Reserved
    55 *
     
    7171    size_t len;
    7272
    73     buffer = caca_export_memory(dp->cv, "caca", &len);
     73    buffer = caca_export_canvas_to_memory(dp->cv, "caca", &len);
    7474    if(!buffer)
    7575        return;
  • libcaca/trunk/caca/figfont.c

    r3484 r3495  
    11/*
    22 *  libcaca       Colour ASCII-Art library
    3  *  Copyright (c) 2006-2007 Sam Hocevar <sam@zoy.org>
     3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
    44 *                All Rights Reserved
    55 *
     
    449449    /* Import buffer into canvas */
    450450    ff->fontcv = caca_create_canvas(0, 0);
    451     caca_import_memory(ff->fontcv, data, i, "utf8");
     451    caca_import_canvas_from_memory(ff->fontcv, data, i, "utf8");
    452452    free(data);
    453453
  • libcaca/trunk/caca/legacy.c

    r2821 r3495  
    11/*
    22 *  libcaca       Colour ASCII-Art library
    3  *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
     3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
    44 *                All Rights Reserved
    55 *
     
    9999{
    100100    caca_canvas_t *cv = caca_create_canvas(0, 0);
    101     int ret = caca_import_memory(cv, (unsigned char const *)buf->data,
    102                                   buf->size, format);
     101    int ret = caca_import_canvas_from_memory(cv,
     102                                             (unsigned char const *)buf->data,
     103                                             buf->size, format);
    103104    if(ret < 0)
    104105    {
     
    125126    }
    126127