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_gdk.h"
25 #include "image_load_jpeg.h"
26 #include "image_load_tiff.h"
27 #include "image_load_dds.h"
28 #include "image_load_pdf.h"
29 #include "image_load_heif.h"
30 #include "image_load_ffmpegthumbnailer.h"
31 #include "image_load_collection.h"
32 #include "image_load_webp.h"
36 #include "ui_fileops.h"
37 #include "gq-marshal.h"
42 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
43 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
46 /**************************************************************************************/
47 /* image loader class */
51 SIGNAL_AREA_READY = 0,
59 static guint signals[SIGNAL_COUNT] = { 0 };
61 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
62 static void image_loader_class_init(ImageLoaderClass *class);
63 static void image_loader_finalize(GObject *object);
64 static void image_loader_stop(ImageLoader *il);
66 GType image_loader_get_type(void)
68 static GType type = 0;
71 static const GTypeInfo info = {
72 sizeof(ImageLoaderClass),
74 NULL, /* base_finalize */
75 (GClassInitFunc)image_loader_class_init, /* class_init */
76 NULL, /* class_finalize */
77 NULL, /* class_data */
80 (GInstanceInitFunc)image_loader_init, /* instance_init */
81 NULL /* value_table */
83 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
88 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
90 ImageLoader *il = (ImageLoader *)instance;
94 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
101 il->idle_done_id = 0;
103 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
104 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
105 il->mapped_file = NULL;
107 il->requested_width = 0;
108 il->requested_height = 0;
109 il->actual_width = 0;
110 il->actual_height = 0;
113 il->can_destroy = TRUE;
116 #if GLIB_CHECK_VERSION(2,32,0)
117 il->data_mutex = g_new(GMutex, 1);
118 g_mutex_init(il->data_mutex);
119 il->can_destroy_cond = g_new(GCond, 1);
120 g_cond_init(il->can_destroy_cond);
122 il->data_mutex = g_mutex_new();
123 il->can_destroy_cond = g_cond_new();
126 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
129 static void image_loader_class_init(ImageLoaderClass *class)
131 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
133 // gobject_class->set_property = image_loader_set_property;
134 // gobject_class->get_property = image_loader_get_property;
136 gobject_class->finalize = image_loader_finalize;
139 signals[SIGNAL_AREA_READY] =
140 g_signal_new("area_ready",
141 G_OBJECT_CLASS_TYPE(gobject_class),
143 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
145 gq_marshal_VOID__INT_INT_INT_INT,
152 signals[SIGNAL_ERROR] =
153 g_signal_new("error",
154 G_OBJECT_CLASS_TYPE(gobject_class),
156 G_STRUCT_OFFSET(ImageLoaderClass, error),
158 g_cclosure_marshal_VOID__VOID,
161 signals[SIGNAL_DONE] =
163 G_OBJECT_CLASS_TYPE(gobject_class),
165 G_STRUCT_OFFSET(ImageLoaderClass, done),
167 g_cclosure_marshal_VOID__VOID,
170 signals[SIGNAL_PERCENT] =
171 g_signal_new("percent",
172 G_OBJECT_CLASS_TYPE(gobject_class),
174 G_STRUCT_OFFSET(ImageLoaderClass, percent),
176 g_cclosure_marshal_VOID__DOUBLE,
180 signals[SIGNAL_SIZE] =
181 g_signal_new("size_prepared",
182 G_OBJECT_CLASS_TYPE(gobject_class),
184 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
186 gq_marshal_VOID__INT_INT,
193 static void image_loader_finalize(GObject *object)
195 ImageLoader *il = (ImageLoader *)object;
197 image_loader_stop(il);
199 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
201 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
203 if (il->idle_done_id)
205 g_source_remove(il->idle_done_id);
206 il->idle_done_id = 0;
209 while (g_source_remove_by_user_data(il))
211 DEBUG_2("pending signals detected");
214 while (il->area_param_list)
216 DEBUG_1("pending area_ready signals detected");
217 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
218 g_free(il->area_param_list->data);
219 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
222 while (il->area_param_delayed_list)
224 g_free(il->area_param_delayed_list->data);
225 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
228 if (il->pixbuf) g_object_unref(il->pixbuf);
230 if (il->error) g_error_free(il->error);
232 file_data_unref(il->fd);
234 #if GLIB_CHECK_VERSION(2,32,0)
235 g_mutex_clear(il->data_mutex);
236 g_free(il->data_mutex);
237 g_cond_clear(il->can_destroy_cond);
238 g_free(il->can_destroy_cond);
240 g_mutex_free(il->data_mutex);
241 g_cond_free(il->can_destroy_cond);
246 void image_loader_free(ImageLoader *il)
249 g_object_unref(G_OBJECT(il));
253 ImageLoader *image_loader_new(FileData *fd)
257 if (!fd) return NULL;
259 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
261 il->fd = file_data_ref(fd);
266 /**************************************************************************************/
267 /* send signals via idle calbacks - the callback are executed in the main thread */
269 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
270 struct _ImageLoaderAreaParam {
279 static gboolean image_loader_emit_area_ready_cb(gpointer data)
281 ImageLoaderAreaParam *par = data;
282 ImageLoader *il = par->il;
284 g_mutex_lock(il->data_mutex);
285 il->area_param_list = g_list_remove(il->area_param_list, par);
291 g_mutex_unlock(il->data_mutex);
293 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
298 static gboolean image_loader_emit_done_cb(gpointer data)
300 ImageLoader *il = data;
301 g_signal_emit(il, signals[SIGNAL_DONE], 0);
305 static gboolean image_loader_emit_error_cb(gpointer data)
307 ImageLoader *il = data;
308 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
312 static gboolean image_loader_emit_percent_cb(gpointer data)
314 ImageLoader *il = data;
315 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
319 static gboolean image_loader_emit_size_cb(gpointer data)
322 ImageLoader *il = data;
323 g_mutex_lock(il->data_mutex);
324 width = il->actual_width;
325 height = il->actual_height;
326 g_mutex_unlock(il->data_mutex);
327 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
332 /* DONE and ERROR are emited only once, thus they can have normal priority
333 PERCENT and AREA_READY should be processed ASAP
336 static void image_loader_emit_done(ImageLoader *il)
338 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
341 static void image_loader_emit_error(ImageLoader *il)
343 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
346 static void image_loader_emit_percent(ImageLoader *il)
348 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
351 static void image_loader_emit_size(ImageLoader *il)
353 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
356 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
360 ImageLoaderAreaParam *prev_par = (*list)->data;
361 if (prev_par->x == x && prev_par->w == w &&
362 prev_par->y + prev_par->h == y)
364 /* we can merge the notifications */
368 if (prev_par->x == x && prev_par->w == w &&
369 y + h == prev_par->y)
371 /* we can merge the notifications */
376 if (prev_par->y == y && prev_par->h == h &&
377 prev_par->x + prev_par->w == x)
379 /* we can merge the notifications */
383 if (prev_par->y == y && prev_par->h == h &&
384 x + w == prev_par->x)
386 /* we can merge the notifications */
393 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
400 *list = g_list_prepend(*list, par);
404 /* this function expects that il->data_mutex is locked by caller */
405 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
407 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
411 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
415 /**************************************************************************************/
416 /* the following functions may be executed in separate thread */
418 /* this function expects that il->data_mutex is locked by caller */
419 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
421 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
426 static gboolean image_loader_get_stopping(ImageLoader *il)
429 if (!il) return FALSE;
431 g_mutex_lock(il->data_mutex);
433 g_mutex_unlock(il->data_mutex);
439 static void image_loader_sync_pixbuf(ImageLoader *il)
443 g_mutex_lock(il->data_mutex);
447 g_mutex_unlock(il->data_mutex);
451 pb = il->backend.get_pixbuf(il->loader);
453 if (pb == il->pixbuf)
455 g_mutex_unlock(il->data_mutex);
459 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
461 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
464 if (il->pixbuf) g_object_unref(il->pixbuf);
467 if (il->pixbuf) g_object_ref(il->pixbuf);
469 g_mutex_unlock(il->data_mutex);
472 static void image_loader_area_updated_cb(gpointer loader,
473 guint x, guint y, guint w, guint h,
476 ImageLoader *il = data;
478 if (!image_loader_get_pixbuf(il))
480 image_loader_sync_pixbuf(il);
481 if (!image_loader_get_pixbuf(il))
483 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
487 g_mutex_lock(il->data_mutex);
488 if (il->delay_area_ready)
489 image_loader_queue_delayed_area_ready(il, x, y, w, h);
491 image_loader_emit_area_ready(il, x, y, w, h);
493 if (il->stopping) il->backend.abort(il->loader);
495 g_mutex_unlock(il->data_mutex);
498 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
500 ImageLoader *il = data;
506 http://bugzilla.gnome.org/show_bug.cgi?id=547669
507 http://bugzilla.gnome.org/show_bug.cgi?id=589334
509 gchar *format = il->backend.get_format_name(loader);
510 if (strcmp(format, "svg") == 0 ||
511 strcmp(format, "xpm") == 0)
519 pb = il->backend.get_pixbuf(loader);
521 h = gdk_pixbuf_get_height(pb);
522 rs = gdk_pixbuf_get_rowstride(pb);
523 pix = gdk_pixbuf_get_pixels(pb);
525 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
529 static void image_loader_size_cb(gpointer loader,
530 gint width, gint height, gpointer data)
532 ImageLoader *il = data;
534 gboolean scale = FALSE;
537 g_mutex_lock(il->data_mutex);
538 il->actual_width = width;
539 il->actual_height = height;
540 if (il->requested_width < 1 || il->requested_height < 1)
542 g_mutex_unlock(il->data_mutex);
543 image_loader_emit_size(il);
546 g_mutex_unlock(il->data_mutex);
548 #ifdef HAVE_FFMPEGTHUMBNAILER
549 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
552 mime_types = il->backend.get_format_mime_types(loader);
554 while (mime_types[n] && !scale)
556 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
559 g_strfreev(mime_types);
563 image_loader_emit_size(il);
567 g_mutex_lock(il->data_mutex);
570 if (width > il->requested_width || height > il->requested_height)
573 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
575 nw = il->requested_width;
576 nh = (gdouble)nw / width * height;
581 nh = il->requested_height;
582 nw = (gdouble)nh / height * width;
586 il->actual_width = nw;
587 il->actual_height = nh;
588 il->backend.set_size(loader, nw, nh);
592 g_mutex_unlock(il->data_mutex);
593 image_loader_emit_size(il);
596 static void image_loader_stop_loader(ImageLoader *il)
602 /* some loaders do not have a pixbuf till close, order is important here */
603 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
604 image_loader_sync_pixbuf(il);
605 il->backend.free(il->loader);
608 g_mutex_lock(il->data_mutex);
610 g_mutex_unlock(il->data_mutex);
613 static void image_loader_setup_loader(ImageLoader *il)
615 g_mutex_lock(il->data_mutex);
616 #ifdef HAVE_FFMPEGTHUMBNAILER
617 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
619 DEBUG_1("Using custom ffmpegthumbnailer loader");
620 image_loader_backend_set_ft(&il->backend);
625 if (il->fd->format_class == FORMAT_CLASS_PDF)
627 DEBUG_1("Using custom pdf loader");
628 image_loader_backend_set_pdf(&il->backend);
633 if (il->bytes_total >= 10 &&
634 (memcmp(il->mapped_file + 4, "ftypheic", 8) == 0))
636 DEBUG_1("Using custom heif loader");
637 image_loader_backend_set_heif(&il->backend);
642 if (il->bytes_total >= 12 &&
643 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
644 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
646 DEBUG_1("Using custom webp loader");
647 image_loader_backend_set_webp(&il->backend);
652 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
654 DEBUG_1("Using custom jpeg loader");
655 image_loader_backend_set_jpeg(&il->backend);
660 if (il->bytes_total >= 10 &&
661 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
662 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
663 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
664 memcmp(il->mapped_file, "II*\0", 4) == 0))
666 DEBUG_1("Using custom tiff loader");
667 image_loader_backend_set_tiff(&il->backend);
671 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
673 DEBUG_1("Using dds loader");
674 image_loader_backend_set_dds(&il->backend);
677 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
679 DEBUG_1("Using custom collection loader");
680 image_loader_backend_set_collection(&il->backend);
683 image_loader_backend_set_default(&il->backend);
685 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
686 g_mutex_unlock(il->data_mutex);
690 static void image_loader_done(ImageLoader *il)
692 image_loader_stop_loader(il);
694 image_loader_emit_done(il);
697 static void image_loader_error(ImageLoader *il)
699 image_loader_stop_loader(il);
701 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
703 image_loader_emit_error(il);
706 static gboolean image_loader_continue(ImageLoader *il)
711 if (!il) return FALSE;
713 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
714 while (c > 0 && !image_loader_get_stopping(il))
716 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
720 image_loader_done(il);
724 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
726 image_loader_error(il);
735 if (il->bytes_total > 0)
737 image_loader_emit_percent(il);
743 static gboolean image_loader_begin(ImageLoader *il)
747 if (il->pixbuf) return FALSE;
749 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
750 if (b < 1) return FALSE;
752 image_loader_setup_loader(il);
754 g_assert(il->bytes_read == 0);
755 if (il->backend.load) {
757 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
759 image_loader_stop_loader(il);
763 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
765 image_loader_stop_loader(il);
771 /* read until size is known */
772 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
774 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
775 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
777 image_loader_stop_loader(il);
782 if (!il->pixbuf) image_loader_sync_pixbuf(il);
784 if (il->bytes_read == il->bytes_total || b < 1)
786 /* done, handle (broken) loaders that do not have pixbuf till close */
787 image_loader_stop_loader(il);
789 if (!il->pixbuf) return FALSE;
791 image_loader_done(il);
797 image_loader_stop_loader(il);
804 /**************************************************************************************/
805 /* the following functions are always executed in the main thread */
808 static gboolean image_loader_setup_source(ImageLoader *il)
813 if (!il || il->loader || il->mapped_file) return FALSE;
815 il->mapped_file = NULL;
819 ExifData *exif = exif_read_fd(il->fd);
821 if (options->thumbnails.use_exif)
822 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
824 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
829 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
831 exif_free_fd(il->fd, exif);
835 if (!il->mapped_file)
840 pathl = path_from_utf8(il->fd->path);
841 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
843 if (load_fd == -1) return FALSE;
845 if (fstat(load_fd, &st) == 0)
847 il->bytes_total = st.st_size;
855 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
857 if (il->mapped_file == MAP_FAILED)
868 static void image_loader_stop_source(ImageLoader *il)
876 exif_free_preview(il->mapped_file);
880 munmap(il->mapped_file, il->bytes_total);
882 il->mapped_file = NULL;
886 static void image_loader_stop(ImageLoader *il)
892 g_source_remove(il->idle_id);
898 /* stop loader in the other thread */
899 g_mutex_lock(il->data_mutex);
901 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
902 g_mutex_unlock(il->data_mutex);
905 image_loader_stop_loader(il);
906 image_loader_stop_source(il);
910 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
912 g_mutex_lock(il->data_mutex);
913 il->delay_area_ready = enable;
918 list = g_list_reverse(il->area_param_delayed_list);
919 il->area_param_delayed_list = NULL;
920 g_mutex_unlock(il->data_mutex);
926 ImageLoaderAreaParam *par = work->data;
929 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
937 g_mutex_unlock(il->data_mutex);
942 /**************************************************************************************/
943 /* execution via idle calls */
945 static gboolean image_loader_idle_cb(gpointer data)
947 gboolean ret = FALSE;
948 ImageLoader *il = data;
952 ret = image_loader_continue(il);
957 image_loader_stop_source(il);
964 static gboolean image_loader_start_idle(ImageLoader *il)
968 if (!il) return FALSE;
970 if (!il->fd) return FALSE;
972 if (!image_loader_setup_source(il)) return FALSE;
974 ret = image_loader_begin(il);
976 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
980 /**************************************************************************************/
981 /* execution via thread */
984 static GThreadPool *image_loader_thread_pool = NULL;
986 static GCond *image_loader_prio_cond = NULL;
987 static GMutex *image_loader_prio_mutex = NULL;
988 static gint image_loader_prio_num = 0;
991 static void image_loader_thread_enter_high(void)
993 g_mutex_lock(image_loader_prio_mutex);
994 image_loader_prio_num++;
995 g_mutex_unlock(image_loader_prio_mutex);
998 static void image_loader_thread_leave_high(void)
1000 g_mutex_lock(image_loader_prio_mutex);
1001 image_loader_prio_num--;
1002 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1003 g_mutex_unlock(image_loader_prio_mutex);
1006 static void image_loader_thread_wait_high(void)
1008 g_mutex_lock(image_loader_prio_mutex);
1009 while (image_loader_prio_num)
1011 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1014 g_mutex_unlock(image_loader_prio_mutex);
1018 static void image_loader_thread_run(gpointer data, gpointer user_data)
1020 ImageLoader *il = data;
1024 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1026 /* low prio, wait untill high prio tasks finishes */
1027 image_loader_thread_wait_high();
1032 image_loader_thread_enter_high();
1035 err = !image_loader_begin(il);
1040 loader failed, we have to send signal
1041 (idle mode returns the image_loader_begin return value directly)
1042 (success is always reported indirectly from image_loader_begin)
1044 image_loader_emit_error(il);
1049 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1051 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1053 /* low prio, wait untill high prio tasks finishes */
1054 image_loader_thread_wait_high();
1056 cont = image_loader_continue(il);
1058 image_loader_stop_loader(il);
1060 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1063 image_loader_thread_leave_high();
1066 g_mutex_lock(il->data_mutex);
1067 il->can_destroy = TRUE;
1068 g_cond_signal(il->can_destroy_cond);
1069 g_mutex_unlock(il->data_mutex);
1074 static gboolean image_loader_start_thread(ImageLoader *il)
1076 if (!il) return FALSE;
1078 if (!il->fd) return FALSE;
1082 if (!image_loader_setup_source(il)) return FALSE;
1084 if (!image_loader_thread_pool)
1086 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1087 #if GLIB_CHECK_VERSION(2,32,0)
1088 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1089 g_cond_init(image_loader_prio_cond);
1090 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1091 g_mutex_init(image_loader_prio_mutex);
1093 image_loader_prio_cond = g_cond_new();
1094 image_loader_prio_mutex = g_mutex_new();
1098 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1100 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1101 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1105 #endif /* HAVE_GTHREAD */
1108 /**************************************************************************************/
1109 /* public interface */
1112 gboolean image_loader_start(ImageLoader *il)
1114 if (!il) return FALSE;
1116 if (!il->fd) return FALSE;
1119 return image_loader_start_thread(il);
1121 return image_loader_start_idle(il);
1126 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1127 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1130 if (!il) return NULL;
1132 g_mutex_lock(il->data_mutex);
1134 g_mutex_unlock(il->data_mutex);
1138 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1142 g_mutex_lock(il->data_mutex);
1143 il->requested_width = width;
1144 il->requested_height = height;
1145 g_mutex_unlock(il->data_mutex);
1148 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1152 g_mutex_lock(il->data_mutex);
1153 il->idle_read_loop_count = count ? count : 1;
1154 g_mutex_unlock(il->data_mutex);
1157 void image_loader_set_priority(ImageLoader *il, gint priority)
1161 if (il->thread) return; /* can't change prio if the thread already runs */
1162 il->idle_priority = priority;
1166 gdouble image_loader_get_percent(ImageLoader *il)
1169 if (!il) return 0.0;
1171 g_mutex_lock(il->data_mutex);
1172 if (il->bytes_total == 0)
1178 ret = (gdouble)il->bytes_read / il->bytes_total;
1180 g_mutex_unlock(il->data_mutex);
1184 gboolean image_loader_get_is_done(ImageLoader *il)
1187 if (!il) return FALSE;
1189 g_mutex_lock(il->data_mutex);
1191 g_mutex_unlock(il->data_mutex);
1196 FileData *image_loader_get_fd(ImageLoader *il)
1199 if (!il) return NULL;
1201 g_mutex_lock(il->data_mutex);
1203 g_mutex_unlock(il->data_mutex);
1208 gboolean image_loader_get_shrunk(ImageLoader *il)
1211 if (!il) return FALSE;
1213 g_mutex_lock(il->data_mutex);
1215 g_mutex_unlock(il->data_mutex);
1219 const gchar *image_loader_get_error(ImageLoader *il)
1221 const gchar *ret = NULL;
1222 if (!il) return NULL;
1223 g_mutex_lock(il->data_mutex);
1224 if (il->error) ret = il->error->message;
1225 g_mutex_unlock(il->data_mutex);
1230 /* FIXME - this can be rather slow and blocks until the size is known */
1231 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1236 il = image_loader_new(fd);
1238 success = image_loader_start_idle(il);
1240 if (success && il->pixbuf)
1242 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1243 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1247 if (width) *width = -1;
1248 if (height) *height = -1;
1251 image_loader_free(il);
1255 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */