[gtk/gamma-shenanigans: 4/11] Load pngs using libpng




commit 08417954f1fbe5ac3712bc9dd1d4d4c807f59a6c
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/gdkglcontext.c | 24 +++++++++---------
 gdk/gdktexture.c   | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdk/meson.build    |  4 +--
 meson.build        |  1 +
 4 files changed, 88 insertions(+), 14 deletions(-)
---
diff --git a/gdk/gdkglcontext.c b/gdk/gdkglcontext.c
index fbb2385d19..464612e616 100644
--- a/gdk/gdkglcontext.c
+++ b/gdk/gdkglcontext.c
@@ -229,6 +229,7 @@ gdk_gl_context_upload_texture (GdkGLContext    *context,
 {
   GdkGLContextPrivate *priv = gdk_gl_context_get_instance_private (context);
   guchar *copy = NULL;
+  guint gl_internalformat;
   guint gl_format;
   guint gl_type;
   guint bpp;
@@ -250,6 +251,7 @@ gdk_gl_context_upload_texture (GdkGLContext    *context,
         }
 
       bpp = 4;
+      gl_internalformat = GL_RGBA8;
       gl_format = GL_RGBA;
       gl_type = GL_UNSIGNED_BYTE;
     }
@@ -257,25 +259,29 @@ gdk_gl_context_upload_texture (GdkGLContext    *context,
     {
       if (data_format == GDK_MEMORY_DEFAULT) /* Cairo surface format */
         {
+          gl_internalformat = GL_RGBA8;
           gl_format = GL_BGRA;
           gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
           bpp = 4;
         }
       else if (data_format == GDK_MEMORY_R8G8B8) /* Pixmap non-alpha data */
         {
+          gl_internalformat = GL_RGBA8;
           gl_format = GL_RGB;
           gl_type = GL_UNSIGNED_BYTE;
           bpp = 3;
         }
       else if (data_format == GDK_MEMORY_B8G8R8)
         {
+          gl_internalformat = GL_RGBA8;
           gl_format = GL_BGR;
           gl_type = GL_UNSIGNED_BYTE;
           bpp = 3;
         }
       else if (data_format == GDK_MEMORY_R16G16B16A16_PREMULTIPLIED)
         {
-          gl_format = GL_RGBA16;
+          gl_internalformat = GL_RGBA16;
+          gl_format = GL_RGBA;
           gl_type = GL_UNSIGNED_SHORT;
           bpp = 8;
         }
@@ -289,26 +295,20 @@ gdk_gl_context_upload_texture (GdkGLContext    *context,
           stride = width * 4;
           bpp = 4;
           data = copy;
+          gl_internalformat = GL_RGBA8;
           gl_format = GL_BGRA;
           gl_type = GL_UNSIGNED_INT_8_8_8_8_REV;
         }
     }
 
-  if (gl_format == GL_RGBA16)
-    {
-      glPixelStorei (GL_UNPACK_ALIGNMENT, 2);
-
-      glTexImage2D (texture_target, 0, GL_RGBA16, width, height, 0, GL_RGBA, GL_UNSIGNED_SHORT, data);
-      glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
-    }
   /* GL_UNPACK_ROW_LENGTH is available on desktop GL, OpenGL ES >= 3.0, or if
    * the GL_EXT_unpack_subimage extension for OpenGL ES 2.0 is available
    */
-  else if (stride == width * bpp)
+  if (stride == width * bpp)
     {
       glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
 
-      glTexImage2D (texture_target, 0, GL_RGBA, width, height, 0, gl_format, gl_type, data);
+      glTexImage2D (texture_target, 0, gl_internalformat, width, height, 0, gl_format, gl_type, data);
       glPixelStorei (GL_UNPACK_ALIGNMENT, 4);
     }
   else if ((!priv->use_es ||
@@ -316,14 +316,14 @@ gdk_gl_context_upload_texture (GdkGLContext    *context,
     {
       glPixelStorei (GL_UNPACK_ROW_LENGTH, stride / bpp);
 
-      glTexImage2D (texture_target, 0, GL_RGBA, width, height, 0, gl_format, gl_type, data);
+      glTexImage2D (texture_target, 0, gl_internalformat, width, height, 0, gl_format, gl_type, data);
 
       glPixelStorei (GL_UNPACK_ROW_LENGTH, 0);
     }
   else
     {
       int i;
-      glTexImage2D (texture_target, 0, GL_RGBA, width, height, 0, gl_format, gl_type, NULL);
+      glTexImage2D (texture_target, 0, gl_internalformat, width, height, 0, gl_format, gl_type, NULL);
       for (i = 0; i < height; i++)
         glTexSubImage2D (texture_target, 0, 0, i, width, 1, gl_format, gl_type, data + (i * stride));
     }
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 7ac4f1bb53..b33a93c38d 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 = g_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,19 @@ 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_check_sig (data, 8))
+    {
+      g_object_unref (stream);
+      return gdk_texture_new_from_png (file, error);
+    }
+
   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]