Debian Codegraph @

tools/gtk-image-tool-compare.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* Copyright 2024 Red Hat, Inc.
2 *
3 * GTK is free software; you can redistribute it and/or modify it
4 * under the terms of the GNU Lesser General Public License as
5 * published by the Free Software Foundation; either version 2 of the
6 * License, or (at your option) any later version.
7 *
8 * GTK is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with GTK; see the file COPYING. If not,
15 * see <http://www.gnu.org/licenses/>.
16 *
17 * Author: Matthias Clasen
18 */
19
20 #include "config.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25
26 #include <glib/gi18n-lib.h>
27 #include <glib/gprintf.h>
28 #include <glib/gstdio.h>
29 #include <gtk/gtk.h>
30 #include "gtk-image-tool.h"
31 #include "gtk-tool-utils.h"
32
33 #include "testsuite/reftests/reftest-compare.h"
34
35 void
36 do_compare (int *argc,
37 const char ***argv)
38 {
39 GOptionContext *context;
40 gboolean opt_quiet = FALSE;
41 char **filenames = NULL;
42 char *opt_filename = NULL;
43 const GOptionEntry entries[] = {
44 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &opt_filename, N_("Output file"), N_("FILE") },
45 { "quiet", 'q', 0, G_OPTION_ARG_NONE, &opt_quiet, "Don't talk", NULL },
46
47 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL, N_("FILE…") },
48 { NULL, }
49 };
50 GdkTexture *texture[2];
51 GdkTexture *diff;
52 GError *error = NULL;
53
54 g_set_prgname ("gtk4-image-tool compare");
55 context = g_option_context_new (NULL);
56 g_option_context_set_translation_domain (context, GETTEXT_PACKAGE);
57 g_option_context_add_main_entries (context, entries, NULL);
58 g_option_context_set_summary (context, _("Compare two images"));
59
60 if (!g_option_context_parse (context, argc, (char ***)argv, &error))
61 {
62 g_printerr ("%s\n", error->message);
63 g_error_free (error);
64 exit (1);
65 }
66
67 g_option_context_free (context);
68
69 if (filenames == NULL)
70 {
71 g_printerr (_("No image file specified\n"));
72 exit (1);
73 }
74
75 if (g_strv_length (filenames) != 2)
76 {
77 g_printerr (_("Can only accept two image files\n"));
78 exit (1);
79 }
80
81 for (int i = 0; i < 2; i++)
82 texture[i] = load_image_file (filenames[i]);
83
84 diff = reftest_compare_textures (texture[0], texture[1]);
85
86 if (opt_filename && diff)
87 {
88 if (!gdk_texture_save_to_png (diff, opt_filename))
89 {
90 g_printerr (_("Could not save diff image to %s\n"), opt_filename);
91 exit (1);
92 }
93 }
94
95 if (!opt_quiet)
96 {
97 if (diff)
98 {
99 if (opt_filename)
100 g_print (_("Differences witten to %s.\n"), opt_filename);
101 else
102 g_print (_("The images are different.\n"));
103 }
104 else
105 g_print (_("No differences.\n"));
106 }
107
108 if (diff)
109 exit (1);
110
111 g_strfreev (filenames);
112 g_free (opt_filename);
113 g_object_unref (texture[0]);
114 g_object_unref (texture[1]);
115 }