2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
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.
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.
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.
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-zxscr.h"
37 #include "image-load-j2k.h"
38 #include "image-load-jpegxl.h"
39 #include "image-load-libraw.h"
40 #include "image-load-svgz.h"
45 #include "ui-fileops.h"
46 #include "gq-marshal.h"
51 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
52 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
54 /* image loader class */
58 SIGNAL_AREA_READY = 0,
66 static guint signals[SIGNAL_COUNT] = { 0 };
68 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
69 static void image_loader_class_init_wrapper(void *data, void *user_data);
70 static void image_loader_class_init(ImageLoaderClass *loader_class);
71 static void image_loader_finalize(GObject *object);
72 static void image_loader_stop(ImageLoader *il);
74 GType image_loader_get_type(void)
76 static GType type = 0;
79 static const GTypeInfo info = {
80 sizeof(ImageLoaderClass),
82 NULL, /* base_finalize */
83 (GClassInitFunc)image_loader_class_init_wrapper, /* class_init */
84 NULL, /* class_finalize */
85 NULL, /* class_data */
88 (GInstanceInitFunc)image_loader_init, /* instance_init */
89 NULL /* value_table */
91 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
96 static void image_loader_init(GTypeInstance *instance, gpointer UNUSED(g_class))
98 ImageLoader *il = (ImageLoader *)instance;
102 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
109 il->idle_done_id = 0;
111 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
112 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
113 il->mapped_file = NULL;
114 il->preview = IMAGE_LOADER_PREVIEW_NONE;
116 il->requested_width = 0;
117 il->requested_height = 0;
118 il->actual_width = 0;
119 il->actual_height = 0;
122 il->can_destroy = TRUE;
124 il->data_mutex = g_new(GMutex, 1);
125 g_mutex_init(il->data_mutex);
126 il->can_destroy_cond = g_new(GCond, 1);
127 g_cond_init(il->can_destroy_cond);
129 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
132 static void image_loader_class_init_wrapper(void *data, void *UNUSED(user_data))
134 image_loader_class_init(data);
137 static void image_loader_class_init(ImageLoaderClass *loader_class)
139 GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
141 // gobject_class->set_property = image_loader_set_property;
142 // gobject_class->get_property = image_loader_get_property;
144 gobject_class->finalize = image_loader_finalize;
147 signals[SIGNAL_AREA_READY] =
148 g_signal_new("area_ready",
149 G_OBJECT_CLASS_TYPE(gobject_class),
151 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
153 gq_marshal_VOID__INT_INT_INT_INT,
160 signals[SIGNAL_ERROR] =
161 g_signal_new("error",
162 G_OBJECT_CLASS_TYPE(gobject_class),
164 G_STRUCT_OFFSET(ImageLoaderClass, error),
166 g_cclosure_marshal_VOID__VOID,
169 signals[SIGNAL_DONE] =
171 G_OBJECT_CLASS_TYPE(gobject_class),
173 G_STRUCT_OFFSET(ImageLoaderClass, done),
175 g_cclosure_marshal_VOID__VOID,
178 signals[SIGNAL_PERCENT] =
179 g_signal_new("percent",
180 G_OBJECT_CLASS_TYPE(gobject_class),
182 G_STRUCT_OFFSET(ImageLoaderClass, percent),
184 g_cclosure_marshal_VOID__DOUBLE,
188 signals[SIGNAL_SIZE] =
189 g_signal_new("size_prepared",
190 G_OBJECT_CLASS_TYPE(gobject_class),
192 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
194 gq_marshal_VOID__INT_INT,
201 static void image_loader_finalize(GObject *object)
203 ImageLoader *il = (ImageLoader *)object;
205 image_loader_stop(il);
207 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
209 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
211 if (il->idle_done_id)
213 g_source_remove(il->idle_done_id);
214 il->idle_done_id = 0;
217 while (g_source_remove_by_user_data(il))
219 DEBUG_2("pending signals detected");
222 while (il->area_param_list)
224 DEBUG_1("pending area_ready signals detected");
225 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
226 g_free(il->area_param_list->data);
227 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
230 while (il->area_param_delayed_list)
232 g_free(il->area_param_delayed_list->data);
233 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
236 if (il->pixbuf) g_object_unref(il->pixbuf);
238 if (il->error) g_error_free(il->error);
240 file_data_unref(il->fd);
242 g_mutex_clear(il->data_mutex);
243 g_free(il->data_mutex);
244 g_cond_clear(il->can_destroy_cond);
245 g_free(il->can_destroy_cond);
248 void image_loader_free(ImageLoader *il)
251 g_object_unref(G_OBJECT(il));
255 ImageLoader *image_loader_new(FileData *fd)
259 if (!fd) return NULL;
261 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
263 il->fd = file_data_ref(fd);
268 /**************************************************************************************/
269 /* send signals via idle calbacks - the callback are executed in the main thread */
271 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
272 struct _ImageLoaderAreaParam {
281 static gboolean image_loader_emit_area_ready_cb(gpointer data)
283 ImageLoaderAreaParam *par = static_cast<ImageLoaderAreaParam *>(data);
284 ImageLoader *il = par->il;
286 g_mutex_lock(il->data_mutex);
287 il->area_param_list = g_list_remove(il->area_param_list, par);
293 g_mutex_unlock(il->data_mutex);
295 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
300 static gboolean image_loader_emit_done_cb(gpointer data)
302 ImageLoader *il = static_cast<ImageLoader *>(data);
303 g_signal_emit(il, signals[SIGNAL_DONE], 0);
307 static gboolean image_loader_emit_error_cb(gpointer data)
309 ImageLoader *il = static_cast<ImageLoader *>(data);
310 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
314 static gboolean image_loader_emit_percent_cb(gpointer data)
316 ImageLoader *il = static_cast<ImageLoader *>(data);
317 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
321 static gboolean image_loader_emit_size_cb(gpointer data)
324 ImageLoader *il = static_cast<ImageLoader *>(data);
325 g_mutex_lock(il->data_mutex);
326 width = il->actual_width;
327 height = il->actual_height;
328 g_mutex_unlock(il->data_mutex);
329 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
334 /* DONE and ERROR are emitted only once, thus they can have normal priority
335 PERCENT and AREA_READY should be processed ASAP
338 static void image_loader_emit_done(ImageLoader *il)
340 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
343 static void image_loader_emit_error(ImageLoader *il)
345 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
348 static void image_loader_emit_percent(ImageLoader *il)
350 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
353 static void image_loader_emit_size(ImageLoader *il)
355 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
358 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
362 ImageLoaderAreaParam *prev_par = (*list)->data;
363 if (prev_par->x == x && prev_par->w == w &&
364 prev_par->y + prev_par->h == y)
366 /* we can merge the notifications */
370 if (prev_par->x == x && prev_par->w == w &&
371 y + h == prev_par->y)
373 /* we can merge the notifications */
378 if (prev_par->y == y && prev_par->h == h &&
379 prev_par->x + prev_par->w == x)
381 /* we can merge the notifications */
385 if (prev_par->y == y && prev_par->h == h &&
386 x + w == prev_par->x)
388 /* we can merge the notifications */
395 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
402 *list = g_list_prepend(*list, par);
406 /* this function expects that il->data_mutex is locked by caller */
407 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
409 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
413 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
417 /**************************************************************************************/
418 /* the following functions may be executed in separate thread */
420 /* this function expects that il->data_mutex is locked by caller */
421 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
423 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
428 static gboolean image_loader_get_stopping(ImageLoader *il)
431 if (!il) return FALSE;
433 g_mutex_lock(il->data_mutex);
435 g_mutex_unlock(il->data_mutex);
441 static void image_loader_sync_pixbuf(ImageLoader *il)
445 g_mutex_lock(il->data_mutex);
449 g_mutex_unlock(il->data_mutex);
453 pb = il->backend.get_pixbuf(il->loader);
455 if (pb == il->pixbuf)
457 g_mutex_unlock(il->data_mutex);
461 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
463 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
466 if (il->pixbuf) g_object_unref(il->pixbuf);
469 if (il->pixbuf) g_object_ref(il->pixbuf);
471 g_mutex_unlock(il->data_mutex);
474 static void image_loader_area_updated_cb(gpointer UNUSED(loader),
475 guint x, guint y, guint w, guint h,
478 ImageLoader *il = static_cast<ImageLoader *>(data);
480 if (!image_loader_get_pixbuf(il))
482 image_loader_sync_pixbuf(il);
483 if (!image_loader_get_pixbuf(il))
485 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
489 g_mutex_lock(il->data_mutex);
490 if (il->delay_area_ready)
491 image_loader_queue_delayed_area_ready(il, x, y, w, h);
493 image_loader_emit_area_ready(il, x, y, w, h);
495 if (il->stopping) il->backend.abort(il->loader);
497 g_mutex_unlock(il->data_mutex);
500 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
502 ImageLoader *il = static_cast<ImageLoader *>(data);
508 https://bugzilla.gnome.org/show_bug.cgi?id=547669
509 https://bugzilla.gnome.org/show_bug.cgi?id=589334
511 gchar *format = il->backend.get_format_name(loader);
512 if (strcmp(format, "svg") == 0 ||
513 strcmp(format, "xpm") == 0)
521 pb = il->backend.get_pixbuf(loader);
523 h = gdk_pixbuf_get_height(pb);
524 rs = gdk_pixbuf_get_rowstride(pb);
525 pix = gdk_pixbuf_get_pixels(pb);
527 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
531 static void image_loader_size_cb(gpointer loader,
532 gint width, gint height, gpointer data)
534 ImageLoader *il = static_cast<ImageLoader *>(data);
536 gboolean scale = FALSE;
539 g_mutex_lock(il->data_mutex);
540 il->actual_width = width;
541 il->actual_height = height;
542 if (il->requested_width < 1 || il->requested_height < 1)
544 g_mutex_unlock(il->data_mutex);
545 image_loader_emit_size(il);
548 g_mutex_unlock(il->data_mutex);
550 #ifdef HAVE_FFMPEGTHUMBNAILER
551 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
554 mime_types = il->backend.get_format_mime_types(loader);
556 while (mime_types[n] && !scale)
558 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
561 g_strfreev(mime_types);
565 image_loader_emit_size(il);
569 g_mutex_lock(il->data_mutex);
572 if (width > il->requested_width || height > il->requested_height)
575 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
577 nw = il->requested_width;
578 nh = (gdouble)nw / width * height;
583 nh = il->requested_height;
584 nw = (gdouble)nh / height * width;
588 il->actual_width = nw;
589 il->actual_height = nh;
590 il->backend.set_size(loader, nw, nh);
594 g_mutex_unlock(il->data_mutex);
595 image_loader_emit_size(il);
598 static void image_loader_stop_loader(ImageLoader *il)
604 /* some loaders do not have a pixbuf till close, order is important here */
605 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
606 image_loader_sync_pixbuf(il);
607 il->backend.free(il->loader);
610 g_mutex_lock(il->data_mutex);
612 g_mutex_unlock(il->data_mutex);
615 static void image_loader_setup_loader(ImageLoader *il)
617 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
621 gint external_preview = 1;
623 g_mutex_lock(il->data_mutex);
625 if (options->external_preview.enable)
628 gchar *tilde_filename;
630 tilde_filename = expand_tilde(options->external_preview.select);
632 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
634 external_preview = runcmd(cmd_line);
636 g_free(tilde_filename);
639 if (external_preview == 0)
641 DEBUG_1("Using custom external loader");
642 image_loader_backend_set_external(&il->backend);
646 #ifdef HAVE_FFMPEGTHUMBNAILER
647 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
649 DEBUG_1("Using custom ffmpegthumbnailer loader");
650 image_loader_backend_set_ft(&il->backend);
655 if (il->bytes_total >= 4 &&
656 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
658 DEBUG_1("Using custom pdf loader");
659 image_loader_backend_set_pdf(&il->backend);
664 if (il->bytes_total >= 12 &&
665 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
666 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
667 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
668 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
670 DEBUG_1("Using custom heif loader");
671 image_loader_backend_set_heif(&il->backend);
676 if (il->bytes_total >= 16 &&
677 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
678 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
680 DEBUG_1("Using custom djvu loader");
681 image_loader_backend_set_djvu(&il->backend);
686 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
688 DEBUG_1("Using custom jpeg loader");
689 image_loader_backend_set_jpeg(&il->backend);
693 if (il->bytes_total >= 11 &&
694 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
695 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
697 DEBUG_1("Using custom cr3 loader");
698 image_loader_backend_set_cr3(&il->backend);
704 if (il->bytes_total >= 10 &&
705 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
706 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
707 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
708 memcmp(il->mapped_file, "II*\0", 4) == 0))
710 DEBUG_1("Using custom tiff loader");
711 image_loader_backend_set_tiff(&il->backend);
715 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
717 DEBUG_1("Using dds loader");
718 image_loader_backend_set_dds(&il->backend);
721 if (il->bytes_total >= 6 &&
722 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
724 DEBUG_1("Using custom psd loader");
725 image_loader_backend_set_psd(&il->backend);
729 if (il->bytes_total >= 12 &&
730 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
732 DEBUG_1("Using custom j2k loader");
733 image_loader_backend_set_j2k(&il->backend);
738 if (il->bytes_total >= 12 &&
739 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
741 DEBUG_1("Using custom jpeg xl loader");
742 image_loader_backend_set_jpegxl(&il->backend);
745 if (il->bytes_total >= 2 &&
746 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
748 DEBUG_1("Using custom jpeg xl loader");
749 image_loader_backend_set_jpegxl(&il->backend);
753 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
754 (file_extension_match(il->fd->path, ".scr")))
756 DEBUG_1("Using custom zxscr loader");
757 image_loader_backend_set_zxscr(&il->backend);
760 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
762 DEBUG_1("Using custom collection loader");
763 image_loader_backend_set_collection(&il->backend);
766 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
768 DEBUG_1("Using custom svgz loader");
769 image_loader_backend_set_svgz(&il->backend);
772 image_loader_backend_set_default(&il->backend);
774 il->loader = static_cast<void *>(il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il));
777 format = il->backend.get_format_name(il->loader);
778 if (g_strcmp0(format, "tiff") == 0)
780 il->backend.set_page_num(il->loader, il->fd->page_num);
786 format = il->backend.get_format_name(il->loader);
787 if (g_strcmp0(format, "pdf") == 0)
789 il->backend.set_page_num(il->loader, il->fd->page_num);
795 format = il->backend.get_format_name(il->loader);
796 if (g_strcmp0(format, "heif") == 0)
798 il->backend.set_page_num(il->loader, il->fd->page_num);
804 format = il->backend.get_format_name(il->loader);
805 if (g_strcmp0(format, "djvu") == 0)
807 il->backend.set_page_num(il->loader, il->fd->page_num);
812 il->fd->format_name = il->backend.get_format_name(il->loader);
814 g_mutex_unlock(il->data_mutex);
818 static void image_loader_done(ImageLoader *il)
820 image_loader_stop_loader(il);
822 image_loader_emit_done(il);
825 static void image_loader_error(ImageLoader *il)
827 image_loader_stop_loader(il);
829 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
831 image_loader_emit_error(il);
834 static gboolean image_loader_continue(ImageLoader *il)
839 if (!il) return FALSE;
841 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
842 while (c > 0 && !image_loader_get_stopping(il))
844 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
848 image_loader_done(il);
852 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
854 image_loader_error(il);
863 if (il->bytes_total > 0)
865 image_loader_emit_percent(il);
871 static gboolean image_loader_begin(ImageLoader *il)
873 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
878 if (il->pixbuf) return FALSE;
880 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
881 if (b < 1) return FALSE;
883 image_loader_setup_loader(il);
885 g_assert(il->bytes_read == 0);
886 if (il->backend.load) {
888 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
890 image_loader_stop_loader(il);
894 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
896 image_loader_stop_loader(il);
901 format = il->backend.get_format_name(il->loader);
902 if (g_strcmp0(format, "pdf") == 0)
904 gint i = il->backend.get_page_total(il->loader);
905 file_data_set_page_total(il->fd, i);
910 format = il->backend.get_format_name(il->loader);
911 if (g_strcmp0(format, "heif") == 0)
913 gint i = il->backend.get_page_total(il->loader);
914 file_data_set_page_total(il->fd, i);
919 format = il->backend.get_format_name(il->loader);
920 if (g_strcmp0(format, "djvu") == 0)
922 gint i = il->backend.get_page_total(il->loader);
923 file_data_set_page_total(il->fd, i);
928 format = il->backend.get_format_name(il->loader);
929 if (g_strcmp0(format, "tiff") == 0)
931 gint i = il->backend.get_page_total(il->loader);
932 file_data_set_page_total(il->fd, i);
939 /* read until size is known */
940 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
942 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
943 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
945 image_loader_stop_loader(il);
950 if (!il->pixbuf) image_loader_sync_pixbuf(il);
952 if (il->bytes_read == il->bytes_total || b < 1)
954 /* done, handle (broken) loaders that do not have pixbuf till close */
955 image_loader_stop_loader(il);
957 if (!il->pixbuf) return FALSE;
959 image_loader_done(il);
965 image_loader_stop_loader(il);
972 /**************************************************************************************/
973 /* the following functions are always executed in the main thread */
976 static gboolean image_loader_setup_source(ImageLoader *il)
981 if (!il || il->loader || il->mapped_file) return FALSE;
983 il->mapped_file = NULL;
987 ExifData *exif = exif_read_fd(il->fd);
989 if (options->thumbnails.use_exif)
991 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
995 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1000 il->mapped_file = libraw_get_preview(il, (guint *)&il->bytes_total);
1002 if (il->mapped_file)
1004 /* Both exiv2 and libraw sometimes return a pointer to a file
1005 * section that is not a jpeg */
1006 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1008 il->mapped_file = NULL;
1012 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1017 /* If libraw does not find a thumbnail, try exiv2 */
1018 if (!il->mapped_file)
1020 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
1022 if (il->mapped_file)
1024 /* Both exiv2 and libraw sometimes return a pointer to a file
1025 * section that is not a jpeg */
1026 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1028 il->mapped_file = NULL;
1032 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1037 if (il->mapped_file)
1039 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1041 exif_free_fd(il->fd, exif);
1045 if (!il->mapped_file)
1050 pathl = path_from_utf8(il->fd->path);
1051 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1053 if (load_fd == -1) return FALSE;
1055 if (fstat(load_fd, &st) == 0)
1057 il->bytes_total = st.st_size;
1065 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
1067 if (il->mapped_file == MAP_FAILED)
1069 il->mapped_file = 0;
1072 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1078 static void image_loader_stop_source(ImageLoader *il)
1082 if (il->mapped_file)
1084 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1086 exif_free_preview(il->mapped_file);
1088 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1090 libraw_free_preview(il->mapped_file);
1094 munmap(il->mapped_file, il->bytes_total);
1096 il->mapped_file = NULL;
1100 static void image_loader_stop(ImageLoader *il)
1106 g_source_remove(il->idle_id);
1112 /* stop loader in the other thread */
1113 g_mutex_lock(il->data_mutex);
1114 il->stopping = TRUE;
1115 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1116 g_mutex_unlock(il->data_mutex);
1119 image_loader_stop_loader(il);
1120 image_loader_stop_source(il);
1124 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1126 g_mutex_lock(il->data_mutex);
1127 il->delay_area_ready = enable;
1132 list = g_list_reverse(il->area_param_delayed_list);
1133 il->area_param_delayed_list = NULL;
1134 g_mutex_unlock(il->data_mutex);
1140 ImageLoaderAreaParam *par = static_cast<ImageLoaderAreaParam *>(work->data);
1143 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1151 g_mutex_unlock(il->data_mutex);
1156 /**************************************************************************************/
1157 /* execution via idle calls */
1159 static gboolean image_loader_idle_cb(gpointer data)
1161 gboolean ret = FALSE;
1162 ImageLoader *il = static_cast<ImageLoader *>(data);
1166 ret = image_loader_continue(il);
1171 image_loader_stop_source(il);
1178 static gboolean image_loader_start_idle(ImageLoader *il)
1182 if (!il) return FALSE;
1184 if (!il->fd) return FALSE;
1186 if (!image_loader_setup_source(il)) return FALSE;
1188 ret = image_loader_begin(il);
1190 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1194 /**************************************************************************************/
1195 /* execution via thread */
1197 static GThreadPool *image_loader_thread_pool = NULL;
1199 static GCond *image_loader_prio_cond = NULL;
1200 static GMutex *image_loader_prio_mutex = NULL;
1201 static gint image_loader_prio_num = 0;
1204 static void image_loader_thread_enter_high(void)
1206 g_mutex_lock(image_loader_prio_mutex);
1207 image_loader_prio_num++;
1208 g_mutex_unlock(image_loader_prio_mutex);
1211 static void image_loader_thread_leave_high(void)
1213 g_mutex_lock(image_loader_prio_mutex);
1214 image_loader_prio_num--;
1215 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1216 g_mutex_unlock(image_loader_prio_mutex);
1219 static void image_loader_thread_wait_high(void)
1221 g_mutex_lock(image_loader_prio_mutex);
1222 while (image_loader_prio_num)
1224 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1227 g_mutex_unlock(image_loader_prio_mutex);
1231 static void image_loader_thread_run(gpointer data, gpointer UNUSED(user_data))
1233 ImageLoader *il = static_cast<ImageLoader *>(data);
1237 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1239 /* low prio, wait until high prio tasks finishes */
1240 image_loader_thread_wait_high();
1245 image_loader_thread_enter_high();
1248 err = !image_loader_begin(il);
1253 loader failed, we have to send signal
1254 (idle mode returns the image_loader_begin return value directly)
1255 (success is always reported indirectly from image_loader_begin)
1257 image_loader_emit_error(il);
1262 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1264 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1266 /* low prio, wait until high prio tasks finishes */
1267 image_loader_thread_wait_high();
1269 cont = image_loader_continue(il);
1271 image_loader_stop_loader(il);
1273 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1276 image_loader_thread_leave_high();
1279 g_mutex_lock(il->data_mutex);
1280 il->can_destroy = TRUE;
1281 g_cond_signal(il->can_destroy_cond);
1282 g_mutex_unlock(il->data_mutex);
1287 static gboolean image_loader_start_thread(ImageLoader *il)
1289 if (!il) return FALSE;
1291 if (!il->fd) return FALSE;
1295 if (!image_loader_setup_source(il)) return FALSE;
1297 if (!image_loader_thread_pool)
1299 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1300 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1301 g_cond_init(image_loader_prio_cond);
1302 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1303 g_mutex_init(image_loader_prio_mutex);
1306 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1308 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1309 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1315 /**************************************************************************************/
1316 /* public interface */
1319 gboolean image_loader_start(ImageLoader *il)
1321 if (!il) return FALSE;
1323 if (!il->fd) return FALSE;
1325 return image_loader_start_thread(il);
1329 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1330 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1333 if (!il) return NULL;
1335 g_mutex_lock(il->data_mutex);
1337 g_mutex_unlock(il->data_mutex);
1341 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1345 g_mutex_lock(il->data_mutex);
1346 il->requested_width = width;
1347 il->requested_height = height;
1348 g_mutex_unlock(il->data_mutex);
1351 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1355 g_mutex_lock(il->data_mutex);
1356 il->idle_read_loop_count = count ? count : 1;
1357 g_mutex_unlock(il->data_mutex);
1360 void image_loader_set_priority(ImageLoader *il, gint priority)
1364 if (il->thread) return; /* can't change prio if the thread already runs */
1365 il->idle_priority = priority;
1369 gdouble image_loader_get_percent(ImageLoader *il)
1372 if (!il) return 0.0;
1374 g_mutex_lock(il->data_mutex);
1375 if (il->bytes_total == 0)
1381 ret = (gdouble)il->bytes_read / il->bytes_total;
1383 g_mutex_unlock(il->data_mutex);
1387 gboolean image_loader_get_is_done(ImageLoader *il)
1390 if (!il) return FALSE;
1392 g_mutex_lock(il->data_mutex);
1394 g_mutex_unlock(il->data_mutex);
1399 FileData *image_loader_get_fd(ImageLoader *il)
1402 if (!il) return NULL;
1404 g_mutex_lock(il->data_mutex);
1406 g_mutex_unlock(il->data_mutex);
1411 gboolean image_loader_get_shrunk(ImageLoader *il)
1414 if (!il) return FALSE;
1416 g_mutex_lock(il->data_mutex);
1418 g_mutex_unlock(il->data_mutex);
1422 const gchar *image_loader_get_error(ImageLoader *il)
1424 const gchar *ret = NULL;
1425 if (!il) return NULL;
1426 g_mutex_lock(il->data_mutex);
1427 if (il->error) ret = il->error->message;
1428 g_mutex_unlock(il->data_mutex);
1432 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1435 gint width_file = 0;
1436 gint height_file = 0;
1438 gdk_pixbuf_get_file_info(fd->path, &width_file, &height_file);
1440 if (width_file && height_file)
1442 *width = width_file;
1443 *height = height_file;
1455 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */