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 (((float)tl->max_w / pw) < ((float)tl->max_h / ph))
188 h = (float)w / pw * ph;
194 w = (float)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);
258 /* this will speed up jpegs by up to 3x in some cases */
259 image_loader_set_requested_size(tl->max_w, tl->max_h);
262 image_loader_set_error_func(tl->il, thumb_loader_error_cb, tl);
263 if (tl->func_progress) image_loader_set_percent_func(tl->il, thumb_loader_percent_cb, tl);
266 void thumb_loader_set_callbacks(ThumbLoader *tl,
267 ThumbLoaderFunc func_done,
268 ThumbLoaderFunc func_error,
269 ThumbLoaderFunc func_progress,
274 if (tl->standard_loader)
276 thumb_loader_std_set_callbacks((ThumbLoaderStd *)tl,
277 (ThumbLoaderStdFunc) func_done,
278 (ThumbLoaderStdFunc) func_error,
279 (ThumbLoaderStdFunc) func_progress,
284 tl->func_done = func_done;
285 tl->func_error = func_error;
286 tl->func_progress = func_progress;
291 void thumb_loader_set_cache(ThumbLoader *tl, gint enable_cache, gint local, gint retry_failed)
295 if (tl->standard_loader)
297 thumb_loader_std_set_cache((ThumbLoaderStd *)tl, enable_cache, local, retry_failed);
301 tl->cache_enable = enable_cache;
303 tl->cache_local = local;
304 tl->cache_retry = retry_failed;
309 gint thumb_loader_start(ThumbLoader *tl, const gchar *path)
311 gchar *cache_path = NULL;
313 if (!tl) return FALSE;
315 if (tl->standard_loader)
317 return thumb_loader_std_start((ThumbLoaderStd *)tl, path);
320 if (!tl->path && !path) return FALSE;
322 if (!tl->path) tl->path = g_strdup(path);
324 if (tl->cache_enable)
326 cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->path);
330 if (cache_time_valid(cache_path, tl->path))
332 if (debug) printf("Found in cache:%s\n", tl->path);
334 if (filesize(cache_path) == 0)
336 if (debug) printf("Broken image mark found:%s\n", cache_path);
341 if (debug) printf("Cache location:%s\n", cache_path);
351 if (!cache_path && use_xvpics_thumbnails)
353 tl->pixbuf = get_xv_thumbnail(tl->path, tl->max_w, tl->max_h);
356 thumb_loader_delay_done(tl);
363 thumb_loader_setup(tl, cache_path);
365 tl->cache_hit = TRUE;
369 thumb_loader_setup(tl, tl->path);
372 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
374 /* try from original if cache attempt */
377 tl->cache_hit = FALSE;
378 print_term(_("Thumbnail image in cache failed to load, trying to recreate.\n"));
380 thumb_loader_setup(tl, tl->path);
381 if (image_loader_start(tl->il, thumb_loader_done_cb, tl)) return TRUE;
383 /* mark failed thumbnail in cache with 0 byte file */
384 if (tl->cache_enable)
386 thumb_loader_mark_failure(tl);
389 image_loader_free(tl->il);
398 gint thumb_loader_to_pixmap(ThumbLoader *tl, GdkPixmap **pixmap, GdkBitmap **mask)
400 if (!tl || !tl->pixbuf) return -1;
402 gdk_pixbuf_render_pixmap_and_mask(tl->pixbuf, pixmap, mask, 128);
404 return thumb_loader_get_space(tl);
408 GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback)
412 if (tl && tl->standard_loader)
414 return thumb_loader_std_get_pixbuf((ThumbLoaderStd *)tl, with_fallback);
417 if (tl && tl->pixbuf)
420 g_object_ref(pixbuf);
422 else if (with_fallback)
426 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
427 w = gdk_pixbuf_get_width(pixbuf);
428 h = gdk_pixbuf_get_height(pixbuf);
429 if ((w > tl->max_w || h > tl->max_h) &&
430 normalize_thumb(&w, &h, tl->max_w, tl->max_h))
435 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
436 gdk_pixbuf_unref(tmp);
448 gint thumb_loader_get_space(ThumbLoader *tl)
452 if (tl->pixbuf) return (tl->max_w - gdk_pixbuf_get_width(tl->pixbuf));
458 ThumbLoader *thumb_loader_new(gint width, gint height)
462 if (thumbnail_spec_standard)
464 return (ThumbLoader *)thumb_loader_std_new(width, height);
467 tl = g_new0(ThumbLoader, 1);
468 tl->standard_loader = FALSE;
470 tl->cache_enable = enable_thumb_caching;
471 tl->cache_hit = FALSE;
472 tl->percent_done = 0.0;
478 tl->idle_done_id = -1;
483 void thumb_loader_free(ThumbLoader *tl)
487 if (tl->standard_loader)
489 thumb_loader_std_free((ThumbLoaderStd *)tl);
493 if (tl->pixbuf) gdk_pixbuf_unref(tl->pixbuf);
494 image_loader_free(tl->il);
497 if (tl->idle_done_id != -1) g_source_remove(tl->idle_done_id);
503 gint thumb_from_xpm_d(const char **data, gint max_w, gint max_h, GdkPixmap **pixmap, GdkBitmap **mask)
508 pixbuf = gdk_pixbuf_new_from_xpm_data(data);
509 w = gdk_pixbuf_get_width(pixbuf);
510 h = gdk_pixbuf_get_height(pixbuf);
512 if ((w > max_w || h > max_h) &&
513 normalize_thumb(&w, &h, max_w, max_h))
519 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
520 gdk_pixbuf_unref(tmp);
523 gdk_pixbuf_render_pixmap_and_mask(pixbuf, pixmap, mask, 128);
524 gdk_pixbuf_unref(pixbuf);
531 *-----------------------------------------------------------------------------
532 * xvpics thumbnail support, read-only (private)
533 *-----------------------------------------------------------------------------
537 * xvpics code originally supplied by:
538 * "Diederen Damien" <D.Diederen@student.ulg.ac.be>
540 * Note: Code has been modified to fit the style of the other code, and to use
541 * a few more glib-isms.
542 * 08-28-2000: Updated to return a gdk_pixbuf, Imlib is dieing a death here.
545 #define XV_BUFFER 2048
546 static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp)
549 gchar buffer[XV_BUFFER];
551 gint width, height, depth;
553 file = fopen(filename, "rt");
554 if(!file) return NULL;
556 fgets(buffer, XV_BUFFER, file);
557 if(strncmp(buffer, "P7 332", 6) != 0)
563 while(fgets(buffer, XV_BUFFER, file) && buffer[0] == '#') /* do_nothing() */;
565 if(sscanf(buffer, "%d %d %d", &width, &height, &depth) != 3)
571 data = g_new(guchar, width * height);
572 fread(data, 1, width * height, file);
581 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h)
586 scale = MIN((gdouble) max_w / *width, (gdouble) max_h / *height);
587 new_w = *width * scale;
588 new_h = *height * scale;
590 if (new_w != *width || new_h != *height)
600 static void free_rgb_buffer(guchar *pixels, gpointer data)
605 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h)
613 tmp_string = path_from_utf8(thumb_filename);
614 last_slash = strrchr(tmp_string, '/');
615 if(!last_slash) return NULL;
616 *last_slash++ = '\0';
618 thumb_name = g_strconcat(tmp_string, "/.xvpics/", last_slash, NULL);
619 packed_data = load_xv_thumbnail(thumb_name, &width, &height);
629 rgb_data = g_new(guchar, width * height * 3);
630 for(i = 0; i < width * height; i++)
632 rgb_data[i * 3 + 0] = (packed_data[i] >> 5) * 36;
633 rgb_data[i * 3 + 1] = ((packed_data[i] & 28) >> 2) * 36;
634 rgb_data[i * 3 + 2] = (packed_data[i] & 3) * 85;
638 pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8,
639 width, height, 3 * width, free_rgb_buffer, NULL);
641 if (normalize_thumb(&width, &height, max_w, max_h))
647 pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_NEAREST);
648 gdk_pixbuf_unref(tmp);