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