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 *loader_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 UNUSED(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;
125 il->data_mutex = g_new(GMutex, 1);
126 g_mutex_init(il->data_mutex);
127 il->can_destroy_cond = g_new(GCond, 1);
128 g_cond_init(il->can_destroy_cond);
130 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", (void *)il, il->read_buffer_size, il->idle_read_loop_count);
133 static void image_loader_class_init_wrapper(void *data, void *UNUSED(user_data))
135 image_loader_class_init(data);
138 static void image_loader_class_init(ImageLoaderClass *loader_class)
140 GObjectClass *gobject_class = G_OBJECT_CLASS (loader_class);
142 // gobject_class->set_property = image_loader_set_property;
143 // gobject_class->get_property = image_loader_get_property;
145 gobject_class->finalize = image_loader_finalize;
148 signals[SIGNAL_AREA_READY] =
149 g_signal_new("area_ready",
150 G_OBJECT_CLASS_TYPE(gobject_class),
152 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
154 gq_marshal_VOID__INT_INT_INT_INT,
161 signals[SIGNAL_ERROR] =
162 g_signal_new("error",
163 G_OBJECT_CLASS_TYPE(gobject_class),
165 G_STRUCT_OFFSET(ImageLoaderClass, error),
167 g_cclosure_marshal_VOID__VOID,
170 signals[SIGNAL_DONE] =
172 G_OBJECT_CLASS_TYPE(gobject_class),
174 G_STRUCT_OFFSET(ImageLoaderClass, done),
176 g_cclosure_marshal_VOID__VOID,
179 signals[SIGNAL_PERCENT] =
180 g_signal_new("percent",
181 G_OBJECT_CLASS_TYPE(gobject_class),
183 G_STRUCT_OFFSET(ImageLoaderClass, percent),
185 g_cclosure_marshal_VOID__DOUBLE,
189 signals[SIGNAL_SIZE] =
190 g_signal_new("size_prepared",
191 G_OBJECT_CLASS_TYPE(gobject_class),
193 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
195 gq_marshal_VOID__INT_INT,
202 static void image_loader_finalize(GObject *object)
204 ImageLoader *il = (ImageLoader *)object;
206 image_loader_stop(il);
208 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
210 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, (void *)il, il->bytes_read);
212 if (il->idle_done_id)
214 g_source_remove(il->idle_done_id);
215 il->idle_done_id = 0;
218 while (g_source_remove_by_user_data(il))
220 DEBUG_2("pending signals detected");
223 while (il->area_param_list)
225 DEBUG_1("pending area_ready signals detected");
226 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
227 g_free(il->area_param_list->data);
228 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
231 while (il->area_param_delayed_list)
233 g_free(il->area_param_delayed_list->data);
234 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
237 if (il->pixbuf) g_object_unref(il->pixbuf);
239 if (il->error) g_error_free(il->error);
241 file_data_unref(il->fd);
243 g_mutex_clear(il->data_mutex);
244 g_free(il->data_mutex);
245 g_cond_clear(il->can_destroy_cond);
246 g_free(il->can_destroy_cond);
249 void image_loader_free(ImageLoader *il)
252 g_object_unref(G_OBJECT(il));
256 ImageLoader *image_loader_new(FileData *fd)
260 if (!fd) return NULL;
262 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
264 il->fd = file_data_ref(fd);
269 /**************************************************************************************/
270 /* send signals via idle calbacks - the callback are executed in the main thread */
272 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
273 struct _ImageLoaderAreaParam {
282 static gboolean image_loader_emit_area_ready_cb(gpointer data)
284 ImageLoaderAreaParam *par = data;
285 ImageLoader *il = par->il;
287 g_mutex_lock(il->data_mutex);
288 il->area_param_list = g_list_remove(il->area_param_list, par);
294 g_mutex_unlock(il->data_mutex);
296 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
301 static gboolean image_loader_emit_done_cb(gpointer data)
303 ImageLoader *il = data;
304 g_signal_emit(il, signals[SIGNAL_DONE], 0);
308 static gboolean image_loader_emit_error_cb(gpointer data)
310 ImageLoader *il = data;
311 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
315 static gboolean image_loader_emit_percent_cb(gpointer data)
317 ImageLoader *il = data;
318 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
322 static gboolean image_loader_emit_size_cb(gpointer data)
325 ImageLoader *il = data;
326 g_mutex_lock(il->data_mutex);
327 width = il->actual_width;
328 height = il->actual_height;
329 g_mutex_unlock(il->data_mutex);
330 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
335 /* DONE and ERROR are emitted only once, thus they can have normal priority
336 PERCENT and AREA_READY should be processed ASAP
339 static void image_loader_emit_done(ImageLoader *il)
341 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
344 static void image_loader_emit_error(ImageLoader *il)
346 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
349 static void image_loader_emit_percent(ImageLoader *il)
351 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
354 static void image_loader_emit_size(ImageLoader *il)
356 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
359 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
363 ImageLoaderAreaParam *prev_par = (*list)->data;
364 if (prev_par->x == x && prev_par->w == w &&
365 prev_par->y + prev_par->h == y)
367 /* we can merge the notifications */
371 if (prev_par->x == x && prev_par->w == w &&
372 y + h == prev_par->y)
374 /* we can merge the notifications */
379 if (prev_par->y == y && prev_par->h == h &&
380 prev_par->x + prev_par->w == x)
382 /* we can merge the notifications */
386 if (prev_par->y == y && prev_par->h == h &&
387 x + w == prev_par->x)
389 /* we can merge the notifications */
396 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
403 *list = g_list_prepend(*list, par);
407 /* this function expects that il->data_mutex is locked by caller */
408 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
410 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
414 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
418 /**************************************************************************************/
419 /* the following functions may be executed in separate thread */
421 /* this function expects that il->data_mutex is locked by caller */
422 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
424 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
429 static gboolean image_loader_get_stopping(ImageLoader *il)
432 if (!il) return FALSE;
434 g_mutex_lock(il->data_mutex);
436 g_mutex_unlock(il->data_mutex);
442 static void image_loader_sync_pixbuf(ImageLoader *il)
446 g_mutex_lock(il->data_mutex);
450 g_mutex_unlock(il->data_mutex);
454 pb = il->backend.get_pixbuf(il->loader);
456 if (pb == il->pixbuf)
458 g_mutex_unlock(il->data_mutex);
462 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
464 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
467 if (il->pixbuf) g_object_unref(il->pixbuf);
470 if (il->pixbuf) g_object_ref(il->pixbuf);
472 g_mutex_unlock(il->data_mutex);
475 static void image_loader_area_updated_cb(gpointer UNUSED(loader),
476 guint x, guint y, guint w, guint h,
479 ImageLoader *il = data;
481 if (!image_loader_get_pixbuf(il))
483 image_loader_sync_pixbuf(il);
484 if (!image_loader_get_pixbuf(il))
486 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
490 g_mutex_lock(il->data_mutex);
491 if (il->delay_area_ready)
492 image_loader_queue_delayed_area_ready(il, x, y, w, h);
494 image_loader_emit_area_ready(il, x, y, w, h);
496 if (il->stopping) il->backend.abort(il->loader);
498 g_mutex_unlock(il->data_mutex);
501 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
503 ImageLoader *il = data;
509 https://bugzilla.gnome.org/show_bug.cgi?id=547669
510 https://bugzilla.gnome.org/show_bug.cgi?id=589334
512 gchar *format = il->backend.get_format_name(loader);
513 if (strcmp(format, "svg") == 0 ||
514 strcmp(format, "xpm") == 0)
522 pb = il->backend.get_pixbuf(loader);
524 h = gdk_pixbuf_get_height(pb);
525 rs = gdk_pixbuf_get_rowstride(pb);
526 pix = gdk_pixbuf_get_pixels(pb);
528 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
532 static void image_loader_size_cb(gpointer loader,
533 gint width, gint height, gpointer data)
535 ImageLoader *il = data;
537 gboolean scale = FALSE;
540 g_mutex_lock(il->data_mutex);
541 il->actual_width = width;
542 il->actual_height = height;
543 if (il->requested_width < 1 || il->requested_height < 1)
545 g_mutex_unlock(il->data_mutex);
546 image_loader_emit_size(il);
549 g_mutex_unlock(il->data_mutex);
551 #ifdef HAVE_FFMPEGTHUMBNAILER
552 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
555 mime_types = il->backend.get_format_mime_types(loader);
557 while (mime_types[n] && !scale)
559 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
562 g_strfreev(mime_types);
566 image_loader_emit_size(il);
570 g_mutex_lock(il->data_mutex);
573 if (width > il->requested_width || height > il->requested_height)
576 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
578 nw = il->requested_width;
579 nh = (gdouble)nw / width * height;
584 nh = il->requested_height;
585 nw = (gdouble)nh / height * width;
589 il->actual_width = nw;
590 il->actual_height = nh;
591 il->backend.set_size(loader, nw, nh);
595 g_mutex_unlock(il->data_mutex);
596 image_loader_emit_size(il);
599 static void image_loader_stop_loader(ImageLoader *il)
605 /* some loaders do not have a pixbuf till close, order is important here */
606 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
607 image_loader_sync_pixbuf(il);
608 il->backend.free(il->loader);
611 g_mutex_lock(il->data_mutex);
613 g_mutex_unlock(il->data_mutex);
616 static void image_loader_setup_loader(ImageLoader *il)
618 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
622 gint external_preview = 1;
624 g_mutex_lock(il->data_mutex);
626 if (options->external_preview.enable)
629 gchar *tilde_filename;
631 tilde_filename = expand_tilde(options->external_preview.select);
633 cmd_line = g_strdup_printf("\"%s\" \"%s\"" , tilde_filename, il->fd->path);
635 external_preview = runcmd(cmd_line);
637 g_free(tilde_filename);
640 if (external_preview == 0)
642 DEBUG_1("Using custom external loader");
643 image_loader_backend_set_external(&il->backend);
647 #ifdef HAVE_FFMPEGTHUMBNAILER
648 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
650 DEBUG_1("Using custom ffmpegthumbnailer loader");
651 image_loader_backend_set_ft(&il->backend);
656 if (il->bytes_total >= 4 &&
657 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
659 DEBUG_1("Using custom pdf loader");
660 image_loader_backend_set_pdf(&il->backend);
665 if (il->bytes_total >= 12 &&
666 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
667 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
668 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0) ||
669 (memcmp(il->mapped_file + 4, "ftypavif", 8) == 0)))
671 DEBUG_1("Using custom heif loader");
672 image_loader_backend_set_heif(&il->backend);
677 if (il->bytes_total >= 12 &&
678 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
679 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
681 DEBUG_1("Using custom webp loader");
682 image_loader_backend_set_webp(&il->backend);
687 if (il->bytes_total >= 16 &&
688 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
689 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
691 DEBUG_1("Using custom djvu loader");
692 image_loader_backend_set_djvu(&il->backend);
697 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
699 DEBUG_1("Using custom jpeg loader");
700 image_loader_backend_set_jpeg(&il->backend);
704 if (il->bytes_total >= 11 &&
705 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
706 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
708 DEBUG_1("Using custom cr3 loader");
709 image_loader_backend_set_cr3(&il->backend);
715 if (il->bytes_total >= 10 &&
716 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
717 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
718 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
719 memcmp(il->mapped_file, "II*\0", 4) == 0))
721 DEBUG_1("Using custom tiff loader");
722 image_loader_backend_set_tiff(&il->backend);
726 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
728 DEBUG_1("Using dds loader");
729 image_loader_backend_set_dds(&il->backend);
732 if (il->bytes_total >= 6 &&
733 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
735 DEBUG_1("Using custom psd loader");
736 image_loader_backend_set_psd(&il->backend);
740 if (il->bytes_total >= 12 &&
741 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
743 DEBUG_1("Using custom j2k loader");
744 image_loader_backend_set_j2k(&il->backend);
749 if (il->bytes_total >= 12 &&
750 (memcmp(il->mapped_file, "\0\0\0\x0C\x4A\x58\x4C\x20\x0D\x0A\x87\x0A", 12) == 0))
752 DEBUG_1("Using custom jpeg xl loader");
753 image_loader_backend_set_jpegxl(&il->backend);
756 if (il->bytes_total >= 2 &&
757 (memcmp(il->mapped_file, "\xFF\x0A", 2) == 0))
759 DEBUG_1("Using custom jpeg xl loader");
760 image_loader_backend_set_jpegxl(&il->backend);
764 if ((il->bytes_total == 6144 || il->bytes_total == 6912) &&
765 (file_extension_match(il->fd->path, ".scr")))
767 DEBUG_1("Using custom zxscr loader");
768 image_loader_backend_set_zxscr(&il->backend);
771 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
773 DEBUG_1("Using custom collection loader");
774 image_loader_backend_set_collection(&il->backend);
777 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
779 DEBUG_1("Using custom svgz loader");
780 image_loader_backend_set_svgz(&il->backend);
783 image_loader_backend_set_default(&il->backend);
785 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
788 format = il->backend.get_format_name(il->loader);
789 if (g_strcmp0(format, "tiff") == 0)
791 il->backend.set_page_num(il->loader, il->fd->page_num);
797 format = il->backend.get_format_name(il->loader);
798 if (g_strcmp0(format, "pdf") == 0)
800 il->backend.set_page_num(il->loader, il->fd->page_num);
806 format = il->backend.get_format_name(il->loader);
807 if (g_strcmp0(format, "heif") == 0)
809 il->backend.set_page_num(il->loader, il->fd->page_num);
815 format = il->backend.get_format_name(il->loader);
816 if (g_strcmp0(format, "djvu") == 0)
818 il->backend.set_page_num(il->loader, il->fd->page_num);
823 il->fd->format_name = il->backend.get_format_name(il->loader);
825 g_mutex_unlock(il->data_mutex);
829 static void image_loader_done(ImageLoader *il)
831 image_loader_stop_loader(il);
833 image_loader_emit_done(il);
836 static void image_loader_error(ImageLoader *il)
838 image_loader_stop_loader(il);
840 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
842 image_loader_emit_error(il);
845 static gboolean image_loader_continue(ImageLoader *il)
850 if (!il) return FALSE;
852 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
853 while (c > 0 && !image_loader_get_stopping(il))
855 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
859 image_loader_done(il);
863 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
865 image_loader_error(il);
874 if (il->bytes_total > 0)
876 image_loader_emit_percent(il);
882 static gboolean image_loader_begin(ImageLoader *il)
884 #if defined HAVE_TIFF || defined HAVE_PDF || defined HAVE_HEIF || defined HAVE_DJVU
889 if (il->pixbuf) return FALSE;
891 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
892 if (b < 1) return FALSE;
894 image_loader_setup_loader(il);
896 g_assert(il->bytes_read == 0);
897 if (il->backend.load) {
899 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
901 image_loader_stop_loader(il);
905 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
907 image_loader_stop_loader(il);
912 format = il->backend.get_format_name(il->loader);
913 if (g_strcmp0(format, "pdf") == 0)
915 gint i = il->backend.get_page_total(il->loader);
916 file_data_set_page_total(il->fd, i);
921 format = il->backend.get_format_name(il->loader);
922 if (g_strcmp0(format, "heif") == 0)
924 gint i = il->backend.get_page_total(il->loader);
925 file_data_set_page_total(il->fd, i);
930 format = il->backend.get_format_name(il->loader);
931 if (g_strcmp0(format, "djvu") == 0)
933 gint i = il->backend.get_page_total(il->loader);
934 file_data_set_page_total(il->fd, i);
939 format = il->backend.get_format_name(il->loader);
940 if (g_strcmp0(format, "tiff") == 0)
942 gint i = il->backend.get_page_total(il->loader);
943 file_data_set_page_total(il->fd, i);
950 /* read until size is known */
951 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
953 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
954 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
956 image_loader_stop_loader(il);
961 if (!il->pixbuf) image_loader_sync_pixbuf(il);
963 if (il->bytes_read == il->bytes_total || b < 1)
965 /* done, handle (broken) loaders that do not have pixbuf till close */
966 image_loader_stop_loader(il);
968 if (!il->pixbuf) return FALSE;
970 image_loader_done(il);
976 image_loader_stop_loader(il);
983 /**************************************************************************************/
984 /* the following functions are always executed in the main thread */
987 static gboolean image_loader_setup_source(ImageLoader *il)
992 if (!il || il->loader || il->mapped_file) return FALSE;
994 il->mapped_file = NULL;
998 ExifData *exif = exif_read_fd(il->fd);
1000 if (options->thumbnails.use_exif)
1002 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
1004 if (il->mapped_file)
1006 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1011 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
1013 if (il->mapped_file)
1015 /* Both exiv2 and libraw sometimes return a pointer to a file
1016 * section that is not a jpeg */
1017 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1019 il->mapped_file = NULL;
1023 il->preview = IMAGE_LOADER_PREVIEW_EXIF;
1028 /* If exiv2 does not find a thumbnail, try libraw (which may be slower) */
1029 if (!il->mapped_file)
1031 il->mapped_file = libraw_get_preview(il, (guint *)&il->bytes_total);
1033 if (il->mapped_file)
1035 if (!(il->mapped_file[0] == 0xFF && il->mapped_file[1] == 0xD8))
1037 il->mapped_file = NULL;
1041 il->preview = IMAGE_LOADER_PREVIEW_LIBRAW;
1046 if (il->mapped_file)
1048 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
1050 exif_free_fd(il->fd, exif);
1054 if (!il->mapped_file)
1059 pathl = path_from_utf8(il->fd->path);
1060 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
1062 if (load_fd == -1) return FALSE;
1064 if (fstat(load_fd, &st) == 0)
1066 il->bytes_total = st.st_size;
1074 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
1076 if (il->mapped_file == MAP_FAILED)
1078 il->mapped_file = 0;
1081 il->preview = IMAGE_LOADER_PREVIEW_NONE;
1087 static void image_loader_stop_source(ImageLoader *il)
1091 if (il->mapped_file)
1093 if (il->preview == IMAGE_LOADER_PREVIEW_EXIF)
1095 exif_free_preview(il->mapped_file);
1097 else if (il->preview == IMAGE_LOADER_PREVIEW_LIBRAW)
1099 libraw_free_preview(il->mapped_file);
1103 munmap(il->mapped_file, il->bytes_total);
1105 il->mapped_file = NULL;
1109 static void image_loader_stop(ImageLoader *il)
1115 g_source_remove(il->idle_id);
1121 /* stop loader in the other thread */
1122 g_mutex_lock(il->data_mutex);
1123 il->stopping = TRUE;
1124 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1125 g_mutex_unlock(il->data_mutex);
1128 image_loader_stop_loader(il);
1129 image_loader_stop_source(il);
1133 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1135 g_mutex_lock(il->data_mutex);
1136 il->delay_area_ready = enable;
1141 list = g_list_reverse(il->area_param_delayed_list);
1142 il->area_param_delayed_list = NULL;
1143 g_mutex_unlock(il->data_mutex);
1149 ImageLoaderAreaParam *par = work->data;
1152 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1160 g_mutex_unlock(il->data_mutex);
1165 /**************************************************************************************/
1166 /* execution via idle calls */
1168 static gboolean image_loader_idle_cb(gpointer data)
1170 gboolean ret = FALSE;
1171 ImageLoader *il = data;
1175 ret = image_loader_continue(il);
1180 image_loader_stop_source(il);
1187 static gboolean image_loader_start_idle(ImageLoader *il)
1191 if (!il) return FALSE;
1193 if (!il->fd) return FALSE;
1195 if (!image_loader_setup_source(il)) return FALSE;
1197 ret = image_loader_begin(il);
1199 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1203 /**************************************************************************************/
1204 /* execution via thread */
1206 static GThreadPool *image_loader_thread_pool = NULL;
1208 static GCond *image_loader_prio_cond = NULL;
1209 static GMutex *image_loader_prio_mutex = NULL;
1210 static gint image_loader_prio_num = 0;
1213 static void image_loader_thread_enter_high(void)
1215 g_mutex_lock(image_loader_prio_mutex);
1216 image_loader_prio_num++;
1217 g_mutex_unlock(image_loader_prio_mutex);
1220 static void image_loader_thread_leave_high(void)
1222 g_mutex_lock(image_loader_prio_mutex);
1223 image_loader_prio_num--;
1224 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1225 g_mutex_unlock(image_loader_prio_mutex);
1228 static void image_loader_thread_wait_high(void)
1230 g_mutex_lock(image_loader_prio_mutex);
1231 while (image_loader_prio_num)
1233 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1236 g_mutex_unlock(image_loader_prio_mutex);
1240 static void image_loader_thread_run(gpointer data, gpointer UNUSED(user_data))
1242 ImageLoader *il = data;
1246 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1248 /* low prio, wait until high prio tasks finishes */
1249 image_loader_thread_wait_high();
1254 image_loader_thread_enter_high();
1257 err = !image_loader_begin(il);
1262 loader failed, we have to send signal
1263 (idle mode returns the image_loader_begin return value directly)
1264 (success is always reported indirectly from image_loader_begin)
1266 image_loader_emit_error(il);
1271 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1273 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1275 /* low prio, wait until high prio tasks finishes */
1276 image_loader_thread_wait_high();
1278 cont = image_loader_continue(il);
1280 image_loader_stop_loader(il);
1282 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1285 image_loader_thread_leave_high();
1288 g_mutex_lock(il->data_mutex);
1289 il->can_destroy = TRUE;
1290 g_cond_signal(il->can_destroy_cond);
1291 g_mutex_unlock(il->data_mutex);
1296 static gboolean image_loader_start_thread(ImageLoader *il)
1298 if (!il) return FALSE;
1300 if (!il->fd) return FALSE;
1304 if (!image_loader_setup_source(il)) return FALSE;
1306 if (!image_loader_thread_pool)
1308 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1309 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1310 g_cond_init(image_loader_prio_cond);
1311 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1312 g_mutex_init(image_loader_prio_mutex);
1315 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1317 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1318 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1324 /**************************************************************************************/
1325 /* public interface */
1328 gboolean image_loader_start(ImageLoader *il)
1330 if (!il) return FALSE;
1332 if (!il->fd) return FALSE;
1334 return image_loader_start_thread(il);
1338 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1339 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1342 if (!il) return NULL;
1344 g_mutex_lock(il->data_mutex);
1346 g_mutex_unlock(il->data_mutex);
1350 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1354 g_mutex_lock(il->data_mutex);
1355 il->requested_width = width;
1356 il->requested_height = height;
1357 g_mutex_unlock(il->data_mutex);
1360 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1364 g_mutex_lock(il->data_mutex);
1365 il->idle_read_loop_count = count ? count : 1;
1366 g_mutex_unlock(il->data_mutex);
1369 void image_loader_set_priority(ImageLoader *il, gint priority)
1373 if (il->thread) return; /* can't change prio if the thread already runs */
1374 il->idle_priority = priority;
1378 gdouble image_loader_get_percent(ImageLoader *il)
1381 if (!il) return 0.0;
1383 g_mutex_lock(il->data_mutex);
1384 if (il->bytes_total == 0)
1390 ret = (gdouble)il->bytes_read / il->bytes_total;
1392 g_mutex_unlock(il->data_mutex);
1396 gboolean image_loader_get_is_done(ImageLoader *il)
1399 if (!il) return FALSE;
1401 g_mutex_lock(il->data_mutex);
1403 g_mutex_unlock(il->data_mutex);
1408 FileData *image_loader_get_fd(ImageLoader *il)
1411 if (!il) return NULL;
1413 g_mutex_lock(il->data_mutex);
1415 g_mutex_unlock(il->data_mutex);
1420 gboolean image_loader_get_shrunk(ImageLoader *il)
1423 if (!il) return FALSE;
1425 g_mutex_lock(il->data_mutex);
1427 g_mutex_unlock(il->data_mutex);
1431 const gchar *image_loader_get_error(ImageLoader *il)
1433 const gchar *ret = NULL;
1434 if (!il) return NULL;
1435 g_mutex_lock(il->data_mutex);
1436 if (il->error) ret = il->error->message;
1437 g_mutex_unlock(il->data_mutex);
1441 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1444 gint width_file = 0;
1445 gint height_file = 0;
1447 gdk_pixbuf_get_file_info(fd->path, &width_file, &height_file);
1449 if (width_file && height_file)
1451 *width = width_file;
1452 *height = height_file;
1464 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */