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"
19 #include "pixbuf_util.h"
20 #include "thumb_standard.h"
21 #include "ui_fileops.h"
26 static void thumb_loader_error_cb(ImageLoader *il, gpointer data);
27 static void thumb_loader_setup(ThumbLoader *tl, gchar *path);
29 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h);
30 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h);
34 *-----------------------------------------------------------------------------
35 * thumbnail routines: creation, caching, and maintenance (public)
36 *-----------------------------------------------------------------------------
39 static gint thumb_loader_save_to_cache(ThumbLoader *tl)
45 if (!tl || !tl->pixbuf) return FALSE;
47 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode);
49 if (cache_ensure_dir_exists(cache_dir, mode))
54 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path),
55 GQVIEW_CACHE_EXT_THUMB, NULL);
57 if (debug) printf("Saving thumb: %s\n", cache_path);
59 pathl = path_from_utf8(cache_path);
60 success = pixbuf_to_file_as_png(tl->pixbuf, pathl);
64 /* set thumb time to that of source file */
66 ut.actime = ut.modtime = filetime(tl->path);
74 if (debug) printf("Saving failed: %s\n", pathl);
86 static gint thumb_loader_mark_failure(ThumbLoader *tl)
92 if (!tl) return FALSE;
94 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode);
96 if (cache_ensure_dir_exists(cache_dir, mode))
102 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path),
103 GQVIEW_CACHE_EXT_THUMB, NULL);
105 if (debug) printf("marking thumb failure: %s\n", cache_path);
107 pathl = path_from_utf8(cache_path);
108 f = fopen(pathl, "w");
115 ut.actime = ut.modtime = filetime(tl->path);
132 static void thumb_loader_percent_cb(ImageLoader *il, gdouble percent, gpointer data)
134 ThumbLoader *tl = data;
136 tl->percent_done = percent;
138 if (tl->func_progress) tl->func_progress(tl, tl->data);
141 static void thumb_loader_done_cb(ImageLoader *il, gpointer data)
143 ThumbLoader *tl = data;
148 if (debug) printf("thumb done: %s\n", tl->path);
150 pixbuf = image_loader_get_pixbuf(tl->il);
153 if (debug) printf("...but no pixbuf: %s\n", tl->path);
154 thumb_loader_error_cb(tl->il, tl);
158 pw = gdk_pixbuf_get_width(pixbuf);
159 ph = gdk_pixbuf_get_height(pixbuf);
161 if (tl->cache_hit && pw != tl->max_w && ph != tl->max_h)
163 /* requested thumbnail size may have changed, load original */
164 if (debug) printf("thumbnail size mismatch, regenerating: %s\n", tl->path);
165 tl->cache_hit = FALSE;
167 thumb_loader_setup(tl, tl->path);
169 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
171 image_loader_free(tl->il);
174 if (debug) printf("regeneration failure: %s\n", tl->path);
175 thumb_loader_error_cb(tl->il, tl);
182 if (pw > tl->max_w || ph > tl->max_h)
186 if (((double)tl->max_w / pw) < ((double)tl->max_h / ph))
189 h = (double)w / pw * ph;
195 w = (double)h / ph * pw;
199 tl->pixbuf = gdk_pixbuf_scale_simple(pixbuf, w, h, (GdkInterpType)thumbnail_quality);
205 gdk_pixbuf_ref(tl->pixbuf);
210 if (tl->cache_enable && save)
212 thumb_loader_save_to_cache(tl);
215 if (tl->func_done) tl->func_done(tl, tl->data);
218 static void thumb_loader_error_cb(ImageLoader *il, gpointer data)
220 ThumbLoader *tl = data;
222 /* if at least some of the image is available, go to done_cb */
223 if (image_loader_get_pixbuf(tl->il) != NULL)
225 thumb_loader_done_cb(il, data);
229 if (debug) printf("thumb error: %s\n", tl->path);
231 image_loader_free(tl->il);
234 if (tl->func_error) tl->func_error(tl, tl->data);
237 static gint thumb_loader_done_delay_cb(gpointer data)
239 ThumbLoader *tl = data;
241 tl->idle_done_id = -1;
243 if (tl->func_done) tl->func_done(tl, tl->data);
248 static void thumb_loader_delay_done(ThumbLoader *tl)
250 if (tl->idle_done_id == -1) tl->idle_done_id = g_idle_add(thumb_loader_done_delay_cb, tl);
253 static void thumb_loader_setup(ThumbLoader *tl, gchar *path)
255 image_loader_free(tl->il);
256 tl->il = image_loader_new(file_data_new_simple(path));
260 /* this will speed up jpegs by up to 3x in some cases */
261 image_loader_set_requested_size(tl->il, tl->max_w, tl->max_h);
264 image_loader_set_error_func(tl->il, thumb_loader_error_cb, tl);
265 if (tl->func_progress) image_loader_set_percent_func(tl->il, thumb_loader_percent_cb, tl);
268 void thumb_loader_set_callbacks(ThumbLoader *tl,
269 ThumbLoaderFunc func_done,
270 ThumbLoaderFunc func_error,
271 ThumbLoaderFunc func_progress,
276 if (tl->standard_loader)
278 thumb_loader_std_set_callbacks((ThumbLoaderStd *)tl,
279 (ThumbLoaderStdFunc) func_done,
280 (ThumbLoaderStdFunc) func_error,
281 (ThumbLoaderStdFunc) func_progress,
286 tl->func_done = func_done;
287 tl->func_error = func_error;
288 tl->func_progress = func_progress;
293 void thumb_loader_set_cache(ThumbLoader *tl, gint enable_cache, gint local, gint retry_failed)
297 if (tl->standard_loader)
299 thumb_loader_std_set_cache((ThumbLoaderStd *)tl, enable_cache, local, retry_failed);
303 tl->cache_enable = enable_cache;
305 tl->cache_local = local;
306 tl->cache_retry = retry_failed;
311 gint thumb_loader_start(ThumbLoader *tl, const gchar *path)
313 gchar *cache_path = NULL;
315 if (!tl) return FALSE;
317 if (tl->standard_loader)
319 return thumb_loader_std_start((ThumbLoaderStd *)tl, path);
322 if (!tl->path && !path) return FALSE;
324 if (!tl->path) tl->path = g_strdup(path);
326 if (tl->cache_enable)
328 cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->path);
332 if (cache_time_valid(cache_path, tl->path))
334 if (debug) printf("Found in cache:%s\n", tl->path);
336 if (filesize(cache_path) == 0)
338 if (debug) printf("Broken image mark found:%s\n", cache_path);
343 if (debug) printf("Cache location:%s\n", cache_path);
353 if (!cache_path && use_xvpics_thumbnails)
355 tl->pixbuf = get_xv_thumbnail(tl->path, tl->max_w, tl->max_h);
358 thumb_loader_delay_done(tl);
365 thumb_loader_setup(tl, cache_path);
367 tl->cache_hit = TRUE;
371 thumb_loader_setup(tl, tl->path);
374 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
376 /* try from original if cache attempt */
379 tl->cache_hit = FALSE;
380 print_term(_("Thumbnail image in cache failed to load, trying to recreate.\n"));
382 thumb_loader_setup(tl, tl->path);
383 if (image_loader_start(tl->il, thumb_loader_done_cb, tl)) return TRUE;
385 /* mark failed thumbnail in cache with 0 byte file */
386 if (tl->cache_enable)
388 thumb_loader_mark_failure(tl);
391 image_loader_free(tl->il);
400 gint thumb_loader_to_pixmap(ThumbLoader *tl, GdkPixmap **pixmap, GdkBitmap **mask)
402 if (!tl || !tl->pixbuf) return -1;
404 gdk_pixbuf_render_pixmap_and_mask(tl->pixbuf, pixmap, mask, 128);
406 return thumb_loader_get_space(tl);
410 GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback)
414 if (tl && tl->standard_loader)
416 return thumb_loader_std_get_pixbuf((ThumbLoaderStd *)tl, with_fallback);
419 if (tl && tl->pixbuf)
422 g_object_ref(pixbuf);
424 else if (with_fallback)
428 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
429 w = gdk_pixbuf_get_width(pixbuf);
430 h = gdk_pixbuf_get_height(pixbuf);
431 if ((w > tl->max_w || h > tl->max_h) &&
432 normalize_thumb(&w, &h, tl->max_w, tl->max_h))
437 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
438 gdk_pixbuf_unref(tmp);
450 gint thumb_loader_get_space(ThumbLoader *tl)
454 if (tl->pixbuf) return (tl->max_w - gdk_pixbuf_get_width(tl->pixbuf));
460 ThumbLoader *thumb_loader_new(gint width, gint height)
464 if (thumbnail_spec_standard)
466 return (ThumbLoader *)thumb_loader_std_new(width, height);
469 tl = g_new0(ThumbLoader, 1);
470 tl->standard_loader = FALSE;
472 tl->cache_enable = enable_thumb_caching;
473 tl->cache_hit = FALSE;
474 tl->percent_done = 0.0;
480 tl->idle_done_id = -1;
485 void thumb_loader_free(ThumbLoader *tl)
489 if (tl->standard_loader)
491 thumb_loader_std_free((ThumbLoaderStd *)tl);
495 if (tl->pixbuf) gdk_pixbuf_unref(tl->pixbuf);
496 image_loader_free(tl->il);
499 if (tl->idle_done_id != -1) g_source_remove(tl->idle_done_id);
505 gint thumb_from_xpm_d(const char **data, gint max_w, gint max_h, GdkPixmap **pixmap, GdkBitmap **mask)
510 pixbuf = gdk_pixbuf_new_from_xpm_data(data);
511 w = gdk_pixbuf_get_width(pixbuf);
512 h = gdk_pixbuf_get_height(pixbuf);
514 if ((w > max_w || h > max_h) &&
515 normalize_thumb(&w, &h, max_w, max_h))
521 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
522 gdk_pixbuf_unref(tmp);
525 gdk_pixbuf_render_pixmap_and_mask(pixbuf, pixmap, mask, 128);
526 gdk_pixbuf_unref(pixbuf);
533 *-----------------------------------------------------------------------------
534 * xvpics thumbnail support, read-only (private)
535 *-----------------------------------------------------------------------------
539 * xvpics code originally supplied by:
540 * "Diederen Damien" <D.Diederen@student.ulg.ac.be>
542 * Note: Code has been modified to fit the style of the other code, and to use
543 * a few more glib-isms.
544 * 08-28-2000: Updated to return a gdk_pixbuf, Imlib is dieing a death here.
547 #define XV_BUFFER 2048
548 static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp)
551 gchar buffer[XV_BUFFER];
553 gint width, height, depth;
555 file = fopen(filename, "rt");
556 if(!file) return NULL;
558 fgets(buffer, XV_BUFFER, file);
559 if(strncmp(buffer, "P7 332", 6) != 0)
565 while(fgets(buffer, XV_BUFFER, file) && buffer[0] == '#') /* do_nothing() */;
567 if(sscanf(buffer, "%d %d %d", &width, &height, &depth) != 3)
573 data = g_new(guchar, width * height);
574 fread(data, 1, width * height, file);
583 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h)
588 scale = MIN((gdouble) max_w / *width, (gdouble) max_h / *height);
589 new_w = *width * scale;
590 new_h = *height * scale;
592 if (new_w != *width || new_h != *height)
602 static void free_rgb_buffer(guchar *pixels, gpointer data)
607 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h)
615 tmp_string = path_from_utf8(thumb_filename);
616 last_slash = strrchr(tmp_string, '/');
617 if(!last_slash) return NULL;
618 *last_slash++ = '\0';
620 thumb_name = g_strconcat(tmp_string, "/.xvpics/", last_slash, NULL);
621 packed_data = load_xv_thumbnail(thumb_name, &width, &height);
631 rgb_data = g_new(guchar, width * height * 3);
632 for(i = 0; i < width * height; i++)
634 rgb_data[i * 3 + 0] = (packed_data[i] >> 5) * 36;
635 rgb_data[i * 3 + 1] = ((packed_data[i] & 28) >> 2) * 36;
636 rgb_data[i * 3 + 2] = (packed_data[i] & 3) * 85;
640 pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8,
641 width, height, 3 * width, free_rgb_buffer, NULL);
643 if (normalize_thumb(&width, &height, max_w, max_h))
649 pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_NEAREST);
650 gdk_pixbuf_unref(tmp);