Update copyright in all files
[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, "II*\0", 4) == 0))
618                 {
619                 DEBUG_1("Using custom tiff loader");
620                 image_loader_backend_set_tiff(&il->backend);
621                 }
622         else
623 #endif
624                 image_loader_backend_set_default(&il->backend);
625
626         il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
627         g_mutex_unlock(il->data_mutex);
628 }
629
630
631 static void image_loader_done(ImageLoader *il)
632 {
633         image_loader_stop_loader(il);
634
635         image_loader_emit_done(il);
636 }
637
638 static void image_loader_error(ImageLoader *il)
639 {
640         image_loader_stop_loader(il);
641
642         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
643
644         image_loader_emit_error(il);
645 }
646
647 static gboolean image_loader_continue(ImageLoader *il)
648 {
649         gint b;
650         gint c;
651
652         if (!il) return FALSE;
653
654         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
655         while (c > 0 && !image_loader_get_stopping(il))
656                 {
657                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
658
659                 if (b == 0)
660                         {
661                         image_loader_done(il);
662                         return FALSE;
663                         }
664
665                 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
666                         {
667                         image_loader_error(il);
668                         return FALSE;
669                         }
670
671                 il->bytes_read += b;
672
673                 c--;
674                 }
675
676         if (il->bytes_total > 0)
677                 {
678                 image_loader_emit_percent(il);
679                 }
680
681         return TRUE;
682 }
683
684 static gboolean image_loader_begin(ImageLoader *il)
685 {
686         gssize b;
687
688         if (il->pixbuf) return FALSE;
689
690         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
691         if (b < 1) return FALSE;
692
693         image_loader_setup_loader(il);
694
695         g_assert(il->bytes_read == 0);
696         if (il->backend.load) {
697                 b = il->bytes_total;
698                 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
699                         {
700                         image_loader_stop_loader(il);
701                         return FALSE;
702                         }
703         }
704         else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
705                 {
706                 image_loader_stop_loader(il);
707                 return FALSE;
708                 }
709
710         il->bytes_read += b;
711
712         /* read until size is known */
713         while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
714                 {
715                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
716                 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
717                         {
718                         image_loader_stop_loader(il);
719                         return FALSE;
720                         }
721                 il->bytes_read += b;
722                 }
723         if (!il->pixbuf) image_loader_sync_pixbuf(il);
724
725         if (il->bytes_read == il->bytes_total || b < 1)
726                 {
727                 /* done, handle (broken) loaders that do not have pixbuf till close */
728                 image_loader_stop_loader(il);
729
730                 if (!il->pixbuf) return FALSE;
731
732                 image_loader_done(il);
733                 return TRUE;
734                 }
735
736         if (!il->pixbuf)
737                 {
738                 image_loader_stop_loader(il);
739                 return FALSE;
740                 }
741
742         return TRUE;
743 }
744
745 /**************************************************************************************/
746 /* the following functions are always executed in the main thread */
747
748
749 static gboolean image_loader_setup_source(ImageLoader *il)
750 {
751         struct stat st;
752         gchar *pathl;
753
754         if (!il || il->loader || il->mapped_file) return FALSE;
755
756         il->mapped_file = NULL;
757
758         if (il->fd)
759                 {
760                 ExifData *exif = exif_read_fd(il->fd);
761
762                 if (options->thumbnails.use_exif)
763                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
764                 else
765                         il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
766
767                 if (il->mapped_file)
768                         {
769                         il->preview = TRUE;
770                         DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
771                         }
772                 exif_free_fd(il->fd, exif);
773                 }
774
775
776         if (!il->mapped_file)
777                 {
778                 /* normal file */
779                 gint load_fd;
780
781                 pathl = path_from_utf8(il->fd->path);
782                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
783                 g_free(pathl);
784                 if (load_fd == -1) return FALSE;
785
786                 if (fstat(load_fd, &st) == 0)
787                         {
788                         il->bytes_total = st.st_size;
789                         }
790                 else
791                         {
792                         close(load_fd);
793                         return FALSE;
794                         }
795
796                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
797                 close(load_fd);
798                 if (il->mapped_file == MAP_FAILED)
799                         {
800                         il->mapped_file = 0;
801                         return FALSE;
802                         }
803                 il->preview = FALSE;
804                 }
805
806         return TRUE;
807 }
808
809 static void image_loader_stop_source(ImageLoader *il)
810 {
811         if (!il) return;
812
813         if (il->mapped_file)
814                 {
815                 if (il->preview)
816                         {
817                         exif_free_preview(il->mapped_file);
818                         }
819                 else
820                         {
821                         munmap(il->mapped_file, il->bytes_total);
822                         }
823                 il->mapped_file = NULL;
824                 }
825 }
826
827 static void image_loader_stop(ImageLoader *il)
828 {
829         if (!il) return;
830
831         if (il->idle_id)
832                 {
833                 g_source_remove(il->idle_id);
834                 il->idle_id = 0;
835                 }
836
837         if (il->thread)
838                 {
839                 /* stop loader in the other thread */
840                 g_mutex_lock(il->data_mutex);
841                 il->stopping = TRUE;
842                 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
843                 g_mutex_unlock(il->data_mutex);
844                 }
845
846         image_loader_stop_loader(il);
847         image_loader_stop_source(il);
848
849 }
850
851 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
852 {
853         g_mutex_lock(il->data_mutex);
854         il->delay_area_ready = enable;
855         if (!enable)
856                 {
857                 /* send delayed */
858                 GList *list, *work;
859                 list = g_list_reverse(il->area_param_delayed_list);
860                 il->area_param_delayed_list = NULL;
861                 g_mutex_unlock(il->data_mutex);
862
863                 work = list;
864
865                 while (work)
866                         {
867                         ImageLoaderAreaParam *par = work->data;
868                         work = work->next;
869
870                         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
871                         g_free(par);
872                         }
873                 g_list_free(list);
874                 }
875         else
876                 {
877                 /* just unlock */
878                 g_mutex_unlock(il->data_mutex);
879                 }
880 }
881
882
883 /**************************************************************************************/
884 /* execution via idle calls */
885
886 static gboolean image_loader_idle_cb(gpointer data)
887 {
888         gboolean ret = FALSE;
889         ImageLoader *il = data;
890
891         if (il->idle_id)
892                 {
893                 ret = image_loader_continue(il);
894                 }
895
896         if (!ret)
897                 {
898                 image_loader_stop_source(il);
899                 }
900
901         return ret;
902 }
903
904
905 static gboolean image_loader_start_idle(ImageLoader *il)
906 {
907         gboolean ret;
908
909         if (!il) return FALSE;
910
911         if (!il->fd) return FALSE;
912
913         if (!image_loader_setup_source(il)) return FALSE;
914
915         ret = image_loader_begin(il);
916
917         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
918         return ret;
919 }
920
921 /**************************************************************************************/
922 /* execution via thread */
923
924 #ifdef HAVE_GTHREAD
925 static GThreadPool *image_loader_thread_pool = NULL;
926
927 static GCond *image_loader_prio_cond = NULL;
928 static GMutex *image_loader_prio_mutex = NULL;
929 static gint image_loader_prio_num = 0;
930
931
932 static void image_loader_thread_enter_high(void)
933 {
934         g_mutex_lock(image_loader_prio_mutex);
935         image_loader_prio_num++;
936         g_mutex_unlock(image_loader_prio_mutex);
937 }
938
939 static void image_loader_thread_leave_high(void)
940 {
941         g_mutex_lock(image_loader_prio_mutex);
942         image_loader_prio_num--;
943         if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
944         g_mutex_unlock(image_loader_prio_mutex);
945 }
946
947 static void image_loader_thread_wait_high(void)
948 {
949         g_mutex_lock(image_loader_prio_mutex);
950         while (image_loader_prio_num)
951                 {
952                 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
953                 }
954
955         g_mutex_unlock(image_loader_prio_mutex);
956 }
957
958
959 static void image_loader_thread_run(gpointer data, gpointer user_data)
960 {
961         ImageLoader *il = data;
962         gboolean cont;
963         gboolean err;
964
965         if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
966                 {
967                 /* low prio, wait untill high prio tasks finishes */
968                 image_loader_thread_wait_high();
969                 }
970         else
971                 {
972                 /* high prio */
973                 image_loader_thread_enter_high();
974                 }
975
976         err = !image_loader_begin(il);
977
978         if (err)
979                 {
980                 /*
981                 loader failed, we have to send signal
982                 (idle mode returns the image_loader_begin return value directly)
983                 (success is always reported indirectly from image_loader_begin)
984                 */
985                 image_loader_emit_error(il);
986                 }
987
988         cont = !err;
989
990         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
991                 {
992                 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
993                         {
994                         /* low prio, wait untill high prio tasks finishes */
995                         image_loader_thread_wait_high();
996                         }
997                 cont = image_loader_continue(il);
998                 }
999         image_loader_stop_loader(il);
1000
1001         if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1002                 {
1003                 /* high prio */
1004                 image_loader_thread_leave_high();
1005                 }
1006
1007         g_mutex_lock(il->data_mutex);
1008         il->can_destroy = TRUE;
1009         g_cond_signal(il->can_destroy_cond);
1010         g_mutex_unlock(il->data_mutex);
1011
1012 }
1013
1014
1015 static gboolean image_loader_start_thread(ImageLoader *il)
1016 {
1017         if (!il) return FALSE;
1018
1019         if (!il->fd) return FALSE;
1020
1021         il->thread = TRUE;
1022
1023         if (!image_loader_setup_source(il)) return FALSE;
1024
1025         if (!image_loader_thread_pool)
1026                 {
1027                 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1028 #if GLIB_CHECK_VERSION(2,32,0)
1029                 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1030                 g_cond_init(image_loader_prio_cond);
1031                 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1032                 g_mutex_init(image_loader_prio_mutex);
1033 #else
1034                 image_loader_prio_cond = g_cond_new();
1035                 image_loader_prio_mutex = g_mutex_new();
1036 #endif
1037                 }
1038
1039         il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1040
1041         g_thread_pool_push(image_loader_thread_pool, il, NULL);
1042         DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1043
1044         return TRUE;
1045 }
1046 #endif /* HAVE_GTHREAD */
1047
1048
1049 /**************************************************************************************/
1050 /* public interface */
1051
1052
1053 gboolean image_loader_start(ImageLoader *il)
1054 {
1055         if (!il) return FALSE;
1056
1057         if (!il->fd) return FALSE;
1058
1059 #ifdef HAVE_GTHREAD
1060         return image_loader_start_thread(il);
1061 #else
1062         return image_loader_start_idle(il);
1063 #endif
1064 }
1065
1066
1067 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1068 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1069 {
1070         GdkPixbuf *ret;
1071         if (!il) return NULL;
1072
1073         g_mutex_lock(il->data_mutex);
1074         ret = il->pixbuf;
1075         g_mutex_unlock(il->data_mutex);
1076         return ret;
1077 }
1078
1079 gchar *image_loader_get_format(ImageLoader *il)
1080 {
1081         gchar **mimev;
1082         gchar *mime;
1083
1084         if (!il || !il->loader) return NULL;
1085
1086         mimev = il->backend.get_format_mime_types(il->loader);
1087         if (!mimev) return NULL;
1088
1089         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
1090         mime = g_strdup(mimev[0]);
1091         g_strfreev(mimev);
1092
1093         return mime;
1094 }
1095
1096 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1097 {
1098         if (!il) return;
1099
1100         g_mutex_lock(il->data_mutex);
1101         il->requested_width = width;
1102         il->requested_height = height;
1103         g_mutex_unlock(il->data_mutex);
1104 }
1105
1106 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1107 {
1108         if (!il) return;
1109
1110         g_mutex_lock(il->data_mutex);
1111         il->idle_read_loop_count = count ? count : 1;
1112         g_mutex_unlock(il->data_mutex);
1113 }
1114
1115 void image_loader_set_priority(ImageLoader *il, gint priority)
1116 {
1117         if (!il) return;
1118
1119         if (il->thread) return; /* can't change prio if the thread already runs */
1120         il->idle_priority = priority;
1121 }
1122
1123
1124 gdouble image_loader_get_percent(ImageLoader *il)
1125 {
1126         gdouble ret;
1127         if (!il) return 0.0;
1128
1129         g_mutex_lock(il->data_mutex);
1130         if (il->bytes_total == 0)
1131                 {
1132                 ret = 0.0;
1133                 }
1134         else
1135                 {
1136                 ret = (gdouble)il->bytes_read / il->bytes_total;
1137                 }
1138         g_mutex_unlock(il->data_mutex);
1139         return ret;
1140 }
1141
1142 gboolean image_loader_get_is_done(ImageLoader *il)
1143 {
1144         gboolean ret;
1145         if (!il) return FALSE;
1146
1147         g_mutex_lock(il->data_mutex);
1148         ret = il->done;
1149         g_mutex_unlock(il->data_mutex);
1150
1151         return ret;
1152 }
1153
1154 FileData *image_loader_get_fd(ImageLoader *il)
1155 {
1156         FileData *ret;
1157         if (!il) return NULL;
1158
1159         g_mutex_lock(il->data_mutex);
1160         ret = il->fd;
1161         g_mutex_unlock(il->data_mutex);
1162
1163         return ret;
1164 }
1165
1166 gboolean image_loader_get_shrunk(ImageLoader *il)
1167 {
1168         gboolean ret;
1169         if (!il) return FALSE;
1170
1171         g_mutex_lock(il->data_mutex);
1172         ret = il->shrunk;
1173         g_mutex_unlock(il->data_mutex);
1174         return ret;
1175 }
1176
1177 const gchar *image_loader_get_error(ImageLoader *il)
1178 {
1179         const gchar *ret = NULL;
1180         if (!il) return NULL;
1181         g_mutex_lock(il->data_mutex);
1182         if (il->error) ret = il->error->message;
1183         g_mutex_unlock(il->data_mutex);
1184         return ret;
1185 }
1186
1187
1188 /* FIXME - this can be rather slow and blocks until the size is known */
1189 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1190 {
1191         ImageLoader *il;
1192         gboolean success;
1193
1194         il = image_loader_new(fd);
1195
1196         success = image_loader_start_idle(il);
1197
1198         if (success && il->pixbuf)
1199                 {
1200                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1201                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1202                 }
1203         else
1204                 {
1205                 if (width) *width = -1;
1206                 if (height) *height = -1;
1207                 }
1208
1209         image_loader_free(il);
1210
1211         return success;
1212 }
1213 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */