texture: Add gdk_texture_save_to_png()
authorBenjamin Otte <otte@redhat.com>
Thu, 12 Apr 2018 11:50:33 +0000 (13:50 +0200)
committerBenjamin Otte <otte@redhat.com>
Thu, 12 Apr 2018 12:02:59 +0000 (14:02 +0200)
It's needed for debugging Timm's code, so better have it here than
hidden in my random-patch vault.

docs/reference/gdk/gdk4-sections.txt
gdk/gdktexture.c
gdk/gdktexture.h

index c3df3779499228b0e94dd173d7450d128ef20e9c..2e12c4ed8132d081630ab65c091ad17d25e19c5a 100644 (file)
@@ -730,6 +730,7 @@ gdk_texture_new_from_file
 gdk_texture_get_width
 gdk_texture_get_height
 gdk_texture_download
+gdk_texture_save_to_png
 GdkMemoryFormat
 GDK_MEMORY_FORMAT_DEFAULT
 gdk_memory_texture_new
index c518bac8a4940424794e13fa347f963a4708324b..3e5f0a401a624ad750f4465b3b335417200b4051 100644 (file)
@@ -552,3 +552,50 @@ gdk_texture_get_render_data (GdkTexture  *self,
 
   return self->render_data;
 }
+
+/**
+ * gdk_texture_save_to_png:
+ * @texture: a #GdkTexture
+ * @filename: the filename to store to
+ *
+ * Store the given @texture to the @filename as a PNG file.
+ *
+ * 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.
+ **/
+gboolean
+gdk_texture_save_to_png (GdkTexture *texture,
+                         const char *filename)
+{
+  cairo_surface_t *surface;
+  cairo_status_t status;
+  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);
+
+  if (status != CAIRO_STATUS_SUCCESS ||
+      cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
+    result = FALSE;
+  else
+    result = TRUE;
+
+  cairo_surface_destroy (surface);
+
+  return result;
+}
+
index 67c14e15380f510fa83bbc878786caeb6b2b27f1..be43990a9fdd238c7fc7b2bd8e0ba51b4c2b049a 100644 (file)
@@ -59,6 +59,9 @@ GDK_AVAILABLE_IN_ALL
 void                    gdk_texture_download                   (GdkTexture      *texture,
                                                                 guchar          *data,
                                                                 gsize            stride);
+GDK_AVAILABLE_IN_ALL
+gboolean                gdk_texture_save_to_png                (GdkTexture      *texture,
+                                                                const char      *filename);
 
 G_END_DECLS