[recipes] Add a utility function for saving rotated images



commit 45d14face26c8501b18f17b21e11ae1babb25707
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Jan 24 09:53:49 2017 +0300

    Add a utility function for saving rotated images
    
    GTK+ CSS (like regular CSS) has no way for use to rotate a
    background image, so we need to save a rotated copy for use
    as a background in tiles/

 src/gr-utils.c |   47 +++++++++++++++++++++++++++++++++++++++++++++++
 src/gr-utils.h |    3 +++
 2 files changed, 50 insertions(+), 0 deletions(-)
---
diff --git a/src/gr-utils.c b/src/gr-utils.c
index 42530f7..27e40c9 100644
--- a/src/gr-utils.c
+++ b/src/gr-utils.c
@@ -457,3 +457,50 @@ portals_available (void)
 
         return owner != NULL;
 }
+
+char *
+ensure_rotated_image (const char *path,
+                      int         angle)
+{
+        g_autofree char *rotated = NULL;
+        g_autofree char *basename = NULL;
+        g_autofree char *subdir = NULL;
+        g_autofree char *dir = NULL;
+
+        if (angle == 0)
+                return g_strdup (path);
+
+        basename = g_path_get_basename (path);
+        subdir = g_strdup_printf ("images%d", angle);
+
+        dir = g_build_filename (g_get_user_cache_dir (), "recipes", "subdir", NULL);
+        g_mkdir_with_parents (dir, 755);
+
+        rotated = g_build_filename (dir, basename, NULL);
+        if (!g_file_test (rotated, G_FILE_TEST_EXISTS)) {
+                g_autoptr(GdkPixbuf) original = NULL;
+                g_autoptr(GdkPixbuf) pb = NULL;
+                g_autoptr(GError) error = NULL;
+                const char *format;
+
+                original = gdk_pixbuf_new_from_file (path, &error);
+                if (original == NULL) {
+                        g_message ("Failed to load image from %s: %s", path, error->message);
+                        return NULL;
+                }
+
+                pb = gdk_pixbuf_rotate_simple (original, angle);
+
+                if (g_str_has_suffix (path, ".jpg"))
+                        format = "jpeg";
+                else
+                        format = "png";
+
+                if (!gdk_pixbuf_save (pb, rotated, format, &error, NULL)) {
+                        g_message ("Failed to save rotated image to %s: %s", rotated, error->message);
+                        return NULL;
+                }
+        }
+
+        return g_strdup (rotated);
+}
diff --git a/src/gr-utils.h b/src/gr-utils.h
index 174d191..07a2377 100644
--- a/src/gr-utils.h
+++ b/src/gr-utils.h
@@ -34,6 +34,9 @@ GdkPixbuf *load_pixbuf_fill_size (const char *path,
                                   int         width,
                                   int         height);
 
+char *ensure_rotated_image (const char *path,
+                            int         angle);
+
 const char *get_pkg_data_dir  (void);
 const char *get_user_data_dir (void);
 


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