Ignore:
Timestamp:
May 21, 2009, 10:55:21 PM (11 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().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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);
Note: See TracChangeset for help on using the changeset viewer.