Adding mailmap file to unify commiter
[geeqie.git] / src / image-load.c
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 "main.h"
23 #include "image-load.h"
24 #include "image_load_gdk.h"
25 #include "image_load_jpeg.h"
26 #include "image_load_tiff.h"
27
28 #include "exif.h"
29 #include "filedata.h"
30 #include "ui_fileops.h"
31 #include "gq-marshal.h"
32
33 #include <fcntl.h>
34 #include <sys/mman.h>
35
36 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT   4096
37 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT       1
38
39
40 /**************************************************************************************/
41 /* image loader class */
42
43
44 enum {
45         SIGNAL_AREA_READY = 0,
46         SIGNAL_ERROR,
47         SIGNAL_DONE,
48         SIGNAL_PERCENT,
49         SIGNAL_SIZE,
50         SIGNAL_COUNT
51 };
52
53 static guint signals[SIGNAL_COUNT] = { 0 };
54
55 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
56 static void image_loader_class_init(ImageLoaderClass *class);
57 static void image_loader_finalize(GObject *object);
58 static void image_loader_stop(ImageLoader *il);
59
60 GType image_loader_get_type(void)
61 {
62         static GType type = 0;
63         if (type == 0)
64                 {
65                 static const GTypeInfo info = {
66                         sizeof(ImageLoaderClass),
67                         NULL,   /* base_init */
68                         NULL,   /* base_finalize */
69                         (GClassInitFunc)image_loader_class_init, /* class_init */
70                         NULL,   /* class_finalize */
71                         NULL,   /* class_data */
72                         sizeof(ImageLoader),
73                         0,      /* n_preallocs */
74                         (GInstanceInitFunc)image_loader_init, /* instance_init */
75                         NULL    /* value_table */
76                         };
77                 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
78                 }
79         return type;
80 }
81
82 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
83 {
84         ImageLoader *il = (ImageLoader *)instance;
85
86         il->pixbuf = NULL;
87         il->idle_id = 0;
88         il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
89         il->done = FALSE;
90         il->loader = NULL;
91
92         il->bytes_read = 0;
93         il->bytes_total = 0;
94
95         il->idle_done_id = 0;
96
97         il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
98         il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
99         il->mapped_file = NULL;
100
101         il->requested_width = 0;
102         il->requested_height = 0;
103         il->actual_width = 0;
104         il->actual_height = 0;
105         il->shrunk = FALSE;
106
107         il->can_destroy = TRUE;
108
109 #ifdef HAVE_GTHREAD
110 #if GLIB_CHECK_VERSION(2,32,0)
111         il->data_mutex = g_new(GMutex, 1);
112         g_mutex_init(il->data_mutex);
113         il->can_destroy_cond = g_new(GCond, 1);
114         g_cond_init(il->can_destroy_cond);
115 #else
116         il->data_mutex = g_mutex_new();
117         il->can_destroy_cond = g_cond_new();
118 #endif
119 #endif
120         DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
121 }
122
123 static void image_loader_class_init(ImageLoaderClass *class)
124 {
125         GObjectClass *gobject_class = G_OBJECT_CLASS (class);
126
127 //      gobject_class->set_property = image_loader_set_property;
128 //      gobject_class->get_property = image_loader_get_property;
129
130         gobject_class->finalize = image_loader_finalize;
131
132
133         signals[SIGNAL_AREA_READY] =
134                 g_signal_new("area_ready",
135                              G_OBJECT_CLASS_TYPE(gobject_class),
136                              G_SIGNAL_RUN_LAST,
137                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
138                              NULL, NULL,
139                              gq_marshal_VOID__INT_INT_INT_INT,
140                              G_TYPE_NONE, 4,
141                              G_TYPE_INT,
142                              G_TYPE_INT,
143                              G_TYPE_INT,
144                              G_TYPE_INT);
145
146         signals[SIGNAL_ERROR] =
147                 g_signal_new("error",
148                              G_OBJECT_CLASS_TYPE(gobject_class),
149                              G_SIGNAL_RUN_LAST,
150                              G_STRUCT_OFFSET(ImageLoaderClass, error),
151                              NULL, NULL,
152                              g_cclosure_marshal_VOID__VOID,
153                              G_TYPE_NONE, 0);
154
155         signals[SIGNAL_DONE] =
156                 g_signal_new("done",
157                              G_OBJECT_CLASS_TYPE(gobject_class),
158                              G_SIGNAL_RUN_LAST,
159                              G_STRUCT_OFFSET(ImageLoaderClass, done),
160                              NULL, NULL,
161                              g_cclosure_marshal_VOID__VOID,
162                              G_TYPE_NONE, 0);
163
164         signals[SIGNAL_PERCENT] =
165                 g_signal_new("percent",
166                              G_OBJECT_CLASS_TYPE(gobject_class),
167                              G_SIGNAL_RUN_LAST,
168                              G_STRUCT_OFFSET(ImageLoaderClass, percent),
169                              NULL, NULL,
170                              g_cclosure_marshal_VOID__DOUBLE,
171                              G_TYPE_NONE, 1,
172                              G_TYPE_DOUBLE);
173
174         signals[SIGNAL_SIZE] =
175                 g_signal_new("size_prepared",
176                              G_OBJECT_CLASS_TYPE(gobject_class),
177                              G_SIGNAL_RUN_LAST,
178                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
179                              NULL, NULL,
180                              gq_marshal_VOID__INT_INT,
181                              G_TYPE_NONE, 2,
182                              G_TYPE_INT,
183                              G_TYPE_INT);
184
185 }
186
187 static void image_loader_finalize(GObject *object)
188 {
189         ImageLoader *il = (ImageLoader *)object;
190
191         image_loader_stop(il);
192
193         if (il->error) DEBUG_1("%s", image_loader_get_error(il));
194
195         DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
196
197         if (il->idle_done_id)
198                 {
199                 g_source_remove(il->idle_done_id);
200                 il->idle_done_id = 0;
201                 }
202
203         while (g_source_remove_by_user_data(il))
204                 {
205                 DEBUG_2("pending signals detected");
206                 }
207
208         while (il->area_param_list)
209                 {
210                 DEBUG_1("pending area_ready signals detected");
211                 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
212                 g_free(il->area_param_list->data);
213                 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
214                 }
215
216         while (il->area_param_delayed_list)
217                 {
218                 g_free(il->area_param_delayed_list->data);
219                 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
220                 }
221
222         if (il->pixbuf) g_object_unref(il->pixbuf);
223
224         if (il->error) g_error_free(il->error);
225
226         file_data_unref(il->fd);
227 #ifdef HAVE_GTHREAD
228 #if GLIB_CHECK_VERSION(2,32,0)
229         g_mutex_clear(il->data_mutex);
230         g_free(il->data_mutex);
231         g_cond_clear(il->can_destroy_cond);
232         g_free(il->can_destroy_cond);
233 #else
234         g_mutex_free(il->data_mutex);
235         g_cond_free(il->can_destroy_cond);
236 #endif
237 #endif
238 }
239
240 void image_loader_free(ImageLoader *il)
241 {
242         if (!il) return;
243         g_object_unref(G_OBJECT(il));
244 }
245
246
247 ImageLoader *image_loader_new(FileData *fd)
248 {
249         ImageLoader *il;
250
251         if (!fd) return NULL;
252
253         il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
254
255         il->fd = file_data_ref(fd);
256
257         return il;
258 }
259
260 /**************************************************************************************/
261 /* send signals via idle calbacks - the callback are executed in the main thread */
262
263 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
264 struct _ImageLoaderAreaParam {
265         ImageLoader *il;
266         guint x;
267         guint y;
268         guint w;
269         guint h;
270 };
271
272
273 static gboolean image_loader_emit_area_ready_cb(gpointer data)
274 {
275         ImageLoaderAreaParam *par = data;
276         ImageLoader *il = par->il;
277         guint x, y, w, h;
278         g_mutex_lock(il->data_mutex);
279         il->area_param_list = g_list_remove(il->area_param_list, par);
280         x = par->x;
281         y = par->y;
282         w = par->w;
283         h = par->h;
284         g_free(par);
285         g_mutex_unlock(il->data_mutex);
286
287         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
288
289         return FALSE;
290 }
291
292 static gboolean image_loader_emit_done_cb(gpointer data)
293 {
294         ImageLoader *il = data;
295         g_signal_emit(il, signals[SIGNAL_DONE], 0);
296         return FALSE;
297 }
298
299 static gboolean image_loader_emit_error_cb(gpointer data)
300 {
301         ImageLoader *il = data;
302         g_signal_emit(il, signals[SIGNAL_ERROR], 0);
303         return FALSE;
304 }
305
306 static gboolean image_loader_emit_percent_cb(gpointer data)
307 {
308         ImageLoader *il = data;
309         g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
310         return FALSE;
311 }
312
313 static gboolean image_loader_emit_size_cb(gpointer data)
314 {
315         gint width, height;
316         ImageLoader *il = data;
317         g_mutex_lock(il->data_mutex);
318         width = il->actual_width;
319         height = il->actual_height;
320         g_mutex_unlock(il->data_mutex);
321         g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
322         return FALSE;
323 }
324
325
326 /* DONE and ERROR are emited only once, thus they can have normal priority
327    PERCENT and AREA_READY should be processed ASAP
328 */
329
330 static void image_loader_emit_done(ImageLoader *il)
331 {
332         g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
333 }
334
335 static void image_loader_emit_error(ImageLoader *il)
336 {
337         g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
338 }
339
340 static void image_loader_emit_percent(ImageLoader *il)
341 {
342         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
343 }
344
345 static void image_loader_emit_size(ImageLoader *il)
346 {
347         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
348 }
349
350 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
351 {
352         if (*list)
353                 {
354                 ImageLoaderAreaParam *prev_par = (*list)->data;
355                 if (prev_par->x == x && prev_par->w == w &&
356                     prev_par->y + prev_par->h == y)
357                         {
358                         /* we can merge the notifications */
359                         prev_par->h += h;
360                         return NULL;
361                         }
362                 if (prev_par->x == x && prev_par->w == w &&
363                     y + h == prev_par->y)
364                         {
365                         /* we can merge the notifications */
366                         prev_par->h += h;
367                         prev_par->y = y;
368                         return NULL;
369                         }
370                 if (prev_par->y == y && prev_par->h == h &&
371                     prev_par->x + prev_par->w == x)
372                         {
373                         /* we can merge the notifications */
374                         prev_par->w += w;
375                         return NULL;
376                         }
377                 if (prev_par->y == y && prev_par->h == h &&
378                     x + w == prev_par->x)
379                         {
380                         /* we can merge the notifications */
381                         prev_par->w += w;
382                         prev_par->x = x;
383                         return NULL;
384                         }
385                 }
386
387         ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
388         par->il = il;
389         par->x = x;
390         par->y = y;
391         par->w = w;
392         par->h = h;
393
394         *list = g_list_prepend(*list, par);
395         return par;
396 }
397
398 /* this function expects that il->data_mutex is locked by caller */
399 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
400 {
401         ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
402
403         if (par)
404                 {
405                 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
406                 }
407 }
408
409 /**************************************************************************************/
410 /* the following functions may be executed in separate thread */
411
412 /* this function expects that il->data_mutex is locked by caller */
413 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
414 {
415         image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
416 }
417
418
419
420 static gboolean image_loader_get_stopping(ImageLoader *il)
421 {
422         gboolean ret;
423         if (!il) return FALSE;
424
425         g_mutex_lock(il->data_mutex);
426         ret = il->stopping;
427         g_mutex_unlock(il->data_mutex);
428
429         return ret;
430 }
431
432
433 static void image_loader_sync_pixbuf(ImageLoader *il)
434 {
435         GdkPixbuf *pb;
436
437         g_mutex_lock(il->data_mutex);
438
439         if (!il->loader)
440                 {
441                 g_mutex_unlock(il->data_mutex);
442                 return;
443                 }
444
445         pb = il->backend.get_pixbuf(il->loader);
446
447         if (pb == il->pixbuf)
448                 {
449                 g_mutex_unlock(il->data_mutex);
450                 return;
451                 }
452
453         if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
454                 {
455                 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
456                 }
457
458         if (il->pixbuf) g_object_unref(il->pixbuf);
459
460         il->pixbuf = pb;
461         if (il->pixbuf) g_object_ref(il->pixbuf);
462
463         g_mutex_unlock(il->data_mutex);
464 }
465
466 static void image_loader_area_updated_cb(gpointer loader,
467                                  guint x, guint y, guint w, guint h,
468                                  gpointer data)
469 {
470         ImageLoader *il = data;
471
472         if (!image_loader_get_pixbuf(il))
473                 {
474                 image_loader_sync_pixbuf(il);
475                 if (!image_loader_get_pixbuf(il))
476                         {
477                         log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
478                         }
479                 }
480
481         g_mutex_lock(il->data_mutex);
482         if (il->delay_area_ready)
483                 image_loader_queue_delayed_area_ready(il, x, y, w, h);
484         else
485                 image_loader_emit_area_ready(il, x, y, w, h);
486
487         if (il->stopping) il->backend.abort(il->loader);
488
489         g_mutex_unlock(il->data_mutex);
490 }
491
492 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
493 {
494         ImageLoader *il = data;
495         GdkPixbuf *pb;
496         guchar *pix;
497         size_t h, rs;
498
499         /* a workaround for
500            http://bugzilla.gnome.org/show_bug.cgi?id=547669
501            http://bugzilla.gnome.org/show_bug.cgi?id=589334
502         */
503         gchar *format = il->backend.get_format_name(loader);
504         if (strcmp(format, "svg") == 0 ||
505             strcmp(format, "xpm") == 0)
506                 {
507                 g_free(format);
508                 return;
509                 }
510
511         g_free(format);
512
513         pb = il->backend.get_pixbuf(loader);
514
515         h = gdk_pixbuf_get_height(pb);
516         rs = gdk_pixbuf_get_rowstride(pb);
517         pix = gdk_pixbuf_get_pixels(pb);
518
519         memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
520
521 }
522
523 static void image_loader_size_cb(gpointer loader,
524                                  gint width, gint height, gpointer data)
525 {
526         ImageLoader *il = data;
527         gchar **mime_types;
528         gboolean scale = FALSE;
529         gint n;
530
531         g_mutex_lock(il->data_mutex);
532         il->actual_width = width;
533         il->actual_height = height;
534         if (il->requested_width < 1 || il->requested_height < 1)
535                 {
536                 g_mutex_unlock(il->data_mutex);
537                 image_loader_emit_size(il);
538                 return;
539                 }
540         g_mutex_unlock(il->data_mutex);
541
542         mime_types = il->backend.get_format_mime_types(loader);
543         n = 0;
544         while (mime_types[n])
545                 {
546                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
547                 n++;
548                 }
549         g_strfreev(mime_types);
550
551         if (!scale)
552                 {
553                 image_loader_emit_size(il);
554                 return;
555                 }
556
557         g_mutex_lock(il->data_mutex);
558
559         gint nw, nh;
560         if (width > il->requested_width || height > il->requested_height)
561                 {
562
563                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
564                         {
565                         nw = il->requested_width;
566                         nh = (gdouble)nw / width * height;
567                         if (nh < 1) nh = 1;
568                         }
569                 else
570                         {
571                         nh = il->requested_height;
572                         nw = (gdouble)nh / height * width;
573                         if (nw < 1) nw = 1;
574                         }
575
576                 il->actual_width = nw;
577                 il->actual_height = nh;
578                 il->backend.set_size(loader, nw, nh);
579                 il->shrunk = TRUE;
580                 }
581
582         g_mutex_unlock(il->data_mutex);
583         image_loader_emit_size(il);
584 }
585
586 static void image_loader_stop_loader(ImageLoader *il)
587 {
588         if (!il) return;
589
590         if (il->loader)
591                 {
592                 /* some loaders do not have a pixbuf till close, order is important here */
593                 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
594                 image_loader_sync_pixbuf(il);
595                 il->backend.free(il->loader);
596                 il->loader = NULL;
597                 }
598         g_mutex_lock(il->data_mutex);
599         il->done = TRUE;
600         g_mutex_unlock(il->data_mutex);
601 }
602
603 static void image_loader_setup_loader(ImageLoader *il)
604 {
605         g_mutex_lock(il->data_mutex);
606 #ifdef HAVE_JPEG
607         if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
608                 {
609                 DEBUG_1("Using custom jpeg loader");
610                 image_loader_backend_set_jpeg(&il->backend);
611                 }
612         else
613 #endif
614 #ifdef HAVE_TIFF
615         if (il->bytes_total >= 10 &&
616             (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
617              memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
618              memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
619              memcmp(il->mapped_file, "II*\0", 4) == 0))
620                 {
621                 DEBUG_1("Using custom tiff loader");
622                 image_loader_backend_set_tiff(&il->backend);
623                 }
624         else
625 #endif
626                 image_loader_backend_set_default(&il->backend);
627
628         il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
629         g_mutex_unlock(il->data_mutex);
630 }
631
632
633 static void image_loader_done(ImageLoader *il)
634 {
635         image_loader_stop_loader(il);
636
637         image_loader_emit_done(il);
638 }
639
640 static void image_loader_error(ImageLoader *il)
641 {
642         image_loader_stop_loader(il);
643
644         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
645
646         image_loader_emit_error(il);
647 }
648
649 static gboolean image_loader_continue(ImageLoader *il)
650 {
651         gint b;
652         gint c;
653
654         if (!il) return FALSE;
655
656         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
657         while (c > 0 && !image_loader_get_stopping(il))
658                 {
659                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
660
661                 if (b == 0)
662                         {
663                         image_loader_done(il);
664                         return FALSE;
665                         }
666
667                 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
668                         {
669                         image_loader_error(il);
670                         return FALSE;
671                         }
672
673                 il->bytes_read += b;
674
675                 c--;
676                 }
677
678         if (il->bytes_total > 0)
679                 {
680                 image_loader_emit_percent(il);
681                 }
682
683         return TRUE;
684 }
685
686 static gboolean image_loader_begin(ImageLoader *il)
687 {
688         gssize b;
689
690         if (il->pixbuf) return FALSE;
691
692         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
693         if (b < 1) return FALSE;
694
695         image_loader_setup_loader(il);
696
697         g_assert(il->bytes_read == 0);
698         if (il->backend.load) {
699                 b = il->bytes_total;
700                 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
701                         {
702                         image_loader_stop_loader(il);
703                         return FALSE;
704                         }
705         }
706         else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
707                 {
708                 image_loader_stop_loader(il);
709                 return FALSE;
710                 }
711
712         il->bytes_read += b;
713
714         /* read until size is known */
715         while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
716                 {
717                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
718                 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
719                         {
720                         image_loader_stop_loader(il);
721                         return FALSE;
722                         }
723                 il->bytes_read += b;
724                 }
725         if (!il->pixbuf) image_loader_sync_pixbuf(il);
726
727         if (il->bytes_read == il->bytes_total || b < 1)
728                 {
729                 /* done, handle (broken) loaders that do not have pixbuf till close */
730                 image_loader_stop_loader(il);
731
732                 if (!il->pixbuf) return FALSE;
733
734                 image_loader_done(il);
735                 return TRUE;
736                 }
737
738         if (!il->pixbuf)
739                 {
740                 image_loader_stop_loader(il);
741                 return FALSE;
742                 }
743
744         return TRUE;
745 }
746
747 /**************************************************************************************/
748 /* the following functions are always executed in the main thread */
749
750
751 static gboolean image_loader_setup_source(ImageLoader *il)
752 {
753         struct stat st;
754         gchar *pathl;
755
756         if (!il || il->loader || il->mapped_file) return FALSE;
757
758         il->mapped_file = NULL;
759
760         if (il->fd)
761                 {
762                 ExifData *exif = exif_read_fd(il->fd);
763
764                 if (options->thumbnails.use_exif)
765                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
766                 else
767                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
768
769                 if (il->mapped_file)
770                         {
771                         il->preview = TRUE;
772                         DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
773                         }
774                 exif_free_fd(il->fd, exif);
775                 }
776
777
778         if (!il->mapped_file)
779                 {
780                 /* normal file */
781                 gint load_fd;
782
783                 pathl = path_from_utf8(il->fd->path);
784                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
785                 g_free(pathl);
786                 if (load_fd == -1) return FALSE;
787
788                 if (fstat(load_fd, &st) == 0)
789                         {
790                         il->bytes_total = st.st_size;
791                         }
792                 else
793                         {
794                         close(load_fd);
795                         return FALSE;
796                         }
797
798                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
799                 close(load_fd);
800                 if (il->mapped_file == MAP_FAILED)
801                         {
802                         il->mapped_file = 0;
803                         return FALSE;
804                         }
805                 il->preview = FALSE;
806                 }
807
808         return TRUE;
809 }
810
811 static void image_loader_stop_source(ImageLoader *il)
812 {
813         if (!il) return;
814
815         if (il->mapped_file)
816                 {
817                 if (il->preview)
818                         {
819                         exif_free_preview(il->mapped_file);
820                         }
821                 else
822                         {
823                         munmap(il->mapped_file, il->bytes_total);
824                         }
825                 il->mapped_file = NULL;
826                 }
827 }
828
829 static void image_loader_stop(ImageLoader *il)
830 {
831         if (!il) return;
832
833         if (il->idle_id)
834                 {
835                 g_source_remove(il->idle_id);
836                 il->idle_id = 0;
837                 }
838
839         if (il->thread)
840                 {
841                 /* stop loader in the other thread */
842                 g_mutex_lock(il->data_mutex);
843                 il->stopping = TRUE;
844                 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
845                 g_mutex_unlock(il->data_mutex);
846                 }
847
848         image_loader_stop_loader(il);
849         image_loader_stop_source(il);
850
851 }
852
853 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
854 {
855         g_mutex_lock(il->data_mutex);
856         il->delay_area_ready = enable;
857         if (!enable)
858                 {
859                 /* send delayed */
860                 GList *list, *work;
861                 list = g_list_reverse(il->area_param_delayed_list);
862                 il->area_param_delayed_list = NULL;
863                 g_mutex_unlock(il->data_mutex);
864
865                 work = list;
866
867                 while (work)
868                         {
869                         ImageLoaderAreaParam *par = work->data;
870                         work = work->next;
871
872                         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
873                         g_free(par);
874                         }
875                 g_list_free(list);
876                 }
877         else
878                 {
879                 /* just unlock */
880                 g_mutex_unlock(il->data_mutex);
881                 }
882 }
883
884
885 /**************************************************************************************/
886 /* execution via idle calls */
887
888 static gboolean image_loader_idle_cb(gpointer data)
889 {
890         gboolean ret = FALSE;
891         ImageLoader *il = data;
892
893         if (il->idle_id)
894                 {
895                 ret = image_loader_continue(il);
896                 }
897
898         if (!ret)
899                 {
900                 image_loader_stop_source(il);
901                 }
902
903         return ret;
904 }
905
906
907 static gboolean image_loader_start_idle(ImageLoader *il)
908 {
909         gboolean ret;
910
911         if (!il) return FALSE;
912
913         if (!il->fd) return FALSE;
914
915         if (!image_loader_setup_source(il)) return FALSE;
916
917         ret = image_loader_begin(il);
918
919         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
920         return ret;
921 }
922
923 /**************************************************************************************/
924 /* execution via thread */
925
926 #ifdef HAVE_GTHREAD
927 static GThreadPool *image_loader_thread_pool = NULL;
928
929 static GCond *image_loader_prio_cond = NULL;
930 static GMutex *image_loader_prio_mutex = NULL;
931 static gint image_loader_prio_num = 0;
932
933
934 static void image_loader_thread_enter_high(void)
935 {
936         g_mutex_lock(image_loader_prio_mutex);
937         image_loader_prio_num++;
938         g_mutex_unlock(image_loader_prio_mutex);
939 }
940
941 static void image_loader_thread_leave_high(void)
942 {
943         g_mutex_lock(image_loader_prio_mutex);
944         image_loader_prio_num--;
945         if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
946         g_mutex_unlock(image_loader_prio_mutex);
947 }
948
949 static void image_loader_thread_wait_high(void)
950 {
951         g_mutex_lock(image_loader_prio_mutex);
952         while (image_loader_prio_num)
953                 {
954                 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
955                 }
956
957         g_mutex_unlock(image_loader_prio_mutex);
958 }
959
960
961 static void image_loader_thread_run(gpointer data, gpointer user_data)
962 {
963         ImageLoader *il = data;
964         gboolean cont;
965         gboolean err;
966
967         if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
968                 {
969                 /* low prio, wait untill high prio tasks finishes */
970                 image_loader_thread_wait_high();
971                 }
972         else
973                 {
974                 /* high prio */
975                 image_loader_thread_enter_high();
976                 }
977
978         err = !image_loader_begin(il);
979
980         if (err)
981                 {
982                 /*
983                 loader failed, we have to send signal
984                 (idle mode returns the image_loader_begin return value directly)
985                 (success is always reported indirectly from image_loader_begin)
986                 */
987                 image_loader_emit_error(il);
988                 }
989
990         cont = !err;
991
992         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
993                 {
994                 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
995                         {
996                         /* low prio, wait untill high prio tasks finishes */
997                         image_loader_thread_wait_high();
998                         }
999                 cont = image_loader_continue(il);
1000                 }
1001         image_loader_stop_loader(il);
1002
1003         if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1004                 {
1005                 /* high prio */
1006                 image_loader_thread_leave_high();
1007                 }
1008
1009         g_mutex_lock(il->data_mutex);
1010         il->can_destroy = TRUE;
1011         g_cond_signal(il->can_destroy_cond);
1012         g_mutex_unlock(il->data_mutex);
1013
1014 }
1015
1016
1017 static gboolean image_loader_start_thread(ImageLoader *il)
1018 {
1019         if (!il) return FALSE;
1020
1021         if (!il->fd) return FALSE;
1022
1023         il->thread = TRUE;
1024
1025         if (!image_loader_setup_source(il)) return FALSE;
1026
1027         if (!image_loader_thread_pool)
1028                 {
1029                 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1030 #if GLIB_CHECK_VERSION(2,32,0)
1031                 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1032                 g_cond_init(image_loader_prio_cond);
1033                 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1034                 g_mutex_init(image_loader_prio_mutex);
1035 #else
1036                 image_loader_prio_cond = g_cond_new();
1037                 image_loader_prio_mutex = g_mutex_new();
1038 #endif
1039                 }
1040
1041         il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1042
1043         g_thread_pool_push(image_loader_thread_pool, il, NULL);
1044         DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1045
1046         return TRUE;
1047 }
1048 #endif /* HAVE_GTHREAD */
1049
1050
1051 /**************************************************************************************/
1052 /* public interface */
1053
1054
1055 gboolean image_loader_start(ImageLoader *il)
1056 {
1057         if (!il) return FALSE;
1058
1059         if (!il->fd) return FALSE;
1060
1061 #ifdef HAVE_GTHREAD
1062         return image_loader_start_thread(il);
1063 #else
1064         return image_loader_start_idle(il);
1065 #endif
1066 }
1067
1068
1069 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1070 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1071 {
1072         GdkPixbuf *ret;
1073         if (!il) return NULL;
1074
1075         g_mutex_lock(il->data_mutex);
1076         ret = il->pixbuf;
1077         g_mutex_unlock(il->data_mutex);
1078         return ret;
1079 }
1080
1081 gchar *image_loader_get_format(ImageLoader *il)
1082 {
1083         gchar **mimev;
1084         gchar *mime;
1085
1086         if (!il || !il->loader) return NULL;
1087
1088         mimev = il->backend.get_format_mime_types(il->loader);
1089         if (!mimev) return NULL;
1090
1091         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
1092         mime = g_strdup(mimev[0]);
1093         g_strfreev(mimev);
1094
1095         return mime;
1096 }
1097
1098 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1099 {
1100         if (!il) return;
1101
1102         g_mutex_lock(il->data_mutex);
1103         il->requested_width = width;
1104         il->requested_height = height;
1105         g_mutex_unlock(il->data_mutex);
1106 }
1107
1108 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1109 {
1110         if (!il) return;
1111
1112         g_mutex_lock(il->data_mutex);
1113         il->idle_read_loop_count = count ? count : 1;
1114         g_mutex_unlock(il->data_mutex);
1115 }
1116
1117 void image_loader_set_priority(ImageLoader *il, gint priority)
1118 {
1119         if (!il) return;
1120
1121         if (il->thread) return; /* can't change prio if the thread already runs */
1122         il->idle_priority = priority;
1123 }
1124
1125
1126 gdouble image_loader_get_percent(ImageLoader *il)
1127 {
1128         gdouble ret;
1129         if (!il) return 0.0;
1130
1131         g_mutex_lock(il->data_mutex);
1132         if (il->bytes_total == 0)
1133                 {
1134                 ret = 0.0;
1135                 }
1136         else
1137                 {
1138                 ret = (gdouble)il->bytes_read / il->bytes_total;
1139                 }
1140         g_mutex_unlock(il->data_mutex);
1141         return ret;
1142 }
1143
1144 gboolean image_loader_get_is_done(ImageLoader *il)
1145 {
1146         gboolean ret;
1147         if (!il) return FALSE;
1148
1149         g_mutex_lock(il->data_mutex);
1150         ret = il->done;
1151         g_mutex_unlock(il->data_mutex);
1152
1153         return ret;
1154 }
1155
1156 FileData *image_loader_get_fd(ImageLoader *il)
1157 {
1158         FileData *ret;
1159         if (!il) return NULL;
1160
1161         g_mutex_lock(il->data_mutex);
1162         ret = il->fd;
1163         g_mutex_unlock(il->data_mutex);
1164
1165         return ret;
1166 }
1167
1168 gboolean image_loader_get_shrunk(ImageLoader *il)
1169 {
1170         gboolean ret;
1171         if (!il) return FALSE;
1172
1173         g_mutex_lock(il->data_mutex);
1174         ret = il->shrunk;
1175         g_mutex_unlock(il->data_mutex);
1176         return ret;
1177 }
1178
1179 const gchar *image_loader_get_error(ImageLoader *il)
1180 {
1181         const gchar *ret = NULL;
1182         if (!il) return NULL;
1183         g_mutex_lock(il->data_mutex);
1184         if (il->error) ret = il->error->message;
1185         g_mutex_unlock(il->data_mutex);
1186         return ret;
1187 }
1188
1189
1190 /* FIXME - this can be rather slow and blocks until the size is known */
1191 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1192 {
1193         ImageLoader *il;
1194         gboolean success;
1195
1196         il = image_loader_new(fd);
1197
1198         success = image_loader_start_idle(il);
1199
1200         if (success && il->pixbuf)
1201                 {
1202                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1203                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1204                 }
1205         else
1206                 {
1207                 if (width) *width = -1;
1208                 if (height) *height = -1;
1209                 }
1210
1211         image_loader_free(il);
1212
1213         return success;
1214 }
1215 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */