texture: Refactor gdk_texture_new_from_bytes()
authorBenjamin Otte <otte@redhat.com>
Fri, 17 Sep 2021 01:42:27 +0000 (03:42 +0200)
committerBenjamin Otte <otte@redhat.com>
Fri, 17 Sep 2021 02:31:46 +0000 (04:31 +0200)
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

index a6e79bca56b5c37cc45a5117141f17750338a2c8..fe02cb8e6307e3308c631d8bee8b3536f5e98b98 100644 (file)
@@ -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_CONTENT) &&
+      !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, error);
 }
 
 /**