[gtk/image-loading: 12/12] texture: Refactor gdk_texture_new_from_bytes()




commit f340bbdb0429651b42f5c3b99b9d206ac686c75f
Author: Benjamin Otte <otte redhat com>
Date:   Fri Sep 17 03:42:27 2021 +0200

    texture: Refactor gdk_texture_new_from_bytes()
    
    1. Split into a real loader and a fake one
    
    2. Only try the fake one if the real one doesn't support the file.

 gdk/gdktexture.c | 85 +++++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 60 insertions(+), 25 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index a6e79bca56..4107c73685 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -41,6 +41,7 @@
 #include "gdktextureprivate.h"
 
 #include "gdkinternals.h"
+#include "gdkintl.h"
 #include "gdkmemorytextureprivate.h"
 #include "gdkpaintable.h"
 #include "gdksnapshot.h"
@@ -418,6 +419,52 @@ gdk_texture_can_load (GBytes *bytes)
          gdk_is_tiff (bytes);
 }
 
+static GdkTexture *
+gdk_texture_new_from_bytes_internal (GBytes  *bytes,
+                                     GError **error)
+{
+  if (gdk_is_png (bytes))
+    {
+      return gdk_load_png (bytes, error);
+    }
+  else if (gdk_is_jpeg (bytes))
+    {
+      return gdk_load_jpeg (bytes, error);
+    }
+  else if (gdk_is_tiff (bytes))
+    {
+      return gdk_load_tiff (bytes, error);
+    }
+  else
+    {
+      g_set_error_literal (error,
+                           GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT,
+                           _("Unknown image format."));
+      return NULL;
+    }
+}
+
+static GdkTexture *
+gdk_texture_new_from_bytes_pixbuf (GBytes  *bytes,
+                                   GError **error)
+{
+  GInputStream *stream;
+  GdkPixbuf *pixbuf;
+  GdkTexture *texture;
+
+  stream = g_memory_input_stream_new_from_bytes (bytes);
+  pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
+  g_object_unref (stream);
+  if (pixbuf == NULL)
+    return NULL;
+
+  texture = gdk_texture_new_for_pixbuf (pixbuf);
+  g_object_unref (pixbuf);
+
+  return texture;
+}
+
+
 /**
  * gdk_texture_new_from_bytes:
  * @bytes: a `GBytes` containing the data to load
@@ -438,38 +485,26 @@ GdkTexture *
 gdk_texture_new_from_bytes (GBytes  *bytes,
                             GError **error)
 {
+  GdkTexture *texture;
+  GError *internal_error = NULL;
+
   g_return_val_if_fail (bytes != NULL, NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
 
-  if (gdk_is_png (bytes))
-    {
-      return gdk_load_png (bytes, error);
-    }
-  else if (gdk_is_jpeg (bytes))
+  texture = gdk_texture_new_from_bytes_internal (bytes, &internal_error);
+  if (texture)
+    return texture;
+
+  if (!g_error_matches (internal_error, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT) &&
+      !g_error_matches (internal_error, GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_FORMAT))
     {
-      return gdk_load_jpeg (bytes, error);
+      g_propagate_error (error, internal_error);
+      return NULL;
     }
-  else if (gdk_is_tiff (bytes))
-    {
-      return gdk_load_tiff (bytes, error);
-    }
-  else
-    {
-      GInputStream *stream;
-      GdkPixbuf *pixbuf;
-      GdkTexture *texture;
 
-      stream = g_memory_input_stream_new_from_bytes (bytes);
-      pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
-      g_object_unref (stream);
-      if (pixbuf == NULL)
-        return NULL;
+  g_clear_error (&internal_error);
 
-      texture = gdk_texture_new_for_pixbuf (pixbuf);
-      g_object_unref (pixbuf);
-
-      return texture;
-    }
+  return gdk_texture_new_from_bytes_pixbuf (bytes, &internal_error);
 }
 
 /**


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