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 ******************************************/
26 #include "thumbnails.h"
30 #include "images_menus.h"
32 extern options_struct
*options
;
34 static GHashTable
*hash_table
;
36 static gchar
*get_key(const gchar
* filename
)
40 key
= g_strdup_printf("%s_%d_%d", filename
,
41 options
->thumb_width
, options
->thumb_height
);
45 static GdkPixbuf
*create_mini(GdkPixbuf
* pixbuf
)
48 gfloat zoom_w
, zoom_h
, zoom
;
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
);
59 /* A thumbnail should not be bigger than the original. */
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
)
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())
88 mini
= g_hash_table_lookup(hash_table
, item
->thumb_key
);
92 pixbuf
= gdk_pixbuf_new_from_file(item
->path
, NULL
);
96 if (canceled_using_tree()) {
97 g_object_unref(pixbuf
);
101 mini
= create_mini(pixbuf
);
103 g_object_unref(pixbuf
);
105 g_hash_table_insert(hash_table
, item
->thumb_key
, mini
);
107 if (canceled_using_tree())
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
;
126 void collection_add_thumbnail(gchar
* key
, GdkPixbuf
* thumb
)
130 g_hash_table_insert(hash_table
, key
, thumb
);