[gtk/image-loading: 1/14] Add code to load and save pngs
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/image-loading: 1/14] Add code to load and save pngs
- Date: Sun, 12 Sep 2021 23:36:41 +0000 (UTC)
commit 6664c946e339887b2d9acdadc85b2a9a1e3e2297
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.
.gitlab-ci/fedora.Dockerfile | 1 +
gdk/loaders/gdkpng.c | 259 +++++++++++++++++++++++++++++++++++++++++++
gdk/loaders/gdkpngprivate.h | 37 +++++++
gdk/meson.build | 5 +-
meson.build | 5 +
subprojects/libpng.wrap | 12 ++
6 files changed, 317 insertions(+), 2 deletions(-)
---
diff --git a/.gitlab-ci/fedora.Dockerfile b/.gitlab-ci/fedora.Dockerfile
index ef0bb1aeb2..ef29a10a77 100644
--- a/.gitlab-ci/fedora.Dockerfile
+++ b/.gitlab-ci/fedora.Dockerfile
@@ -53,6 +53,7 @@ RUN dnf -y install \
libpng-devel \
librsvg2 \
libselinux-devel \
+ libtiff-devel \
libubsan \
libXcomposite-devel \
libXcursor-devel \
diff --git a/gdk/loaders/gdkpng.c b/gdk/loaders/gdkpng.c
new file mode 100644
index 0000000000..47d16e20a2
--- /dev/null
+++ b/gdk/loaders/gdkpng.c
@@ -0,0 +1,259 @@
+/* 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 "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 (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;
+}
+
+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;
+
+ image.width = width;
+ image.height = height;
+
+ 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;
+}
+
+ /* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/gdk/loaders/gdkpngprivate.h b/gdk/loaders/gdkpngprivate.h
new file mode 100644
index 0000000000..b3f3994709
--- /dev/null
+++ b/gdk/loaders/gdkpngprivate.h
@@ -0,0 +1,37 @@
+/* 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 "gdkmemorytexture.h"
+#include <gio/gio.h>
+
+#define PNG_SIGNATURE "\x89PNG"
+
+GdkTexture *gdk_load_png (GBytes *bytes,
+ GError **error);
+
+gboolean gdk_save_png (GOutputStream *stream,
+ const guchar *data,
+ int width,
+ int height,
+ int stride,
+ GdkMemoryFormat format,
+ GError **error);
+
+#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..eaf2da6e0a 100644
--- a/meson.build
+++ b/meson.build
@@ -199,6 +199,7 @@ check_functions = [
'mallinfo2',
'sincos',
'sincosf',
+ 'fopencookie',
]
foreach func : check_functions
@@ -389,6 +390,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]