png: Use GdkTexureDownloader when saving

This commit is contained in:
Benjamin Otte 2023-02-14 07:19:18 +01:00
parent f05d801e28
commit eb45b8083a

View File

@ -21,9 +21,9 @@
#include <glib/gi18n-lib.h>
#include "gdkmemoryformatprivate.h"
#include "gdkmemorytextureprivate.h"
#include "gdkmemorytexture.h"
#include "gdkprofilerprivate.h"
#include "gdktexture.h"
#include "gdktexturedownloaderprivate.h"
#include "gdktextureprivate.h"
#include "gsk/gl/fp16private.h"
#include <png.h>
@ -297,11 +297,12 @@ gdk_save_png (GdkTexture *texture)
png_info *info;
png_io io = { NULL, 0, 0 };
int width, height;
int y;
GdkMemoryFormat format;
GdkTextureDownloader downloader;
GBytes *bytes;
gsize stride;
const guchar *data;
int y;
GdkMemoryTexture *memtex;
GdkMemoryFormat format;
int png_format;
int depth;
@ -370,11 +371,15 @@ gdk_save_png (GdkTexture *texture)
return NULL;
}
memtex = gdk_memory_texture_from_texture (texture, format);
gdk_texture_downloader_init (&downloader, texture);
gdk_texture_downloader_set_format (&downloader, format);
bytes = gdk_texture_downloader_download_bytes (&downloader, &stride);
gdk_texture_downloader_finish (&downloader);
data = g_bytes_get_data (bytes, NULL);
if (sigsetjmp (png_jmpbuf (png), 1))
{
g_object_unref (memtex);
g_bytes_unref (bytes);
g_free (io.data);
png_destroy_read_struct (&png, &info, NULL);
return NULL;
@ -394,8 +399,6 @@ gdk_save_png (GdkTexture *texture)
png_set_swap (png);
#endif
data = gdk_memory_texture_get_data (memtex);
stride = gdk_memory_texture_get_stride (memtex);
for (y = 0; y < height; y++)
png_write_row (png, data + y * stride);
@ -403,7 +406,7 @@ gdk_save_png (GdkTexture *texture)
png_destroy_write_struct (&png, &info);
g_object_unref (memtex);
g_bytes_unref (bytes);
return g_bytes_new_take (io.data, io.size);
}