1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* See the COPYING file for license information.
*
* Guillaume Chazarain <guichaz@gmail.com>
*/
/******************************************
* Make thumbnails and keep track of them *
******************************************/
#include "gliv.h"
#include "thumbnails.h"
#include "options.h"
#include "tree.h"
#include "thread.h"
#include "callbacks.h"
extern options_struct *options;
static GHashTable *thumbs_hash_table = NULL;
gchar *get_absolute_filename(const gchar * filename)
{
static gchar *cwd = NULL;
if (filename[0] == '/')
return g_strdup(filename);
if (cwd == NULL)
cwd = g_get_current_dir();
while (filename[0] == '.' && filename[1] == '/') {
filename += 2;
while (filename[0] == '/')
filename++;
}
return g_build_filename(cwd, filename, NULL);
}
/*
* We include the thumbnails wanted dimensions in the hash key, this way we can
* keep track of many thumbnails with different sizes for a given filename.
*/
static gchar *get_key(const gchar * filename)
{
gchar *key, *fullpath;
fullpath = get_absolute_filename(filename);
key = g_strdup_printf("%s_%d_%d", fullpath,
options->thumb_width, options->thumb_height);
g_free(fullpath);
return key;
}
static GdkPixbuf *create_mini(GdkPixbuf * pixbuf)
{
gint w, h;
gfloat zoom_w, zoom_h, zoom;
gint mini_w, mini_h;
GdkPixbuf *res;
w = gdk_pixbuf_get_width(pixbuf);
h = gdk_pixbuf_get_height(pixbuf);
zoom_w = (gfloat) w / options->thumb_width;
zoom_h = (gfloat) h / options->thumb_height;
zoom = MAX(zoom_w, zoom_h);
if (zoom <= 1.0) {
/* A thumbnail should not be bigger than the original. */
g_object_ref(pixbuf);
return pixbuf;
}
mini_w = (gint) (w / zoom);
mini_h = (gint) (h / zoom);
mini_w = MAX(mini_w, 1);
mini_h = MAX(mini_h, 1);
res = gdk_pixbuf_scale_simple(pixbuf, mini_w, mini_h, GDK_INTERP_BILINEAR);
return res;
}
static void init_hash(void)
{
if (thumbs_hash_table == NULL)
/* First time. */
thumbs_hash_table = g_hash_table_new(g_str_hash, g_str_equal);
}
/* Runs in a separate thread. */
static GdkPixbuf *get_mini(const gchar * filename)
{
GdkPixbuf *pixbuf, *mini;
if (canceled_using_tree())
return NULL;
pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
if (pixbuf == NULL)
return NULL;
if (canceled_using_tree()) {
g_object_unref(pixbuf);
return NULL;
}
mini = create_mini(pixbuf);
g_object_unref(pixbuf);
return mini;
}
/* Called when we have loaded an image, to add its thumbnail. */
void add_thumbnail(const gchar * filename, GdkPixbuf * pixbuf)
{
gchar *key;
GdkPixbuf *mini;
init_hash();
key = get_key(filename);
if (g_hash_table_lookup(thumbs_hash_table, key) != NULL) {
g_free(key);
return;
}
mini = do_threaded((GThreadFunc) create_mini, pixbuf);
if (mini == NULL) {
g_free(key);
return;
}
g_hash_table_insert(thumbs_hash_table, key, mini);
if (mini == pixbuf)
g_object_ref(pixbuf);
}
gboolean fill_thumbnail(tree_item * item)
{
if (item->thumb != NULL) {
/* Simulate the do_threaded() effect. */
process_events();
return TRUE;
}
if (item->thumb_key == NULL)
item->thumb_key = get_key(item->path);
init_hash();
item->thumb = g_hash_table_lookup(thumbs_hash_table, item->thumb_key);
if (item->thumb != NULL)
return TRUE;
item->thumb = do_threaded((GThreadFunc) get_mini, item->path);
if (item->thumb == NULL)
return FALSE;
g_hash_table_insert(thumbs_hash_table, g_strdup(item->thumb_key),
item->thumb);
return TRUE;
}
void collection_add_thumbnail(gchar * key, GdkPixbuf * thumb)
{
init_hash();
g_hash_table_insert(thumbs_hash_table, key, thumb);
}
GdkPixbuf *get_thumbnail(const gchar * filename, gchar ** thumb_key)
{
tree_item item;
item.name = NULL;
item.path = (gchar *) filename;
item.thumb_key = NULL;
item.thumb = NULL;
fill_thumbnail(&item);
if (thumb_key)
*thumb_key = item.thumb_key;
return item.thumb;
}
|