[gtk/image-loading: 5/18] Add code to load and save pngs
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/image-loading: 5/18] Add code to load and save pngs
- Date: Mon, 13 Sep 2021 02:29:45 +0000 (UTC)
commit 59e7dbef9552b986d4ebc03020ddc9434b135913
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/loaders/gdkpng.c | 138 ++++++++++++++++++++++++++++++++++++++++++++
gdk/loaders/gdkpngprivate.h | 31 ++++++++++
gdk/meson.build | 5 +-
meson.build | 5 ++
subprojects/libpng.wrap | 12 ++++
5 files changed, 189 insertions(+), 2 deletions(-)
---
diff --git a/gdk/loaders/gdkpng.c b/gdk/loaders/gdkpng.c
new file mode 100644
index 0000000000..5e51110395
--- /dev/null
+++ b/gdk/loaders/gdkpng.c
@@ -0,0 +1,138 @@
+/* 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>
+
+/* {{{ 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;
+ }
+}
+
+/* }}} */
+/* {{{ 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;
+ guchar *data = NULL;
+ png_alloc_size_t size;
+ gpointer buffer;
+ gboolean result;
+
+ image.width = gdk_texture_get_width (texture);
+ image.height = gdk_texture_get_height (texture);
+
+ stride = image.width * 4;
+ data = g_malloc (stride * image.height);
+ gdk_texture_download (texture, data, stride);
+
+#if G_BYTE_ORDER == G_LITTLE_ENDIAN
+ flip_02 (data, image.width, image.height, stride);
+#endif
+
+ image.format = PNG_FORMAT_RGBA;
+
+ 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);
+
+ png_image_free (&image);
+ g_free (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..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]