[gtk/image-loading: 1/9] Add code to load and save pngs




commit 122ef6dfd39ec10947c784cb461c8c5763cb84eb
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Sep 10 12:44:43 2021 -0400

    Add code to load and save pngs
    
    Using libpng instead of the lowest-common-denominator
    gdk-pixbuf loader allows us to load >8bit data, and
    apply gamma correction.
    
    As a consequence, we are now linking against libpng.

 gdk/gdkpng.c    | 381 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdk/gdkpng.h    |  56 +++++++++
 gdk/meson.build |   5 +-
 meson.build     |   2 +
 4 files changed, 442 insertions(+), 2 deletions(-)
---
diff --git a/gdk/gdkpng.c b/gdk/gdkpng.c
new file mode 100644
index 0000000000..740b2233a6
--- /dev/null
+++ b/gdk/gdkpng.c
@@ -0,0 +1,381 @@
+/* GDK - The GIMP Drawing Kit
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gdkpng.h"
+
+#include "gdktexture.h"
+#include "gdkmemorytextureprivate.h"
+#include "gsk/ngl/fp16private.h"
+#include <png.h>
+#include <stdio.h>
+
+/* {{{ IO handling */
+
+#ifdef HAVE_FOPENCOOKIE
+
+static ssize_t
+read_stream (void   *cookie,
+             char   *buf,
+             size_t  size)
+{
+  GInputStream *stream = cookie;
+
+  return g_input_stream_read (stream, buf, size, NULL, NULL);
+}
+
+static ssize_t
+write_stream (void       *cookie,
+              const char *buf,
+              size_t      size)
+{
+  GOutputStream *stream = cookie;
+
+  return g_output_stream_write (stream, buf, size, NULL, NULL);
+}
+
+static int
+seek_stream (void    *cookie,
+             off64_t *offset,
+             int      whence)
+{
+  GSeekable *seekable = cookie;
+  GSeekType seek_type;
+
+  if (whence == SEEK_SET)
+    seek_type = G_SEEK_SET;
+  else if (whence == SEEK_CUR)
+    seek_type = G_SEEK_CUR;
+  else if (whence == SEEK_END)
+    seek_type = G_SEEK_END;
+  else
+    g_assert_not_reached ();
+
+  if (g_seekable_seek (seekable, *offset, seek_type, NULL, NULL))
+    {
+      *offset = g_seekable_tell (seekable);
+      return 0;
+    }
+
+  return -1;
+}
+
+static int
+close_stream (void *cookie)
+{
+  GObject *stream = cookie;
+
+  g_object_unref (stream);
+
+  return 0;
+}
+
+static cookie_io_functions_t cookie_funcs = {
+  read_stream,
+  write_stream,
+  seek_stream,
+  close_stream
+};
+
+#else
+
+static GBytes *
+read_all_data (GInputStream  *source,
+               GError       **error)
+{
+  GOutputStream *output;
+  gssize size;
+  GBytes *bytes;
+
+  output = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
+  size = g_output_stream_splice (output, source, 0, NULL, error);
+  if (size == -1)
+    {
+      g_object_unref (output);
+      return NULL;
+    }
+
+  g_output_stream_close (output, NULL, NULL);
+  bytes = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (output));
+  g_object_unref (output);
+
+  return bytes;
+}
+
+#endif
+
+/* }}} */
+/* {{{ Format conversion */
+
+static void
+convert (guchar            *dest_data,
+         gsize              dest_stride,
+         GdkMemoryFormat    dest_format,
+         const guchar      *src_data,
+         gsize              src_stride,
+         GdkMemoryFormat    src_format,
+         gsize              width,
+         gsize              height)
+{
+  if (dest_format < 3)
+    gdk_memory_convert (dest_data, dest_stride, dest_format,
+                        src_data, src_stride, src_format,
+                        width, height);
+}
+
+/* }}} */
+/* {{{ Public API */
+
+GdkTexture *
+gdk_load_png (GInputStream  *stream,
+              GError       **error)
+{
+  png_image image = { NULL, PNG_IMAGE_VERSION, 0, };
+  gsize size;
+  gsize stride;
+  guchar *buffer;
+  GBytes *bytes;
+  GdkTexture *texture;
+
+#ifdef HAVE_FOPENCOOKIE
+  FILE *file = fopencookie (g_object_ref (stream), "r", cookie_funcs);
+
+  png_image_begin_read_from_stdio (&image, file);
+#else
+  GBytes *data;
+
+  data = read_all_data (stream, error);
+  if (!data)
+    return NULL;
+  png_image_begin_read_from_memory (&image,
+                                    g_bytes_get_data (data, NULL),
+                                    g_bytes_get_size (data));
+#endif
+
+  image.format = PNG_FORMAT_RGBA;
+
+  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);
+
+#ifdef HAVE_FOPENCOOKIE
+  fclose (file);
+#else
+  g_bytes_unref (data);
+#endif
+
+  if (image.format & PNG_FORMAT_FLAG_LINEAR)
+    stride *= 2;
+
+  bytes = g_bytes_new_take (buffer, size);
+
+  texture = gdk_memory_texture_new (image.width, image.height,
+                                    GDK_MEMORY_R8G8B8A8_PREMULTIPLIED,
+                                    bytes, stride);
+
+  g_bytes_unref (bytes);
+
+  png_image_free (&image);
+
+  return texture;
+}
+
+gboolean
+gdk_save_png (GOutputStream    *stream,
+              const guchar     *data,
+              int               width,
+              int               height,
+              int               stride,
+              GdkMemoryFormat   format,
+              GError          **error)
+{
+  png_image image = { NULL, PNG_IMAGE_VERSION, 0, };
+  gboolean result;
+  guchar *new_data = NULL;
+
+  switch ((int)format)
+    {
+    case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
+      image.format = PNG_FORMAT_RGBA;
+      break;
+    case GDK_MEMORY_B8G8R8A8_PREMULTIPLIED:
+    case GDK_MEMORY_A8R8G8B8_PREMULTIPLIED:
+    case GDK_MEMORY_B8G8R8A8:
+    case GDK_MEMORY_A8R8G8B8:
+    case GDK_MEMORY_R8G8B8A8:
+    case GDK_MEMORY_A8B8G8R8:
+    case GDK_MEMORY_R8G8B8:
+    case GDK_MEMORY_B8G8R8:
+      stride = width * 4;
+      new_data = g_malloc (stride * height);
+      convert (new_data, stride, GDK_MEMORY_R8G8B8A8_PREMULTIPLIED,
+               data, width * gdk_memory_format_bytes_per_pixel (format), format,
+               width, height);
+      data = new_data;
+      image.format = PNG_FORMAT_RGBA;
+      break;
+    default:
+      g_assert_not_reached ();
+    }
+
+  if (image.format & PNG_FORMAT_FLAG_LINEAR)
+    stride /= 2;
+
+#ifdef HAVE_FOPENCOOKIE
+  FILE *file = fopencookie (g_object_ref (stream), "w", cookie_funcs);
+  result = png_image_write_to_stdio (&image, file, FALSE, data, stride, NULL);
+  fclose (file);
+#else
+  gsize written;
+  png_alloc_size_t size;
+  gpointer buffer;
+  png_image_write_get_memory_size (image, size, FALSE, data, stride, NULL);
+  buffer = g_malloc (size);
+  result = png_image_write_to_memory (&image, buffer, &size, FALSE, data, stride, NULL);
+  if (result)
+    result = g_output_stream_write_all (stream, buffer, (gsize)size, &written, NULL, NULL);
+  g_free (buffer);
+#endif
+
+  if (!result)
+    {
+      g_set_error_literal (error,
+                           G_IO_ERROR, G_IO_ERROR_FAILED,
+                           "Saving png failed");
+    }
+
+  png_image_free (&image);
+
+  g_free (new_data);
+
+  return result;
+}
+
+/* }}} */
+/* {{{ Async code */
+
+static void
+load_png_in_thread (GTask        *task,
+                    gpointer      source_object,
+                    gpointer      task_data,
+                    GCancellable *cancellable)
+{
+  GInputStream *stream = source_object;
+  GdkTexture *texture;
+  GError *error = NULL;
+
+  texture = gdk_load_png (stream, &error);
+
+  if (texture)
+    g_task_return_pointer (task, texture, g_object_unref);
+  else
+    g_task_return_error (task, error);
+}
+
+void
+gdk_load_png_async (GInputStream         *stream,
+                    GCancellable         *cancellable,
+                    GAsyncReadyCallback   callback,
+                    gpointer              user_data)
+{
+  GTask *task;
+
+  task = g_task_new (stream, cancellable, callback, user_data);
+  g_task_run_in_thread (task, load_png_in_thread);
+  g_object_unref (task);
+}
+
+GdkTexture *
+gdk_load_png_finish (GAsyncResult  *result,
+                     GError       **error)
+{
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+typedef struct {
+  const guchar *data;
+  int width;
+  int height;
+  int stride;
+  GdkMemoryFormat format;
+} SavePngData;
+
+static void
+save_png_in_thread (GTask        *task,
+                    gpointer      source_object,
+                    gpointer      task_data,
+                    GCancellable *cancellable)
+{
+  GOutputStream *stream = source_object;
+  SavePngData *data = task_data;
+  GError *error = NULL;
+  gboolean result;
+
+  result = gdk_save_png (stream,
+                         data->data,
+                         data->width,
+                         data->height,
+                         data->stride,
+                         data->format,
+                         &error);
+
+  if (result)
+    g_task_return_boolean (task, result);
+  else
+    g_task_return_error (task, error);
+}
+
+void
+gdk_save_png_async (GOutputStream          *stream,
+                    const guchar           *data,
+                    int                     width,
+                    int                     height,
+                    int                     stride,
+                    GdkMemoryFormat         format,
+                    GCancellable           *cancellable,
+                    GAsyncReadyCallback     callback,
+                    gpointer                user_data)
+{
+  GTask *task;
+  SavePngData *save_data;
+
+  save_data = g_new0 (SavePngData, 1);
+  save_data->data = data;
+  save_data->width = width;
+  save_data->height = height;
+  save_data->stride = stride;
+  save_data->format = format;
+
+  task = g_task_new (stream, cancellable, callback, user_data);
+  g_task_set_task_data (task, save_data, g_free);
+  g_task_run_in_thread (task, save_png_in_thread);
+  g_object_unref (task);
+}
+
+gboolean
+gdk_save_png_finish (GAsyncResult  *result,
+                     GError       **error)
+{
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
+
+/* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/gdk/gdkpng.h b/gdk/gdkpng.h
new file mode 100644
index 0000000000..fcc444a5b1
--- /dev/null
+++ b/gdk/gdkpng.h
@@ -0,0 +1,56 @@
+/* GDK - The GIMP Drawing Kit
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GDK_PNG_H__
+#define __GDK_PNG_H__
+
+#include "gdkmemorytexture.h"
+#include <gio/gio.h>
+
+GdkTexture *gdk_load_png        (GInputStream           *stream,
+                                 GError                **error);
+
+void        gdk_load_png_async  (GInputStream           *stream,
+                                 GCancellable           *cancellable,
+                                 GAsyncReadyCallback     callback,
+                                 gpointer                user_data);
+
+GdkTexture *gdk_load_png_finish (GAsyncResult           *result,
+                                 GError                **error);
+
+gboolean    gdk_save_png        (GOutputStream          *stream,
+                                 const guchar           *data,
+                                 int                     width,
+                                 int                     height,
+                                 int                     stride,
+                                 GdkMemoryFormat         format,
+                                 GError                **error);
+
+void        gdk_save_png_async  (GOutputStream          *stream,
+                                 const guchar           *data,
+                                 int                     width,
+                                 int                     height,
+                                 int                     stride,
+                                 GdkMemoryFormat         format,
+                                 GCancellable           *cancellable,
+                                 GAsyncReadyCallback     callback,
+                                 gpointer                user_data);
+
+gboolean    gdk_save_png_finish (GAsyncResult           *result,
+                                 GError                **error);
+
+#endif
diff --git a/gdk/meson.build b/gdk/meson.build
index db64565c2c..47729fb1dc 100644
--- a/gdk/meson.build
+++ b/gdk/meson.build
@@ -50,6 +50,7 @@ gdk_public_sources = files([
   'gdktoplevelsize.c',
   'gdktoplevel.c',
   'gdkdragsurface.c',
+  'gdkpng.c',
 ])
 
 gdk_public_headers = files([
@@ -254,8 +255,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 070aa6a80d..5d2b32502f 100644
--- a/meson.build
+++ b/meson.build
@@ -199,6 +199,7 @@ check_functions = [
   'mallinfo2',
   'sincos',
   'sincosf',
+  'fopencookie',
 ]
 
 foreach func : check_functions
@@ -389,6 +390,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]