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().

Location:
libcaca/trunk/caca/codec
Files:
2 edited

Legend:

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