Include debug.h from main.h since debug macros may be used anywhere in the code.
[geeqie.git] / src / thumb.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "thumb.h"
16
17 #include "cache.h"
18 #include "image-load.h"
19 #include "filedata.h"
20 #include "pixbuf_util.h"
21 #include "thumb_standard.h"
22 #include "ui_fileops.h"
23
24 #include <utime.h>
25
26
27 static void thumb_loader_error_cb(ImageLoader *il, gpointer data);
28 static void thumb_loader_setup(ThumbLoader *tl, gchar *path);
29
30 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h);
31 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h);
32
33
34 /*
35  *-----------------------------------------------------------------------------
36  * thumbnail routines: creation, caching, and maintenance (public)
37  *-----------------------------------------------------------------------------
38  */
39
40 static gint thumb_loader_save_to_cache(ThumbLoader *tl)
41 {
42         gchar *cache_dir;
43         gint success = FALSE;
44         mode_t mode = 0755;
45
46         if (!tl || !tl->pixbuf) return FALSE;
47
48         cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode);
49
50         if (cache_ensure_dir_exists(cache_dir, mode))
51                 {
52                 gchar *cache_path;
53                 gchar *pathl;
54
55                 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path),
56                                          GQ_CACHE_EXT_THUMB, NULL);
57
58                 DEBUG_1("Saving thumb: %s", cache_path);
59
60                 pathl = path_from_utf8(cache_path);
61                 success = pixbuf_to_file_as_png(tl->pixbuf, pathl);
62                 if (success)
63                         {
64                         struct utimbuf ut;
65                         /* set thumb time to that of source file */
66
67                         ut.actime = ut.modtime = filetime(tl->path);
68                         if (ut.modtime > 0)
69                                 {
70                                 utime(pathl, &ut);
71                                 }
72                         }
73                 else
74                         {
75                         DEBUG_1("Saving failed: %s", pathl);
76                         }
77
78                 g_free(pathl);
79                 g_free(cache_path);
80                 }
81
82         g_free(cache_dir);
83
84         return success;
85 }
86
87 static gint thumb_loader_mark_failure(ThumbLoader *tl)
88 {
89         gchar *cache_dir;
90         gint success = FALSE;
91         mode_t mode = 0755;
92
93         if (!tl) return FALSE;
94
95         cache_dir = cache_get_location(CACHE_TYPE_THUMB, tl->path, FALSE, &mode);
96
97         if (cache_ensure_dir_exists(cache_dir, mode))
98                 {
99                 gchar *cache_path;
100                 gchar *pathl;
101                 FILE *f;
102
103                 cache_path = g_strconcat(cache_dir, "/", filename_from_path(tl->path),
104                                          GQ_CACHE_EXT_THUMB, NULL);
105
106                 DEBUG_1("marking thumb failure: %s", cache_path);
107
108                 pathl = path_from_utf8(cache_path);
109                 f = fopen(pathl, "w");
110                 if (f)
111                         {
112                         struct utimbuf ut;
113
114                         fclose(f);
115
116                         ut.actime = ut.modtime = filetime(tl->path);
117                         if (ut.modtime > 0)
118                                 {
119                                 utime(pathl, &ut);
120                                 }
121
122                         success = TRUE;
123                         }
124
125                 g_free(pathl);
126                 g_free(cache_path);
127                 }
128
129         g_free(cache_dir);
130         return success;
131 }
132
133 static void thumb_loader_percent_cb(ImageLoader *il, gdouble percent, gpointer data)
134 {
135         ThumbLoader *tl = data;
136
137         tl->percent_done = percent;
138
139         if (tl->func_progress) tl->func_progress(tl, tl->data);
140 }
141
142 static void thumb_loader_done_cb(ImageLoader *il, gpointer data)
143 {
144         ThumbLoader *tl = data;
145         GdkPixbuf *pixbuf;
146         gint pw, ph;
147         gint save;
148
149         DEBUG_1("thumb done: %s", tl->path);
150
151         pixbuf = image_loader_get_pixbuf(tl->il);
152         if (!pixbuf)
153                 {
154                 DEBUG_1("...but no pixbuf: %s", tl->path);
155                 thumb_loader_error_cb(tl->il, tl);
156                 return;
157                 }
158
159         pw = gdk_pixbuf_get_width(pixbuf);
160         ph = gdk_pixbuf_get_height(pixbuf);
161
162         if (tl->cache_hit && pw != tl->max_w && ph != tl->max_h)
163                 {
164                 /* requested thumbnail size may have changed, load original */
165                 DEBUG_1("thumbnail size mismatch, regenerating: %s", tl->path);
166                 tl->cache_hit = FALSE;
167
168                 thumb_loader_setup(tl, tl->path);
169
170                 if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
171                         {
172                         image_loader_free(tl->il);
173                         tl->il = NULL;
174
175                         DEBUG_1("regeneration failure: %s", tl->path);
176                         thumb_loader_error_cb(tl->il, tl);
177                         }
178                 return;
179                 }
180
181         /* scale ?? */
182
183         if (pw > tl->max_w || ph > tl->max_h)
184                 {
185                 gint w, h;
186
187                 if (((double)tl->max_w / pw) < ((double)tl->max_h / ph))
188                         {
189                         w = tl->max_w;
190                         h = (double)w / pw * ph;
191                         if (h < 1) h = 1;
192                         }
193                 else
194                         {
195                         h = tl->max_h;
196                         w = (double)h / ph * pw;
197                         if (w < 1) w = 1;
198                         }
199
200                 tl->pixbuf = gdk_pixbuf_scale_simple(pixbuf, w, h, (GdkInterpType)options->thumbnails.quality);
201                 save = TRUE;
202                 }
203         else
204                 {
205                 tl->pixbuf = pixbuf;
206                 gdk_pixbuf_ref(tl->pixbuf);
207                 save = il->shrunk;
208                 }
209
210         /* save it ? */
211         if (tl->cache_enable && save)
212                 {
213                 thumb_loader_save_to_cache(tl);
214                 }
215
216         if (tl->func_done) tl->func_done(tl, tl->data);
217 }
218
219 static void thumb_loader_error_cb(ImageLoader *il, gpointer data)
220 {
221         ThumbLoader *tl = data;
222
223         /* if at least some of the image is available, go to done_cb */
224         if (image_loader_get_pixbuf(tl->il) != NULL)
225                 {
226                 thumb_loader_done_cb(il, data);
227                 return;
228                 }
229
230         DEBUG_1("thumb error: %s", tl->path);
231
232         image_loader_free(tl->il);
233         tl->il = NULL;
234
235         if (tl->func_error) tl->func_error(tl, tl->data);
236 }
237
238 static gint thumb_loader_done_delay_cb(gpointer data)
239 {
240         ThumbLoader *tl = data;
241
242         tl->idle_done_id = -1;
243
244         if (tl->func_done) tl->func_done(tl, tl->data);
245
246         return FALSE;
247 }
248
249 static void thumb_loader_delay_done(ThumbLoader *tl)
250 {
251         if (tl->idle_done_id == -1) tl->idle_done_id = g_idle_add(thumb_loader_done_delay_cb, tl);
252 }
253
254 static void thumb_loader_setup(ThumbLoader *tl, gchar *path)
255 {
256         image_loader_free(tl->il);
257         tl->il = image_loader_new(file_data_new_simple(path));
258
259         if (options->thumbnails.fast)
260                 {
261                 /* this will speed up jpegs by up to 3x in some cases */
262                 image_loader_set_requested_size(tl->il, tl->max_w, tl->max_h);
263                 }
264
265         image_loader_set_error_func(tl->il, thumb_loader_error_cb, tl);
266         if (tl->func_progress) image_loader_set_percent_func(tl->il, thumb_loader_percent_cb, tl);
267 }
268
269 void thumb_loader_set_callbacks(ThumbLoader *tl,
270                                 ThumbLoaderFunc func_done,
271                                 ThumbLoaderFunc func_error,
272                                 ThumbLoaderFunc func_progress,
273                                 gpointer data)
274 {
275         if (!tl) return;
276
277         if (tl->standard_loader)
278                 {
279                 thumb_loader_std_set_callbacks((ThumbLoaderStd *)tl,
280                                                (ThumbLoaderStdFunc) func_done,
281                                                (ThumbLoaderStdFunc) func_error,
282                                                (ThumbLoaderStdFunc) func_progress,
283                                                data);
284                 return;
285                 }
286
287         tl->func_done = func_done;
288         tl->func_error = func_error;
289         tl->func_progress = func_progress;
290
291         tl->data = data;
292 }
293
294 void thumb_loader_set_cache(ThumbLoader *tl, gint enable_cache, gint local, gint retry_failed)
295 {
296         if (!tl) return;
297
298         if (tl->standard_loader)
299                 {
300                 thumb_loader_std_set_cache((ThumbLoaderStd *)tl, enable_cache, local, retry_failed);
301                 return;
302                 }
303
304         tl->cache_enable = enable_cache;
305 #if 0
306         tl->cache_local = local;
307         tl->cache_retry = retry_failed;
308 #endif
309 }
310
311
312 gint thumb_loader_start(ThumbLoader *tl, const gchar *path)
313 {
314         gchar *cache_path = NULL;
315
316         if (!tl) return FALSE;
317
318         if (tl->standard_loader)
319                 {
320                 return thumb_loader_std_start((ThumbLoaderStd *)tl, path);
321                 }
322
323         if (!tl->path && !path) return FALSE;
324
325         if (!tl->path) tl->path = g_strdup(path);
326
327         if (tl->cache_enable)
328                 {
329                 cache_path = cache_find_location(CACHE_TYPE_THUMB, tl->path);
330
331                 if (cache_path)
332                         {
333                         if (cache_time_valid(cache_path, tl->path))
334                                 {
335                                 DEBUG_1("Found in cache:%s", tl->path);
336
337                                 if (filesize(cache_path) == 0)
338                                         {
339                                         DEBUG_1("Broken image mark found:%s", cache_path);
340                                         g_free(cache_path);
341                                         return FALSE;
342                                         }
343
344                                 DEBUG_1("Cache location:%s", cache_path);
345                                 }
346                         else
347                                 {
348                                 g_free(cache_path);
349                                 cache_path = NULL;
350                                 }
351                         }
352                 }
353
354         if (!cache_path && options->thumbnails.use_xvpics)
355                 {
356                 tl->pixbuf = get_xv_thumbnail(tl->path, tl->max_w, tl->max_h);
357                 if (tl->pixbuf)
358                         {
359                         thumb_loader_delay_done(tl);
360                         return TRUE;
361                         }
362                 }
363
364         if (cache_path)
365                 {
366                 thumb_loader_setup(tl, cache_path);
367                 g_free(cache_path);
368                 tl->cache_hit = TRUE;
369                 }
370         else
371                 {
372                 thumb_loader_setup(tl, tl->path);
373                 }
374
375         if (!image_loader_start(tl->il, thumb_loader_done_cb, tl))
376                 {
377                 /* try from original if cache attempt */
378                 if (tl->cache_hit)
379                         {
380                         tl->cache_hit = FALSE;
381                         print_term(_("Thumbnail image in cache failed to load, trying to recreate.\n"));
382
383                         thumb_loader_setup(tl, tl->path);
384                         if (image_loader_start(tl->il, thumb_loader_done_cb, tl)) return TRUE;
385                         }
386                 /* mark failed thumbnail in cache with 0 byte file */
387                 if (tl->cache_enable)
388                         {
389                         thumb_loader_mark_failure(tl);
390                         }
391
392                 image_loader_free(tl->il);
393                 tl->il = NULL;
394                 return FALSE;
395                 }
396
397         return TRUE;
398 }
399
400 #if 0
401 gint thumb_loader_to_pixmap(ThumbLoader *tl, GdkPixmap **pixmap, GdkBitmap **mask)
402 {
403         if (!tl || !tl->pixbuf) return -1;
404
405         gdk_pixbuf_render_pixmap_and_mask(tl->pixbuf, pixmap, mask, 128);
406
407         return thumb_loader_get_space(tl);
408 }
409 #endif
410
411 GdkPixbuf *thumb_loader_get_pixbuf(ThumbLoader *tl, gint with_fallback)
412 {
413         GdkPixbuf *pixbuf;
414
415         if (tl && tl->standard_loader)
416                 {
417                 return thumb_loader_std_get_pixbuf((ThumbLoaderStd *)tl, with_fallback);
418                 }
419
420         if (tl && tl->pixbuf)
421                 {
422                 pixbuf = tl->pixbuf;
423                 g_object_ref(pixbuf);
424                 }
425         else if (with_fallback)
426                 {
427                 gint w, h;
428
429                 pixbuf = pixbuf_inline(PIXBUF_INLINE_BROKEN);
430                 w = gdk_pixbuf_get_width(pixbuf);
431                 h = gdk_pixbuf_get_height(pixbuf);
432                 if ((w > tl->max_w || h > tl->max_h) &&
433                     normalize_thumb(&w, &h, tl->max_w, tl->max_h))
434                         {
435                         GdkPixbuf *tmp;
436
437                         tmp = pixbuf;
438                         pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
439                         gdk_pixbuf_unref(tmp);
440                         }
441                 }
442         else
443                 {
444                 pixbuf = NULL;
445                 }
446
447         return pixbuf;
448 }
449
450 #if 0
451 gint thumb_loader_get_space(ThumbLoader *tl)
452 {
453         if (!tl) return 0;
454
455         if (tl->pixbuf) return (tl->max_w - gdk_pixbuf_get_width(tl->pixbuf));
456
457         return tl->max_w;
458 }
459 #endif
460
461 ThumbLoader *thumb_loader_new(gint width, gint height)
462 {
463         ThumbLoader *tl;
464
465         if (options->thumbnails.spec_standard)
466                 {
467                 return (ThumbLoader *)thumb_loader_std_new(width, height);
468                 }
469
470         tl = g_new0(ThumbLoader, 1);
471         tl->standard_loader = FALSE;
472         tl->path = NULL;
473         tl->cache_enable = options->thumbnails.enable_caching;
474         tl->cache_hit = FALSE;
475         tl->percent_done = 0.0;
476         tl->max_w = width;
477         tl->max_h = height;
478
479         tl->il = NULL;
480
481         tl->idle_done_id = -1;
482
483         return tl;
484 }
485
486 void thumb_loader_free(ThumbLoader *tl)
487 {
488         if (!tl) return;
489
490         if (tl->standard_loader)
491                 {
492                 thumb_loader_std_free((ThumbLoaderStd *)tl);
493                 return;
494                 }
495
496         if (tl->pixbuf) gdk_pixbuf_unref(tl->pixbuf);
497         image_loader_free(tl->il);
498         g_free(tl->path);
499
500         if (tl->idle_done_id != -1) g_source_remove(tl->idle_done_id);
501
502         g_free(tl);
503 }
504
505 #if 0
506 gint thumb_from_xpm_d(const char **data, gint max_w, gint max_h, GdkPixmap **pixmap, GdkBitmap **mask)
507 {
508         GdkPixbuf *pixbuf;
509         gint w, h;
510
511         pixbuf = gdk_pixbuf_new_from_xpm_data(data);
512         w = gdk_pixbuf_get_width(pixbuf);
513         h = gdk_pixbuf_get_height(pixbuf);
514
515         if ((w > max_w || h > max_h) &&
516             normalize_thumb(&w, &h, max_w, max_h))
517                 {
518                 /* scale */
519                 GdkPixbuf *tmp;
520
521                 tmp = pixbuf;
522                 pixbuf = gdk_pixbuf_scale_simple(tmp, w, h, GDK_INTERP_NEAREST);
523                 gdk_pixbuf_unref(tmp);
524                 }
525
526         gdk_pixbuf_render_pixmap_and_mask(pixbuf, pixmap, mask, 128);
527         gdk_pixbuf_unref(pixbuf);
528
529         return w;
530 }
531 #endif
532
533 /*
534  *-----------------------------------------------------------------------------
535  * xvpics thumbnail support, read-only (private)
536  *-----------------------------------------------------------------------------
537  */
538
539 /*
540  * xvpics code originally supplied by:
541  * "Diederen Damien" <D.Diederen@student.ulg.ac.be>
542  *
543  * Note: Code has been modified to fit the style of the other code, and to use
544  *       a few more glib-isms.
545  * 08-28-2000: Updated to return a gdk_pixbuf, Imlib is dieing a death here.
546  */
547
548 #define XV_BUFFER 2048
549 static guchar *load_xv_thumbnail(gchar *filename, gint *widthp, gint *heightp)
550 {
551         FILE *file;
552         gchar buffer[XV_BUFFER];
553         guchar *data;
554         gint width, height, depth;
555
556         file = fopen(filename, "rt");
557         if(!file) return NULL;
558
559         fgets(buffer, XV_BUFFER, file);
560         if(strncmp(buffer, "P7 332", 6) != 0)
561                 {
562                 fclose(file);
563                 return NULL;
564                 }
565
566         while (fgets(buffer, XV_BUFFER, file) && buffer[0] == '#') /* do_nothing() */;
567
568         if(sscanf(buffer, "%d %d %d", &width, &height, &depth) != 3)
569                 {
570                 fclose(file);
571                 return NULL;
572                 }
573
574         data = g_new(guchar, width * height);
575         fread(data, 1, width * height, file);
576
577         fclose(file);
578         *widthp = width;
579         *heightp = height;
580         return data;
581 }
582 #undef XV_BUFFER
583
584 static gint normalize_thumb(gint *width, gint *height, gint max_w, gint max_h)
585 {
586         gdouble scale;
587         gint new_w, new_h;
588
589         scale = MIN((gdouble) max_w / *width, (gdouble) max_h / *height);
590         new_w = *width * scale;
591         new_h = *height * scale;
592
593         if (new_w != *width || new_h != *height)
594                 {
595                 *width = new_w;
596                 *height = new_h;
597                 return TRUE;
598                 }
599
600         return FALSE;
601 }
602
603 static void free_rgb_buffer(guchar *pixels, gpointer data)
604 {
605         g_free(pixels);
606 }
607
608 static GdkPixbuf *get_xv_thumbnail(gchar *thumb_filename, gint max_w, gint max_h)
609 {
610         gint width, height;
611         gchar *thumb_name;
612         gchar *tmp_string;
613         gchar *last_slash;
614         guchar *packed_data;
615
616         tmp_string = path_from_utf8(thumb_filename);
617         last_slash = strrchr(tmp_string, '/');
618         if(!last_slash) return NULL;
619         *last_slash++ = '\0';
620
621         thumb_name = g_strconcat(tmp_string, "/.xvpics/", last_slash, NULL);
622         packed_data = load_xv_thumbnail(thumb_name, &width, &height);
623         g_free(tmp_string);
624         g_free(thumb_name);
625
626         if(packed_data)
627                 {
628                 guchar *rgb_data;
629                 GdkPixbuf *pixbuf;
630                 gint i;
631
632                 rgb_data = g_new(guchar, width * height * 3);
633                 for (i = 0; i < width * height; i++)
634                         {
635                         rgb_data[i * 3 + 0] = (packed_data[i] >> 5) * 36;
636                         rgb_data[i * 3 + 1] = ((packed_data[i] & 28) >> 2) * 36;
637                         rgb_data[i * 3 + 2] = (packed_data[i] & 3) * 85;
638                         }
639                 g_free(packed_data);
640
641                 pixbuf = gdk_pixbuf_new_from_data(rgb_data, GDK_COLORSPACE_RGB, FALSE, 8,
642                                                   width, height, 3 * width, free_rgb_buffer, NULL);
643
644                 if (normalize_thumb(&width, &height, max_w, max_h))
645                         {
646                         /* scale */
647                         GdkPixbuf *tmp;
648
649                         tmp = pixbuf;
650                         pixbuf = gdk_pixbuf_scale_simple(tmp, width, height, GDK_INTERP_NEAREST);
651                         gdk_pixbuf_unref(tmp);
652                         }
653
654                 return pixbuf;
655                 }
656
657         return NULL;
658 }