[gtk/gamma-shenanigans] Load pngs using libpng




commit 4378c59846d7165d87a32303b8d1d09a91de5c7f
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Sep 5 20:11:40 2021 -0400

    Load pngs using libpng
    
    Using libpng instead of the lowest-common-denominator
    gdk-pixbuf loader allows us to load >8bit data, and
    apply gamma correction.
    
    For other file formats, we still fall back to using
    gdk-pixbuf.
    
    As a consequence, we are now linking against libpng.

 gdk/gdktexture.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdk/meson.build  |  4 +--
 meson.build      |  1 +
 3 files changed, 79 insertions(+), 2 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 7ac4f1bb53..59059e22a1 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
@@ -341,6 +342,58 @@ gdk_texture_new_from_resource (const char *resource_path)
   return texture;
 }
 
+static 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;
+}
+
 /**
  * gdk_texture_new_from_file:
  * @file: `GFile` to load
@@ -351,6 +404,10 @@ gdk_texture_new_from_resource (const char *resource_path)
  * The file format is detected automatically. The supported formats
  * are PNG and JPEG, though more formats might be available.
  *
+ * For PNG files, this function supports conversion from SRGB to
+ * linear data (including gamma correction) and can load 16bit
+ * data.
+ *
  * If %NULL is returned, then @error will be set.
  *
  * Return value: A newly-created `GdkTexture`
@@ -362,6 +419,9 @@ gdk_texture_new_from_file (GFile   *file,
   GdkTexture *texture;
   GdkPixbuf *pixbuf;
   GInputStream *stream;
+  GInputStream *buffered;
+  const void *data;
+  gsize size;
 
   g_return_val_if_fail (G_IS_FILE (file), NULL);
   g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -370,6 +430,22 @@ gdk_texture_new_from_file (GFile   *file,
   if (stream == NULL)
     return NULL;
 
+  buffered = g_buffered_input_stream_new (stream);
+  g_object_unref (stream);
+  stream = buffered;
+
+  g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream), 8, NULL, NULL);
+  data = g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (stream), &size);
+
+  if (png_sig_check (data, 8))
+    {
+      g_object_unref (stream);
+      g_print ("loading png with libpng\n");
+      return gdk_texture_new_from_png (file, error);
+    }
+
+  g_print ("loading non-png with gdk-pixbuf\n");
+
   pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
   g_object_unref (stream);
   if (pixbuf == NULL)
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]