summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2014-05-16 14:20:14 +0100
committerRichard Hughes <richard@hughsie.com>2014-05-16 14:20:14 +0100
commit18a94a423484340a0e442b54bb89c819a68feb8f (patch)
tree3dad2e870ce2957f44220774e49398c40bfa0c6b
parent7b9cdbd74a056d49cd498af3de50b02b71675d17 (diff)
downloadappstream-glib-18a94a423484340a0e442b54bb89c819a68feb8f.tar.gz
Add as_image_save_pixbuf()
-rw-r--r--libappstream-glib/as-image.c103
-rw-r--r--libappstream-glib/as-image.h4
2 files changed, 67 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;
}
diff --git a/libappstream-glib/as-image.h b/libappstream-glib/as-image.h
index 17f6f4b..1c94639 100644
--- a/libappstream-glib/as-image.h
+++ b/libappstream-glib/as-image.h
@@ -122,6 +122,10 @@ void as_image_set_pixbuf (AsImage *image,
gboolean as_image_load_filename (AsImage *image,
const gchar *filename,
GError **error);
+GdkPixbuf *as_image_save_pixbuf (AsImage *image,
+ guint width,
+ guint height,
+ AsImageSaveFlags flags);
gboolean as_image_save_filename (AsImage *image,
const gchar *filename,
guint width,