diff --git a/cairocffi/ffi_build.py b/cairocffi/ffi_build.py index b29097c..c51561c 100644 --- a/cairocffi/ffi_build.py +++ b/cairocffi/ffi_build.py @@ -68,6 +68,8 @@ gboolean gdk_pixbuf_loader_write ( GdkPixbufLoader *loader, const guchar *buf, gsize count, GError **error); + void gdk_pixbuf_loader_set_size ( + GdkPixbufLoader *loader, int width, int height); gboolean gdk_pixbuf_loader_close ( GdkPixbufLoader *loader, GError **error); diff --git a/cairocffi/pixbuf.py b/cairocffi/pixbuf.py index 545a776..345c868 100644 --- a/cairocffi/pixbuf.py +++ b/cairocffi/pixbuf.py @@ -69,11 +69,13 @@ def __getattr__(self, name): return partial(function, self._pointer) -def decode_to_pixbuf(image_data): +def decode_to_pixbuf(image_data, width=None, height=None): """Decode an image from memory with GDK-PixBuf. The file format is detected automatically. :param image_data: A byte string + :param width: Integer width in pixels or None + :param height: Integer height in pixels or None :returns: A tuple of a new :class:`PixBuf` object and the name of the detected image format. @@ -85,6 +87,8 @@ def decode_to_pixbuf(image_data): loader = ffi.gc( gdk_pixbuf.gdk_pixbuf_loader_new(), gobject.g_object_unref) error = ffi.new('GError **') + if width and height: + gdk_pixbuf.gdk_pixbuf_loader_set_size(loader, width, height) handle_g_error(error, gdk_pixbuf.gdk_pixbuf_loader_write( loader, ffi.new('guchar[]', image_data), len(image_data), error)) handle_g_error(error, gdk_pixbuf.gdk_pixbuf_loader_close(loader, error)) @@ -101,11 +105,13 @@ def decode_to_pixbuf(image_data): return Pixbuf(pixbuf), format_name -def decode_to_image_surface(image_data): +def decode_to_image_surface(image_data, width=None, height=None): """Decode an image from memory into a cairo surface. The file format is detected automatically. :param image_data: A byte string + :param width: Integer width in pixels or None + :param height: Integer height in pixels or None :returns: A tuple of a new :class:`~cairocffi.ImageSurface` object and the name of the detected image format. @@ -114,7 +120,7 @@ def decode_to_image_surface(image_data): or in an unsupported format. """ - pixbuf, format_name = decode_to_pixbuf(image_data) + pixbuf, format_name = decode_to_pixbuf(image_data, width, height) surface = ( pixbuf_to_cairo_gdk(pixbuf) if gdk is not None else pixbuf_to_cairo_slices(pixbuf) if not pixbuf.get_has_alpha()