[gtk/gamma-shenanigans: 2/3] Add gdk_texture_new_from_png
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/gamma-shenanigans: 2/3] Add gdk_texture_new_from_png
- Date: Mon, 6 Sep 2021 00:16:00 +0000 (UTC)
commit 5eb11870da803746a301b0942fca896174deb256
Author: Matthias Clasen <mclasen redhat com>
Date: Sun Sep 5 20:11:40 2021 -0400
Add gdk_texture_new_from_png
This is using libpng to load a png into a memory
texture, applying gamma from the file.
As a consequence, we are now linking against libpng.
gdk/gdktexture.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
gdk/gdktexture.h | 4 ++++
gdk/meson.build | 4 ++--
meson.build | 1 +
4 files changed, 78 insertions(+), 2 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 7ac4f1bb53..ece69ffe95 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -46,6 +46,7 @@
#include "gdksnapshot.h"
#include <graphene.h>
+#include <png.h>
/* HACK: So we don't need to include any (not-yet-created) GSK or GTK headers */
void
@@ -575,3 +576,73 @@ gdk_texture_save_to_png (GdkTexture *texture,
return result;
}
+
+/**
+ * gdk_texture_new_from_png:
+ * @file: `GFile` to load
+ * @error: Return location for an error
+ *
+ * Creates a new texture by loading an png image from a file.
+ *
+ * In contrast to [ctor@Gdk.Texture.new_from_file], this
+ * function tries to preserve all the data that the PNG
+ * format provides, and e.g. applies Gamma correction if
+ * that information is available in the file.
+ *
+ * If %NULL is returned, then @error will be set.
+ *
+ * Return value: A newly-created `GdkTexture`
+ *
+ * Since: 4.6
+ */
+GdkTexture *
+gdk_texture_new_from_png (GFile *file,
+ GError **error)
+{
+ png_image image = { NULL, PNG_IMAGE_VERSION, 0, };
+ GBytes *contents;
+ gconstpointer data;
+ gsize size;
+ gsize stride;
+ guint16 *buffer;
+ GBytes *bytes;
+ GdkTexture *texture;
+
+ contents = g_file_load_bytes (file, NULL, NULL, error);
+ if (!contents)
+ return NULL;
+
+ data = g_bytes_get_data (contents, &size);
+ png_image_begin_read_from_memory (&image, data, size);
+
+ image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
+
+ stride = PNG_IMAGE_ROW_STRIDE (image);
+ size = PNG_IMAGE_BUFFER_SIZE (image, stride);
+ buffer = malloc (size);
+
+ png_image_finish_read (&image, NULL, buffer, stride, NULL);
+
+ g_bytes_unref (contents);
+
+ if (PNG_IMAGE_FAILED (image))
+ {
+ g_free (buffer);
+ g_set_error (error,
+ G_IO_ERROR, G_IO_ERROR_FAILED,
+ "%s", image.message);
+ return NULL;
+ }
+
+ bytes = g_bytes_new_take (buffer, size);
+
+ texture = gdk_memory_texture_new (image.width, image.height,
+ GDK_MEMORY_R16G16B16A16_PREMULTIPLIED,
+ bytes, 2 * stride);
+
+ g_bytes_unref (bytes);
+
+ png_image_free (&image);
+
+ return texture;
+}
diff --git a/gdk/gdktexture.h b/gdk/gdktexture.h
index f3d0bc9765..2c822e1118 100644
--- a/gdk/gdktexture.h
+++ b/gdk/gdktexture.h
@@ -50,6 +50,10 @@ GDK_AVAILABLE_IN_ALL
GdkTexture * gdk_texture_new_from_file (GFile *file,
GError **error);
+GDK_AVAILABLE_IN_4_6
+GdkTexture * gdk_texture_new_from_png (GFile *file,
+ GError **error);
+
GDK_AVAILABLE_IN_ALL
int gdk_texture_get_width (GdkTexture *texture) G_GNUC_PURE;
GDK_AVAILABLE_IN_ALL
diff --git a/gdk/meson.build b/gdk/meson.build
index db64565c2c..6539c225f4 100644
--- a/gdk/meson.build
+++ b/gdk/meson.build
@@ -254,8 +254,8 @@ endif
libgdk = static_library('gdk',
sources: [gdk_sources, gdk_backends_gen_headers, gdkconfig],
- dependencies: gdk_deps + [libgtk_css_dep],
- link_with: [libgtk_css, ],
+ dependencies: gdk_deps + [libgtk_css_dep, png_dep],
+ link_with: [libgtk_css],
include_directories: [confinc, gdkx11_inc, wlinc],
c_args: libgdk_c_args + common_cflags,
link_whole: gdk_backends,
diff --git a/meson.build b/meson.build
index c0b5407a0d..fc50b6e3eb 100644
--- a/meson.build
+++ b/meson.build
@@ -394,6 +394,7 @@ pangocairo_dep = dependency('pangocairo', version: pango_req,
pixbuf_dep = dependency('gdk-pixbuf-2.0', version: gdk_pixbuf_req,
fallback : ['gdk-pixbuf', 'gdkpixbuf_dep'],
default_options: ['png=enabled', 'jpeg=enabled', 'builtin_loaders=png,jpeg',
'man=false'])
+png_dep = dependency('libpng16')
epoxy_dep = dependency('epoxy', version: epoxy_req,
fallback: ['libepoxy', 'libepoxy_dep'])
harfbuzz_dep = dependency('harfbuzz', version: '>= 2.1.0', required: false,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]