Try to regenerate low quality thumbnails

This commit is contained in:
Přemysl Eric Janouch 2021-12-27 23:51:38 +01:00
parent 004919cbc5
commit af2eb411d9
Signed by: p
GPG Key ID: A0420B94F92B9493
3 changed files with 34 additions and 6 deletions

View File

@ -351,6 +351,8 @@ rescale_thumbnail(cairo_surface_t *thumbnail, double row_height)
pixman_image_unref(src);
pixman_image_unref(dest);
cairo_surface_set_user_data(
scaled, &fiv_io_key_thumbnail_lq, (void *) (intptr_t) 1, NULL);
cairo_surface_destroy(thumbnail);
cairo_surface_mark_dirty(scaled);
return scaled;
@ -490,6 +492,7 @@ on_thumbnailer_ready(GObject *object, GAsyncResult *res, gpointer user_data)
static void
thumbnailer_next(FivBrowser *self)
{
// TODO(p): At least launch multiple thumbnailers in parallel.
GList *link = self->thumbnail_queue;
if (!link)
return;
@ -540,15 +543,17 @@ thumbnailer_start(FivBrowser *self)
{
thumbnailer_abort(self);
// TODO(p): Also collect rescaled images.
GList *missing = NULL, *rescaled = NULL;
GList *missing = NULL, *lq = NULL;
for (guint i = self->entries->len; i--; ) {
Entry *entry = &g_array_index(self->entries, Entry, i);
if (entry->icon)
missing = g_list_prepend(missing, entry);
else if (cairo_surface_get_user_data(
entry->thumbnail, &fiv_io_key_thumbnail_lq))
lq = g_list_prepend(lq, entry);
}
self->thumbnail_queue = g_list_concat(missing, rescaled);
self->thumbnail_queue = g_list_concat(missing, lq);
thumbnailer_next(self);
}

View File

@ -2288,6 +2288,8 @@ cairo_user_data_key_t fiv_io_key_loops;
cairo_user_data_key_t fiv_io_key_page_next;
cairo_user_data_key_t fiv_io_key_page_previous;
cairo_user_data_key_t fiv_io_key_thumbnail_lq;
cairo_surface_t *
fiv_io_open(
const gchar *path, FivIoProfile profile, gboolean enhance, GError **error)
@ -2764,6 +2766,13 @@ FivIoThumbnailSizeInfo
// TODO(p): Put the constant in a header file, share with fiv-browser.c.
static const double g_wide_thumbnail_factor = 2;
static void
mark_thumbnail_lq(cairo_surface_t *surface)
{
cairo_surface_set_user_data(
surface, &fiv_io_key_thumbnail_lq, (void *) (intptr_t) 1, NULL);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// In principle similar to rescale_thumbnail() from fiv-browser.c.
@ -2804,6 +2813,7 @@ rescale_thumbnail(cairo_surface_t *thumbnail, double row_height)
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);
cairo_destroy(cr);
mark_thumbnail_lq(scaled);
return scaled;
}
@ -2849,6 +2859,8 @@ fiv_io_produce_thumbnail(GFile *target, FivIoThumbnailSize size, GError **error)
gchar *sum = g_compute_checksum_for_string(G_CHECKSUM_MD5, uri, -1);
gchar *cache_dir = get_xdg_home_dir("XDG_CACHE_HOME", ".cache");
// TODO(p): Never produce thumbnails for thumbnail directories.
for (int use = size; use >= FIV_IO_THUMBNAIL_SIZE_MIN; use--) {
cairo_surface_t *scaled =
rescale_thumbnail(surface, fiv_io_thumbnail_sizes[use].size);
@ -3058,7 +3070,7 @@ fiv_io_lookup_thumbnail(GFile *target, FivIoThumbnailSize size)
cairo_surface_t *result = NULL;
GError *error = NULL;
for (int i = 0; i < FIV_IO_THUMBNAIL_SIZE_COUNT; i++) {
int use = size + i;
FivIoThumbnailSize use = size + i;
if (use > FIV_IO_THUMBNAIL_SIZE_MAX)
use = FIV_IO_THUMBNAIL_SIZE_MAX - i;
@ -3071,8 +3083,13 @@ fiv_io_lookup_thumbnail(GFile *target, FivIoThumbnailSize size)
g_clear_error(&error);
}
g_free(wide);
if (result)
if (result) {
// Higher up we can't distinguish images smaller than the thumbnail.
// Also, try not to rescale the already rescaled.
if (use != size)
mark_thumbnail_lq(result);
break;
}
gchar *path =
g_strdup_printf("%s/thumbnails/%s/%s.png", cache_dir, name, sum);
@ -3082,9 +3099,12 @@ fiv_io_lookup_thumbnail(GFile *target, FivIoThumbnailSize size)
g_clear_error(&error);
}
g_free(path);
if (result)
if (result) {
// Whatever produced it, we may be able to outclass it.
mark_thumbnail_lq(result);
break;
}
}
// TODO(p): We can definitely extract embedded thumbnails, but it should be
// done as a separate stage--the file may be stored on a slow device.

View File

@ -67,6 +67,9 @@ extern cairo_user_data_key_t fiv_io_key_page_next;
/// There is no wrap-around. This is a weak pointer.
extern cairo_user_data_key_t fiv_io_key_page_previous;
/// If non-NULL, indicates a thumbnail of insufficient quality.
extern cairo_user_data_key_t fiv_io_key_thumbnail_lq;
cairo_surface_t *fiv_io_open(
const gchar *path, FivIoProfile profile, gboolean enhance, GError **error);
cairo_surface_t *fiv_io_open_from_data(const char *data, size_t len,