diff options
author | Richard Hughes <richard@hughsie.com> | 2014-05-16 14:20:14 +0100 |
---|---|---|
committer | Richard Hughes <richard@hughsie.com> | 2014-05-16 14:20:14 +0100 |
commit | 18a94a423484340a0e442b54bb89c819a68feb8f (patch) | |
tree | 3dad2e870ce2957f44220774e49398c40bfa0c6b /libappstream-glib/as-image.c | |
parent | 7b9cdbd74a056d49cd498af3de50b02b71675d17 (diff) | |
download | appstream-glib-18a94a423484340a0e442b54bb89c819a68feb8f.tar.gz |
Add as_image_save_pixbuf()
Diffstat (limited to 'libappstream-glib/as-image.c')
-rw-r--r-- | libappstream-glib/as-image.c | 103 |
1 files changed, 63 insertions, 40 deletions
diff --git a/libappstream-glib/as-image.c b/libappstream-glib/as-image.c index 8ec2b88..730da46 100644 --- a/libappstream-glib/as-image.c +++ b/libappstream-glib/as-image.c @@ -448,32 +448,27 @@ out: } /** - * as_image_save_filename: + * as_image_save_pixbuf: * @image: a #AsImage instance. - * @filename: filename to write to * @width: target width, or 0 for default * @height: target height, or 0 for default * @flags: some #AsImageSaveFlags values, e.g. %AS_IMAGE_SAVE_FLAG_PAD_16_9 - * @error: A #GError or %NULL. * - * Saves a pixbuf to a file. + * Resamples a pixbuf to a specific size. * - * Returns: %TRUE for success + * Returns: (transfer full): A #GdkPixbuf of the specified size * * Since: 0.1.6 **/ -gboolean -as_image_save_filename (AsImage *image, - const gchar *filename, - guint width, - guint height, - AsImageSaveFlags flags, - GError **error) +GdkPixbuf * +as_image_save_pixbuf (AsImage *image, + guint width, + guint height, + AsImageSaveFlags flags) { AsImagePrivate *priv = GET_PRIVATE (image); GdkPixbuf *pixbuf = NULL; GdkPixbuf *pixbuf_tmp = NULL; - gboolean ret; guint tmp_height; guint tmp_width; guint pixbuf_height; @@ -490,35 +485,65 @@ as_image_save_filename (AsImage *image, pixbuf_height = gdk_pixbuf_get_height (priv->pixbuf); if (flags == AS_IMAGE_SAVE_FLAG_NONE || (pixbuf_width / 16) * 9 == pixbuf_height) { - pixbuf = gdk_pixbuf_scale_simple (priv->pixbuf, - width, height, - GDK_INTERP_BILINEAR); + return gdk_pixbuf_scale_simple (priv->pixbuf, + width, height, + GDK_INTERP_BILINEAR); + } + + /* create new 16:9 pixbuf with alpha padding */ + pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, + TRUE, 8, + width, + height); + gdk_pixbuf_fill (pixbuf, 0x00000000); + if ((pixbuf_width / 16) * 9 > pixbuf_height) { + tmp_width = width; + tmp_height = width * pixbuf_height / pixbuf_width; } else { - /* create new 16:9 pixbuf with alpha padding */ - pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, - TRUE, 8, - width, - height); - gdk_pixbuf_fill (pixbuf, 0x00000000); - if ((pixbuf_width / 16) * 9 > pixbuf_height) { - tmp_width = width; - tmp_height = width * pixbuf_height / pixbuf_width; - } else { - tmp_width = height * pixbuf_width / pixbuf_height; - tmp_height = height; - } - pixbuf_tmp = gdk_pixbuf_scale_simple (priv->pixbuf, - tmp_width, tmp_height, - GDK_INTERP_BILINEAR); - gdk_pixbuf_copy_area (pixbuf_tmp, - 0, 0, /* of src */ - tmp_width, tmp_height, - pixbuf, - (width - tmp_width) / 2, - (height - tmp_height) / 2); + tmp_width = height * pixbuf_width / pixbuf_height; + tmp_height = height; } + pixbuf_tmp = gdk_pixbuf_scale_simple (priv->pixbuf, + tmp_width, tmp_height, + GDK_INTERP_BILINEAR); + gdk_pixbuf_copy_area (pixbuf_tmp, + 0, 0, /* of src */ + tmp_width, tmp_height, + pixbuf, + (width - tmp_width) / 2, + (height - tmp_height) / 2); + g_object_unref (pixbuf_tmp); + return pixbuf; +} + +/** + * as_image_save_filename: + * @image: a #AsImage instance. + * @filename: filename to write to + * @width: target width, or 0 for default + * @height: target height, or 0 for default + * @flags: some #AsImageSaveFlags values, e.g. %AS_IMAGE_SAVE_FLAG_PAD_16_9 + * @error: A #GError or %NULL. + * + * Saves a pixbuf to a file. + * + * Returns: %TRUE for success + * + * Since: 0.1.6 + **/ +gboolean +as_image_save_filename (AsImage *image, + const gchar *filename, + guint width, + guint height, + AsImageSaveFlags flags, + GError **error) +{ + GdkPixbuf *pixbuf; + gboolean ret; /* save source file */ + pixbuf = as_image_save_pixbuf (image, width, height, flags); ret = gdk_pixbuf_save (pixbuf, filename, "png", @@ -529,8 +554,6 @@ as_image_save_filename (AsImage *image, out: if (pixbuf != NULL) g_object_unref (pixbuf); - if (pixbuf_tmp != NULL) - g_object_unref (pixbuf_tmp); return ret; } |