Remove the trace of a TODO file.
[gliv.git] / src / thumbnails.c
blob0711c616b22afae1270d9cdcf257e2163af587f8
1 /*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * See the COPYING file for license information.
18 * Guillaume Chazarain <guichaz@yahoo.fr>
21 /******************************************
22 * Make thumbnails and keep track of them *
23 ******************************************/
25 #include "gliv.h"
26 #include "thumbnails.h"
27 #include "options.h"
28 #include "tree.h"
29 #include "thread.h"
30 #include "images_menus.h"
32 extern options_struct *options;
34 static GHashTable *hash_table;
36 static gchar *get_key(const gchar * filename)
38 gchar *key;
40 key = g_strdup_printf("%s_%d_%d", filename,
41 options->thumb_width, options->thumb_height);
42 return key;
45 static GdkPixbuf *create_mini(GdkPixbuf * pixbuf)
47 gint w, h;
48 gfloat zoom_w, zoom_h, zoom;
49 gint mini_w, mini_h;
51 w = gdk_pixbuf_get_width(pixbuf);
52 h = gdk_pixbuf_get_height(pixbuf);
54 zoom_w = (gfloat) w / options->thumb_width;
55 zoom_h = (gfloat) h / options->thumb_height;
56 zoom = MAX(zoom_w, zoom_h);
58 if (zoom <= 1.0)
59 /* A thumbnail should not be bigger than the original. */
60 return pixbuf;
62 mini_w = (gint) (w / zoom);
63 mini_h = (gint) (h / zoom);
65 mini_w = MAX(mini_w, 1);
66 mini_h = MAX(mini_h, 1);
68 return gdk_pixbuf_scale_simple(pixbuf, mini_w, mini_h, GDK_INTERP_BILINEAR);
71 static void init_hash(void)
73 if (hash_table == NULL)
74 /* First time. */
75 hash_table = g_hash_table_new(g_str_hash, g_str_equal);
78 /* Runs in a separate thread. */
79 static GdkPixbuf *get_mini(tree_item * item)
81 GdkPixbuf *pixbuf, *mini;
83 if (canceled_using_tree())
84 return NULL;
86 init_hash();
88 mini = g_hash_table_lookup(hash_table, item->thumb_key);
89 if (mini != NULL)
90 return mini;
92 pixbuf = gdk_pixbuf_new_from_file(item->path, NULL);
93 if (pixbuf == NULL)
94 return NULL;
96 if (canceled_using_tree()) {
97 g_object_unref(pixbuf);
98 return NULL;
101 mini = create_mini(pixbuf);
102 if (mini != pixbuf)
103 g_object_unref(pixbuf);
105 g_hash_table_insert(hash_table, item->thumb_key, mini);
107 if (canceled_using_tree())
108 mini = NULL;
110 return mini;
113 gboolean fill_thumbnail(tree_item * item)
115 if (item->thumb == NULL) {
116 if (item->thumb_key == NULL)
117 item->thumb_key = get_key(item->path);
119 item->thumb = do_threaded((GThreadFunc) get_mini, item);
120 return item->thumb != NULL;
123 return TRUE;
126 void collection_add_thumbnail(gchar * key, GdkPixbuf * thumb)
128 init_hash();
130 g_hash_table_insert(hash_table, key, thumb);