image tool: Load and save avif using the loader

We don't want to use the gdk-pixbuf loader, since it doesn not
produce dmabufs, and has no idea about color states and >8bit
formats.

With this commit, the image tool will support avif in both
the relabel and the convert commands (provided we are building
with avif support).
This commit is contained in:
Matthias Clasen
2024-08-08 08:07:27 -04:00
parent d536d4ab78
commit 00aec81d79
6 changed files with 66 additions and 20 deletions

View File

@@ -79,10 +79,10 @@ do_compare (int *argc,
for (int i = 0; i < 2; i++)
{
texture[i] = gdk_texture_new_from_filename (filenames[i], &error);
texture[i] = load_image_file (filenames[i]);
if (texture[i] == NULL)
{
g_printerr (_("Failed to load %s: %s\n"), filenames[i], error->message);
g_printerr (_("Failed to load %s\n"), filenames[i]);
exit (1);
}
}

View File

@@ -31,10 +31,10 @@
static void
save_image (const char *filename,
const char *output,
GdkMemoryFormat format,
GdkColorState *color_state)
convert_image (const char *filename,
const char *output,
GdkMemoryFormat format,
GdkColorState *color_state)
{
GdkTexture *orig;
GdkTextureDownloader *downloader;
@@ -61,10 +61,7 @@ save_image (const char *filename,
texture = gdk_memory_texture_builder_build (builder);
if (g_str_has_suffix (output, ".tiff"))
gdk_texture_save_to_tiff (texture, output);
else
gdk_texture_save_to_png (texture, output);
save_texture (texture, output);
g_object_unref (texture);
g_bytes_unref (bytes);
@@ -173,7 +170,7 @@ do_convert (int *argc,
if (!color_state)
color_state = gdk_color_state_get_srgb ();
save_image (filenames[0], filenames[1], format, color_state);
convert_image (filenames[0], filenames[1], format, color_state);
g_strfreev (filenames);
}

View File

@@ -60,10 +60,7 @@ relabel_image (const char *filename,
texture = gdk_memory_texture_builder_build (builder);
if (g_str_has_suffix (output, ".tiff"))
gdk_texture_save_to_tiff (texture, output);
else
gdk_texture_save_to_png (texture, output);
save_texture (texture, output);
g_object_unref (texture);
g_bytes_unref (bytes);

View File

@@ -28,15 +28,29 @@
#include <glib/gstdio.h>
#include <gtk/gtk.h>
#include "gtk-image-tool.h"
#include "gdk/loaders/gdkavifprivate.h"
GdkTexture *
load_image_file (const char *filename)
{
GError *error = NULL;
GdkTexture *texture;
GdkTexture *texture = NULL;
char *data;
gsize size;
GBytes *bytes;
if (g_file_get_contents (filename, &data, &size, &error))
{
bytes = g_bytes_new_take (data, size);
#ifdef HAVE_AVIF
if (gdk_is_avif (bytes))
texture = gdk_load_avif (bytes, &error);
else
#endif
texture = gdk_texture_new_from_bytes (bytes, &error);
}
texture = gdk_texture_new_from_filename (filename, &error);
if (!texture)
{
g_printerr ("%s\n", error->message);
@@ -44,9 +58,39 @@ load_image_file (const char *filename)
exit (1);
}
g_bytes_unref (bytes);
return texture;
}
gboolean
save_texture (GdkTexture *texture,
const char *filename)
{
if (g_str_has_suffix (filename, ".png"))
return gdk_texture_save_to_png (texture, filename);
else if (g_str_has_suffix (filename, ".tiff"))
return gdk_texture_save_to_tiff (texture, filename);
#ifdef HAVE_AVIF
else if (g_str_has_suffix (filename, ".avif"))
{
GBytes *bytes;
gboolean result;
bytes = gdk_save_avif (texture);
result = g_file_set_contents (filename,
g_bytes_get_data (bytes, NULL),
g_bytes_get_size (bytes),
NULL);
g_bytes_unref (bytes);
return result;
}
#endif
return FALSE;
}
gboolean
find_format_by_name (const char *name,
GdkMemoryFormat *format)

View File

@@ -8,7 +8,8 @@ void do_relabel (int *argc, const char ***argv);
void do_show (int *argc, const char ***argv);
GdkTexture * load_image_file (const char *filename);
gboolean save_texture (GdkTexture *texture,
const char *filename);
gboolean find_format_by_name (const char *name,
GdkMemoryFormat *format);

View File

@@ -22,6 +22,12 @@ if win32_enabled
extra_update_icon_cache_objs = import('windows').compile_resources(uac_rc)
endif
extra_image_tool_sources = []
if avif_dep.found()
extra_image_tool_sources = ['../gdk/loaders/gdkavif.c']
endif
gtk_tools = [
['gtk4-path-tool', ['gtk-path-tool.c',
'gtk-path-tool-decompose.c',
@@ -56,7 +62,8 @@ gtk_tools = [
'gtk-image-tool-relabel.c',
'gtk-image-tool-show.c',
'gtk-image-tool-utils.c',
'../testsuite/reftests/reftest-compare.c'], [libgtk_dep] ],
'../testsuite/reftests/reftest-compare.c'] + extra_image_tool_sources,
[libgtk_dep] ],
['gtk4-update-icon-cache', ['updateiconcache.c', '../gtk/gtkiconcachevalidator.c' ] + extra_update_icon_cache_objs, [ libgtk_dep ] ],
['gtk4-encode-symbolic-svg', ['encodesymbolic.c'], [ libgtk_static_dep ] ],
]