4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
18 #include "image-load.h"
20 #include "pixbuf_util.h"
21 #include "thumb_standard.h"
22 #include "ui_fileops.h"
27 static void thumb_loader_error_cb(ImageLoader *il, gpointer data);
28 static void thumb_loader_setup(ThumbLoader *tl, gchar *path);
30 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h);
31 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h);
35 *-----------------------------------------------------------------------------
36 * thumbnail routines: creation, caching, and maintenance (public)
37 *-----------------------------------------------------------------------------
40 static gint thumb_loader_save_to_cache(ThumbLoader *tl)
46 if (!tl || !tl->pixbuf) return FALSE;
48 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode);
50 if (cache_ensure_dir_exists(cache_dir, mode))
55 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path),
56 GQ_CACHE_EXT_THUMB, NULL);
58 DEBUG_1("Saving thumb: %s", cache_path);
60 pathl = path_from_utf8(cache_path);
61 success = pixbuf_to_file_as_png(tl->pixbuf, pathl);
65 /* set thumb time to that of source file */
67 ut.actime = ut.modtime = filetime(tl->path);
75 DEBUG_1("Saving failed: %s", pathl);
87 static gint thumb_loader_mark_failure(ThumbLoader *tl)
93 if (!tl) return FALSE;
95 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode);
97 if (cache_ensure_dir_exists(cache_dir, mode))
103 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path),
104 GQ_CACHE_EXT_THUMB, NULL);
106 DEBUG_1("marking thumb failure: %s", cache_path);
108 pathl = path_from_utf8(cache_path);
109 f = fopen(pathl, "w");
116 ut.actime = ut.modtime = filetime(tl->path);
133 static void thumb_loader_percent_cb(ImageLoader *il, gdouble percent, gpointer data)
135 ThumbLoader *tl = data;
137 tl->percent_done = percent;
139 if (tl->func_progress) tl->func_progress(tl, tl->data);
142 static void thumb_loader_done_cb(ImageLoader *il, gpointer data)
144 ThumbLoader *tl = data;
149 DEBUG_1("thumb done: %s", tl->path);
151 pixbuf = image_loader_get_pixbuf(tl->il);
154 DEBUG_1("...but no pixbuf: %s", tl->path);
155 thumb_loader_error_cb(tl->il, tl);
159 pw = gdk_pixbuf_get_width(pixbuf);
160 ph = gdk_pixbuf_get_height(pixbuf);
162 if (tl->cache_hit && pw != tl->max_w && ph != tl->max_h)
164 /* requested thumbnail size may have changed, load original */
165 DEBUG_1("thumbnail size mismatch, regenerating: %s", tl->path);
166 tl->cache_hit = FALSE;
168 thumb_loader_setup(tl, tl->path);
170 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
172 image_loader_free(tl->il);
175 DEBUG_1("regeneration failure: %s", tl->path);
176 thumb_loader_error_cb(tl->il, tl);
183 if (pw > tl->max_w || ph > tl->max_h)
187 if (((double)tl->max_w / pw) < ((double)tl->max_h / ph))
190 h = (double)w / pw * ph;
196 w = (double)h / ph * pw;
200 tl->pixbuf = gdk_pixbuf_scale_simple(pixbuf, w, h, (GdkInterpType)options->thumbnails.quality);
206 gdk_pixbuf_ref(tl->pixbuf);
211 if (tl->cache_enable && save)
213 thumb_loader_save_to_cache(tl);
216 if (tl->func_done) tl->func_done(tl, tl->data);
219 static void thumb_loader_error_cb(ImageLoader *il, gpointer data)
221 ThumbLoader *tl = data;
223 /* if at least some of the image is available, go to done_cb */
224 if (image_loader_get_pixbuf(tl->il) != NULL)
226 thumb_loader_done_cb(il, data);
230 DEBUG_1("thumb error: %s", tl->path);
232 image_loader_free(tl->il);
235 if (tl->func_error) tl->func_error(tl, tl->data);
238 static gint thumb_loader_done_delay_cb(gpointer data)
240 ThumbLoader *tl = data;
242 tl->idle_done_id = -1;
244 if (tl->func_done) tl->func_done(tl, tl->data);
249 static void thumb_loader_delay_done(ThumbLoader *tl)
251 if (tl->idle_done_id == -1) tl->idle_done_id = g_idle_add(thumb_loader_done_delay_cb, tl);
254 static void thumb_loader_setup(ThumbLoader *tl, gchar *path)
256 image_loader_free(tl->il);
257 tl->il = image_loader_new(file_data_new_simple(path));
259 if (options->thumbnails.fast)
261 /* this will speed up jpegs by up to 3x in some cases */
262 image_loader_set_requested_size(tl->il, tl->max_w, tl->max_h);
265 image_loader_set_error_func(tl->il, thumb_loader_error_cb, tl);
266 if (tl->func_progress) image_loader_set_percent_func(tl->il, thumb_loader_percent_cb, tl);
269 void thumb_loader_set_callbacks(ThumbLoader *tl,
270 ThumbLoaderFunc func_done,
271 ThumbLoaderFunc func_error,
272 ThumbLoaderFunc func_progress,
277 if (tl->standard_loader)
279 thumb_loader_std_set_callbacks((ThumbLoaderStd *)tl,
280 (ThumbLoaderStdFunc) func_done,
281 (ThumbLoaderStdFunc) func_error,
282 (ThumbLoaderStdFunc) func_progress,
287 tl->func_done = func_done;
288 tl->func_error = func_error;
289 tl->func_progress = func_progress;
294 void thumb_loader_set_cache(ThumbLoader *tl, gint enable_cache, gint local, gint retry_failed)
298 if (tl->standard_loader)
300 thumb_loader_std_set_cache((ThumbLoaderStd *)tl, enable_cache, local, retry_failed);
304 tl->cache_enable = enable_cache;
306 tl->cache_local = local;
307 tl->cache_retry = retry_failed;
312 gint thumb_loader_start(ThumbLoader *tl, const gchar *path)
314 gchar *cache_path = NULL;
316 if (!tl) return FALSE;
318 if (tl->standard_loader)
320 return thumb_loader_std_start((ThumbLoaderStd *)tl, path);
323 if (!tl->path && !path) return FALSE;
325 if (!tl->path) tl->path = g_strdup(path);
327 if (tl->cache_enable)
329 cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->path);
333 if (cache_time_valid(cache_path, tl->path))
335 DEBUG_1("Found in cache:%s", tl->path);
337 if (filesize(cache_path) == 0)
339 DEBUG_1("Broken image mark found:%s", cache_path);
344 DEBUG_1("Cache location:%s", cache_path);
354 if (!cache_path && options->thumbnails.use_xvpics)
356 tl->pixbuf = get_xv_thumbnail(tl->path, tl->max_w, tl->max_h);
359 thumb_loader_delay_done(tl);
366 thumb_loader_setup(tl, cache_path);
368 tl->cache_hit = TRUE;
372 thumb_loader_setup(tl, tl->path);
375 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
377 /* try from original if cache attempt */
380 tl->cache_hit = FALSE;
381 log_printf("%s", _("Thumbnail image in cache failed to load, trying to recreate.\n"));
383 thumb_loader_setup(tl, tl->path);
384 if (image_loader_start(tl->il, thumb_loader_done_cb, tl)) return TRUE;
386 /* mark failed thumbnail in cache with 0 byte file */
387 if (tl->cache_enable)
389 thumb_loader_mark_failure(tl);
392 image_loader_free(tl->il);
401 gint thumb_loader_to_pixmap(ThumbLoader *tl, GdkPixmap **pixmap, GdkBitmap **mask)
403 if (!tl || !tl->pixbuf) return -1;
405 gdk_pixbuf_render_pixmap_and_mask(tl->pixbuf, pixmap, mask, 128);
407 return thumb_loader_get_space(tl);
411 GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback)
415 if (tl && tl->standard_loader)
417 return thumb_loader_std_get_pixbuf((ThumbLoaderStd *)tl, with_fallback);
420 if (tl && tl->pixbuf)
423 g_object_ref(pixbuf);
425 else if (with_fallback)
429 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
430 w = gdk_pixbuf_get_width(pixbuf);
431 h = gdk_pixbuf_get_height(pixbuf);
432 if ((w > tl->max_w || h > tl->max_h) &&
433 normalize_thumb(&w, &h, tl->max_w, tl->max_h))
438 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
439 gdk_pixbuf_unref(tmp);
451 gint thumb_loader_get_space(ThumbLoader *tl)
455 if (tl->pixbuf) return (tl->max_w - gdk_pixbuf_get_width(tl->pixbuf));
461 ThumbLoader *thumb_loader_new(gint width, gint height)
465 if (options->thumbnails.spec_standard)
467 return (ThumbLoader *)thumb_loader_std_new(width, height);
470 tl = g_new0(ThumbLoader, 1);
471 tl->standard_loader = FALSE;
473 tl->cache_enable = options->thumbnails.enable_caching;
474 tl->cache_hit = FALSE;
475 tl->percent_done = 0.0;
481 tl->idle_done_id = -1;
486 void thumb_loader_free(ThumbLoader *tl)
490 if (tl->standard_loader)
492 thumb_loader_std_free((ThumbLoaderStd *)tl);
496 if (tl->pixbuf) gdk_pixbuf_unref(tl->pixbuf);
497 image_loader_free(tl->il);
500 if (tl->idle_done_id != -1) g_source_remove(tl->idle_done_id);
506 gint thumb_from_xpm_d(const char **data, gint max_w, gint max_h, GdkPixmap **pixmap, GdkBitmap **mask)
511 pixbuf = gdk_pixbuf_new_from_xpm_data(data);
512 w = gdk_pixbuf_get_width(pixbuf);
513 h = gdk_pixbuf_get_height(pixbuf);
515 if ((w > max_w || h > max_h) &&
516 normalize_thumb(&w, &h, max_w, max_h))
522 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
523 gdk_pixbuf_unref(tmp);
526 gdk_pixbuf_render_pixmap_and_mask(pixbuf, pixmap, mask, 128);
527 gdk_pixbuf_unref(pixbuf);
534 *-----------------------------------------------------------------------------
535 * xvpics thumbnail support, read-only (private)
536 *-----------------------------------------------------------------------------
540 * xvpics code originally supplied by:
541 * "Diederen Damien" <D.Diederen@student.ulg.ac.be>
543 * Note: Code has been modified to fit the style of the other code, and to use
544 * a few more glib-isms.
545 * 08-28-2000: Updated to return a gdk_pixbuf, Imlib is dieing a death here.
548 #define XV_BUFFER 2048
549 static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp)
552 gchar buffer[XV_BUFFER];
554 gint width, height, depth;
556 file = fopen(filename, "rt");
557 if(!file) return NULL;
559 fgets(buffer, XV_BUFFER, file);
560 if(strncmp(buffer, "P7 332", 6) != 0)
566 while (fgets(buffer, XV_BUFFER, file) && buffer[0] == '#') /* do_nothing() */;
568 if(sscanf(buffer, "%d %d %d", &width, &height, &depth) != 3)
574 data = g_new(guchar, width * height);
575 fread(data, 1, width * height, file);
584 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h)
589 scale = MIN((gdouble) max_w / *width, (gdouble) max_h / *height);
590 new_w = *width * scale;
591 new_h = *height * scale;
593 if (new_w != *width || new_h != *height)
603 static void free_rgb_buffer(guchar *pixels, gpointer data)
608 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h)
616 tmp_string = path_from_utf8(thumb_filename);
617 last_slash = strrchr(tmp_string, '/');
618 if(!last_slash) return NULL;
619 *last_slash++ = '\0';
621 thumb_name = g_strconcat(tmp_string, "/.xvpics/", last_slash, NULL);
622 packed_data = load_xv_thumbnail(thumb_name, &width, &height);
632 rgb_data = g_new(guchar, width * height * 3);
633 for (i = 0; i < width * height; i++)
635 rgb_data[i * 3 + 0] = (packed_data[i] >> 5) * 36;
636 rgb_data[i * 3 + 1] = ((packed_data[i] & 28) >> 2) * 36;
637 rgb_data[i * 3 + 2] = (packed_data[i] & 3) * 85;
641 pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8,
642 width, height, 3 * width, free_rgb_buffer, NULL);
644 if (normalize_thumb(&width, &height, max_w, max_h))
650 pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_NEAREST);
651 gdk_pixbuf_unref(tmp);