Silence GTK deprecation warning in unused function
[geeqie.git] / src / thumb.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "thumb.h"
23
24 #include <sys/types.h>
25 #include <utime.h>
26
27 #include <cstdio>
28 #include <cstring>
29
30 #include <glib-object.h>
31
32 #include "cache.h"
33 #include "debug.h"
34 #include "exif.h"
35 #include "filedata.h"
36 #include "image-load.h"
37 #include "intl.h"
38 #include "metadata.h"
39 #include "options.h"
40 #include "pixbuf-util.h"
41 #include "thumb-standard.h"
42 #include "ui-fileops.h"
43
44
45 static void thumb_loader_error_cb(ImageLoader *il, gpointer data);
46 static void thumb_loader_setup(ThumbLoader *tl, FileData *fd);
47
48 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h);
49
50
51 /*
52  *-----------------------------------------------------------------------------
53  * thumbnail routines: creation, caching, and maintenance (public)
54  *-----------------------------------------------------------------------------
55  */
56
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)
60 {
61         gchar *cache_dir;
62         gboolean success = FALSE;
63         mode_t mode = 0755;
64
65         if (!tl || !tl->fd) return FALSE;
66         if (!mark_failure && !tl->fd->thumb_pixbuf) return FALSE;
67
68         cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->fd->path, FALSE, &mode);
69
70         if (recursive_mkdir_if_not_exists(cache_dir, mode))
71                 {
72                 gchar *cache_path;
73                 gchar *pathl;
74                 gchar *name = g_strconcat(filename_from_path(tl->fd->path), GQ_CACHE_EXT_THUMB, NULL);
75
76                 cache_path = g_build_filename(cache_dir, name, NULL);
77                 g_free(name);
78
79                 pathl = path_from_utf8(cache_path);
80
81                 if (mark_failure)
82                         {
83                         FILE *f = fopen(pathl, "w"); ;
84
85                         DEBUG_1("Marking thumb failure: %s", cache_path);
86                         if (f)
87                                 {
88                                 fclose(f);
89                                 success = TRUE;
90                                 }
91                         }
92                 else
93                         {
94                         DEBUG_1("Saving thumb: %s", cache_path);
95                         success = pixbuf_to_file_as_png(tl->fd->thumb_pixbuf, pathl);
96                         }
97
98                 if (success)
99                         {
100                         struct utimbuf ut;
101                         /* set thumb time to that of source file */
102
103                         ut.actime = ut.modtime = filetime(tl->fd->path);
104                         if (ut.modtime > 0)
105                                 {
106                                 utime(pathl, &ut);
107                                 }
108                         }
109                 else
110                         {
111                         DEBUG_1("Saving failed: %s", pathl);
112                         }
113
114                 g_free(pathl);
115                 g_free(cache_path);
116                 }
117
118         g_free(cache_dir);
119
120         return success;
121 }
122
123 static void thumb_loader_percent_cb(ImageLoader *, gdouble percent, gpointer data)
124 {
125         auto tl = static_cast<ThumbLoader *>(data);
126
127         tl->percent_done = percent;
128
129         if (tl->func_progress) tl->func_progress(tl, tl->data);
130 }
131
132 static void thumb_loader_set_fallback(ThumbLoader *tl)
133 {
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);
136 }
137
138 static void thumb_loader_done_cb(ImageLoader *il, gpointer data)
139 {
140         auto tl = static_cast<ThumbLoader *>(data);
141         GdkPixbuf *pixbuf;
142         gint pw;
143         gint ph;
144         gint save;
145         GdkPixbuf *rotated = nullptr;
146
147         DEBUG_1("thumb done: %s", tl->fd->path);
148
149         pixbuf = image_loader_get_pixbuf(tl->il);
150         if (!pixbuf)
151                 {
152                 DEBUG_1("...but no pixbuf: %s", tl->fd->path);
153                 thumb_loader_error_cb(tl->il, tl);
154                 return;
155                 }
156
157         if(!tl->cache_hit)
158                 {
159                         // apply color correction, if required
160                         thumb_loader_std_calibrate_pixbuf(tl->fd, pixbuf);
161                 }
162
163         if (!tl->cache_hit && options->image.exif_rotate_enable)
164                 {
165                 if (!tl->fd->exif_orientation)
166                         {
167                         if (g_strcmp0(il->fd->format_name, "heif") != 0)
168                                 {
169                                 tl->fd->exif_orientation = metadata_read_int(tl->fd, ORIENTATION_KEY, EXIF_ORIENTATION_TOP_LEFT);
170                                 }
171                         else
172                                 {
173                                 tl->fd->exif_orientation = EXIF_ORIENTATION_TOP_LEFT;
174                                 }
175                         }
176
177                 if (tl->fd->exif_orientation != EXIF_ORIENTATION_TOP_LEFT)
178                         {
179                         rotated = pixbuf_apply_orientation(pixbuf, tl->fd->exif_orientation);
180                         pixbuf = rotated;
181                         }
182                 }
183
184         pw = gdk_pixbuf_get_width(pixbuf);
185         ph = gdk_pixbuf_get_height(pixbuf);
186
187         if (tl->cache_hit && pw != tl->max_w && ph != tl->max_h)
188                 {
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;
192
193                 thumb_loader_setup(tl, tl->fd);
194
195                 g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_done_cb, tl);
196
197                 if (!image_loader_start(tl->il))
198                         {
199                         image_loader_free(tl->il);
200                         tl->il = nullptr;
201
202                         DEBUG_1("regeneration failure: %s", tl->fd->path);
203                         thumb_loader_error_cb(tl->il, tl);
204                         }
205                 return;
206                 }
207
208         /* scale ?? */
209
210         if (pw > tl->max_w || ph > tl->max_h)
211                 {
212                 gint w;
213                 gint h;
214
215                 if ((static_cast<gdouble>(tl->max_w) / pw) < (static_cast<gdouble>(tl->max_h) / ph))
216                         {
217                         w = tl->max_w;
218                         h = static_cast<gdouble>(w) / pw * ph;
219                         if (h < 1) h = 1;
220                         }
221                 else
222                         {
223                         h = tl->max_h;
224                         w = static_cast<gdouble>(h) / ph * pw;
225                         if (w < 1) w = 1;
226                         }
227
228                 if (tl->fd)
229                         {
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));
232                         }
233                 save = TRUE;
234                 }
235         else
236                 {
237                 if (tl->fd)
238                         {
239                         if (tl->fd->thumb_pixbuf) g_object_unref(tl->fd->thumb_pixbuf);
240                         tl->fd->thumb_pixbuf = pixbuf;
241
242                         g_object_ref(tl->fd->thumb_pixbuf);
243                         }
244                 save = image_loader_get_shrunk(il);
245                 }
246
247         if (rotated) g_object_unref(rotated);
248
249         /* save it ? */
250         if (tl->cache_enable && save)
251                 {
252                 thumb_loader_save_thumbnail(tl, FALSE);
253                 }
254
255         if (tl->func_done) tl->func_done(tl, tl->data);
256 }
257
258 static void thumb_loader_error_cb(ImageLoader *il, gpointer data)
259 {
260         auto tl = static_cast<ThumbLoader *>(data);
261
262         /* if at least some of the image is available, go to done_cb */
263         if (image_loader_get_pixbuf(tl->il) != nullptr)
264                 {
265                 thumb_loader_done_cb(il, data);
266                 return;
267                 }
268
269         DEBUG_1("thumb error: %s", tl->fd->path);
270
271         image_loader_free(tl->il);
272         tl->il = nullptr;
273
274         thumb_loader_set_fallback(tl);
275
276         if (tl->func_error) tl->func_error(tl, tl->data);
277 }
278
279 static gboolean thumb_loader_done_delay_cb(gpointer data)
280 {
281         auto tl = static_cast<ThumbLoader *>(data);
282
283         tl->idle_done_id = 0;
284
285         if (tl->func_done) tl->func_done(tl, tl->data);
286
287         return G_SOURCE_REMOVE;
288 }
289
290 static void thumb_loader_delay_done(ThumbLoader *tl)
291 {
292         if (!tl->idle_done_id) tl->idle_done_id = g_idle_add(thumb_loader_done_delay_cb, tl);
293 }
294
295 static void thumb_loader_setup(ThumbLoader *tl, FileData *fd)
296 {
297         image_loader_free(tl->il);
298         tl->il = image_loader_new(fd);
299         image_loader_set_priority(tl->il, G_PRIORITY_LOW);
300
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);
303
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);
306 }
307
308 void thumb_loader_set_callbacks(ThumbLoader *tl,
309                                 ThumbLoader::Func func_done,
310                                 ThumbLoader::Func func_error,
311                                 ThumbLoader::Func func_progress,
312                                 gpointer data)
313 {
314         if (!tl) return;
315
316         if (tl->standard_loader)
317                 {
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),
322                                                data);
323                 return;
324                 }
325
326         tl->func_done = func_done;
327         tl->func_error = func_error;
328         tl->func_progress = func_progress;
329
330         tl->data = data;
331 }
332
333 void thumb_loader_set_cache(ThumbLoader *tl, gboolean enable_cache, gboolean local, gboolean retry_failed)
334 {
335         if (!tl) return;
336
337         if (tl->standard_loader)
338                 {
339                 thumb_loader_std_set_cache(reinterpret_cast<ThumbLoaderStd *>(tl), enable_cache, local, retry_failed);
340                 return;
341                 }
342
343         tl->cache_enable = enable_cache;
344 }
345
346
347 gboolean thumb_loader_start(ThumbLoader *tl, FileData *fd)
348 {
349         gchar *cache_path = nullptr;
350
351         if (!tl) return FALSE;
352
353         if (tl->standard_loader)
354                 {
355                 return thumb_loader_std_start(reinterpret_cast<ThumbLoaderStd *>(tl), fd);
356                 }
357
358         if (!tl->fd && !fd) return FALSE;
359
360         if (!tl->fd) tl->fd = file_data_ref(fd);
361
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)
363                 {
364                 thumb_loader_set_fallback(tl);
365                 return FALSE;
366                 }
367
368         if (tl->cache_enable)
369                 {
370                 cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->fd->path);
371
372                 if (cache_path)
373                         {
374                         if (cache_time_valid(cache_path, tl->fd->path))
375                                 {
376                                 DEBUG_1("Found in cache:%s", tl->fd->path);
377
378                                 if (filesize(cache_path) == 0)
379                                         {
380                                         DEBUG_1("Broken image mark found:%s", cache_path);
381                                         g_free(cache_path);
382                                         thumb_loader_set_fallback(tl);
383                                         return FALSE;
384                                         }
385
386                                 DEBUG_1("Cache location:%s", cache_path);
387                                 }
388                         else
389                                 {
390                                 g_free(cache_path);
391                                 cache_path = nullptr;
392                                 }
393                         }
394                 }
395
396         if (!cache_path && options->thumbnails.use_xvpics)
397                 {
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)
401                         {
402                         thumb_loader_delay_done(tl);
403                         return TRUE;
404                         }
405                 }
406
407         if (cache_path)
408                 {
409                 FileData *fd = file_data_new_no_grouping(cache_path);
410                 thumb_loader_setup(tl, fd);
411                 file_data_unref(fd);
412                 g_free(cache_path);
413                 tl->cache_hit = TRUE;
414                 }
415         else
416                 {
417                 thumb_loader_setup(tl, tl->fd);
418                 }
419
420         g_signal_connect(G_OBJECT(tl->il), "done", (GCallback)thumb_loader_done_cb, tl);
421         if (!image_loader_start(tl->il))
422                 {
423                 /* try from original if cache attempt */
424                 if (tl->cache_hit)
425                         {
426                         tl->cache_hit = FALSE;
427                         log_printf("%s", _("Thumbnail image in cache failed to load, trying to recreate.\n"));
428
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;
432                         }
433                 /* mark failed thumbnail in cache with 0 byte file */
434                 if (tl->cache_enable)
435                         {
436                         thumb_loader_save_thumbnail(tl, TRUE);
437                         }
438
439                 image_loader_free(tl->il);
440                 tl->il = nullptr;
441                 thumb_loader_set_fallback(tl);
442                 return FALSE;
443                 }
444
445         return TRUE;
446 }
447
448 GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl)
449 {
450         GdkPixbuf *pixbuf;
451
452         if (tl && tl->standard_loader)
453                 {
454                 return thumb_loader_std_get_pixbuf(reinterpret_cast<ThumbLoaderStd *>(tl));
455                 }
456
457         if (tl && tl->fd && tl->fd->thumb_pixbuf)
458                 {
459                 pixbuf = tl->fd->thumb_pixbuf;
460                 g_object_ref(pixbuf);
461                 }
462         else
463                 {
464                 pixbuf = pixbuf_fallback(nullptr, tl->max_w, tl->max_h);
465                 }
466
467         return pixbuf;
468 }
469
470 ThumbLoader *thumb_loader_new(gint width, gint height)
471 {
472         ThumbLoader *tl;
473
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)
479                 {
480                 return reinterpret_cast<ThumbLoader *>(thumb_loader_std_new(width, height));
481                 }
482
483         tl = g_new0(ThumbLoader, 1);
484
485         tl->cache_enable = options->thumbnails.enable_caching;
486         tl->percent_done = 0.0;
487         tl->max_w = width;
488         tl->max_h = height;
489
490         return tl;
491 }
492
493 void thumb_loader_free(ThumbLoader *tl)
494 {
495         if (!tl) return;
496
497         if (tl->standard_loader)
498                 {
499                 thumb_loader_std_free(reinterpret_cast<ThumbLoaderStd *>(tl));
500                 return;
501                 }
502
503         image_loader_free(tl->il);
504         file_data_unref(tl->fd);
505
506         if (tl->idle_done_id) g_source_remove(tl->idle_done_id);
507
508         g_free(tl);
509 }
510
511 /* release thumb_pixbuf on file change - this forces reload. */
512 void thumb_notify_cb(FileData *fd, NotifyType type, gpointer)
513 {
514         if ((type & (NOTIFY_REREAD | NOTIFY_CHANGE)) && fd->thumb_pixbuf)
515                 {
516                 DEBUG_1("Notify thumb: %s %04x", fd->path, type);
517                 g_object_unref(fd->thumb_pixbuf);
518                 fd->thumb_pixbuf = nullptr;
519                 }
520 }
521
522
523 /*
524  *-----------------------------------------------------------------------------
525  * xvpics thumbnail support, read-only (private)
526  *-----------------------------------------------------------------------------
527  */
528
529 /*
530  * xvpics code originally supplied by:
531  * "Diederen Damien" <D.Diederen@student.ulg.ac.be>
532  *
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.
536  */
537
538 #define XV_BUFFER 2048
539 static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp)
540 {
541         FILE *file;
542         gchar buffer[XV_BUFFER];
543         guchar *data = nullptr;
544
545         file = fopen(filename, "rt");
546         if (!file) return nullptr;
547
548         if (fgets(buffer, XV_BUFFER, file) != nullptr
549             && strncmp(buffer, "P7 332", 6) == 0)
550                 {
551                 gint width;
552                 gint height;
553                 gint depth;
554
555                 while (fgets(buffer, XV_BUFFER, file) && buffer[0] == '#') /* do_nothing() */;
556
557                 if (sscanf(buffer, "%d %d %d", &width, &height, &depth) == 3)
558                         {
559                         gsize size = width * height;
560
561                         data = g_new(guchar, size);
562                         if (data && fread(data, 1, size, file) == size)
563                                 {
564                                 *widthp = width;
565                                 *heightp = height;
566                                 }
567                         }
568                 }
569
570         fclose(file);
571         return data;
572 }
573 #undef XV_BUFFER
574
575 static void free_rgb_buffer(guchar *pixels, gpointer)
576 {
577         g_free(pixels);
578 }
579
580 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h)
581 {
582         gint width;
583         gint height;
584         gchar *thumb_name;
585         gchar *path;
586         gchar *directory;
587         gchar *name;
588         guchar *packed_data;
589
590         path = path_from_utf8(thumb_filename);
591         directory = g_path_get_dirname(path);
592         name = g_path_get_basename(path);
593
594         thumb_name = g_build_filename(directory, ".xvpics", name, NULL);
595
596         g_free(name);
597         g_free(directory);
598         g_free(path);
599
600         packed_data = load_xv_thumbnail(thumb_name, &width, &height);
601         g_free(thumb_name);
602
603         if (packed_data)
604                 {
605                 guchar *rgb_data;
606                 GdkPixbuf *pixbuf;
607                 gint i;
608
609                 rgb_data = g_new(guchar, width * height * 3);
610                 for (i = 0; i < width * height; i++)
611                         {
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;
615                         }
616                 g_free(packed_data);
617
618                 pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8,
619                                                   width, height, 3 * width, free_rgb_buffer, nullptr);
620
621                 if (pixbuf_scale_aspect(width, height, max_w, max_h, &width, &height))
622                         {
623                         /* scale */
624                         GdkPixbuf *tmp;
625
626                         tmp = pixbuf;
627                         pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_NEAREST);
628                         g_object_unref(tmp);
629                         }
630
631                 return pixbuf;
632                 }
633
634         return nullptr;
635 }
636 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */