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_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"
46 #include "ui_fileops.h"
47 #include "gq-marshal.h"
52 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
53 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
55 /* image loader class */
59 SIGNAL_AREA_READY = 0,
67 static guint signals[SIGNAL_COUNT] = { 0 };
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);
75 GType image_loader_get_type(void)
77 static GType type = 0;
80 static const GTypeInfo info = {
81 sizeof(ImageLoaderClass),
83 NULL, /* base_finalize */
84 (GClassInitFunc)image_loader_class_init_wrapper, /* class_init */
85 NULL, /* class_finalize */
86 NULL, /* class_data */
89 (GInstanceInitFunc)image_loader_init, /* instance_init */
90 NULL /* value_table */
92 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
97 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
99 ImageLoader *il = (ImageLoader *)instance;
103 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
110 il->idle_done_id = 0;
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;
117 il->requested_width = 0;
118 il->requested_height = 0;
119 il->actual_width = 0;
120 il->actual_height = 0;
123 il->can_destroy = TRUE;
126 #if GLIB_CHECK_VERSION(2,32,0)
127 il->data_mutex = g_new(GMutex, 1);
128 g_mutex_init(il->data_mutex);
129 il->can_destroy_cond = g_new(GCond, 1);
130 g_cond_init(il->can_destroy_cond);
132 il->data_mutex = g_mutex_new();
133 il->can_destroy_cond = g_cond_new();
136 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
139 static void image_loader_class_init_wrapper(void *data, void *user_data)
141 image_loader_class_init(data);
144 static void image_loader_class_init(ImageLoaderClass *class)
146 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
148 // gobject_class->set_property = image_loader_set_property;
149 // gobject_class->get_property = image_loader_get_property;
151 gobject_class->finalize = image_loader_finalize;
154 signals[SIGNAL_AREA_READY] =
155 g_signal_new("area_ready",
156 G_OBJECT_CLASS_TYPE(gobject_class),
158 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
160 gq_marshal_VOID__INT_INT_INT_INT,
167 signals[SIGNAL_ERROR] =
168 g_signal_new("error",
169 G_OBJECT_CLASS_TYPE(gobject_class),
171 G_STRUCT_OFFSET(ImageLoaderClass, error),
173 g_cclosure_marshal_VOID__VOID,
176 signals[SIGNAL_DONE] =
178 G_OBJECT_CLASS_TYPE(gobject_class),
180 G_STRUCT_OFFSET(ImageLoaderClass, done),
182 g_cclosure_marshal_VOID__VOID,
185 signals[SIGNAL_PERCENT] =
186 g_signal_new("percent",
187 G_OBJECT_CLASS_TYPE(gobject_class),
189 G_STRUCT_OFFSET(ImageLoaderClass, percent),
191 g_cclosure_marshal_VOID__DOUBLE,
195 signals[SIGNAL_SIZE] =
196 g_signal_new("size_prepared",
197 G_OBJECT_CLASS_TYPE(gobject_class),
199 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
201 gq_marshal_VOID__INT_INT,
208 static void image_loader_finalize(GObject *object)
210 ImageLoader *il = (ImageLoader *)object;
212 image_loader_stop(il);
214 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
216 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
218 if (il->idle_done_id)
220 g_source_remove(il->idle_done_id);
221 il->idle_done_id = 0;
224 while (g_source_remove_by_user_data(il))
226 DEBUG_2("pending signals detected");
229 while (il->area_param_list)
231 DEBUG_1("pending area_ready signals detected");
232 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
233 g_free(il->area_param_list->data);
234 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
237 while (il->area_param_delayed_list)
239 g_free(il->area_param_delayed_list->data);
240 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
243 if (il->pixbuf) g_object_unref(il->pixbuf);
245 if (il->error) g_error_free(il->error);
247 file_data_unref(il->fd);
249 #if GLIB_CHECK_VERSION(2,32,0)
250 g_mutex_clear(il->data_mutex);
251 g_free(il->data_mutex);
252 g_cond_clear(il->can_destroy_cond);
253 g_free(il->can_destroy_cond);
255 g_mutex_free(il->data_mutex);
256 g_cond_free(il->can_destroy_cond);
261 void image_loader_free(ImageLoader *il)
264 g_object_unref(G_OBJECT(il));
268 ImageLoader *image_loader_new(FileData *fd)
272 if (!fd) return NULL;
274 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
276 il->fd = file_data_ref(fd);
281 /**************************************************************************************/
282 /* send signals via idle calbacks - the callback are executed in the main thread */
284 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
285 struct _ImageLoaderAreaParam {
294 static gboolean image_loader_emit_area_ready_cb(gpointer data)
296 ImageLoaderAreaParam *par = data;
297 ImageLoader *il = par->il;
299 g_mutex_lock(il->data_mutex);
300 il->area_param_list = g_list_remove(il->area_param_list, par);
306 g_mutex_unlock(il->data_mutex);
308 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
313 static gboolean image_loader_emit_done_cb(gpointer data)
315 ImageLoader *il = data;
316 g_signal_emit(il, signals[SIGNAL_DONE], 0);
320 static gboolean image_loader_emit_error_cb(gpointer data)
322 ImageLoader *il = data;
323 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
327 static gboolean image_loader_emit_percent_cb(gpointer data)
329 ImageLoader *il = data;
330 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
334 static gboolean image_loader_emit_size_cb(gpointer data)
337 ImageLoader *il = data;
338 g_mutex_lock(il->data_mutex);
339 width = il->actual_width;
340 height = il->actual_height;
341 g_mutex_unlock(il->data_mutex);
342 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
347 /* DONE and ERROR are emitted only once, thus they can have normal priority
348 PERCENT and AREA_READY should be processed ASAP
351 static void image_loader_emit_done(ImageLoader *il)
353 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
356 static void image_loader_emit_error(ImageLoader *il)
358 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
361 static void image_loader_emit_percent(ImageLoader *il)
363 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
366 static void image_loader_emit_size(ImageLoader *il)
368 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
371 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
375 ImageLoaderAreaParam *prev_par = (*list)->data;
376 if (prev_par->x == x && prev_par->w == w &&
377 prev_par->y + prev_par->h == y)
379 /* we can merge the notifications */
383 if (prev_par->x == x && prev_par->w == w &&
384 y + h == prev_par->y)
386 /* we can merge the notifications */
391 if (prev_par->y == y && prev_par->h == h &&
392 prev_par->x + prev_par->w == x)
394 /* we can merge the notifications */
398 if (prev_par->y == y && prev_par->h == h &&
399 x + w == prev_par->x)
401 /* we can merge the notifications */
408 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
415 *list = g_list_prepend(*list, par);
419 /* this function expects that il->data_mutex is locked by caller */
420 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
422 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
426 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
430 /**************************************************************************************/
431 /* the following functions may be executed in separate thread */
433 /* this function expects that il->data_mutex is locked by caller */
434 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
436 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
441 static gboolean image_loader_get_stopping(ImageLoader *il)
444 if (!il) return FALSE;
446 g_mutex_lock(il->data_mutex);
448 g_mutex_unlock(il->data_mutex);
454 static void image_loader_sync_pixbuf(ImageLoader *il)
458 g_mutex_lock(il->data_mutex);
462 g_mutex_unlock(il->data_mutex);
466 pb = il->backend.get_pixbuf(il->loader);
468 if (pb == il->pixbuf)
470 g_mutex_unlock(il->data_mutex);
474 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
476 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
479 if (il->pixbuf) g_object_unref(il->pixbuf);
482 if (il->pixbuf) g_object_ref(il->pixbuf);
484 g_mutex_unlock(il->data_mutex);
487 static void image_loader_area_updated_cb(gpointer loader,
488 guint x, guint y, guint w, guint h,
491 ImageLoader *il = data;
493 if (!image_loader_get_pixbuf(il))
495 image_loader_sync_pixbuf(il);
496 if (!image_loader_get_pixbuf(il))
498 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
502 g_mutex_lock(il->data_mutex);
503 if (il->delay_area_ready)
504 image_loader_queue_delayed_area_ready(il, x, y, w, h);
506 image_loader_emit_area_ready(il, x, y, w, h);
508 if (il->stopping) il->backend.abort(il->loader);
510 g_mutex_unlock(il->data_mutex);
513 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
515 ImageLoader *il = data;
521 https://bugzilla.gnome.org/show_bug.cgi?id=547669
522 https://bugzilla.gnome.org/show_bug.cgi?id=589334
524 gchar *format = il->backend.get_format_name(loader);
525 if (strcmp(format, "svg") == 0 ||
526 strcmp(format, "xpm") == 0)
534 pb = il->backend.get_pixbuf(loader);
536 h = gdk_pixbuf_get_height(pb);
537 rs = gdk_pixbuf_get_rowstride(pb);
538 pix = gdk_pixbuf_get_pixels(pb);
540 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
544 static void image_loader_size_cb(gpointer loader,
545 gint width, gint height, gpointer data)
547 ImageLoader *il = data;
549 gboolean scale = FALSE;
552 g_mutex_lock(il->data_mutex);
553 il->actual_width = width;
554 il->actual_height = height;
555 if (il->requested_width < 1 || il->requested_height < 1)
557 g_mutex_unlock(il->data_mutex);
558 image_loader_emit_size(il);
561 g_mutex_unlock(il->data_mutex);
563 #ifdef HAVE_FFMPEGTHUMBNAILER
564 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
567 mime_types = il->backend.get_format_mime_types(loader);
569 while (mime_types[n] && !scale)
571 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
574 g_strfreev(mime_types);
578 image_loader_emit_size(il);
582 g_mutex_lock(il->data_mutex);
585 if (width > il->requested_width || height > il->requested_height)
588 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
590 nw = il->requested_width;
591 nh = (gdouble)nw / width * height;
596 nh = il->requested_height;
597 nw = (gdouble)nh / height * width;
601 il->actual_width = nw;
602 il->actual_height = nh;
603 il->backend.set_size(loader, nw, nh);
607 g_mutex_unlock(il->data_mutex);
608 image_loader_emit_size(il);
611 static void image_loader_stop_loader(ImageLoader *il)
617 /* some loaders do not have a pixbuf till close, order is important here */
618 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
619 image_loader_sync_pixbuf(il);
620 il->backend.free(il->loader);
623 g_mutex_lock(il->data_mutex);
625 g_mutex_unlock(il->data_mutex);
628 static void image_loader_setup_loader(ImageLoader *il)
630 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
634 gint external_preview = 1;
636 g_mutex_lock(il->data_mutex);
638 if (options->external_preview.enable)
641 gchar *tilde_filename;
643 tilde_filename = expand_tilde(options->external_preview.select);
645 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
647 external_preview = runcmd(cmd_line);
649 g_free(tilde_filename);
652 if (external_preview == 0)
654 DEBUG_1("Using custom external loader");
655 image_loader_backend_set_external(&il->backend);
659 #ifdef HAVE_FFMPEGTHUMBNAILER
660 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
662 DEBUG_1("Using custom ffmpegthumbnailer loader");
663 image_loader_backend_set_ft(&il->backend);
668 if (il->bytes_total >= 4 &&
669 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
671 DEBUG_1("Using custom pdf loader");
672 image_loader_backend_set_pdf(&il->backend);
677 if (il->bytes_total >= 12 &&
678 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
679 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
680 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
681 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
683 DEBUG_1("Using custom heif loader");
684 image_loader_backend_set_heif(&il->backend);
689 if (il->bytes_total >= 12 &&
690 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
691 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
693 DEBUG_1("Using custom webp loader");
694 image_loader_backend_set_webp(&il->backend);
699 if (il->bytes_total >= 16 &&
700 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
701 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
703 DEBUG_1("Using custom djvu loader");
704 image_loader_backend_set_djvu(&il->backend);
709 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
711 DEBUG_1("Using custom jpeg loader");
712 image_loader_backend_set_jpeg(&il->backend);
716 if (il->bytes_total >= 11 &&
717 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
718 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
720 DEBUG_1("Using custom cr3 loader");
721 image_loader_backend_set_cr3(&il->backend);
727 if (il->bytes_total >= 10 &&
728 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
729 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
730 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
731 memcmp(il->mapped_file, "II*\0", 4) == 0))
733 DEBUG_1("Using custom tiff loader");
734 image_loader_backend_set_tiff(&il->backend);
738 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
740 DEBUG_1("Using dds loader");
741 image_loader_backend_set_dds(&il->backend);
744 if (il->bytes_total >= 6 &&
745 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
747 DEBUG_1("Using custom psd loader");
748 image_loader_backend_set_psd(&il->backend);
752 if (il->bytes_total >= 12 &&
753 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
755 DEBUG_1("Using custom j2k loader");
756 image_loader_backend_set_j2k(&il->backend);
761 if (il->bytes_total >= 12 &&
762 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
764 DEBUG_1("Using custom jpeg xl loader");
765 image_loader_backend_set_jpegxl(&il->backend);
768 if (il->bytes_total >= 2 &&
769 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
771 DEBUG_1("Using custom jpeg xl loader");
772 image_loader_backend_set_jpegxl(&il->backend);
776 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
777 (file_extension_match(il->fd->path, ".scr")))
779 DEBUG_1("Using custom zxscr loader");
780 image_loader_backend_set_zxscr(&il->backend);
783 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
785 DEBUG_1("Using custom collection loader");
786 image_loader_backend_set_collection(&il->backend);
789 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
791 DEBUG_1("Using custom svgz loader");
792 image_loader_backend_set_svgz(&il->backend);
795 image_loader_backend_set_default(&il->backend);
797 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
800 format = il->backend.get_format_name(il->loader);
801 if (g_strcmp0(format, "tiff") == 0)
803 il->backend.set_page_num(il->loader, il->fd->page_num);
809 format = il->backend.get_format_name(il->loader);
810 if (g_strcmp0(format, "pdf") == 0)
812 il->backend.set_page_num(il->loader, il->fd->page_num);
818 format = il->backend.get_format_name(il->loader);
819 if (g_strcmp0(format, "heif") == 0)
821 il->backend.set_page_num(il->loader, il->fd->page_num);
827 format = il->backend.get_format_name(il->loader);
828 if (g_strcmp0(format, "djvu") == 0)
830 il->backend.set_page_num(il->loader, il->fd->page_num);
835 il->fd->format_name = il->backend.get_format_name(il->loader);
837 g_mutex_unlock(il->data_mutex);
841 static void image_loader_done(ImageLoader *il)
843 image_loader_stop_loader(il);
845 image_loader_emit_done(il);
848 static void image_loader_error(ImageLoader *il)
850 image_loader_stop_loader(il);
852 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
854 image_loader_emit_error(il);
857 static gboolean image_loader_continue(ImageLoader *il)
862 if (!il) return FALSE;
864 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
865 while (c > 0 && !image_loader_get_stopping(il))
867 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
871 image_loader_done(il);
875 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
877 image_loader_error(il);
886 if (il->bytes_total > 0)
888 image_loader_emit_percent(il);
894 static gboolean image_loader_begin(ImageLoader *il)
896 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
901 if (il->pixbuf) return FALSE;
903 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
904 if (b < 1) return FALSE;
906 image_loader_setup_loader(il);
908 g_assert(il->bytes_read == 0);
909 if (il->backend.load) {
911 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
913 image_loader_stop_loader(il);
917 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
919 image_loader_stop_loader(il);
924 format = il->backend.get_format_name(il->loader);
925 if (g_strcmp0(format, "pdf") == 0)
927 gint i = il->backend.get_page_total(il->loader);
928 file_data_set_page_total(il->fd, i);
933 format = il->backend.get_format_name(il->loader);
934 if (g_strcmp0(format, "heif") == 0)
936 gint i = il->backend.get_page_total(il->loader);
937 file_data_set_page_total(il->fd, i);
942 format = il->backend.get_format_name(il->loader);
943 if (g_strcmp0(format, "djvu") == 0)
945 gint i = il->backend.get_page_total(il->loader);
946 file_data_set_page_total(il->fd, i);
951 format = il->backend.get_format_name(il->loader);
952 if (g_strcmp0(format, "tiff") == 0)
954 gint i = il->backend.get_page_total(il->loader);
955 file_data_set_page_total(il->fd, i);
962 /* read until size is known */
963 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
965 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
966 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
968 image_loader_stop_loader(il);
973 if (!il->pixbuf) image_loader_sync_pixbuf(il);
975 if (il->bytes_read == il->bytes_total || b < 1)
977 /* done, handle (broken) loaders that do not have pixbuf till close */
978 image_loader_stop_loader(il);
980 if (!il->pixbuf) return FALSE;
982 image_loader_done(il);
988 image_loader_stop_loader(il);
995 /**************************************************************************************/
996 /* the following functions are always executed in the main thread */
999 static gboolean image_loader_setup_source(ImageLoader *il)
1004 if (!il || il->loader || il->mapped_file) return FALSE;
1006 il->mapped_file = NULL;
1010 ExifData *exif = exif_read_fd(il->fd);
1012 if (options->thumbnails.use_exif)
1014 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
1016 if (il->mapped_file)
1018 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1023 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
1025 if (il->mapped_file)
1027 /* Both exiv2 and libraw sometimes return a pointer to a file
1028 * section that is not a jpeg */
1029 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1031 il->mapped_file = NULL;
1035 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1040 /* If exiv2 does not find a thumbnail, try libraw (which may be slower) */
1041 if (!il->mapped_file)
1043 il->mapped_file = libraw_get_preview(il, (guint *)&il->bytes_total);
1045 if (il->mapped_file)
1047 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1049 il->mapped_file = NULL;
1053 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1058 if (il->mapped_file)
1060 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1062 exif_free_fd(il->fd, exif);
1066 if (!il->mapped_file)
1071 pathl = path_from_utf8(il->fd->path);
1072 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1074 if (load_fd == -1) return FALSE;
1076 if (fstat(load_fd, &st) == 0)
1078 il->bytes_total = st.st_size;
1086 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
1088 if (il->mapped_file == MAP_FAILED)
1090 il->mapped_file = 0;
1093 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1099 static void image_loader_stop_source(ImageLoader *il)
1103 if (il->mapped_file)
1105 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1107 exif_free_preview(il->mapped_file);
1109 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1111 libraw_free_preview(il->mapped_file);
1115 munmap(il->mapped_file, il->bytes_total);
1117 il->mapped_file = NULL;
1121 static void image_loader_stop(ImageLoader *il)
1127 g_source_remove(il->idle_id);
1133 /* stop loader in the other thread */
1134 g_mutex_lock(il->data_mutex);
1135 il->stopping = TRUE;
1136 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1137 g_mutex_unlock(il->data_mutex);
1140 image_loader_stop_loader(il);
1141 image_loader_stop_source(il);
1145 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1147 g_mutex_lock(il->data_mutex);
1148 il->delay_area_ready = enable;
1153 list = g_list_reverse(il->area_param_delayed_list);
1154 il->area_param_delayed_list = NULL;
1155 g_mutex_unlock(il->data_mutex);
1161 ImageLoaderAreaParam *par = work->data;
1164 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1172 g_mutex_unlock(il->data_mutex);
1177 /**************************************************************************************/
1178 /* execution via idle calls */
1180 static gboolean image_loader_idle_cb(gpointer data)
1182 gboolean ret = FALSE;
1183 ImageLoader *il = data;
1187 ret = image_loader_continue(il);
1192 image_loader_stop_source(il);
1199 static gboolean image_loader_start_idle(ImageLoader *il)
1203 if (!il) return FALSE;
1205 if (!il->fd) return FALSE;
1207 if (!image_loader_setup_source(il)) return FALSE;
1209 ret = image_loader_begin(il);
1211 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1215 /**************************************************************************************/
1216 /* execution via thread */
1219 static GThreadPool *image_loader_thread_pool = NULL;
1221 static GCond *image_loader_prio_cond = NULL;
1222 static GMutex *image_loader_prio_mutex = NULL;
1223 static gint image_loader_prio_num = 0;
1226 static void image_loader_thread_enter_high(void)
1228 g_mutex_lock(image_loader_prio_mutex);
1229 image_loader_prio_num++;
1230 g_mutex_unlock(image_loader_prio_mutex);
1233 static void image_loader_thread_leave_high(void)
1235 g_mutex_lock(image_loader_prio_mutex);
1236 image_loader_prio_num--;
1237 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1238 g_mutex_unlock(image_loader_prio_mutex);
1241 static void image_loader_thread_wait_high(void)
1243 g_mutex_lock(image_loader_prio_mutex);
1244 while (image_loader_prio_num)
1246 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1249 g_mutex_unlock(image_loader_prio_mutex);
1253 static void image_loader_thread_run(gpointer data, gpointer user_data)
1255 ImageLoader *il = data;
1259 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1261 /* low prio, wait until high prio tasks finishes */
1262 image_loader_thread_wait_high();
1267 image_loader_thread_enter_high();
1270 err = !image_loader_begin(il);
1275 loader failed, we have to send signal
1276 (idle mode returns the image_loader_begin return value directly)
1277 (success is always reported indirectly from image_loader_begin)
1279 image_loader_emit_error(il);
1284 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1286 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1288 /* low prio, wait until high prio tasks finishes */
1289 image_loader_thread_wait_high();
1291 cont = image_loader_continue(il);
1293 image_loader_stop_loader(il);
1295 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1298 image_loader_thread_leave_high();
1301 g_mutex_lock(il->data_mutex);
1302 il->can_destroy = TRUE;
1303 g_cond_signal(il->can_destroy_cond);
1304 g_mutex_unlock(il->data_mutex);
1309 static gboolean image_loader_start_thread(ImageLoader *il)
1311 if (!il) return FALSE;
1313 if (!il->fd) return FALSE;
1317 if (!image_loader_setup_source(il)) return FALSE;
1319 if (!image_loader_thread_pool)
1321 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1322 #if GLIB_CHECK_VERSION(2,32,0)
1323 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1324 g_cond_init(image_loader_prio_cond);
1325 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1326 g_mutex_init(image_loader_prio_mutex);
1328 image_loader_prio_cond = g_cond_new();
1329 image_loader_prio_mutex = g_mutex_new();
1333 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1335 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1336 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1340 #endif /* HAVE_GTHREAD */
1343 /**************************************************************************************/
1344 /* public interface */
1347 gboolean image_loader_start(ImageLoader *il)
1349 if (!il) return FALSE;
1351 if (!il->fd) return FALSE;
1354 return image_loader_start_thread(il);
1356 return image_loader_start_idle(il);
1361 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1362 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1365 if (!il) return NULL;
1367 g_mutex_lock(il->data_mutex);
1369 g_mutex_unlock(il->data_mutex);
1373 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1377 g_mutex_lock(il->data_mutex);
1378 il->requested_width = width;
1379 il->requested_height = height;
1380 g_mutex_unlock(il->data_mutex);
1383 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1387 g_mutex_lock(il->data_mutex);
1388 il->idle_read_loop_count = count ? count : 1;
1389 g_mutex_unlock(il->data_mutex);
1392 void image_loader_set_priority(ImageLoader *il, gint priority)
1396 if (il->thread) return; /* can't change prio if the thread already runs */
1397 il->idle_priority = priority;
1401 gdouble image_loader_get_percent(ImageLoader *il)
1404 if (!il) return 0.0;
1406 g_mutex_lock(il->data_mutex);
1407 if (il->bytes_total == 0)
1413 ret = (gdouble)il->bytes_read / il->bytes_total;
1415 g_mutex_unlock(il->data_mutex);
1419 gboolean image_loader_get_is_done(ImageLoader *il)
1422 if (!il) return FALSE;
1424 g_mutex_lock(il->data_mutex);
1426 g_mutex_unlock(il->data_mutex);
1431 FileData *image_loader_get_fd(ImageLoader *il)
1434 if (!il) return NULL;
1436 g_mutex_lock(il->data_mutex);
1438 g_mutex_unlock(il->data_mutex);
1443 gboolean image_loader_get_shrunk(ImageLoader *il)
1446 if (!il) return FALSE;
1448 g_mutex_lock(il->data_mutex);
1450 g_mutex_unlock(il->data_mutex);
1454 const gchar *image_loader_get_error(ImageLoader *il)
1456 const gchar *ret = NULL;
1457 if (!il) return NULL;
1458 g_mutex_lock(il->data_mutex);
1459 if (il->error) ret = il->error->message;
1460 g_mutex_unlock(il->data_mutex);
1466 * @FIXME this can be rather slow and blocks until the size is known
1468 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1473 il = image_loader_new(fd);
1475 success = image_loader_start_idle(il);
1477 if (success && il->pixbuf)
1479 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1480 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1484 if (width) *width = -1;
1485 if (height) *height = -1;
1488 image_loader_free(il);
1492 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */