[gtk/image-loading] texture: Split out type detection
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/image-loading] texture: Split out type detection
- Date: Tue, 14 Sep 2021 16:22:35 +0000 (UTC)
commit 1ba8190cd3257ad2c06771bcbd5489776a2cf4fd
Author: Benjamin Otte <otte redhat com>
Date: Tue Sep 14 17:03:49 2021 +0200
texture: Split out type detection
This way, the code using it becomes clearer and we can use it in
multiple places without accidentally doing it wrong (hint: see next
commit).
gdk/gdktexture.c | 36 +++++++++++-------------------------
gdk/loaders/gdkjpegprivate.h | 12 ++++++++++++
gdk/loaders/gdkpngprivate.h | 12 ++++++++++++
gdk/loaders/gdktiffprivate.h | 14 ++++++++++++++
4 files changed, 49 insertions(+), 25 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index aca0a415b4..4abb7fb87d 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -431,52 +431,38 @@ GdkTexture *
gdk_texture_new_from_bytes (GBytes *bytes,
GError **error)
{
- const char *data;
- gsize size;
-
g_return_val_if_fail (bytes != NULL, NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
- data = g_bytes_get_data (bytes, &size);
-
- if (size > strlen (PNG_SIGNATURE) &&
- memcmp (data, PNG_SIGNATURE, strlen (PNG_SIGNATURE)) == 0)
+ if (gdk_is_png (bytes))
{
return gdk_load_png (bytes, error);
}
- else if ((size > strlen (TIFF_SIGNATURE1) &&
- memcmp (data, TIFF_SIGNATURE1, strlen (TIFF_SIGNATURE1)) == 0) ||
- (size > strlen (TIFF_SIGNATURE2) &&
- memcmp (data, TIFF_SIGNATURE2, strlen (TIFF_SIGNATURE2)) == 0))
+ else if (gdk_is_jpeg (bytes))
{
- return gdk_load_tiff (bytes, error);
+ return gdk_load_jpeg (bytes, error);
}
- else if (size > strlen (JPEG_SIGNATURE) &&
- memcmp (data, JPEG_SIGNATURE, strlen (JPEG_SIGNATURE)) == 0)
+ else if (gdk_is_tiff (bytes))
{
- return gdk_load_jpeg (bytes, error);
+ 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;
- if (pixbuf)
- {
- GdkTexture *texture;
-
- texture = gdk_texture_new_for_pixbuf (pixbuf);
- g_object_unref (pixbuf);
+ texture = gdk_texture_new_for_pixbuf (pixbuf);
+ g_object_unref (pixbuf);
- return texture;
- }
+ return texture;
}
-
- return NULL;
}
/**
diff --git a/gdk/loaders/gdkjpegprivate.h b/gdk/loaders/gdkjpegprivate.h
index a8e6bd8a82..4dcccbaf8d 100644
--- a/gdk/loaders/gdkjpegprivate.h
+++ b/gdk/loaders/gdkjpegprivate.h
@@ -26,4 +26,16 @@
GdkTexture *gdk_load_jpeg (GBytes *bytes,
GError **error);
+static inline gboolean
+gdk_is_jpeg (GBytes *bytes)
+{
+ const char *data;
+ gsize size;
+
+ data = g_bytes_get_data (bytes, &size);
+
+ return size > strlen (JPEG_SIGNATURE) &&
+ memcmp (data, JPEG_SIGNATURE, strlen (JPEG_SIGNATURE)) == 0;
+}
+
#endif
diff --git a/gdk/loaders/gdkpngprivate.h b/gdk/loaders/gdkpngprivate.h
index ca824579a2..cbe073b315 100644
--- a/gdk/loaders/gdkpngprivate.h
+++ b/gdk/loaders/gdkpngprivate.h
@@ -28,4 +28,16 @@ GdkTexture *gdk_load_png (GBytes *bytes,
GBytes *gdk_save_png (GdkTexture *texture);
+static inline gboolean
+gdk_is_png (GBytes *bytes)
+{
+ const char *data;
+ gsize size;
+
+ data = g_bytes_get_data (bytes, &size);
+
+ return size > strlen (PNG_SIGNATURE) &&
+ memcmp (data, PNG_SIGNATURE, strlen (PNG_SIGNATURE)) == 0;
+}
+
#endif
diff --git a/gdk/loaders/gdktiffprivate.h b/gdk/loaders/gdktiffprivate.h
index ee97884e75..839e8855f0 100644
--- a/gdk/loaders/gdktiffprivate.h
+++ b/gdk/loaders/gdktiffprivate.h
@@ -29,4 +29,18 @@ GdkTexture *gdk_load_tiff (GBytes *bytes,
GBytes * gdk_save_tiff (GdkTexture *texture);
+static inline gboolean
+gdk_is_tiff (GBytes *bytes)
+{
+ const char *data;
+ gsize size;
+
+ data = g_bytes_get_data (bytes, &size);
+
+ return (size > strlen (TIFF_SIGNATURE1) &&
+ memcmp (data, TIFF_SIGNATURE1, strlen (TIFF_SIGNATURE1)) == 0) ||
+ (size > strlen (TIFF_SIGNATURE2) &&
+ memcmp (data, TIFF_SIGNATURE2, strlen (TIFF_SIGNATURE2)) == 0);
+}
+
#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]