7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
17 #include "image-load.h"
18 #include "pixbuf_util.h"
19 #include "thumb_standard.h"
20 #include "ui_fileops.h"
25 static void thumb_loader_error_cb(ImageLoader *il, gpointer data);
26 static void thumb_loader_setup(ThumbLoader *tl, gchar *path);
28 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h);
29 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h);
33 *-----------------------------------------------------------------------------
34 * thumbnail routines: creation, caching, and maintenance (public)
35 *-----------------------------------------------------------------------------
38 static gint thumb_loader_save_to_cache(ThumbLoader *tl)
44 if (!tl || !tl->pixbuf) return FALSE;
46 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode);
48 if (cache_ensure_dir_exists(cache_dir, mode))
53 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path),
54 GQVIEW_CACHE_EXT_THUMB, NULL);
56 if (debug) printf("Saving thumb: %s\n", cache_path);
58 pathl = path_from_utf8(cache_path);
59 success = pixbuf_to_file_as_png(tl->pixbuf, pathl);
63 /* set thumb time to that of source file */
65 ut.actime = ut.modtime = filetime(tl->path);
73 if (debug) printf("Saving failed: %s\n", pathl);
85 static gint thumb_loader_mark_failure(ThumbLoader *tl)
91 if (!tl) return FALSE;
93 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode);
95 if (cache_ensure_dir_exists(cache_dir, mode))
101 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path),
102 GQVIEW_CACHE_EXT_THUMB, NULL);
104 if (debug) printf("marking thumb failure: %s\n", cache_path);
106 pathl = path_from_utf8(cache_path);
107 f = fopen(pathl, "w");
114 ut.actime = ut.modtime = filetime(tl->path);
131 static void thumb_loader_percent_cb(ImageLoader *il, gdouble percent, gpointer data)
133 ThumbLoader *tl = data;
135 tl->percent_done = percent;
137 if (tl->func_progress) tl->func_progress(tl, tl->data);
140 static void thumb_loader_done_cb(ImageLoader *il, gpointer data)
142 ThumbLoader *tl = data;
147 if (debug) printf("thumb done: %s\n", tl->path);
149 pixbuf = image_loader_get_pixbuf(tl->il);
152 if (debug) printf("...but no pixbuf: %s\n", tl->path);
153 thumb_loader_error_cb(tl->il, tl);
157 pw = gdk_pixbuf_get_width(pixbuf);
158 ph = gdk_pixbuf_get_height(pixbuf);
160 if (tl->cache_hit && pw != tl->max_w && ph != tl->max_h)
162 /* requested thumbnail size may have changed, load original */
163 if (debug) printf("thumbnail size mismatch, regenerating: %s\n", tl->path);
164 tl->cache_hit = FALSE;
166 thumb_loader_setup(tl, tl->path);
168 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
170 image_loader_free(tl->il);
173 if (debug) printf("regeneration failure: %s\n", tl->path);
174 thumb_loader_error_cb(tl->il, tl);
181 if (pw > tl->max_w || ph > tl->max_h)
185 if (((double)tl->max_w / pw) < ((double)tl->max_h / ph))
188 h = (double)w / pw * ph;
194 w = (double)h / ph * pw;
198 tl->pixbuf = gdk_pixbuf_scale_simple(pixbuf, w, h, (GdkInterpType)thumbnail_quality);
204 gdk_pixbuf_ref(tl->pixbuf);
209 if (tl->cache_enable && save)
211 thumb_loader_save_to_cache(tl);
214 if (tl->func_done) tl->func_done(tl, tl->data);
217 static void thumb_loader_error_cb(ImageLoader *il, gpointer data)
219 ThumbLoader *tl = data;
221 /* if at least some of the image is available, go to done_cb */
222 if (image_loader_get_pixbuf(tl->il) != NULL)
224 thumb_loader_done_cb(il, data);
228 if (debug) printf("thumb error: %s\n", tl->path);
230 image_loader_free(tl->il);
233 if (tl->func_error) tl->func_error(tl, tl->data);
236 static gint thumb_loader_done_delay_cb(gpointer data)
238 ThumbLoader *tl = data;
240 tl->idle_done_id = -1;
242 if (tl->func_done) tl->func_done(tl, tl->data);
247 static void thumb_loader_delay_done(ThumbLoader *tl)
249 if (tl->idle_done_id == -1) tl->idle_done_id = g_idle_add(thumb_loader_done_delay_cb, tl);
252 static void thumb_loader_setup(ThumbLoader *tl, gchar *path)
254 image_loader_free(tl->il);
255 tl->il = image_loader_new(path);
259 /* this will speed up jpegs by up to 3x in some cases */
260 image_loader_set_requested_size(tl->il, tl->max_w, tl->max_h);
263 image_loader_set_error_func(tl->il, thumb_loader_error_cb, tl);
264 if (tl->func_progress) image_loader_set_percent_func(tl->il, thumb_loader_percent_cb, tl);
267 void thumb_loader_set_callbacks(ThumbLoader *tl,
268 ThumbLoaderFunc func_done,
269 ThumbLoaderFunc func_error,
270 ThumbLoaderFunc func_progress,
275 if (tl->standard_loader)
277 thumb_loader_std_set_callbacks((ThumbLoaderStd *)tl,
278 (ThumbLoaderStdFunc) func_done,
279 (ThumbLoaderStdFunc) func_error,
280 (ThumbLoaderStdFunc) func_progress,
285 tl->func_done = func_done;
286 tl->func_error = func_error;
287 tl->func_progress = func_progress;
292 void thumb_loader_set_cache(ThumbLoader *tl, gint enable_cache, gint local, gint retry_failed)
296 if (tl->standard_loader)
298 thumb_loader_std_set_cache((ThumbLoaderStd *)tl, enable_cache, local, retry_failed);
302 tl->cache_enable = enable_cache;
304 tl->cache_local = local;
305 tl->cache_retry = retry_failed;
310 gint thumb_loader_start(ThumbLoader *tl, const gchar *path)
312 gchar *cache_path = NULL;
314 if (!tl) return FALSE;
316 if (tl->standard_loader)
318 return thumb_loader_std_start((ThumbLoaderStd *)tl, path);
321 if (!tl->path && !path) return FALSE;
323 if (!tl->path) tl->path = g_strdup(path);
325 if (tl->cache_enable)
327 cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->path);
331 if (cache_time_valid(cache_path, tl->path))
333 if (debug) printf("Found in cache:%s\n", tl->path);
335 if (filesize(cache_path) == 0)
337 if (debug) printf("Broken image mark found:%s\n", cache_path);
342 if (debug) printf("Cache location:%s\n", cache_path);
352 if (!cache_path && use_xvpics_thumbnails)
354 tl->pixbuf = get_xv_thumbnail(tl->path, tl->max_w, tl->max_h);
357 thumb_loader_delay_done(tl);
364 thumb_loader_setup(tl, cache_path);
366 tl->cache_hit = TRUE;
370 thumb_loader_setup(tl, tl->path);
373 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
375 /* try from original if cache attempt */
378 tl->cache_hit = FALSE;
379 print_term(_("Thumbnail image in cache failed to load, trying to recreate.\n"));
381 thumb_loader_setup(tl, tl->path);
382 if (image_loader_start(tl->il, thumb_loader_done_cb, tl)) return TRUE;
384 /* mark failed thumbnail in cache with 0 byte file */
385 if (tl->cache_enable)
387 thumb_loader_mark_failure(tl);
390 image_loader_free(tl->il);
399 gint thumb_loader_to_pixmap(ThumbLoader *tl, GdkPixmap **pixmap, GdkBitmap **mask)
401 if (!tl || !tl->pixbuf) return -1;
403 gdk_pixbuf_render_pixmap_and_mask(tl->pixbuf, pixmap, mask, 128);
405 return thumb_loader_get_space(tl);
409 GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback)
413 if (tl && tl->standard_loader)
415 return thumb_loader_std_get_pixbuf((ThumbLoaderStd *)tl, with_fallback);
418 if (tl && tl->pixbuf)
421 g_object_ref(pixbuf);
423 else if (with_fallback)
427 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
428 w = gdk_pixbuf_get_width(pixbuf);
429 h = gdk_pixbuf_get_height(pixbuf);
430 if ((w > tl->max_w || h > tl->max_h) &&
431 normalize_thumb(&w, &h, tl->max_w, tl->max_h))
436 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
437 gdk_pixbuf_unref(tmp);
449 gint thumb_loader_get_space(ThumbLoader *tl)
453 if (tl->pixbuf) return (tl->max_w - gdk_pixbuf_get_width(tl->pixbuf));
459 ThumbLoader *thumb_loader_new(gint width, gint height)
463 if (thumbnail_spec_standard)
465 return (ThumbLoader *)thumb_loader_std_new(width, height);
468 tl = g_new0(ThumbLoader, 1);
469 tl->standard_loader = FALSE;
471 tl->cache_enable = enable_thumb_caching;
472 tl->cache_hit = FALSE;
473 tl->percent_done = 0.0;
479 tl->idle_done_id = -1;
484 void thumb_loader_free(ThumbLoader *tl)
488 if (tl->standard_loader)
490 thumb_loader_std_free((ThumbLoaderStd *)tl);
494 if (tl->pixbuf) gdk_pixbuf_unref(tl->pixbuf);
495 image_loader_free(tl->il);
498 if (tl->idle_done_id != -1) g_source_remove(tl->idle_done_id);
504 gint thumb_from_xpm_d(const char **data, gint max_w, gint max_h, GdkPixmap **pixmap, GdkBitmap **mask)
509 pixbuf = gdk_pixbuf_new_from_xpm_data(data);
510 w = gdk_pixbuf_get_width(pixbuf);
511 h = gdk_pixbuf_get_height(pixbuf);
513 if ((w > max_w || h > max_h) &&
514 normalize_thumb(&w, &h, max_w, max_h))
520 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
521 gdk_pixbuf_unref(tmp);
524 gdk_pixbuf_render_pixmap_and_mask(pixbuf, pixmap, mask, 128);
525 gdk_pixbuf_unref(pixbuf);
532 *-----------------------------------------------------------------------------
533 * xvpics thumbnail support, read-only (private)
534 *-----------------------------------------------------------------------------
538 * xvpics code originally supplied by:
539 * "Diederen Damien" <D.Diederen@student.ulg.ac.be>
541 * Note: Code has been modified to fit the style of the other code, and to use
542 * a few more glib-isms.
543 * 08-28-2000: Updated to return a gdk_pixbuf, Imlib is dieing a death here.
546 #define XV_BUFFER 2048
547 static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp)
550 gchar buffer[XV_BUFFER];
552 gint width, height, depth;
554 file = fopen(filename, "rt");
555 if(!file) return NULL;
557 fgets(buffer, XV_BUFFER, file);
558 if(strncmp(buffer, "P7 332", 6) != 0)
564 while(fgets(buffer, XV_BUFFER, file) && buffer[0] == '#') /* do_nothing() */;
566 if(sscanf(buffer, "%d %d %d", &width, &height, &depth) != 3)
572 data = g_new(guchar, width * height);
573 fread(data, 1, width * height, file);
582 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h)
587 scale = MIN((gdouble) max_w / *width, (gdouble) max_h / *height);
588 new_w = *width * scale;
589 new_h = *height * scale;
591 if (new_w != *width || new_h != *height)
601 static void free_rgb_buffer(guchar *pixels, gpointer data)
606 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h)
614 tmp_string = path_from_utf8(thumb_filename);
615 last_slash = strrchr(tmp_string, '/');
616 if(!last_slash) return NULL;
617 *last_slash++ = '\0';
619 thumb_name = g_strconcat(tmp_string, "/.xvpics/", last_slash, NULL);
620 packed_data = load_xv_thumbnail(thumb_name, &width, &height);
630 rgb_data = g_new(guchar, width * height * 3);
631 for(i = 0; i < width * height; i++)
633 rgb_data[i * 3 + 0] = (packed_data[i] >> 5) * 36;
634 rgb_data[i * 3 + 1] = ((packed_data[i] & 28) >> 2) * 36;
635 rgb_data[i * 3 + 2] = (packed_data[i] & 3) * 85;
639 pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8,
640 width, height, 3 * width, free_rgb_buffer, NULL);
642 if (normalize_thumb(&width, &height, max_w, max_h))
648 pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_NEAREST);
649 gdk_pixbuf_unref(tmp);