2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <sys/types.h>
30 #include <glib-object.h>
36 #include "image-load.h"
40 #include "pixbuf-util.h"
41 #include "thumb-standard.h"
42 #include "ui-fileops.h"
45 static void thumb_loader_error_cb(ImageLoader *il, gpointer data);
46 static void thumb_loader_setup(ThumbLoader *tl, FileData *fd);
48 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h);
52 *-----------------------------------------------------------------------------
53 * thumbnail routines: creation, caching, and maintenance (public)
54 *-----------------------------------------------------------------------------
57 /* Save thumbnail to disk
58 * or just mark failed thumbnail with 0 byte file (mark_failure = TRUE) */
59 static gboolean thumb_loader_save_thumbnail(ThumbLoader *tl, gboolean mark_failure)
62 gboolean success = FALSE;
65 if (!tl || !tl->fd) return FALSE;
66 if (!mark_failure && !tl->fd->thumb_pixbuf) return FALSE;
68 cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->fd->path, FALSE, &mode);
70 if (recursive_mkdir_if_not_exists(cache_dir, mode))
74 gchar *name = g_strconcat(filename_from_path(tl->fd->path), GQ_CACHE_EXT_THUMB, NULL);
76 cache_path = g_build_filename(cache_dir, name, NULL);
79 pathl = path_from_utf8(cache_path);
83 FILE *f = fopen(pathl, "w"); ;
85 DEBUG_1("Marking thumb failure: %s", cache_path);
94 DEBUG_1("Saving thumb: %s", cache_path);
95 success = pixbuf_to_file_as_png(tl->fd->thumb_pixbuf, pathl);
101 /* set thumb time to that of source file */
103 ut.actime = ut.modtime = filetime(tl->fd->path);
111 DEBUG_1("Saving failed: %s", pathl);
123 static void thumb_loader_percent_cb(ImageLoader *, gdouble percent, gpointer data)
125 auto tl = static_cast<ThumbLoader *>(data);
127 tl->percent_done = percent;
129 if (tl->func_progress) tl->func_progress(tl, tl->data);
132 static void thumb_loader_set_fallback(ThumbLoader *tl)
134 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
135 tl->fd->thumb_pixbuf = pixbuf_fallback(tl->fd, tl->max_w, tl->max_h);
138 static void thumb_loader_done_cb(ImageLoader *il, gpointer data)
140 auto tl = static_cast<ThumbLoader *>(data);
145 GdkPixbuf *rotated = nullptr;
147 DEBUG_1("thumb done: %s", tl->fd->path);
149 pixbuf = image_loader_get_pixbuf(tl->il);
152 DEBUG_1("...but no pixbuf: %s", tl->fd->path);
153 thumb_loader_error_cb(tl->il, tl);
159 // apply color correction, if required
160 thumb_loader_std_calibrate_pixbuf(tl->fd, pixbuf);
163 if (!tl->cache_hit && options->image.exif_rotate_enable)
165 if (!tl->fd->exif_orientation)
167 if (g_strcmp0(il->fd->format_name, "heif") != 0)
169 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
173 tl->fd->exif_orientation = EXIF_ORIENTATION_TOP_LEFT;
177 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
179 rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
184 pw = gdk_pixbuf_get_width(pixbuf);
185 ph = gdk_pixbuf_get_height(pixbuf);
187 if (tl->cache_hit && pw != tl->max_w && ph != tl->max_h)
189 /* requested thumbnail size may have changed, load original */
190 DEBUG_1("thumbnail size mismatch, regenerating: %s", tl->fd->path);
191 tl->cache_hit = FALSE;
193 thumb_loader_setup(tl, tl->fd);
195 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_done_cb, tl);
197 if (!image_loader_start(tl->il))
199 image_loader_free(tl->il);
202 DEBUG_1("regeneration failure: %s", tl->fd->path);
203 thumb_loader_error_cb(tl->il, tl);
210 if (pw > tl->max_w || ph > tl->max_h)
215 if ((static_cast<gdouble>(tl->max_w) / pw) < (static_cast<gdouble>(tl->max_h) / ph))
218 h = static_cast<gdouble>(w) / pw * ph;
224 w = static_cast<gdouble>(h) / ph * pw;
230 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
231 tl->fd->thumb_pixbuf = gdk_pixbuf_scale_simple(pixbuf, w, h, static_cast<GdkInterpType>(options->thumbnails.quality));
239 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
240 tl->fd->thumb_pixbuf = pixbuf;
242 g_object_ref(tl->fd->thumb_pixbuf);
244 save = image_loader_get_shrunk(il);
247 if (rotated) g_object_unref(rotated);
250 if (tl->cache_enable && save)
252 thumb_loader_save_thumbnail(tl, FALSE);
255 if (tl->func_done) tl->func_done(tl, tl->data);
258 static void thumb_loader_error_cb(ImageLoader *il, gpointer data)
260 auto tl = static_cast<ThumbLoader *>(data);
262 /* if at least some of the image is available, go to done_cb */
263 if (image_loader_get_pixbuf(tl->il) != nullptr)
265 thumb_loader_done_cb(il, data);
269 DEBUG_1("thumb error: %s", tl->fd->path);
271 image_loader_free(tl->il);
274 thumb_loader_set_fallback(tl);
276 if (tl->func_error) tl->func_error(tl, tl->data);
279 static gboolean thumb_loader_done_delay_cb(gpointer data)
281 auto tl = static_cast<ThumbLoader *>(data);
283 tl->idle_done_id = 0;
285 if (tl->func_done) tl->func_done(tl, tl->data);
287 return G_SOURCE_REMOVE;
290 static void thumb_loader_delay_done(ThumbLoader *tl)
292 if (!tl->idle_done_id) tl->idle_done_id = g_idle_add(thumb_loader_done_delay_cb, tl);
295 static void thumb_loader_setup(ThumbLoader *tl, FileData *fd)
297 image_loader_free(tl->il);
298 tl->il = image_loader_new(fd);
299 image_loader_set_priority(tl->il, G_PRIORITY_LOW);
301 /* this will speed up jpegs by up to 3x in some cases */
302 image_loader_set_requested_size(tl->il, tl->max_w, tl->max_h);
304 g_signal_connect(G_OBJECT(tl->il), "error", (GCallback)thumb_loader_error_cb, tl);
305 if (tl->func_progress) g_signal_connect(G_OBJECT(tl->il), "percent", (GCallback)thumb_loader_percent_cb, tl);
308 void thumb_loader_set_callbacks(ThumbLoader *tl,
309 ThumbLoader::Func func_done,
310 ThumbLoader::Func func_error,
311 ThumbLoader::Func func_progress,
316 if (tl->standard_loader)
318 thumb_loader_std_set_callbacks(reinterpret_cast<ThumbLoaderStd *>(tl),
319 reinterpret_cast<ThumbLoaderStd::Func>(func_done),
320 reinterpret_cast<ThumbLoaderStd::Func>(func_error),
321 reinterpret_cast<ThumbLoaderStd::Func>(func_progress),
326 tl->func_done = func_done;
327 tl->func_error = func_error;
328 tl->func_progress = func_progress;
333 void thumb_loader_set_cache(ThumbLoader *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
337 if (tl->standard_loader)
339 thumb_loader_std_set_cache(reinterpret_cast<ThumbLoaderStd *>(tl), enable_cache, local, retry_failed);
343 tl->cache_enable = enable_cache;
347 gboolean thumb_loader_start(ThumbLoader *tl, FileData *fd)
349 gchar *cache_path = nullptr;
351 if (!tl) return FALSE;
353 if (tl->standard_loader)
355 return thumb_loader_std_start(reinterpret_cast<ThumbLoaderStd *>(tl), fd);
358 if (!tl->fd && !fd) return FALSE;
360 if (!tl->fd) tl->fd = file_data_ref(fd);
362 if (tl->fd->format_class != FORMAT_CLASS_IMAGE && tl->fd->format_class != FORMAT_CLASS_RAWIMAGE && tl->fd->format_class != FORMAT_CLASS_COLLECTION && tl->fd->format_class != FORMAT_CLASS_VIDEO && tl->fd->format_class != FORMAT_CLASS_DOCUMENT && !options->file_filter.disable)
364 thumb_loader_set_fallback(tl);
368 if (tl->cache_enable)
370 cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->fd->path);
374 if (cache_time_valid(cache_path, tl->fd->path))
376 DEBUG_1("Found in cache:%s", tl->fd->path);
378 if (filesize(cache_path) == 0)
380 DEBUG_1("Broken image mark found:%s", cache_path);
382 thumb_loader_set_fallback(tl);
386 DEBUG_1("Cache location:%s", cache_path);
391 cache_path = nullptr;
396 if (!cache_path && options->thumbnails.use_xvpics)
398 if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
399 tl->fd->thumb_pixbuf = get_xv_thumbnail(tl->fd->path, tl->max_w, tl->max_h);
400 if (tl->fd->thumb_pixbuf)
402 thumb_loader_delay_done(tl);
409 FileData *fd = file_data_new_no_grouping(cache_path);
410 thumb_loader_setup(tl, fd);
413 tl->cache_hit = TRUE;
417 thumb_loader_setup(tl, tl->fd);
420 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_done_cb, tl);
421 if (!image_loader_start(tl->il))
423 /* try from original if cache attempt */
426 tl->cache_hit = FALSE;
427 log_printf("%s", _("Thumbnail image in cache failed to load, trying to recreate.\n"));
429 thumb_loader_setup(tl, tl->fd);
430 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_done_cb, tl);
431 if (image_loader_start(tl->il)) return TRUE;
433 /* mark failed thumbnail in cache with 0 byte file */
434 if (tl->cache_enable)
436 thumb_loader_save_thumbnail(tl, TRUE);
439 image_loader_free(tl->il);
441 thumb_loader_set_fallback(tl);
448 GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl)
452 if (tl && tl->standard_loader)
454 return thumb_loader_std_get_pixbuf(reinterpret_cast<ThumbLoaderStd *>(tl));
457 if (tl && tl->fd && tl->fd->thumb_pixbuf)
459 pixbuf = tl->fd->thumb_pixbuf;
460 g_object_ref(pixbuf);
464 pixbuf = pixbuf_fallback(nullptr, tl->max_w, tl->max_h);
470 ThumbLoader *thumb_loader_new(gint width, gint height)
474 /* non-std thumb loader is more effective for configurations with disabled caching
475 because it loads the thumbnails at the required size. loader_std loads
476 the thumbnails at the sizes appropriate for standard cache (typically 256x256 pixels)
477 and then performs one additional scaling */
478 if (options->thumbnails.spec_standard && options->thumbnails.enable_caching)
480 return reinterpret_cast<ThumbLoader *>(thumb_loader_std_new(width, height));
483 tl = g_new0(ThumbLoader, 1);
485 tl->cache_enable = options->thumbnails.enable_caching;
486 tl->percent_done = 0.0;
493 void thumb_loader_free(ThumbLoader *tl)
497 if (tl->standard_loader)
499 thumb_loader_std_free(reinterpret_cast<ThumbLoaderStd *>(tl));
503 image_loader_free(tl->il);
504 file_data_unref(tl->fd);
506 if (tl->idle_done_id) g_source_remove(tl->idle_done_id);
511 /* release thumb_pixbuf on file change - this forces reload. */
512 void thumb_notify_cb(FileData *fd, NotifyType type, gpointer)
514 if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE)) && fd->thumb_pixbuf)
516 DEBUG_1("Notify thumb: %s %04x", fd->path, type);
517 g_object_unref(fd->thumb_pixbuf);
518 fd->thumb_pixbuf = nullptr;
524 *-----------------------------------------------------------------------------
525 * xvpics thumbnail support, read-only (private)
526 *-----------------------------------------------------------------------------
530 * xvpics code originally supplied by:
531 * "Diederen Damien" <D.Diederen@student.ulg.ac.be>
533 * Note: Code has been modified to fit the style of the other code, and to use
534 * a few more glib-isms.
535 * 08-28-2000: Updated to return a gdk_pixbuf, Imlib is dying a death here.
538 #define XV_BUFFER 2048
539 static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp)
542 gchar buffer[XV_BUFFER];
543 guchar *data = nullptr;
545 file = fopen(filename, "rt");
546 if (!file) return nullptr;
548 if (fgets(buffer, XV_BUFFER, file) != nullptr
549 && strncmp(buffer, "P7 332", 6) == 0)
555 while (fgets(buffer, XV_BUFFER, file) && buffer[0] == '#') /* do_nothing() */;
557 if (sscanf(buffer, "%d %d %d", &width, &height, &depth) == 3)
559 gsize size = width * height;
561 data = g_new(guchar, size);
562 if (data && fread(data, 1, size, file) == size)
575 static void free_rgb_buffer(guchar *pixels, gpointer)
580 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h)
590 path = path_from_utf8(thumb_filename);
591 directory = g_path_get_dirname(path);
592 name = g_path_get_basename(path);
594 thumb_name = g_build_filename(directory, ".xvpics", name, NULL);
600 packed_data = load_xv_thumbnail(thumb_name, &width, &height);
609 rgb_data = g_new(guchar, width * height * 3);
610 for (i = 0; i < width * height; i++)
612 rgb_data[i * 3 + 0] = (packed_data[i] >> 5) * 36;
613 rgb_data[i * 3 + 1] = ((packed_data[i] & 28) >> 2) * 36;
614 rgb_data[i * 3 + 2] = (packed_data[i] & 3) * 85;
618 pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8,
619 width, height, 3 * width, free_rgb_buffer, nullptr);
621 if (pixbuf_scale_aspect(width, height, max_w, max_h, &width, &height))
627 pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_NEAREST);
636 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */