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