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