Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add width and height options to pixbuf.decode_to_image_surface #98

Merged
merged 2 commits into from
May 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cairocffi/ffi_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
12 changes: 9 additions & 3 deletions cairocffi/pixbuf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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))
Expand All @@ -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.
Expand All @@ -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()
Expand Down