[gtk/image-loading: 3/14] Save pngs without gdk-pixbuf




commit 4d0468115f8edc0fc02301aba63667946cde509a
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Sep 12 09:24:59 2021 -0400

    Save pngs without gdk-pixbuf
    
    Use our own loader for pngs, which will allow
    us to save e.g. 16-bit data in the future.

 gdk/gdktexture.c | 47 ++++++++++++++++++++++++++++-------------------
 1 file changed, 28 insertions(+), 19 deletions(-)
---
diff --git a/gdk/gdktexture.c b/gdk/gdktexture.c
index a0b59cb073..10a08c9a09 100644
--- a/gdk/gdktexture.c
+++ b/gdk/gdktexture.c
@@ -627,30 +627,39 @@ gboolean
 gdk_texture_save_to_png (GdkTexture *texture,
                          const char *filename)
 {
-  cairo_surface_t *surface;
-  cairo_status_t status;
+  int width, height, stride;
+  guchar *data;
+  GdkMemoryFormat format;
+  GFile *file;
+  GOutputStream *stream;
   gboolean result;
 
-  g_return_val_if_fail (GDK_IS_TEXTURE (texture), FALSE);
-  g_return_val_if_fail (filename != NULL, FALSE);
+  width = gdk_texture_get_width (texture);
+  height = gdk_texture_get_height (texture);
 
-  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);
+  stride = width * 4;
+  data = g_malloc (stride * height);
+
+  gdk_texture_download (texture, data, stride);
+
+  format = GDK_MEMORY_DEFAULT;
 
-  status = cairo_surface_write_to_png (surface, filename);
+  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);
 
-  if (status != CAIRO_STATUS_SUCCESS ||
-      cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
-    result = FALSE;
-  else
-    result = TRUE;
+  result = gdk_save_png (stream,
+                         data,
+                         width, height, stride,
+                         format,
+                         NULL);
+
+  g_output_stream_close (stream, NULL, NULL);
+  g_object_unref (stream);
 
-  cairo_surface_destroy (surface);
+  g_free (data);
 
-  return result;
+   return result;
 }


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