[gtk/gamma-shenanigans: 2/2] wip: Try loading pngs with gamma




commit 15ad488d16ff8b0289a53cb03404ac54646686fc
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Sep 5 17:40:02 2021 -0400

    wip: Try loading pngs with gamma
    
    This is some test code to demonstrate
    loading pngs while applying gamma.

 tests/meson.build   |  4 +++-
 tests/testtexture.c | 54 ++++++++++++++++++++++++++++++++++++++++++++---------
 2 files changed, 48 insertions(+), 10 deletions(-)
---
diff --git a/tests/meson.build b/tests/meson.build
index 9acf884231..82102ebb46 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -126,6 +126,8 @@ if x11_enabled
   gtk_tests += [['testerrors']]
 endif
 
+libpng = dependency('libpng16')
+
 # Pass the source dir here so programs can change into the source directory
 # and find .ui files and .png files and such that they load at runtime
 test_args = ['-DGTK_SRCDIR="@0@"'.format(meson.current_source_dir())]
@@ -137,7 +139,7 @@ foreach t: gtk_tests
     sources: test_srcs,
     include_directories: [confinc, gdkinc],
     c_args: test_args + common_cflags,
-    dependencies: [libgtk_dep, libm],
+    dependencies: [libgtk_dep, libm, libpng],
   )
 endforeach
 
diff --git a/tests/testtexture.c b/tests/testtexture.c
index 78c5464949..82e38c83f5 100644
--- a/tests/testtexture.c
+++ b/tests/testtexture.c
@@ -1,6 +1,5 @@
 #include <gtk/gtk.h>
-
-
+#include <png.h>
 
 typedef struct _GtkTextureView      GtkTextureView;
 typedef struct _GtkTextureViewClass GtkTextureViewClass;
@@ -111,13 +110,53 @@ quit_cb (GtkWidget *widget,
   g_main_context_wakeup (NULL);
 }
 
+static GdkTexture *
+load_png (const char  *file_name,
+          GError     **error)
+{
+  png_image image = { NULL, PNG_IMAGE_VERSION, 0, };
+  gsize stride;
+  gsize size;
+  guint16 *buffer;
+  GBytes *bytes;
+  GdkTexture *texture;
+
+  png_image_begin_read_from_file (&image, file_name);
+
+  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);
+
+  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;
+}
+
 int
 main (int argc, char **argv)
 {
   GtkWidget *window;
   GtkWidget *view;
   GdkTexture *texture;
-  GFile *file;
   GError *error = NULL;
   gboolean done = FALSE;
 
@@ -126,16 +165,15 @@ main (int argc, char **argv)
   if (argc != 2)
     {
       g_error ("No texture file path given.");
-      return -1;
+      return 1;
     }
 
-  file = g_file_new_for_path (argv[1]);
-  texture = gdk_texture_new_from_file (file, &error);
+  texture = load_png (argv[1], &error);
 
   if (error != NULL)
     {
       g_error ("Error: %s", error->message);
-      return -1;
+      return 1;
     }
 
   window = gtk_window_new ();
@@ -150,7 +188,5 @@ main (int argc, char **argv)
   while (!done)
     g_main_context_iteration (NULL, TRUE);
 
-  g_object_unref (file);
-
   return 0;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]