1 >From de5d91aa15cc98795a68c8e553eb4baadaa0e501 Mon Sep 17 00:00:00 2001
2 From: Carlos Garnacho <carlosg@gnome.org>
3 Date: Fri, 17 May 2013 15:56:28 +0200
4 Subject: [PATCH] pixbuf: load "@2x" variants as pixbuf gobject data
6 if a variant of the filename is found that has a "@2x" appended
7 to the file name (before the extension), such file is loaded
8 and added as GObject data to the pixbuf
10 gdk-pixbuf/gdk-pixbuf-io.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++
11 1 file changed, 55 insertions(+)
13 diff --git a/gdk-pixbuf/gdk-pixbuf-io.c b/gdk-pixbuf/gdk-pixbuf-io.c
14 index dac21b8..ed98cd3 100644
15 --- a/gdk-pixbuf/gdk-pixbuf-io.c
16 +++ b/gdk-pixbuf/gdk-pixbuf-io.c
17 @@ -1025,6 +1025,40 @@ _gdk_pixbuf_generic_image_load (GdkPixbufModule *module,
22 +_gdk_pixbuf_file_is_scaled (const gchar *filename)
24 + gchar *basename, *ext;
26 + basename = g_path_get_basename (filename);
27 + ext = strrchr (basename, '.');
30 + ext = &basename[strlen(basename)];
32 + if (ext > basename + 3 && strncmp (ext - 3, "@2x", 3) == 0)
39 +_gdk_pixbuf_compose_scaled_filename (const gchar *filename)
41 + gchar *ext, *first, *composed;
43 + ext = strrchr (filename, '.');
48 + first = g_strndup (filename, ext - filename);
49 + composed = g_strdup_printf ("%s@2x%s", first, ext);
56 * gdk_pixbuf_new_from_file:
57 * @filename: Name of file to load, in the GLib file name encoding
58 @@ -1049,11 +1083,13 @@ gdk_pixbuf_new_from_file (const char *filename,
59 guchar buffer[SNIFF_BUFFER_SIZE];
60 GdkPixbufModule *image_module;
62 + gboolean filename_is_scaled;
64 g_return_val_if_fail (filename != NULL, NULL);
65 g_return_val_if_fail (error == NULL || *error == NULL, NULL);
67 display_name = g_filename_display_name (filename);
68 + filename_is_scaled = _gdk_pixbuf_file_is_scaled (filename);
70 f = g_fopen (filename, "rb");
72 @@ -1097,6 +1133,25 @@ gdk_pixbuf_new_from_file (const char *filename,
73 pixbuf = _gdk_pixbuf_generic_image_load (image_module, f, error);
76 + if (pixbuf && !filename_is_scaled) {
77 + GdkPixbuf *scaled_pixbuf = NULL;
78 + gchar *scaled_filename;
80 + scaled_filename = _gdk_pixbuf_compose_scaled_filename (filename);
82 + if (scaled_filename) {
83 + scaled_pixbuf = gdk_pixbuf_new_from_file (scaled_filename, NULL);
84 + g_free (scaled_filename);
87 + if (scaled_pixbuf) {
88 + g_object_set_data_full (G_OBJECT (pixbuf),
89 + "gdk-pixbuf-2x-variant",
91 + (GDestroyNotify) g_object_unref);
95 if (pixbuf == NULL && error != NULL && *error == NULL) {
97 /* I don't trust these crufty longjmp()'ing image libs