Reduce pixbuf helpers
authorMatthias Clasen <mclasen@redhat.com>
Wed, 17 May 2023 01:53:55 +0000 (21:53 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Wed, 17 May 2023 01:56:22 +0000 (21:56 -0400)
Concentrate pixbuf handling in gdkpixbufutils.c.

gtk/gdkpixbufutils.c
gtk/gdkpixbufutilsprivate.h
gtk/gtkcssimagerecolor.c
gtk/gtkicontheme.c
tools/encodesymbolic.c

index c60335e228c50bcf9575a58aa429aae5412e7656..b96282c94d22a651aafc6c0507b8336747463d77 100644 (file)
@@ -22,6 +22,8 @@
 
 #include "gdk/gdktextureprivate.h"
 
+/* {{{ Pixbuf helpers */
+
 static GdkPixbuf *
 load_from_stream (GdkPixbufLoader  *loader,
                   GInputStream     *stream,
@@ -145,7 +147,7 @@ size_prepared_cb2 (GdkPixbufLoader *loader,
   gdk_pixbuf_loader_set_size (loader, width, height);
 }
 
-GdkPixbuf *
+static GdkPixbuf *
 _gdk_pixbuf_new_from_stream_at_scale (GInputStream  *stream,
                                       int            width,
                                       int            height,
@@ -172,15 +174,7 @@ _gdk_pixbuf_new_from_stream_at_scale (GInputStream  *stream,
   return pixbuf;
 }
 
-GdkPixbuf *
-_gdk_pixbuf_new_from_stream (GInputStream  *stream,
-                             GCancellable  *cancellable,
-                             GError       **error)
-{
-  return _gdk_pixbuf_new_from_stream_scaled (stream, 0, cancellable, error);
-}
-
-GdkPixbuf *
+static GdkPixbuf *
 _gdk_pixbuf_new_from_resource_at_scale (const char   *resource_path,
                                         int           width,
                                         int           height,
@@ -198,9 +192,11 @@ _gdk_pixbuf_new_from_resource_at_scale (const char   *resource_path,
   g_object_unref (stream);
 
   return pixbuf;
-
 }
 
+/* }}} */
+/* {{{ Symbolic processing */
+
 static GdkPixbuf *
 load_symbolic_svg (const char     *escaped_file_data,
                    int             width,
@@ -306,19 +302,17 @@ gtk_make_symbolic_pixbuf_from_data (const char  *file_data,
   char *escaped_file_data;
 
   /* Fetch size from the original icon */
-  {
-    GInputStream *stream = g_memory_input_stream_new_from_data (file_data, file_len, NULL);
-    GdkPixbuf *reference = gdk_pixbuf_new_from_stream (stream, NULL, error);
+  GInputStream *stream = g_memory_input_stream_new_from_data (file_data, file_len, NULL);
+  GdkPixbuf *reference = gdk_pixbuf_new_from_stream (stream, NULL, error);
 
-    g_object_unref (stream);
+  g_object_unref (stream);
 
-    if (!reference)
-      return NULL;
+  if (!reference)
+    return NULL;
 
-    icon_width = gdk_pixbuf_get_width (reference);
-    icon_height = gdk_pixbuf_get_height (reference);
-    g_object_unref (reference);
-  }
+  icon_width = gdk_pixbuf_get_width (reference);
+  icon_height = gdk_pixbuf_get_height (reference);
+  g_object_unref (reference);
 
   escaped_file_data = g_base64_encode ((guchar *) file_data, file_len);
   icon_width_str = g_strdup_printf ("%d", icon_width);
@@ -389,12 +383,12 @@ out:
   return pixbuf;
 }
 
-GdkPixbuf *
-gtk_make_symbolic_pixbuf_from_resource (const char  *path,
-                                        int          width,
-                                        int          height,
-                                        double       scale,
-                                        GError     **error)
+static GdkPixbuf *
+make_symbolic_pixbuf_from_resource (const char  *path,
+                                    int          width,
+                                    int          height,
+                                    double       scale,
+                                    GError     **error)
 {
   GBytes *bytes;
   const char *data;
@@ -414,12 +408,12 @@ gtk_make_symbolic_pixbuf_from_resource (const char  *path,
   return pixbuf;
 }
 
-GdkPixbuf *
-gtk_make_symbolic_pixbuf_from_path (const char  *path,
-                                    int          width,
-                                    int          height,
-                                    double       scale,
-                                    GError     **error)
+static GdkPixbuf *
+make_symbolic_pixbuf_from_path (const char  *path,
+                                int          width,
+                                int          height,
+                                double       scale,
+                                GError     **error)
 {
   char *data;
   gsize size;
@@ -436,11 +430,11 @@ gtk_make_symbolic_pixbuf_from_path (const char  *path,
 }
 
 static GdkPixbuf *
-gtk_make_symbolic_pixbuf_from_file (GFile       *file,
-                                    int          width,
-                                    int          height,
-                                    double       scale,
-                                    GError     **error)
+make_symbolic_pixbuf_from_file (GFile       *file,
+                                int          width,
+                                int          height,
+                                double       scale,
+                                GError     **error)
 {
   char *data;
   gsize size;
@@ -456,6 +450,91 @@ gtk_make_symbolic_pixbuf_from_file (GFile       *file,
   return pixbuf;
 }
 
+/* }}} */
+/* {{{ Texture API */
+
+GdkTexture *
+gdk_texture_new_from_stream_at_scale (GInputStream  *stream,
+                                      int            width,
+                                      int            height,
+                                      gboolean       aspect,
+                                      GCancellable  *cancellable,
+                                      GError       **error)
+{
+  GdkPixbuf *pixbuf;
+  GdkTexture *texture = NULL;
+
+  pixbuf = _gdk_pixbuf_new_from_stream_at_scale (stream, width, height, aspect, cancellable, error);
+  if (pixbuf)
+    {
+      texture = gdk_texture_new_for_pixbuf (pixbuf);
+      g_object_unref (pixbuf);
+    }
+
+  return texture;
+}
+
+GdkTexture *
+gdk_texture_new_from_stream (GInputStream  *stream,
+                             GCancellable  *cancellable,
+                             GError       **error)
+{
+  GdkPixbuf *pixbuf;
+  GdkTexture *texture = NULL;
+
+  pixbuf = _gdk_pixbuf_new_from_stream_scaled (stream, 0, cancellable, error);
+  if (pixbuf)
+    {
+      texture = gdk_texture_new_for_pixbuf (pixbuf);
+      g_object_unref (pixbuf);
+    }
+
+  return texture;
+}
+
+GdkTexture *
+gdk_texture_new_from_resource_at_scale (const char    *path,
+                                        int            width,
+                                        int            height,
+                                        gboolean       preserve_aspect,
+                                        GError       **error)
+{
+  GdkPixbuf *pixbuf;
+  GdkTexture *texture = NULL;
+
+  pixbuf = _gdk_pixbuf_new_from_resource_at_scale (path, width, height, preserve_aspect, error);
+  if (pixbuf)
+    {
+      texture = gdk_texture_new_for_pixbuf (pixbuf);
+      g_object_unref (pixbuf);
+    }
+
+  return texture;
+}
+
+/* }}} */
+/* {{{ Symbolic texture API */
+
+GdkTexture *
+gdk_texture_new_from_path_symbolic (const char    *path,
+                                    int            width,
+                                    int            height,
+                                    double         scale,
+                                    GError       **error)
+{
+  GdkPixbuf *pixbuf;
+  GdkTexture *texture = NULL;
+
+  pixbuf = make_symbolic_pixbuf_from_path (path, width, height, scale, error);
+  if (pixbuf)
+    {
+      texture = gdk_texture_new_for_pixbuf (pixbuf);
+      g_object_unref (pixbuf);
+    }
+
+  return texture;
+}
+
 GdkTexture *
 gtk_load_symbolic_texture_from_resource (const char *path)
 {
@@ -463,16 +542,16 @@ gtk_load_symbolic_texture_from_resource (const char *path)
 }
 
 GdkTexture *
-gtk_make_symbolic_texture_from_resource (const char  *path,
-                                         int          width,
-                                         int          height,
-                                         double       scale,
-                                         GError     **error)
+gdk_texture_new_from_resource_symbolic (const char  *path,
+                                        int          width,
+                                        int          height,
+                                        double       scale,
+                                        GError     **error)
 {
   GdkPixbuf *pixbuf;
   GdkTexture *texture = NULL;
 
-  pixbuf = gtk_make_symbolic_pixbuf_from_resource (path, width, height, scale, error);
+  pixbuf = make_symbolic_pixbuf_from_resource (path, width, height, scale, error);
   if (pixbuf)
     {
       texture = gdk_texture_new_for_pixbuf (pixbuf);
@@ -493,7 +572,7 @@ gtk_load_symbolic_texture_from_file (GFile *file)
   if (stream == NULL)
     return NULL;
 
-  pixbuf = _gdk_pixbuf_new_from_stream (stream, NULL, NULL);
+  pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL);
   g_object_unref (stream);
   if (pixbuf == NULL)
     return NULL;
@@ -505,22 +584,25 @@ gtk_load_symbolic_texture_from_file (GFile *file)
 }
 
 GdkTexture *
-gtk_make_symbolic_texture_from_file (GFile       *file,
-                                     int          width,
-                                     int          height,
-                                     double       scale,
-                                     GError     **error)
+gdk_texture_new_from_file_symbolic (GFile       *file,
+                                    int          width,
+                                    int          height,
+                                    double       scale,
+                                    GError     **error)
 {
   GdkPixbuf *pixbuf;
   GdkTexture *texture;
 
-  pixbuf = gtk_make_symbolic_pixbuf_from_file (file, width, height, scale, error);
+  pixbuf = make_symbolic_pixbuf_from_file (file, width, height, scale, error);
   texture = gdk_texture_new_for_pixbuf (pixbuf);
   g_object_unref (pixbuf);
 
   return texture;
 }
 
+/* }}} */
+/* {{{ Scaled paintable API */
+
 typedef struct {
   int scale_factor;
 } LoaderData;
@@ -651,3 +733,7 @@ gdk_paintable_new_from_file_scaled (GFile *file,
 
   return paintable;
 }
+
+/* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
index 10801722e42838b32bec2d5bbe4e91182a093347..77e21a4c75b87292bbb5f762896d50ea98854410 100644 (file)
 
 G_BEGIN_DECLS
 
-GdkPixbuf *_gdk_pixbuf_new_from_stream              (GInputStream  *stream,
+GdkPixbuf *gtk_make_symbolic_pixbuf_from_data       (const char    *data,
+                                                     gsize          len,
+                                                     int            width,
+                                                     int            height,
+                                                     double         scale,
+                                                     const char    *debug_output_to,
+                                                     GError       **error);
+
+GdkTexture *gdk_texture_new_from_stream             (GInputStream  *stream,
                                                      GCancellable  *cancellable,
                                                      GError       **error);
-GdkPixbuf *_gdk_pixbuf_new_from_stream_at_scale     (GInputStream  *stream,
+GdkTexture *gdk_texture_new_from_stream_at_scale    (GInputStream  *stream,
                                                      int            width,
                                                      int            height,
                                                      gboolean       aspect,
                                                      GCancellable  *cancellable,
                                                      GError       **error);
-GdkPixbuf *_gdk_pixbuf_new_from_resource_at_scale   (const char    *resource_path,
+GdkTexture *gdk_texture_new_from_resource_at_scale  (const char    *path,
                                                      int            width,
                                                      int            height,
-                                                     gboolean       preserve_aspect,
+                                                     gboolean       aspect,
                                                      GError       **error);
-GdkPixbuf *gtk_make_symbolic_pixbuf_from_data       (const char    *data,
-                                                     gsize          len,
+
+GdkTexture *gdk_texture_new_from_path_symbolic      (const char    *path,
                                                      int            width,
                                                      int            height,
                                                      double         scale,
-                                                     const char    *debug_output_to,
                                                      GError       **error);
-GdkPixbuf *gtk_make_symbolic_pixbuf_from_path       (const char    *path,
+GdkTexture *gdk_texture_new_from_file_symbolic      (GFile         *file,
                                                      int            width,
                                                      int            height,
                                                      double         scale,
                                                      GError       **error);
-GdkPixbuf *gtk_make_symbolic_pixbuf_from_resource   (const char    *path,
+GdkTexture *gdk_texture_new_from_resource_symbolic  (const char    *path,
                                                      int            width,
                                                      int            height,
                                                      double         scale,
                                                      GError       **error);
+
 GdkTexture *gtk_load_symbolic_texture_from_file     (GFile         *file);
-GdkTexture *gtk_make_symbolic_texture_from_file     (GFile         *file,
-                                                     int            width,
-                                                     int            height,
-                                                     double         scale,
-                                                     GError       **error);
 GdkTexture *gtk_load_symbolic_texture_from_resource (const char    *data);
-GdkTexture *gtk_make_symbolic_texture_from_resource (const char    *path,
-                                                     int            width,
-                                                     int            height,
-                                                     double         scale,
-                                                     GError       **error);
+
 GdkPaintable *gdk_paintable_new_from_path_scaled     (const char    *path,
                                                       int            scale_factor);
 GdkPaintable *gdk_paintable_new_from_resource_scaled (const char    *path,
@@ -72,4 +71,3 @@ GdkPaintable *gdk_paintable_new_from_file_scaled     (GFile         *file,
                                                       int            scale_factor);
 
 G_END_DECLS
-
index 62ca62790c745a7b6ed8eca68408ab99078e550f..b9760aff4b3b1f9eca6b9e71205a17c52aa06283 100644 (file)
@@ -110,7 +110,7 @@ gtk_css_image_recolor_load_texture (GtkCssImageRecolor  *recolor,
       if (g_str_has_suffix (uri, ".symbolic.png"))
         recolor->texture = gtk_load_symbolic_texture_from_resource (resource_path);
       else
-        recolor->texture = gtk_make_symbolic_texture_from_resource (resource_path, 0, 0, 1.0, NULL);
+        recolor->texture = gdk_texture_new_from_resource_symbolic (resource_path, 0, 0, 1.0, NULL);
 
       g_free (resource_path);
     }
@@ -119,7 +119,7 @@ gtk_css_image_recolor_load_texture (GtkCssImageRecolor  *recolor,
       if (g_str_has_suffix (uri, ".symbolic.png"))
         recolor->texture = gtk_load_symbolic_texture_from_file (recolor->file);
       else
-        recolor->texture = gtk_make_symbolic_texture_from_file (recolor->file, 0, 0, 1.0, NULL);
+        recolor->texture = gdk_texture_new_from_file_symbolic (recolor->file, 0, 0, 1.0, NULL);
     }
 
   g_free (uri);
index b07313340174293f59e8a2c59c88b219aba20432..7b19810ebf8a827f01fa29f44ab80ec9f5e024f6 100644 (file)
@@ -3747,22 +3747,15 @@ icon_ensure_texture__locked (GtkIconPaintable *icon,
     {
       if (icon->is_svg)
         {
-          GdkPixbuf *source_pixbuf;
-
           if (gtk_icon_paintable_is_symbolic (icon))
-            source_pixbuf = gtk_make_symbolic_pixbuf_from_resource (icon->filename,
+            icon->texture = gdk_texture_new_from_resource_symbolic (icon->filename,
                                                                     pixel_size, pixel_size,
                                                                     icon->desired_scale,
                                                                     &load_error);
           else
-            source_pixbuf = _gdk_pixbuf_new_from_resource_at_scale (icon->filename,
+            icon->texture = gdk_texture_new_from_resource_at_scale (icon->filename,
                                                                     pixel_size, pixel_size,
                                                                     TRUE, &load_error);
-          if (source_pixbuf)
-            {
-              icon->texture = gdk_texture_new_for_pixbuf (source_pixbuf);
-              g_object_unref (source_pixbuf);
-            }
         }
       else
         icon->texture = gdk_texture_new_from_resource (icon->filename);
@@ -3771,10 +3764,8 @@ icon_ensure_texture__locked (GtkIconPaintable *icon,
     {
       if (icon->is_svg)
         {
-          GdkPixbuf *source_pixbuf;
-
           if (gtk_icon_paintable_is_symbolic (icon))
-            source_pixbuf = gtk_make_symbolic_pixbuf_from_path (icon->filename,
+            icon->texture = gdk_texture_new_from_path_symbolic (icon->filename,
                                                                 pixel_size, pixel_size,
                                                                 icon->desired_scale,
                                                                 &load_error);
@@ -3783,22 +3774,16 @@ icon_ensure_texture__locked (GtkIconPaintable *icon,
               GFile *file = g_file_new_for_path (icon->filename);
               GInputStream *stream = G_INPUT_STREAM (g_file_read (file, NULL, &load_error));
 
-              g_object_unref (file);
               if (stream)
                 {
-                  source_pixbuf = _gdk_pixbuf_new_from_stream_at_scale (stream,
+                  icon->texture = gdk_texture_new_from_stream_at_scale (stream,
                                                                         pixel_size, pixel_size,
                                                                         TRUE, NULL,
                                                                         &load_error);
                   g_object_unref (stream);
                 }
-              else
-                source_pixbuf = NULL;
-            }
-          if (source_pixbuf)
-            {
-              icon->texture = gdk_texture_new_for_pixbuf (source_pixbuf);
-              g_object_unref (source_pixbuf);
+
+              g_object_unref (file);
             }
         }
       else
@@ -3809,35 +3794,24 @@ icon_ensure_texture__locked (GtkIconPaintable *icon,
   else
     {
       GInputStream *stream;
-      GdkPixbuf *source_pixbuf;
 
       g_assert (icon->loadable);
 
-      stream = g_loadable_icon_load (icon->loadable,
-                                     pixel_size,
-                                     NULL, NULL,
-                                     &load_error);
+      stream = g_loadable_icon_load (icon->loadable, pixel_size, NULL, NULL, &load_error);
       if (stream)
         {
           /* SVG icons are a special case - we just immediately scale them
            * to the desired size
            */
           if (icon->is_svg)
-            {
-              source_pixbuf = _gdk_pixbuf_new_from_stream_at_scale (stream,
-                                                                    pixel_size, pixel_size,
-                                                                    TRUE, NULL,
-                                                                    &load_error);
-            }
+            icon->texture = gdk_texture_new_from_stream_at_scale (stream,
+                                                                  pixel_size, pixel_size,
+                                                                  TRUE, NULL,
+                                                                  &load_error);
           else
-            source_pixbuf = _gdk_pixbuf_new_from_stream (stream,
-                                                         NULL, &load_error);
+            icon->texture = gdk_texture_new_from_stream (stream, NULL, &load_error);
+
           g_object_unref (stream);
-          if (source_pixbuf)
-            {
-              icon->texture = gdk_texture_new_for_pixbuf (source_pixbuf);
-              g_object_unref (source_pixbuf);
-            }
         }
     }
 
index 90887f336557e6c79b42ae1a4825ad1f41beeb92..bfe96005b8a73b206523575a9acb7ef943b8871b 100644 (file)
@@ -133,9 +133,9 @@ main (int argc, char **argv)
 
 
   out = g_file_replace (dest,
-                       NULL, FALSE,
-                       G_FILE_CREATE_REPLACE_DESTINATION,
-                       NULL, &error);
+                        NULL, FALSE,
+                        G_FILE_CREATE_REPLACE_DESTINATION,
+                        NULL, &error);
   if (out == NULL)
     {
       g_printerr (_("Can’t save file %s: %s\n"), pngpath, error->message);