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