[gtk/gamma-shenanigans: 2/6] Support loading and saving 16bit pngs
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/gamma-shenanigans: 2/6] Support loading and saving 16bit pngs
- Date: Wed, 8 Sep 2021 05:00:40 +0000 (UTC)
commit 6c3661d2409f7b251633ef63338626752c92e9f0
Author: Matthias Clasen <mclasen redhat com>
Date: Tue Sep 7 09:27:01 2021 -0400
Support loading and saving 16bit pngs
Using libpng instead of the lowest-common-denominator
gdk-pixbuf loader allows us to load >8bit data, and
apply gamma correction.
For other file formats, we still fall back to using
gdk-pixbuf.
As a consequence, we are now linking against libpng.
gdk/gdktexture.c | 119 +++++++++++++++++++++++++++++++++++++++++++++++--------
gdk/meson.build | 4 +-
meson.build | 1 +
3 files changed, 106 insertions(+), 18 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index 3e289ddf4b..d12c5af02b 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -46,6 +46,7 @@
#include "gdksnapshot.h"
#include <graphene.h>
+#include <png.h>
/* HACK: So we don't need to include any (not-yet-created) GSK or GTK headers */
void
@@ -349,6 +350,58 @@ gdk_texture_new_from_resource (const char *resource_path)
return texture;
}
+static GdkTexture *
+gdk_texture_new_from_png (GFile *file,
+ GError **error)
+{
+ png_image image = { NULL, PNG_IMAGE_VERSION, 0, };
+ GBytes *contents;
+ gconstpointer data;
+ gsize size;
+ gsize stride;
+ guint16 *buffer;
+ GBytes *bytes;
+ GdkTexture *texture;
+
+ contents = g_file_load_bytes (file, NULL, NULL, error);
+ if (!contents)
+ return NULL;
+
+ data = g_bytes_get_data (contents, &size);
+ png_image_begin_read_from_memory (&image, data, size);
+
+ image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
+
+ 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);
+
+ g_bytes_unref (contents);
+
+ 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;
+}
+
/**
* gdk_texture_new_from_file:
* @file: `GFile` to load
@@ -359,6 +412,10 @@ gdk_texture_new_from_resource (const char *resource_path)
* The file format is detected automatically. The supported formats
* are PNG and JPEG, though more formats might be available.
*
+ * For PNG files, this function supports conversion from SRGB to
+ * linear data (including gamma correction) and can load 16bit
+ * data.
+ *
* If %NULL is returned, then @error will be set.
*
* Return value: A newly-created `GdkTexture`
@@ -370,6 +427,9 @@ gdk_texture_new_from_file (GFile *file,
GdkTexture *texture;
GdkPixbuf *pixbuf;
GInputStream *stream;
+ GInputStream *buffered;
+ const void *data;
+ gsize size;
g_return_val_if_fail (G_IS_FILE (file), NULL);
g_return_val_if_fail (error == NULL || *error == NULL, NULL);
@@ -378,6 +438,19 @@ gdk_texture_new_from_file (GFile *file,
if (stream == NULL)
return NULL;
+ buffered = g_buffered_input_stream_new (stream);
+ g_object_unref (stream);
+ stream = buffered;
+
+ g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (stream), 8, NULL, NULL);
+ data = g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (stream), &size);
+
+ if (png_check_sig (data, 8))
+ {
+ g_object_unref (stream);
+ return gdk_texture_new_from_png (file, error);
+ }
+
pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
g_object_unref (stream);
if (pixbuf == NULL)
@@ -552,6 +625,9 @@ gdk_texture_get_render_data (GdkTexture *self,
*
* Store the given @texture to the @filename as a PNG file.
*
+ * If the texture contains 16bit data, the generated PNG file
+ * will have linear 16bit data, otherwise it will contain SRGB.
+ *
* This is a utility function intended for debugging and testing.
* If you want more control over formats, proper error handling or
* want to store to a `GFile` or other location, you might want to
@@ -563,30 +639,41 @@ gboolean
gdk_texture_save_to_png (GdkTexture *texture,
const char *filename)
{
- cairo_surface_t *surface;
- cairo_status_t status;
+ png_image image = { NULL, PNG_IMAGE_VERSION, 0, };
+ GBytes *bytes;
gboolean result;
g_return_val_if_fail (GDK_IS_TEXTURE (texture), FALSE);
g_return_val_if_fail (filename != NULL, FALSE);
- surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
- gdk_texture_get_width (texture),
- gdk_texture_get_height (texture));
- gdk_texture_download (texture,
- cairo_image_surface_get_data (surface),
- cairo_image_surface_get_stride (surface));
- cairo_surface_mark_dirty (surface);
-
- status = cairo_surface_write_to_png (surface, filename);
+ image.width = gdk_texture_get_width (texture);
+ image.height = gdk_texture_get_height (texture);
- if (status != CAIRO_STATUS_SUCCESS ||
- cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
- result = FALSE;
+ bytes = gdk_texture_download_format (texture, GDK_MEMORY_R16G16B16A16_PREMULTIPLIED);
+ if (bytes)
+ {
+ image.format = PNG_FORMAT_LINEAR_RGB_ALPHA;
+ }
else
- result = TRUE;
+ {
+ int stride = image.width * 4;
+ gpointer data;
+
+ data = g_malloc (image.height * stride);
+
+ gdk_texture_download (texture, data, stride);
+
+ bytes = g_bytes_new_take (data, image.height * stride);
+
+ image.format = PNG_FORMAT_RGBA;
+ }
+
+ result = png_image_write_to_file (&image, filename, FALSE,
+ g_bytes_get_data (bytes, NULL),
+ 0, NULL);
+ g_bytes_unref (bytes);
- cairo_surface_destroy (surface);
+ png_image_free (&image);
return result;
}
diff --git a/gdk/meson.build b/gdk/meson.build
index db64565c2c..6539c225f4 100644
--- a/gdk/meson.build
+++ b/gdk/meson.build
@@ -254,8 +254,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 c0b5407a0d..fc50b6e3eb 100644
--- a/meson.build
+++ b/meson.build
@@ -394,6 +394,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]