diff --git a/tools/gtk-image-tool-compare.c b/tools/gtk-image-tool-compare.c index 79de7895e2..7963305790 100644 --- a/tools/gtk-image-tool-compare.c +++ b/tools/gtk-image-tool-compare.c @@ -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); } } diff --git a/tools/gtk-image-tool-convert.c b/tools/gtk-image-tool-convert.c index 79e33050ff..a794412fa6 100644 --- a/tools/gtk-image-tool-convert.c +++ b/tools/gtk-image-tool-convert.c @@ -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); } diff --git a/tools/gtk-image-tool-relabel.c b/tools/gtk-image-tool-relabel.c index 3b872fb379..6af43db049 100644 --- a/tools/gtk-image-tool-relabel.c +++ b/tools/gtk-image-tool-relabel.c @@ -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); diff --git a/tools/gtk-image-tool-utils.c b/tools/gtk-image-tool-utils.c index 8cbc1b5362..469546dd22 100644 --- a/tools/gtk-image-tool-utils.c +++ b/tools/gtk-image-tool-utils.c @@ -28,15 +28,29 @@ #include #include #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) diff --git a/tools/gtk-image-tool.h b/tools/gtk-image-tool.h index 12e39dfa37..83db96dfd8 100644 --- a/tools/gtk-image-tool.h +++ b/tools/gtk-image-tool.h @@ -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); diff --git a/tools/meson.build b/tools/meson.build index a5cfd739f0..628845b15f 100644 --- a/tools/meson.build +++ b/tools/meson.build @@ -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 ] ], ]