[retro-gtk] Add RetroPixbuf



commit 1d0cf94afc55dadcabf0a5251bc23f1722ccc183
Author: Alexander Mikhaylenko <exalm7659 gmail com>
Date:   Wed Aug 14 15:45:33 2019 +0500

    Add RetroPixbuf
    
    Add helper functions for working with aspect ratio embedded into pixbufs
    as the "aspect-ratio" pixbuf option.

 retro-gtk/meson.build    |  2 ++
 retro-gtk/retro-gtk.h    |  1 +
 retro-gtk/retro-pixbuf.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++
 retro-gtk/retro-pixbuf.h | 21 +++++++++++++++++++
 4 files changed, 78 insertions(+)
---
diff --git a/retro-gtk/meson.build b/retro-gtk/meson.build
index 12914a2..96d979f 100644
--- a/retro-gtk/meson.build
+++ b/retro-gtk/meson.build
@@ -33,6 +33,7 @@ retro_gtk_sources = [
   'retro-option.c',
   'retro-option-iterator.c',
   'retro-pa-player.c',
+  'retro-pixbuf.c',
   'retro-pixdata.c',
   'retro-pixel-format.c',
   'retro-rumble-effect.c',
@@ -59,6 +60,7 @@ retro_gtk_headers = [
   'retro-module-query.h',
   'retro-option.h',
   'retro-option-iterator.h',
+  'retro-pixbuf.h',
   'retro-pixdata.h',
   'retro-rumble-effect.h',
   'retro-video-filter.h',
diff --git a/retro-gtk/retro-gtk.h b/retro-gtk/retro-gtk.h
index bcbb818..9cb91cd 100644
--- a/retro-gtk/retro-gtk.h
+++ b/retro-gtk/retro-gtk.h
@@ -25,6 +25,7 @@
 #include "retro-module-query.h"
 #include "retro-option.h"
 #include "retro-option-iterator.h"
+#include "retro-pixbuf.h"
 #include "retro-pixdata.h"
 #include "retro-video-filter.h"
 
diff --git a/retro-gtk/retro-pixbuf.c b/retro-gtk/retro-pixbuf.c
new file mode 100644
index 0000000..900417d
--- /dev/null
+++ b/retro-gtk/retro-pixbuf.c
@@ -0,0 +1,54 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#include "retro-pixbuf.h"
+#include <string.h>
+
+/**
+ * retro_pixbuf_get_aspect_ratio:
+ * @pixbuf: a #GdkPixbuf
+ *
+ * Gets the aspect ratio of @pixbuf by reading the 'aspect-ratio' pixbuf option.
+ *
+ * Returns: the aspect ratio, or 0 if the option is not set or its value is invalid.
+ */
+gfloat
+retro_pixbuf_get_aspect_ratio (GdkPixbuf *pixbuf)
+{
+  const gchar *aspect_ratio_str;
+  gfloat result = 0.f;
+
+  g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), 0.f);
+
+  aspect_ratio_str = gdk_pixbuf_get_option (pixbuf, "aspect-ratio");
+
+  if (!aspect_ratio_str)
+    return 0.f;
+
+  sscanf (aspect_ratio_str, "%g", &result);
+
+  return result;
+}
+
+/**
+ * retro_pixbuf_set_aspect_ratio:
+ * @pixbuf: a #GdkPixbuf
+ * @aspect_ratio: the aspect ratio value
+ *
+ * Sets the aspect ratio of @pixbuf by setting the 'aspect-ratio' pixbuf option.
+ * Use retro_pixbuf_get_aspect_ratio() to retrieve it.
+ */
+void
+retro_pixbuf_set_aspect_ratio (GdkPixbuf *pixbuf,
+                               gfloat     aspect_ratio)
+{
+  g_autofree gchar *aspect_ratio_string = NULL;
+
+  g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
+  g_return_if_fail (aspect_ratio > 0.f);
+
+  aspect_ratio_string = g_strdup_printf ("%g", aspect_ratio);
+
+  gdk_pixbuf_remove_option (pixbuf, "aspect-ratio");
+
+  gdk_pixbuf_set_option (pixbuf, "aspect-ratio", aspect_ratio_string);
+}
diff --git a/retro-gtk/retro-pixbuf.h b/retro-gtk/retro-pixbuf.h
new file mode 100644
index 0000000..fa3d53e
--- /dev/null
+++ b/retro-gtk/retro-pixbuf.h
@@ -0,0 +1,21 @@
+// This file is part of retro-gtk. License: GPL-3.0+.
+
+#ifndef RETRO_PIXBUF_H
+#define RETRO_PIXBUF_H
+
+#if !defined(__RETRO_GTK_INSIDE__) && !defined(RETRO_GTK_COMPILATION)
+# error "Only <retro-gtk.h> can be included directly."
+#endif
+
+#include <gdk-pixbuf/gdk-pixbuf.h>
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+gfloat retro_pixbuf_get_aspect_ratio (GdkPixbuf *pixbuf);
+void   retro_pixbuf_set_aspect_ratio (GdkPixbuf *pixbuf,
+                                      gfloat     aspect_ratio);
+
+G_END_DECLS
+
+#endif /* RETRO_PIXBUF_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]