summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2014-05-13 17:10:48 +0100
committerRichard Hughes <richard@hughsie.com>2014-05-13 17:10:48 +0100
commitd57a0ac8bc9d814ab29a7cbcfddd77b4e33f64f3 (patch)
tree6a6e2bdf33508b81bd043c413ebadba3c6b22bb5
parent2def4da5f807f3e9466c1d3f9d676bc3de4a4106 (diff)
downloadappstream-glib-d57a0ac8bc9d814ab29a7cbcfddd77b4e33f64f3.tar.gz
Add as_image_save_filename()
-rw-r--r--libappstream-glib/as-image.c87
-rw-r--r--libappstream-glib/as-image.h22
2 files changed, 109 insertions, 0 deletions
diff --git a/libappstream-glib/as-image.c b/libappstream-glib/as-image.c
index 3642e8f..4ddcdbd 100644
--- a/libappstream-glib/as-image.c
+++ b/libappstream-glib/as-image.c
@@ -372,6 +372,93 @@ as_image_node_parse (AsImage *image, GNode *node, GError **error)
}
/**
+ * 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)
+{
+ AsImagePrivate *priv = GET_PRIVATE (image);
+ GdkPixbuf *pixbuf = NULL;
+ GdkPixbuf *pixbuf_tmp = NULL;
+ gboolean ret;
+ guint tmp_height;
+ guint tmp_width;
+ guint pixbuf_height;
+ guint pixbuf_width;
+
+ /* 0 means 'default' */
+ if (width == 0)
+ width = gdk_pixbuf_get_width (priv->pixbuf);
+ if (height == 0)
+ height = gdk_pixbuf_get_height (priv->pixbuf);
+
+ /* is the aspect ratio of the source perfectly 16:9 */
+ pixbuf_width = gdk_pixbuf_get_width (priv->pixbuf);
+ 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);
+ } 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);
+ }
+
+ /* save source file */
+ ret = gdk_pixbuf_save (pixbuf,
+ filename,
+ "png",
+ error,
+ NULL);
+ if (!ret)
+ goto out;
+out:
+ if (pixbuf != NULL)
+ g_object_unref (pixbuf);
+ if (pixbuf_tmp != NULL)
+ g_object_unref (pixbuf_tmp);
+ return ret;
+}
+
+/**
* as_image_new:
*
* Creates a new #AsImage.
diff --git a/libappstream-glib/as-image.h b/libappstream-glib/as-image.h
index 4d82f87..ecca9f3 100644
--- a/libappstream-glib/as-image.h
+++ b/libappstream-glib/as-image.h
@@ -76,6 +76,20 @@ typedef enum {
AS_IMAGE_KIND_LAST
} AsImageKind;
+/**
+ * AsImageSaveFlag:
+ * @AS_IMAGE_SAVE_FLAG_NONE: No special flags set
+ * @AS_IMAGE_SAVE_FLAG_PAD_16_9: Pad with alpha to 16:9 aspect
+ *
+ * The flags used for saving images.
+ **/
+typedef enum {
+ AS_IMAGE_SAVE_FLAG_NONE = 0, /* Since: 0.1.6 */
+ AS_IMAGE_SAVE_FLAG_PAD_16_9 = 1, /* Since: 0.1.6 */
+ /*< private >*/
+ AS_IMAGE_SAVE_FLAG_LAST
+} AsImageSaveFlags;
+
GType as_image_get_type (void);
AsImage *as_image_new (void);
@@ -103,6 +117,14 @@ void as_image_set_kind (AsImage *image,
void as_image_set_pixbuf (AsImage *image,
GdkPixbuf *pixbuf);
+/* object methods */
+gboolean as_image_save_filename (AsImage *image,
+ const gchar *filename,
+ guint width,
+ guint height,
+ AsImageSaveFlags flags,
+ GError **error);
+
G_END_DECLS
#endif /* __AS_IMAGE_H */