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




commit c4728446c00d8db00478da94d9c684c3d8f41561
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. This will allow us to load >8bit data,
    and apply gamma and color correction in the future.
    For now, this still just provides RGBA8 data.
    
    As a consequence, we are now linking against libpng.

 gdk/loaders/gdkpng.c        | 220 ++++++++++++++++++++++++++++++++++++++++++++
 gdk/loaders/gdkpngprivate.h |  31 +++++++
 gdk/meson.build             |   5 +-
 meson.build                 |   4 +
 subprojects/libpng.wrap     |  12 +++
 5 files changed, 270 insertions(+), 2 deletions(-)
---
diff --git a/gdk/loaders/gdkpng.c b/gdk/loaders/gdkpng.c
new file mode 100644
index 0000000000..b4aac4e481
--- /dev/null
+++ b/gdk/loaders/gdkpng.c
@@ -0,0 +1,220 @@
+/* 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 "gdkpngprivate.h"
+
+#include "gdktexture.h"
+#include "gdktextureprivate.h"
+#include "gdkmemorytextureprivate.h"
+#include "gsk/ngl/fp16private.h"
+#include <png.h>
+#include <stdio.h>
+
+/* The main difference between the png load/save code here and
+ * gdk-pixbuf is that we can support loading 16-bit data in the
+ * future, and we can extract gamma and colorspace information
+ * to produce linear, color-corrected data.
+ */
+
+/* {{{ Format conversion */
+
+static void
+flip_02 (guchar *data,
+         int     width,
+         int     height,
+         int     stride)
+{
+  gsize x, y;
+
+  for (y = 0; y < height; y++)
+    {
+      for (x = 0; x < width; x++)
+        {
+          guchar tmp;
+          tmp = data[x * 4];
+          data[x * 4] = data[x * 4 + 2];
+          data[x * 4 + 2] = tmp;
+        }
+      data += stride;
+    }
+}
+
+static void
+convert_float_to_16bit_inplace (float   *src,
+                                int      width,
+                                int      height)
+{
+  gsize x, y;
+  guint16 *dest = (guint16 *)src;
+
+  for (y = 0; y < height; y++)
+    {
+      for (x = 0; x < width; x++)
+        {
+          dest[4 * x    ] = (guint16)(65535 * src[x * 4    ]);
+          dest[4 * x + 1] = (guint16)(65535 * src[x * 4 + 1]);
+          dest[4 * x + 2] = (guint16)(65535 * src[x * 4 + 2]);
+          dest[4 * x + 3] = (guint16)(65535 * src[x * 4 + 3]);
+        }
+      dest += width * 4;
+      src += width * 4;
+    }
+}
+
+/* }}} */
+/* {{{ Public API */
+
+GdkTexture *
+gdk_load_png (GBytes  *bytes,
+              GError **error)
+{
+  png_image image = { NULL, PNG_IMAGE_VERSION, 0, };
+  gsize size;
+  gsize stride;
+  guchar *buffer;
+  GdkTexture *texture;
+  GBytes *out_bytes;
+
+  png_image_begin_read_from_memory (&image,
+                                    g_bytes_get_data (bytes, NULL),
+                                    g_bytes_get_size (bytes));
+
+  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);
+
+  if (image.format & PNG_FORMAT_FLAG_LINEAR)
+    stride *= 2;
+
+  out_bytes = g_bytes_new_take (buffer, size);
+
+  texture = gdk_memory_texture_new (image.width, image.height,
+                                    GDK_MEMORY_R8G8B8A8_PREMULTIPLIED,
+                                    out_bytes, stride);
+
+  g_bytes_unref (out_bytes);
+
+  png_image_free (&image);
+
+  return texture;
+}
+
+GBytes *
+gdk_save_png (GdkTexture *texture)
+{
+  png_image image = { NULL, PNG_IMAGE_VERSION, 0, };
+  int stride;
+  const guchar *data;
+  guchar *new_data = NULL;
+  png_alloc_size_t size;
+  gpointer buffer;
+  gboolean result;
+  GdkTexture *memory_texture;
+  GdkMemoryFormat format;
+
+  image.width = gdk_texture_get_width (texture);
+  image.height = gdk_texture_get_height (texture);
+
+  memory_texture = gdk_texture_download_texture (texture);
+  format = gdk_memory_texture_get_format (GDK_MEMORY_TEXTURE (memory_texture));
+
+  switch (format)
+    {
+    case GDK_MEMORY_R8G8B8A8_PREMULTIPLIED:
+      data = gdk_memory_texture_get_data (GDK_MEMORY_TEXTURE (memory_texture));
+      stride = gdk_memory_texture_get_stride (GDK_MEMORY_TEXTURE (memory_texture));
+      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 = image.width * 4;
+      new_data = g_malloc (stride * image.height);
+      gdk_texture_download (memory_texture, new_data, stride);
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+      flip_02 (new_data, image.width, image.height, stride);
+#endif
+      data = new_data;
+      image.format = PNG_FORMAT_RGBA;
+      break;
+
+    case GDK_MEMORY_R16G16B16:
+      data = gdk_memory_texture_get_data (GDK_MEMORY_TEXTURE (memory_texture));
+      stride = gdk_memory_texture_get_stride (GDK_MEMORY_TEXTURE (memory_texture));
+      image.format = PNG_FORMAT_LINEAR_RGB;
+      break;
+
+    case GDK_MEMORY_R16G16B16A16_PREMULTIPLIED:
+      data = gdk_memory_texture_get_data (GDK_MEMORY_TEXTURE (memory_texture));
+      stride = gdk_memory_texture_get_stride (GDK_MEMORY_TEXTURE (memory_texture));
+      image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
+      break;
+
+    case GDK_MEMORY_R16G16B16_FLOAT:
+    case GDK_MEMORY_R32G32B32_FLOAT:
+    case GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED:
+    case GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED:
+      /* This isn't very efficient */
+      new_data = g_malloc (image.width * image.height * 16);
+      gdk_texture_download_float (memory_texture, (float *)new_data, image.width * 16);
+      convert_float_to_16bit_inplace ((float *)new_data, image.width, image.height);
+      data = new_data;
+      stride = image.width * 8;
+      image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
+      break;
+
+    case GDK_MEMORY_N_FORMATS:
+    default:
+      g_assert_not_reached ();
+    }
+
+  if (image.format & PNG_FORMAT_FLAG_LINEAR)
+    stride /= 2;
+
+  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);
+
+  g_object_unref (memory_texture);
+  png_image_free (&image);
+  g_free (new_data);
+
+  if (!result)
+    {
+      g_free (buffer);
+      return NULL;
+    }
+
+  return g_bytes_new_take (buffer, size);
+}
+
+ /* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/gdk/loaders/gdkpngprivate.h b/gdk/loaders/gdkpngprivate.h
new file mode 100644
index 0000000000..ca824579a2
--- /dev/null
+++ b/gdk/loaders/gdkpngprivate.h
@@ -0,0 +1,31 @@
+/* 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_PRIVATE_H__
+#define __GDK_PNG_PRIVATE_H__
+
+#include "gdktexture.h"
+#include <gio/gio.h>
+
+#define PNG_SIGNATURE "\x89PNG"
+
+GdkTexture *gdk_load_png        (GBytes         *bytes,
+                                 GError        **error);
+
+GBytes     *gdk_save_png        (GdkTexture     *texture);
+
+#endif
diff --git a/gdk/meson.build b/gdk/meson.build
index ccc1738eab..cdd3bc1b31 100644
--- a/gdk/meson.build
+++ b/gdk/meson.build
@@ -51,6 +51,7 @@ gdk_public_sources = files([
   'gdktoplevelsize.c',
   'gdktoplevel.c',
   'gdkdragsurface.c',
+  'loaders/gdkpng.c',
 ])
 
 gdk_public_headers = files([
@@ -256,8 +257,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..dfa44c093b 100644
--- a/meson.build
+++ b/meson.build
@@ -389,6 +389,10 @@ 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('libpng',
+                            fallback: ['libpng', 'libpng_dep'],
+                            required: true)
+
 epoxy_dep      = dependency('epoxy', version: epoxy_req,
                             fallback: ['libepoxy', 'libepoxy_dep'])
 harfbuzz_dep   = dependency('harfbuzz', version: '>= 2.1.0', required: false,
diff --git a/subprojects/libpng.wrap b/subprojects/libpng.wrap
new file mode 100644
index 0000000000..9d6c6b3078
--- /dev/null
+++ b/subprojects/libpng.wrap
@@ -0,0 +1,12 @@
+[wrap-file]
+directory = libpng-1.6.37
+source_url = https://github.com/glennrp/libpng/archive/v1.6.37.tar.gz
+source_filename = libpng-1.6.37.tar.gz
+source_hash = ca74a0dace179a8422187671aee97dd3892b53e168627145271cad5b5ac81307
+patch_url = https://wrapdb.mesonbuild.com/v2/libpng_1.6.37-3/get_patch
+patch_filename = libpng-1.6.37-3-wrap.zip
+patch_hash = 6c9f32fd9150b3a96ab89be52af664e32207e10aa9f5fb9aa015989ee2dd7100
+
+[provide]
+libpng = libpng_dep
+


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