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_pdf.h"
31 #include "image_load_psd.h"
32 #include "image_load_heif.h"
33 #include "image_load_ffmpegthumbnailer.h"
34 #include "image_load_collection.h"
35 #include "image_load_webp.h"
36 #include "image_load_j2k.h"
37 #include "image_load_svgz.h"
41 #include "ui_fileops.h"
42 #include "gq-marshal.h"
47 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
48 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
51 /**************************************************************************************/
52 /* image loader class */
56 SIGNAL_AREA_READY = 0,
64 static guint signals[SIGNAL_COUNT] = { 0 };
66 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
67 static void image_loader_class_init_wrapper(void *data, void *user_data);
68 static void image_loader_class_init(ImageLoaderClass *class);
69 static void image_loader_finalize(GObject *object);
70 static void image_loader_stop(ImageLoader *il);
72 GType image_loader_get_type(void)
74 static GType type = 0;
77 static const GTypeInfo info = {
78 sizeof(ImageLoaderClass),
80 NULL, /* base_finalize */
81 (GClassInitFunc)image_loader_class_init_wrapper, /* class_init */
82 NULL, /* class_finalize */
83 NULL, /* class_data */
86 (GInstanceInitFunc)image_loader_init, /* instance_init */
87 NULL /* value_table */
89 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
94 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
96 ImageLoader *il = (ImageLoader *)instance;
100 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
107 il->idle_done_id = 0;
109 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
110 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
111 il->mapped_file = NULL;
113 il->requested_width = 0;
114 il->requested_height = 0;
115 il->actual_width = 0;
116 il->actual_height = 0;
119 il->can_destroy = TRUE;
122 #if GLIB_CHECK_VERSION(2,32,0)
123 il->data_mutex = g_new(GMutex, 1);
124 g_mutex_init(il->data_mutex);
125 il->can_destroy_cond = g_new(GCond, 1);
126 g_cond_init(il->can_destroy_cond);
128 il->data_mutex = g_mutex_new();
129 il->can_destroy_cond = g_cond_new();
132 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
135 static void image_loader_class_init_wrapper(void *data, void *user_data)
137 image_loader_class_init(data);
140 static void image_loader_class_init(ImageLoaderClass *class)
142 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
144 // gobject_class->set_property = image_loader_set_property;
145 // gobject_class->get_property = image_loader_get_property;
147 gobject_class->finalize = image_loader_finalize;
150 signals[SIGNAL_AREA_READY] =
151 g_signal_new("area_ready",
152 G_OBJECT_CLASS_TYPE(gobject_class),
154 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
156 gq_marshal_VOID__INT_INT_INT_INT,
163 signals[SIGNAL_ERROR] =
164 g_signal_new("error",
165 G_OBJECT_CLASS_TYPE(gobject_class),
167 G_STRUCT_OFFSET(ImageLoaderClass, error),
169 g_cclosure_marshal_VOID__VOID,
172 signals[SIGNAL_DONE] =
174 G_OBJECT_CLASS_TYPE(gobject_class),
176 G_STRUCT_OFFSET(ImageLoaderClass, done),
178 g_cclosure_marshal_VOID__VOID,
181 signals[SIGNAL_PERCENT] =
182 g_signal_new("percent",
183 G_OBJECT_CLASS_TYPE(gobject_class),
185 G_STRUCT_OFFSET(ImageLoaderClass, percent),
187 g_cclosure_marshal_VOID__DOUBLE,
191 signals[SIGNAL_SIZE] =
192 g_signal_new("size_prepared",
193 G_OBJECT_CLASS_TYPE(gobject_class),
195 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
197 gq_marshal_VOID__INT_INT,
204 static void image_loader_finalize(GObject *object)
206 ImageLoader *il = (ImageLoader *)object;
208 image_loader_stop(il);
210 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
212 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
214 if (il->idle_done_id)
216 g_source_remove(il->idle_done_id);
217 il->idle_done_id = 0;
220 while (g_source_remove_by_user_data(il))
222 DEBUG_2("pending signals detected");
225 while (il->area_param_list)
227 DEBUG_1("pending area_ready signals detected");
228 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
229 g_free(il->area_param_list->data);
230 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
233 while (il->area_param_delayed_list)
235 g_free(il->area_param_delayed_list->data);
236 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
239 if (il->pixbuf) g_object_unref(il->pixbuf);
241 if (il->error) g_error_free(il->error);
243 file_data_unref(il->fd);
245 #if GLIB_CHECK_VERSION(2,32,0)
246 g_mutex_clear(il->data_mutex);
247 g_free(il->data_mutex);
248 g_cond_clear(il->can_destroy_cond);
249 g_free(il->can_destroy_cond);
251 g_mutex_free(il->data_mutex);
252 g_cond_free(il->can_destroy_cond);
257 void image_loader_free(ImageLoader *il)
260 g_object_unref(G_OBJECT(il));
264 ImageLoader *image_loader_new(FileData *fd)
268 if (!fd) return NULL;
270 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
272 il->fd = file_data_ref(fd);
277 /**************************************************************************************/
278 /* send signals via idle calbacks - the callback are executed in the main thread */
280 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
281 struct _ImageLoaderAreaParam {
290 static gboolean image_loader_emit_area_ready_cb(gpointer data)
292 ImageLoaderAreaParam *par = data;
293 ImageLoader *il = par->il;
295 g_mutex_lock(il->data_mutex);
296 il->area_param_list = g_list_remove(il->area_param_list, par);
302 g_mutex_unlock(il->data_mutex);
304 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
309 static gboolean image_loader_emit_done_cb(gpointer data)
311 ImageLoader *il = data;
312 g_signal_emit(il, signals[SIGNAL_DONE], 0);
316 static gboolean image_loader_emit_error_cb(gpointer data)
318 ImageLoader *il = data;
319 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
323 static gboolean image_loader_emit_percent_cb(gpointer data)
325 ImageLoader *il = data;
326 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
330 static gboolean image_loader_emit_size_cb(gpointer data)
333 ImageLoader *il = data;
334 g_mutex_lock(il->data_mutex);
335 width = il->actual_width;
336 height = il->actual_height;
337 g_mutex_unlock(il->data_mutex);
338 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
343 /* DONE and ERROR are emited only once, thus they can have normal priority
344 PERCENT and AREA_READY should be processed ASAP
347 static void image_loader_emit_done(ImageLoader *il)
349 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
352 static void image_loader_emit_error(ImageLoader *il)
354 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
357 static void image_loader_emit_percent(ImageLoader *il)
359 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
362 static void image_loader_emit_size(ImageLoader *il)
364 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
367 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
371 ImageLoaderAreaParam *prev_par = (*list)->data;
372 if (prev_par->x == x && prev_par->w == w &&
373 prev_par->y + prev_par->h == y)
375 /* we can merge the notifications */
379 if (prev_par->x == x && prev_par->w == w &&
380 y + h == prev_par->y)
382 /* we can merge the notifications */
387 if (prev_par->y == y && prev_par->h == h &&
388 prev_par->x + prev_par->w == x)
390 /* we can merge the notifications */
394 if (prev_par->y == y && prev_par->h == h &&
395 x + w == prev_par->x)
397 /* we can merge the notifications */
404 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
411 *list = g_list_prepend(*list, par);
415 /* this function expects that il->data_mutex is locked by caller */
416 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
418 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
422 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
426 /**************************************************************************************/
427 /* the following functions may be executed in separate thread */
429 /* this function expects that il->data_mutex is locked by caller */
430 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
432 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
437 static gboolean image_loader_get_stopping(ImageLoader *il)
440 if (!il) return FALSE;
442 g_mutex_lock(il->data_mutex);
444 g_mutex_unlock(il->data_mutex);
450 static void image_loader_sync_pixbuf(ImageLoader *il)
454 g_mutex_lock(il->data_mutex);
458 g_mutex_unlock(il->data_mutex);
462 pb = il->backend.get_pixbuf(il->loader);
464 if (pb == il->pixbuf)
466 g_mutex_unlock(il->data_mutex);
470 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
472 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
475 if (il->pixbuf) g_object_unref(il->pixbuf);
478 if (il->pixbuf) g_object_ref(il->pixbuf);
480 g_mutex_unlock(il->data_mutex);
483 static void image_loader_area_updated_cb(gpointer loader,
484 guint x, guint y, guint w, guint h,
487 ImageLoader *il = data;
489 if (!image_loader_get_pixbuf(il))
491 image_loader_sync_pixbuf(il);
492 if (!image_loader_get_pixbuf(il))
494 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
498 g_mutex_lock(il->data_mutex);
499 if (il->delay_area_ready)
500 image_loader_queue_delayed_area_ready(il, x, y, w, h);
502 image_loader_emit_area_ready(il, x, y, w, h);
504 if (il->stopping) il->backend.abort(il->loader);
506 g_mutex_unlock(il->data_mutex);
509 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
511 ImageLoader *il = data;
517 http://bugzilla.gnome.org/show_bug.cgi?id=547669
518 http://bugzilla.gnome.org/show_bug.cgi?id=589334
520 gchar *format = il->backend.get_format_name(loader);
521 if (strcmp(format, "svg") == 0 ||
522 strcmp(format, "xpm") == 0)
530 pb = il->backend.get_pixbuf(loader);
532 h = gdk_pixbuf_get_height(pb);
533 rs = gdk_pixbuf_get_rowstride(pb);
534 pix = gdk_pixbuf_get_pixels(pb);
536 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
540 static void image_loader_size_cb(gpointer loader,
541 gint width, gint height, gpointer data)
543 ImageLoader *il = data;
545 gboolean scale = FALSE;
548 g_mutex_lock(il->data_mutex);
549 il->actual_width = width;
550 il->actual_height = height;
551 if (il->requested_width < 1 || il->requested_height < 1)
553 g_mutex_unlock(il->data_mutex);
554 image_loader_emit_size(il);
557 g_mutex_unlock(il->data_mutex);
559 #ifdef HAVE_FFMPEGTHUMBNAILER
560 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
563 mime_types = il->backend.get_format_mime_types(loader);
565 while (mime_types[n] && !scale)
567 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
570 g_strfreev(mime_types);
574 image_loader_emit_size(il);
578 g_mutex_lock(il->data_mutex);
581 if (width > il->requested_width || height > il->requested_height)
584 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
586 nw = il->requested_width;
587 nh = (gdouble)nw / width * height;
592 nh = il->requested_height;
593 nw = (gdouble)nh / height * width;
597 il->actual_width = nw;
598 il->actual_height = nh;
599 il->backend.set_size(loader, nw, nh);
603 g_mutex_unlock(il->data_mutex);
604 image_loader_emit_size(il);
607 static void image_loader_stop_loader(ImageLoader *il)
613 /* some loaders do not have a pixbuf till close, order is important here */
614 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
615 image_loader_sync_pixbuf(il);
616 il->backend.free(il->loader);
619 g_mutex_lock(il->data_mutex);
621 g_mutex_unlock(il->data_mutex);
624 static void image_loader_setup_loader(ImageLoader *il)
628 g_mutex_lock(il->data_mutex);
629 #ifdef HAVE_FFMPEGTHUMBNAILER
630 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
632 DEBUG_1("Using custom ffmpegthumbnailer loader");
633 image_loader_backend_set_ft(&il->backend);
638 if (il->bytes_total >= 4 &&
639 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
641 DEBUG_1("Using custom pdf loader");
642 image_loader_backend_set_pdf(&il->backend);
647 if (il->bytes_total >= 12 &&
648 ((memcmp(il->mapped_file + 4, "ftypheic", 8) == 0) ||
649 (memcmp(il->mapped_file + 4, "ftypmsf1", 8) == 0) ||
650 (memcmp(il->mapped_file + 4, "ftypmif1", 8) == 0)))
652 DEBUG_1("Using custom heif loader");
653 image_loader_backend_set_heif(&il->backend);
658 if (il->bytes_total >= 12 &&
659 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
660 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
662 DEBUG_1("Using custom webp loader");
663 image_loader_backend_set_webp(&il->backend);
668 if (il->bytes_total >= 16 &&
669 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
670 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
672 DEBUG_1("Using custom djvu loader");
673 image_loader_backend_set_djvu(&il->backend);
678 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
680 DEBUG_1("Using custom jpeg loader");
681 image_loader_backend_set_jpeg(&il->backend);
684 if (il->bytes_total >= 11 &&
685 (memcmp(il->mapped_file + 4, "ftypcrx", 7) == 0) &&
686 (memcmp(il->mapped_file + 64, "CanonCR3", 8) == 0))
688 DEBUG_1("Using custom cr3 loader");
689 image_loader_backend_set_cr3(&il->backend);
694 if (il->bytes_total >= 10 &&
695 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
696 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
697 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
698 memcmp(il->mapped_file, "II*\0", 4) == 0))
700 DEBUG_1("Using custom tiff loader");
701 image_loader_backend_set_tiff(&il->backend);
705 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
707 DEBUG_1("Using dds loader");
708 image_loader_backend_set_dds(&il->backend);
711 if (il->bytes_total >= 6 &&
712 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
714 DEBUG_1("Using custom psd loader");
715 image_loader_backend_set_psd(&il->backend);
719 if (il->bytes_total >= 12 &&
720 (memcmp(il->mapped_file, "\0\0\0\x0CjP\x20\x20\x0D\x0A\x87\x0A", 12) == 0))
722 DEBUG_1("Using custom j2k loader");
723 image_loader_backend_set_j2k(&il->backend);
727 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
729 DEBUG_1("Using custom collection loader");
730 image_loader_backend_set_collection(&il->backend);
733 if (g_strcmp0(strrchr(il->fd->path, '.'), ".svgz") == 0)
735 DEBUG_1("Using custom svgz loader");
736 image_loader_backend_set_svgz(&il->backend);
739 image_loader_backend_set_default(&il->backend);
741 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
744 format = il->backend.get_format_name(il->loader);
745 if (g_strcmp0(format, "tiff") == 0)
747 il->backend.set_page_num(il->loader, il->fd->page_num);
753 format = il->backend.get_format_name(il->loader);
754 if (g_strcmp0(format, "pdf") == 0)
756 il->backend.set_page_num(il->loader, il->fd->page_num);
762 format = il->backend.get_format_name(il->loader);
763 if (g_strcmp0(format, "heif") == 0)
765 il->backend.set_page_num(il->loader, il->fd->page_num);
771 format = il->backend.get_format_name(il->loader);
772 if (g_strcmp0(format, "djvu") == 0)
774 il->backend.set_page_num(il->loader, il->fd->page_num);
779 g_mutex_unlock(il->data_mutex);
783 static void image_loader_done(ImageLoader *il)
785 image_loader_stop_loader(il);
787 image_loader_emit_done(il);
790 static void image_loader_error(ImageLoader *il)
792 image_loader_stop_loader(il);
794 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
796 image_loader_emit_error(il);
799 static gboolean image_loader_continue(ImageLoader *il)
804 if (!il) return FALSE;
806 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
807 while (c > 0 && !image_loader_get_stopping(il))
809 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
813 image_loader_done(il);
817 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
819 image_loader_error(il);
828 if (il->bytes_total > 0)
830 image_loader_emit_percent(il);
836 static gboolean image_loader_begin(ImageLoader *il)
841 if (il->pixbuf) return FALSE;
843 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
844 if (b < 1) return FALSE;
846 image_loader_setup_loader(il);
848 g_assert(il->bytes_read == 0);
849 if (il->backend.load) {
851 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
853 image_loader_stop_loader(il);
857 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
859 image_loader_stop_loader(il);
864 format = il->backend.get_format_name(il->loader);
865 if (g_strcmp0(format, "pdf") == 0)
867 gint i = il->backend.get_page_total(il->loader);
868 file_data_set_page_total(il->fd, i);
873 format = il->backend.get_format_name(il->loader);
874 if (g_strcmp0(format, "heif") == 0)
876 gint i = il->backend.get_page_total(il->loader);
877 file_data_set_page_total(il->fd, i);
882 format = il->backend.get_format_name(il->loader);
883 if (g_strcmp0(format, "djvu") == 0)
885 gint i = il->backend.get_page_total(il->loader);
886 file_data_set_page_total(il->fd, i);
891 format = il->backend.get_format_name(il->loader);
892 if (g_strcmp0(format, "tiff") == 0)
894 gint i = il->backend.get_page_total(il->loader);
895 file_data_set_page_total(il->fd, i);
902 /* read until size is known */
903 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
905 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
906 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
908 image_loader_stop_loader(il);
913 if (!il->pixbuf) image_loader_sync_pixbuf(il);
915 if (il->bytes_read == il->bytes_total || b < 1)
917 /* done, handle (broken) loaders that do not have pixbuf till close */
918 image_loader_stop_loader(il);
920 if (!il->pixbuf) return FALSE;
922 image_loader_done(il);
928 image_loader_stop_loader(il);
935 /**************************************************************************************/
936 /* the following functions are always executed in the main thread */
939 static gboolean image_loader_setup_source(ImageLoader *il)
944 if (!il || il->loader || il->mapped_file) return FALSE;
946 il->mapped_file = NULL;
950 ExifData *exif = exif_read_fd(il->fd);
952 if (options->thumbnails.use_exif)
953 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
955 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
960 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
962 exif_free_fd(il->fd, exif);
966 if (!il->mapped_file)
971 pathl = path_from_utf8(il->fd->path);
972 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
974 if (load_fd == -1) return FALSE;
976 if (fstat(load_fd, &st) == 0)
978 il->bytes_total = st.st_size;
986 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
988 if (il->mapped_file == MAP_FAILED)
999 static void image_loader_stop_source(ImageLoader *il)
1003 if (il->mapped_file)
1007 exif_free_preview(il->mapped_file);
1011 munmap(il->mapped_file, il->bytes_total);
1013 il->mapped_file = NULL;
1017 static void image_loader_stop(ImageLoader *il)
1023 g_source_remove(il->idle_id);
1029 /* stop loader in the other thread */
1030 g_mutex_lock(il->data_mutex);
1031 il->stopping = TRUE;
1032 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
1033 g_mutex_unlock(il->data_mutex);
1036 image_loader_stop_loader(il);
1037 image_loader_stop_source(il);
1041 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
1043 g_mutex_lock(il->data_mutex);
1044 il->delay_area_ready = enable;
1049 list = g_list_reverse(il->area_param_delayed_list);
1050 il->area_param_delayed_list = NULL;
1051 g_mutex_unlock(il->data_mutex);
1057 ImageLoaderAreaParam *par = work->data;
1060 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1068 g_mutex_unlock(il->data_mutex);
1073 /**************************************************************************************/
1074 /* execution via idle calls */
1076 static gboolean image_loader_idle_cb(gpointer data)
1078 gboolean ret = FALSE;
1079 ImageLoader *il = data;
1083 ret = image_loader_continue(il);
1088 image_loader_stop_source(il);
1095 static gboolean image_loader_start_idle(ImageLoader *il)
1099 if (!il) return FALSE;
1101 if (!il->fd) return FALSE;
1103 if (!image_loader_setup_source(il)) return FALSE;
1105 ret = image_loader_begin(il);
1107 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1111 /**************************************************************************************/
1112 /* execution via thread */
1115 static GThreadPool *image_loader_thread_pool = NULL;
1117 static GCond *image_loader_prio_cond = NULL;
1118 static GMutex *image_loader_prio_mutex = NULL;
1119 static gint image_loader_prio_num = 0;
1122 static void image_loader_thread_enter_high(void)
1124 g_mutex_lock(image_loader_prio_mutex);
1125 image_loader_prio_num++;
1126 g_mutex_unlock(image_loader_prio_mutex);
1129 static void image_loader_thread_leave_high(void)
1131 g_mutex_lock(image_loader_prio_mutex);
1132 image_loader_prio_num--;
1133 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1134 g_mutex_unlock(image_loader_prio_mutex);
1137 static void image_loader_thread_wait_high(void)
1139 g_mutex_lock(image_loader_prio_mutex);
1140 while (image_loader_prio_num)
1142 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1145 g_mutex_unlock(image_loader_prio_mutex);
1149 static void image_loader_thread_run(gpointer data, gpointer user_data)
1151 ImageLoader *il = data;
1155 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1157 /* low prio, wait untill high prio tasks finishes */
1158 image_loader_thread_wait_high();
1163 image_loader_thread_enter_high();
1166 err = !image_loader_begin(il);
1171 loader failed, we have to send signal
1172 (idle mode returns the image_loader_begin return value directly)
1173 (success is always reported indirectly from image_loader_begin)
1175 image_loader_emit_error(il);
1180 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1182 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1184 /* low prio, wait untill high prio tasks finishes */
1185 image_loader_thread_wait_high();
1187 cont = image_loader_continue(il);
1189 image_loader_stop_loader(il);
1191 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1194 image_loader_thread_leave_high();
1197 g_mutex_lock(il->data_mutex);
1198 il->can_destroy = TRUE;
1199 g_cond_signal(il->can_destroy_cond);
1200 g_mutex_unlock(il->data_mutex);
1205 static gboolean image_loader_start_thread(ImageLoader *il)
1207 if (!il) return FALSE;
1209 if (!il->fd) return FALSE;
1213 if (!image_loader_setup_source(il)) return FALSE;
1215 if (!image_loader_thread_pool)
1217 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1218 #if GLIB_CHECK_VERSION(2,32,0)
1219 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1220 g_cond_init(image_loader_prio_cond);
1221 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1222 g_mutex_init(image_loader_prio_mutex);
1224 image_loader_prio_cond = g_cond_new();
1225 image_loader_prio_mutex = g_mutex_new();
1229 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1231 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1232 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1236 #endif /* HAVE_GTHREAD */
1239 /**************************************************************************************/
1240 /* public interface */
1243 gboolean image_loader_start(ImageLoader *il)
1245 if (!il) return FALSE;
1247 if (!il->fd) return FALSE;
1250 return image_loader_start_thread(il);
1252 return image_loader_start_idle(il);
1257 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1258 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1261 if (!il) return NULL;
1263 g_mutex_lock(il->data_mutex);
1265 g_mutex_unlock(il->data_mutex);
1269 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1273 g_mutex_lock(il->data_mutex);
1274 il->requested_width = width;
1275 il->requested_height = height;
1276 g_mutex_unlock(il->data_mutex);
1279 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1283 g_mutex_lock(il->data_mutex);
1284 il->idle_read_loop_count = count ? count : 1;
1285 g_mutex_unlock(il->data_mutex);
1288 void image_loader_set_priority(ImageLoader *il, gint priority)
1292 if (il->thread) return; /* can't change prio if the thread already runs */
1293 il->idle_priority = priority;
1297 gdouble image_loader_get_percent(ImageLoader *il)
1300 if (!il) return 0.0;
1302 g_mutex_lock(il->data_mutex);
1303 if (il->bytes_total == 0)
1309 ret = (gdouble)il->bytes_read / il->bytes_total;
1311 g_mutex_unlock(il->data_mutex);
1315 gboolean image_loader_get_is_done(ImageLoader *il)
1318 if (!il) return FALSE;
1320 g_mutex_lock(il->data_mutex);
1322 g_mutex_unlock(il->data_mutex);
1327 FileData *image_loader_get_fd(ImageLoader *il)
1330 if (!il) return NULL;
1332 g_mutex_lock(il->data_mutex);
1334 g_mutex_unlock(il->data_mutex);
1339 gboolean image_loader_get_shrunk(ImageLoader *il)
1342 if (!il) return FALSE;
1344 g_mutex_lock(il->data_mutex);
1346 g_mutex_unlock(il->data_mutex);
1350 const gchar *image_loader_get_error(ImageLoader *il)
1352 const gchar *ret = NULL;
1353 if (!il) return NULL;
1354 g_mutex_lock(il->data_mutex);
1355 if (il->error) ret = il->error->message;
1356 g_mutex_unlock(il->data_mutex);
1361 /* FIXME - this can be rather slow and blocks until the size is known */
1362 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1367 il = image_loader_new(fd);
1369 success = image_loader_start_idle(il);
1371 if (success && il->pixbuf)
1373 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1374 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1378 if (width) *width = -1;
1379 if (height) *height = -1;
1382 image_loader_free(il);
1386 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */