[gtk/image-loading: 8/14] Add gdk_texture_save_to_tiff




commit 76839d00f8120c99df354ff3ac2af985ff2dca59
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Sep 7 09:20:05 2021 -0400

    Add gdk_texture_save_to_tiff
    
    This is a companion to gdk_texture_save_to_png, using
    the tiff format, which will let us avoid lossy conversion
    of HDR data, since we can store floating point data.

 gdk/gdktexture.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdk/gdktexture.h |  3 +++
 2 files changed, 62 insertions(+)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index e370fb0760..7cc5783ba8 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -670,3 +670,62 @@ gdk_texture_save_to_png (GdkTexture *texture,
 
    return result;
 }
+
+/**
+ * gdk_texture_save_to_tiff:
+ * @texture: a `GdkTexture`
+ * @filename: (type filename): the filename to store to
+ *
+ * Store the given @texture to the @filename as a TIFF file.
+ *
+ * GTK will attempt to store data without loss.
+ *
+ * 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
+ * look into using the gdk-pixbuf library.
+ *
+ * Returns: %TRUE if saving succeeded, %FALSE on failure.
+ *
+ * Since: 4.6
+ */
+gboolean
+gdk_texture_save_to_tiff (GdkTexture  *texture,
+                          const char  *filename)
+{
+  int width, height, stride;
+  guchar *data;
+  GdkMemoryFormat format;
+  GFile *file;
+  GOutputStream *stream;
+  gboolean result;
+
+  width = gdk_texture_get_width (texture);
+  height = gdk_texture_get_height (texture);
+
+  stride = width * 4;
+  data = g_malloc (stride * height);
+
+  gdk_texture_download (texture, data, stride);
+
+  format = GDK_MEMORY_DEFAULT;
+
+  file = g_file_new_for_path (filename);
+  stream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE,
+                                            G_FILE_CREATE_NONE,
+                                            NULL, NULL));
+  g_object_unref (file);
+
+  result = gdk_save_tiff (stream,
+                          data,
+                          width, height, stride,
+                          format,
+                          NULL);
+
+  g_output_stream_close (stream, NULL, NULL);
+  g_object_unref (stream);
+
+  g_free (data);
+
+   return result;
+}
diff --git a/gdk/gdktexture.h b/gdk/gdktexture.h
index 9e3b7a93fa..dd7480d13b 100644
--- a/gdk/gdktexture.h
+++ b/gdk/gdktexture.h
@@ -66,6 +66,9 @@ void                    gdk_texture_download_float             (GdkTexture
 GDK_AVAILABLE_IN_ALL
 gboolean                gdk_texture_save_to_png                (GdkTexture      *texture,
                                                                 const char      *filename);
+GDK_AVAILABLE_IN_4_6
+gboolean                gdk_texture_save_to_tiff               (GdkTexture      *texture,
+                                                                const char      *filename);
 
 G_END_DECLS
 


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