diff --git a/src/guacenc/buffer.c b/src/guacenc/buffer.c index 69531499..a4776289 100644 --- a/src/guacenc/buffer.c +++ b/src/guacenc/buffer.c @@ -25,6 +25,7 @@ #include +#include #include #include @@ -141,3 +142,33 @@ int guacenc_buffer_fit(guacenc_buffer* buffer, int x, int y) { } +int guacenc_buffer_copy(guacenc_buffer* dst, guacenc_buffer* src) { + + /* Resize destination to exactly fit source */ + if (guacenc_buffer_resize(dst, src->width, src->height)) + return 1; + + /* Copy surface contents identically */ + if (src->surface != NULL) { + + /* Destination must be non-NULL as its size is that of the source */ + assert(dst->cairo != NULL); + + /* Reset state of destination */ + cairo_t* cairo = dst->cairo; + cairo_reset_clip(cairo); + + /* Overwrite destination with contents of source */ + cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); + cairo_set_source_surface(cairo, src->surface, 0, 0); + cairo_paint(cairo); + + /* Reset operator of destination to default */ + cairo_set_operator(cairo, CAIRO_OPERATOR_OVER); + + } + + return 0; + +} + diff --git a/src/guacenc/buffer.h b/src/guacenc/buffer.h index 1cb0c7ae..caf11e56 100644 --- a/src/guacenc/buffer.h +++ b/src/guacenc/buffer.h @@ -136,5 +136,22 @@ int guacenc_buffer_resize(guacenc_buffer* buffer, int width, int height); */ int guacenc_buffer_fit(guacenc_buffer* buffer, int x, int y); +/** + * Copies the entire contents of the given source buffer to the destination + * buffer, ignoring the current contents of the destination. The destination + * buffer's contents are entirely replaced. + * + * @param dst + * The destination buffer whose contents should be replaced. + * + * @param src + * The source buffer whose contents should replace those of the destination + * buffer. + * + * @return + * Zero if the copy operation was successful, non-zero on failure. + */ +int guacenc_buffer_copy(guacenc_buffer* dst, guacenc_buffer* src); + #endif