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